Skip to content

Commit 3974c97

Browse files
authored
Merge pull request #14 from fullstack-devops/bugfix/add-date-locale
add locale to calendar
2 parents 2f537bd + fd11969 commit 3974c97

File tree

9 files changed

+78
-44
lines changed

9 files changed

+78
-44
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"javascript.updateImportsOnFileMove.enabled": "always",
1515
"typescript.updateImportsOnFileMove.enabled": "always",
1616
"[typescript]": {
17+
"editor.defaultFormatter": "esbenp.prettier-vscode",
1718
"editor.codeActionsOnSave": {
1819
"source.organizeImports": true
1920
}

angular.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"build": {
1313
"builder": "@angular-devkit/build-angular:ng-packagr",
1414
"options": {
15+
"tsConfig": "projects/ng-mat-components/tsconfig.lib.json",
1516
"project": "projects/ng-mat-components/ng-package.json"
1617
},
1718
"configurations": {
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
1-
import { CalenderShowcaseComponent } from './content/calender-showcase/calender-showcase.component';
21
import { NgModule } from '@angular/core';
32
import { PreloadAllModules, RouterModule } from '@angular/router';
43
import { FrameRoutes } from 'projects/ng-mat-components/src/public-api';
4+
import { CalenderShowcaseComponent } from './content/calender-showcase/calender-showcase.component';
55
import { HomeComponent } from './content/home/home.component';
66
import { SubComponent } from './content/home/sub/sub.component';
77
import { TestComponent } from './content/test/test.component';
88

9-
109
export const routes: FrameRoutes = [
1110
{
12-
path: '', redirectTo: 'home', pathMatch: 'full',
13-
},
14-
{
15-
path: 'home', component: HomeComponent,
11+
path: 'home',
12+
component: HomeComponent,
1613
data: {
1714
displaySidemenu: true,
1815
sidenavIcon: 'home',
19-
sidenavText: 'Home'
16+
sidenavText: 'Home',
2017
},
2118
children: [
2219
{
23-
path: 'sub', component: SubComponent,
24-
}
25-
]
20+
path: 'sub',
21+
component: SubComponent,
22+
},
23+
],
2624
},
2725
{
28-
path: 'test', component: TestComponent,
26+
path: 'test',
27+
component: TestComponent,
2928
data: {
3029
displaySidemenu: true,
3130
sidenavIcon: 'dashboard',
32-
sidenavText: 'Dashboard'
31+
sidenavText: 'Dashboard',
3332
},
3433
},
3534
{
36-
path: 'calendar-showcase', component: CalenderShowcaseComponent,
35+
path: 'calendar-showcase',
36+
component: CalenderShowcaseComponent,
3737
data: {
3838
displaySidemenu: true,
3939
sidenavIcon: 'calendar_month',
40-
sidenavText: 'Calendar'
40+
sidenavText: 'Calendar',
4141
},
4242
},
43+
{
44+
path: '**',
45+
redirectTo: 'home',
46+
pathMatch: 'full',
47+
},
4348
];
4449

4550
@NgModule({
46-
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
47-
exports: [RouterModule]
51+
imports: [
52+
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }),
53+
],
54+
exports: [RouterModule],
4855
})
49-
export class AppRoutingModule { }
56+
export class AppRoutingModule {}

projects/lib-workspace/src/app/app.module.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { CalenderShowcaseComponent } from './content/calender-showcase/calender-showcase.component';
21
import { LOCALE_ID, NgModule } from '@angular/core';
32
import { BrowserModule } from '@angular/platform-browser';
3+
import { CalenderShowcaseComponent } from './content/calender-showcase/calender-showcase.component';
44

55
import { APP_BASE_HREF } from '@angular/common';
6+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
7+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
8+
import { de } from 'date-fns/locale';
69
import { AppRoutingModule } from './app-routing.module';
710
import { AppComponent } from './app.component';
8-
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
9-
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
1011
import { MaterialModule } from './material.module';
1112

1213
import {
1314
FsCalendarModule,
14-
FsUiFrameModule,
15+
FsCalendarService,
1516
FsUiFrameContentModule,
17+
FsUiFrameModule,
1618
FsUiFrameToolbarModule,
1719
} from 'projects/ng-mat-components/src/public-api';
1820
import { HomeComponent } from './content/home/home.component';
@@ -46,6 +48,11 @@ import { TestComponent } from './content/test/test.component';
4648
useValue: '.',
4749
},
4850
{ provide: LOCALE_ID, useValue: 'de-DE' },
51+
{
52+
provide: 'FS_DATE_LOCALE',
53+
useClass: FsCalendarService,
54+
useValue: de,
55+
},
4956
],
5057
bootstrap: [AppComponent],
5158
})

projects/ng-mat-components/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"peerDependencies": {
66
"@angular/common": "^13.0.0",
77
"@angular/core": "^13.0.0",
8-
"moment": "^2.29.0",
9-
"@angular/material": "^13.0.0"
8+
"@angular/material": "^13.0.0",
9+
"date-fns": "^2.29.3"
1010
},
1111
"dependencies": {
1212
"tslib": "^2.3.0"

projects/ng-mat-components/src/lib/fs-calendar/fs-calendar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<div style="width: 40px"></div>
2828
</ng-template>
2929
<div>
30-
{{ month.name | uppercase }}
30+
{{ month.name }}
3131
{{ config.displayYear ? month.year : "" }}
3232
</div>
3333
<button

projects/ng-mat-components/src/lib/fs-calendar/fs-calendar.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import {
66
OnInit,
77
Output,
88
} from '@angular/core';
9+
import * as dateFns from 'date-fns';
910
import {
1011
Calendar,
1112
CalendarConfig,
1213
calendarSelected,
1314
Day,
1415
} from './calendar.models';
15-
import { dateFns, FsCalendarService } from './fs-calendar.service';
16+
import { FsCalendarService } from './fs-calendar.service';
1617

1718
@Component({
1819
selector: 'fs-calendar',
@@ -168,10 +169,11 @@ export class FsCalendarComponent implements OnInit {
168169
this.selectedDayStart != undefined &&
169170
this.selectedDayEnd == undefined
170171
) {
172+
console.log(this.selectedDayStart, dateComp);
171173
this.selectedDayBetween = this.calendar.daysAbsolute.filter((date) => {
172174
return (
173-
dateFns.isBefore(date, this.selectedDayStart as Date) &&
174-
dateFns.isAfter(date, dateComp)
175+
dateFns.isAfter(date, this.selectedDayStart as Date) &&
176+
dateFns.isBefore(date, dateComp)
175177
);
176178
});
177179
}

projects/ng-mat-components/src/lib/fs-calendar/fs-calendar.service.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { Inject, Injectable, LOCALE_ID } from '@angular/core';
2-
import * as dateFns_ from 'date-fns';
1+
import { Inject, Injectable } from '@angular/core';
2+
import * as dateFns from 'date-fns';
33
import { Calendar, Day, Month } from './calendar.models';
44

5-
export const dateFns = dateFns_;
6-
75
@Injectable({
86
providedIn: 'root',
97
})
108
export class FsCalendarService {
11-
dayNamesDeVor: any;
12-
dayNamesDe: any;
13-
9+
dayNames: string[] = this.getWeekDayNames();
1410
dataSourceCustom: Day[] = [];
1511
daysAbsolute: Date[] = [];
1612

17-
constructor(@Inject(LOCALE_ID) private appLocale: string) {}
13+
constructor(@Inject('FS_DATE_LOCALE') private appLocale: dateFns.Locale) {}
1814

1915
/**
2016
* @param {String} mode calendar mode (monthly|annual)
@@ -65,7 +61,7 @@ export class FsCalendarService {
6561
}
6662
cal = {
6763
months: months,
68-
dayNames: this.dayNamesDeVor,
64+
dayNames: this.dayNames,
6965
year: year,
7066
daysAbsolute: this.daysAbsolute,
7167
};
@@ -145,7 +141,7 @@ export class FsCalendarService {
145141
break;
146142
case 'placeholderDay':
147143
newDay = {
148-
dayNumber: dateFns.format(date, 'd'),
144+
dayNumber: dateFns.format(date, 'd', { locale: this.appLocale }),
149145
date: date,
150146
kw: dateFns.getWeek(date, { weekStartsOn: 4 }),
151147
type: 'placeholderDay',
@@ -163,7 +159,7 @@ export class FsCalendarService {
163159
}
164160
return {
165161
year: year,
166-
dayNames: [],
162+
dayNames: this.dayNames,
167163
months: months,
168164
daysAbsolute: this.daysAbsolute,
169165
};
@@ -180,7 +176,7 @@ export class FsCalendarService {
180176
}
181177

182178
return {
183-
name: dateFns.format(days[0].date, 'LLLL'),
179+
name: dateFns.format(days[0].date, 'MMMM', { locale: this.appLocale }),
184180
year: year,
185181
days: days,
186182
render: [[]],
@@ -235,25 +231,44 @@ export class FsCalendarService {
235231
day.toolTip = toolTip;
236232
day['kw'] = dateFns.getWeek(dateToGenerate);
237233
day.date = dateToGenerate;
238-
day.dayNumber = dateFns.format(dateToGenerate, 'd');
234+
day.dayNumber = dateFns.format(dateToGenerate, 'd', {
235+
locale: this.appLocale,
236+
});
239237
day['isWeekendDay'] = dateFns.isWeekend(dateToGenerate);
240238
} else {
241239
day = {
242240
kw: dateFns.getWeek(dateToGenerate),
243-
dayNumber: dateFns.format(dateToGenerate, 'd'),
241+
dayNumber: dateFns.format(dateToGenerate, 'd', {
242+
locale: this.appLocale,
243+
}),
244244
date: dateToGenerate,
245245
isWeekendDay: dateFns.isWeekend(dateToGenerate),
246246
};
247247
}
248248
} else {
249249
day = {
250250
kw: dateFns.getWeek(dateToGenerate),
251-
dayNumber: dateFns.format(dateToGenerate, 'd'),
251+
dayNumber: dateFns.format(dateToGenerate, 'd', {
252+
locale: this.appLocale,
253+
}),
252254
date: dateToGenerate,
253255
isWeekendDay: dateFns.isWeekend(dateToGenerate),
254256
};
255257
}
256258
this.daysAbsolute.push(dateToGenerate);
257259
return day;
258260
}
261+
262+
getWeekDayNames(): string[] {
263+
let now = new Date();
264+
let arr = dateFns.eachDayOfInterval({
265+
start: dateFns.startOfWeek(now, { weekStartsOn: 1 }),
266+
end: dateFns.endOfWeek(now, { weekStartsOn: 1 }),
267+
});
268+
let arrOfDays: string[] = [];
269+
arr.map((a) =>
270+
arrOfDays.push(dateFns.format(a, 'EEEEEE', { locale: this.appLocale }))
271+
);
272+
return arrOfDays;
273+
}
259274
}

projects/ng-mat-components/src/lib/fs-calendar/public-api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Public API Surface of ng-mat-components
33
*/
44

5-
export { FsCalendarModule } from './fs-calendar.module';
6-
export { FsCalendarComponent } from './fs-calendar.component';
75
export * from './calendar.models';
6+
export { FsCalendarComponent } from './fs-calendar.component';
7+
export { FsCalendarModule } from './fs-calendar.module';
8+
export { FsCalendarService } from './fs-calendar.service';

0 commit comments

Comments
 (0)