Skip to content

Commit

Permalink
feat(calendars/ethiopian): add calendar pipe
Browse files Browse the repository at this point in the history
fix(calendars/ethiopian): wrong month bounds in iso mode
  • Loading branch information
trik committed Feb 5, 2020
1 parent e40f6cc commit 31add4a
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/calendars/ethiopian/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ng_module(
deps = [
"//src/core/calendar",
"@npm//@angular/core",
"@npm//date-fns",
],
)

Expand Down
7 changes: 7 additions & 0 deletions src/calendars/ethiopian/calendar-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
import {AjfCalendarService} from '@ajf/core/calendar';
import {NgModule} from '@angular/core';

import {AjfEthiopianDatePipe} from './ethiopian-date-pipe';
import {AjfEthiopianCalendarService} from './calendar-service';

@NgModule({
declarations: [
AjfEthiopianDatePipe,
],
exports: [
AjfEthiopianDatePipe,
],
providers: [
{provide: AjfCalendarService, useClass: AjfEthiopianCalendarService},
],
Expand Down
42 changes: 34 additions & 8 deletions src/calendars/ethiopian/calendar-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@
import {AjfCalendarEntry, AjfCalendarService, AjfCalendarParams,
AjfCalendarView, AjfCalendarViewMode} from '@ajf/core/calendar';
import {Injectable} from '@angular/core';
import {addDays, addYears, setISODay, startOfWeek} from 'date-fns';
import {addDays, addWeeks, addYears, endOfISOWeek, getISODay, setISODay, startOfISOWeek,
startOfWeek, subWeeks} from 'date-fns';

import {EthiopianDate} from './ethiopian-date';

function getMonthBounds(date: EthiopianDate): {start: EthiopianDate, end: EthiopianDate} {
const year = date.getFullYear();
const month = date.getMonth();
const start = new EthiopianDate(year, month, 1);
const endDay = month < 12 ? 30 : (year % 4 === 3 ? 6 : 5);
const end = new EthiopianDate(year, month, endDay);
return {start, end};
}

@Injectable({providedIn: 'root'})
export class AjfEthiopianCalendarService extends AjfCalendarService {
buildView(params: AjfCalendarParams): AjfCalendarView {
Expand Down Expand Up @@ -73,17 +83,34 @@ export class AjfEthiopianCalendarService extends AjfCalendarService {
monthBounds(date: Date, isoMode: boolean): {start: Date, end: Date} {
if (!isoMode) {
const ecDate = EthiopianDate.gregorianToEthiopian(date);
const year = ecDate.getFullYear();
const month = ecDate.getMonth();
const start = new EthiopianDate(year, month, 1);
const endDay = month < 12 ? 30 : (year % 4 === 3 ? 6 : 5);
const end = new EthiopianDate(year, month, endDay);
const {start, end} = getMonthBounds(ecDate);
return {
start: EthiopianDate.ethiopianToGregorian(start),
end: EthiopianDate.ethiopianToGregorian(end),
};
} else {
let isoDay = getISODay(date);
const ecDate = EthiopianDate.gregorianToEthiopian(date);
let {start, end} = getMonthBounds(ecDate);
if (ecDate.getMonth() === 12) {
start = EthiopianDate.gregorianToEthiopian(startOfISOWeek(start.getGregorianDate()));
end = EthiopianDate.gregorianToEthiopian(endOfISOWeek(end.getGregorianDate()));
} else {
date = isoDay < 4 ? endOfISOWeek(date) : startOfISOWeek(date);
const startWeekDay = start.getDay();
const endWeekDay = end.getDay();
if (startWeekDay == 0 || startWeekDay > 4) {
start = EthiopianDate.gregorianToEthiopian(addWeeks(start.getGregorianDate(), 1));
}
if (endWeekDay > 0 && endWeekDay < 4) {
end = EthiopianDate.gregorianToEthiopian(subWeeks(end.getGregorianDate(), 1));
}
}
return {
start: startOfISOWeek(start.getGregorianDate()),
end: endOfISOWeek(end.getGregorianDate()),
};
}
return super.monthBounds(date, isoMode);
}

nextView(viewDate: Date, viewMode: AjfCalendarViewMode): Date {
Expand Down Expand Up @@ -159,7 +186,6 @@ export class AjfEthiopianCalendarService extends AjfCalendarService {
}
rows.push(row);
}
console.log(rows);

return rows;
}
Expand Down
39 changes: 39 additions & 0 deletions src/calendars/ethiopian/ethiopian-date-pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright (C) 2018 Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {Injectable, Pipe, PipeTransform} from '@angular/core';
import {EthiopianDate} from './ethiopian-date';

@Injectable()
@Pipe({name: 'ajfEthiopianDate'})
export class AjfEthiopianDatePipe implements PipeTransform {
transform(value: any): string|null {
try {
const ed = EthiopianDate.gregorianToEthiopian(value);
const date = `0${ed.getDate()}`.slice(-2);
const month = `0${ed.getMonth()}`.slice(-2);
return `${date}/${month}/${ed.getFullYear()}`;
} catch (e) {
return null;
}
}
}
22 changes: 21 additions & 1 deletion src/calendars/ethiopian/ethiopian-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class EthiopianDate {
return this._date;
}

getDay(): number {
return this._gc.getDay();
}

getDayOfWeek() {
const weekDay = this.getGCWeekDay();
return WEEK_NAMES[weekDay];
Expand Down Expand Up @@ -103,7 +107,23 @@ export class EthiopianDate {
: null;
}

toString() {
getHours(): number {
return 0;
}

getMinutes(): number {
return 0;
}

getSeconds(): number {
return 0;
}

getMilliseconds(): number {
return 0;
}

toString(): string {
return `${this._year}-${this._month + 1}-${this._date}`;
}

Expand Down
2 changes: 2 additions & 0 deletions src/calendars/ethiopian/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@

export * from './calendar-module';
export * from './calendar-service';
export * from './ethiopian-date';
export * from './ethiopian-date-pipe';
2 changes: 1 addition & 1 deletion src/core/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export abstract class AjfCalendar implements AfterContentInit, ControlValueAcces
get isoMode(): boolean { return this._isoMode; }
set isoMode(isoMode: boolean) {
this._isoMode = isoMode;
this._cdr.markForCheck();
this._buildCalendar();
}

private _minDate: Date | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import {AjfEthiopianCalendarModule} from '@ajf/calendars/ethiopian';
import {AjfCalendarModule} from '@ajf/material/calendar';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {RouterModule} from '@angular/router';

import {CalendarEthiopianDemo} from './calendar-ethiopian-demo';
Expand All @@ -31,6 +33,8 @@ import {CalendarEthiopianDemo} from './calendar-ethiopian-demo';
imports: [
AjfCalendarModule,
AjfEthiopianCalendarModule,
FormsModule,
MatCheckboxModule,
RouterModule.forChild([{path: '', component: CalendarEthiopianDemo}]),
],
declarations: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="demo-calendar">
<h4 class="demo-section-header">Ethiopian calendar</h4>
<section>
<ajf-calendar></ajf-calendar>
<mat-checkbox [(ngModel)]="isoMode">Iso mode</mat-checkbox>
<ajf-calendar [isoMode]="isoMode"></ajf-calendar>
</section>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ import {Component} from '@angular/core';
styleUrls: ['calendar-ethiopian-demo.css'],
})
export class CalendarEthiopianDemo {
isoMode: boolean;
}
4 changes: 4 additions & 0 deletions src/dev-app-mat/calendar/calendar-demo-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {RouterModule} from '@angular/router';

import {AjfCalendarModule} from '@ajf/material/calendar';
Expand All @@ -30,6 +32,8 @@ import {CalendarDemo} from './calendar-demo';
@NgModule({
imports: [
AjfCalendarModule,
FormsModule,
MatCheckboxModule,
RouterModule.forChild([{path: '', component: CalendarDemo}]),
],
declarations: [
Expand Down
3 changes: 2 additions & 1 deletion src/dev-app-mat/calendar/calendar-demo.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="demo-calendar">
<h4 class="demo-section-header">Calendar</h4>
<section>
<ajf-calendar></ajf-calendar>
<mat-checkbox [(ngModel)]="isoMode">Iso mode</mat-checkbox>
<ajf-calendar [isoMode]="isoMode"></ajf-calendar>
</section>
</div>

0 comments on commit 31add4a

Please sign in to comment.