Skip to content

Commit eacce90

Browse files
authored
fix: calendar panels value binding (#46)
1 parent b8eea35 commit eacce90

File tree

12 files changed

+70
-30
lines changed

12 files changed

+70
-30
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { BrowserModule } from '@angular/platform-browser';
44
import { APP_BASE_HREF } from '@angular/common';
55
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
66
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
7-
import { de } from 'date-fns/locale';
87
import { AppRoutingModule } from './app-routing.module';
98
import { AppComponent } from './app.component';
109

@@ -16,12 +15,15 @@ import { MatIconModule } from '@angular/material/icon';
1615
import { MatInputModule } from '@angular/material/input';
1716
import { MatSelectModule } from '@angular/material/select';
1817
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
18+
import * as dateFnsLocales from 'date-fns/locale';
1919
import { FsCalendarModule, FsCalendarService, FsNavFrameModule } from 'projects/ng-mat-components/src/public-api';
2020
import { HomeComponent } from './content/home/home.component';
2121
import { ShowcaseCalendarPanelsComponent } from './content/showcase-calendar-panels/showcase-calendar-panels.component';
2222
import { ShowcaseCalendarTableComponent } from './content/showcase-calendar-table/showcase-calendar-table.component';
2323
import { ShowcaseNavFrameComponent } from './content/showcase-nav-frame/showcase-nav-frame.component';
2424

25+
const locales: any = dateFnsLocales;
26+
2527
@NgModule({
2628
declarations: [AppComponent, HomeComponent, ShowcaseCalendarPanelsComponent, ShowcaseCalendarTableComponent, ShowcaseNavFrameComponent],
2729
imports: [
@@ -52,7 +54,7 @@ import { ShowcaseNavFrameComponent } from './content/showcase-nav-frame/showcase
5254
{
5355
provide: 'FS_DATE_LOCALE',
5456
useClass: FsCalendarService,
55-
useValue: de,
57+
useValue: locales[navigator.language],
5658
},
5759
],
5860
bootstrap: [AppComponent],
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
<fs-calendar-table class="mat-elevation-z4" [dataSource]="calTableData">
1+
<mat-card class="flex-row-wrap" style="margin-bottom: 0.25rem">
2+
<div>
3+
<p>Selected month: {{ month + 1 }}, Selected year: {{ year }}</p>
4+
</div>
5+
</mat-card>
6+
7+
<fs-calendar-table class="mat-elevation-z2" [(month)]="month" [(year)]="year" [dataSource]="calTableData">
28
<fs-calendar-table-name>Persons</fs-calendar-table-name>
39
</fs-calendar-table>

projects/lib-workspace/src/app/content/showcase-calendar-table/showcase-calendar-table.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { CalendarTableEntry } from 'projects/ng-mat-components/src/public-api';
88
})
99
export class ShowcaseCalendarTableComponent implements OnInit {
1010
today = new Date();
11+
month: number = this.today.getMonth();
12+
year: number = this.today.getFullYear();
1113

1214
calTableData: CalendarTableEntry[] = [
1315
{
@@ -37,7 +39,7 @@ export class ShowcaseCalendarTableComponent implements OnInit {
3739
},
3840
},
3941
{
40-
date: new Date(this.today.getFullYear(), this.today.getMonth(), 10),
42+
date: new Date(this.today.getFullYear(), this.today.getMonth(), 11),
4143
toolTip: 'Some text',
4244
char: 'U',
4345
colors: {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<div style="width: 40px"></div>
3434
</ng-template>
3535
</div>
36-
<table class="fs-calendar-table" [style.width]="dataSource.config.panelWidth">
37-
<thead class="fs-calendar-table-header">
36+
<table class="fs-calendar-panels-table" [style.width]="dataSource.config.panelWidth">
37+
<thead class="fs-calendar-panels-table-header">
3838
<tr>
3939
<th scope="col" *ngIf="dataSource.config.calendarWeek"></th>
4040
<th *ngFor="let dayname of calendar.dayNames" scope="col">

projects/ng-mat-components/src/fs-calendar/calendar-panels/calendar-panels.component.scss

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
justify-content: space-between;
1818
}
1919

20-
.fs-calendar-table {
20+
.fs-calendar-panels-table {
2121
border-spacing: 0;
2222
border-collapse: collapse;
2323
width: 100%;
@@ -37,7 +37,7 @@
3737
min-height: 10px;
3838
}
3939

40-
.fs-calendar-table-header th {
40+
.fs-calendar-panels-table-header th {
4141
text-align: center;
4242
padding: 0 0 8px 0;
4343
font-size: 13px;
@@ -49,15 +49,15 @@
4949
font-size: 13px;
5050
}
5151

52-
.fs-calendar-table thead tr td button {
52+
.fs-calendar-panels-table thead tr td button {
5353
visibility: hidden;
5454
}
5555

56-
.fs-calendar-table:first-child thead tr td:first-child button {
56+
.fs-calendar-panels-table:first-child thead tr td:first-child button {
5757
visibility: visible;
5858
}
5959

60-
.fs-calendar-table:last-child thead tr td:last-child button {
60+
.fs-calendar-panels-table:last-child thead tr td:last-child button {
6161
visibility: visible;
6262
}
6363

@@ -147,4 +147,4 @@
147147

148148
.tooltip .tooltiptext {
149149
padding: 16px;
150-
}
150+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,22 @@ export class FsCalendarPanelsComponent implements OnInit {
116116
this.selectedDayStart = day.date;
117117
} else {
118118
this.selectedDayEnd = day.date;
119+
120+
let daysBetween: number = dateFns.differenceInDays(this.selectedDayStart, this.selectedDayEnd);
121+
let daysAffected: Date[] = [];
122+
123+
daysAffected.push(this.selectedDayStart);
124+
if (daysBetween < 0) {
125+
for (let index = 1; index < daysBetween * -1 + 1; index++) {
126+
daysAffected.push(dateFns.addDays(this.selectedDayStart, index));
127+
}
128+
}
129+
119130
this.selection.emit({
120131
type: 'range',
121132
start: this.selectedDayStart,
122133
end: this.selectedDayEnd,
134+
affectedDays: daysAffected,
123135
});
124136
}
125137
} else {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
</svg>
1414
</button>
1515
</th>
16-
<th *ngFor="let day of currentMonth.days" [class.date-today]="isToday(day.date)">
16+
<th
17+
*ngFor="let day of currentMonth.days"
18+
[class.date-today]="isToday(day.date)"
19+
[class.weekend]="day._meta?.isWeekendDay && !isToday(day.date) && markWeekend">
1720
{{ day._meta?.dayNumber }}
1821
<br />
19-
{{ day.date | date : 'EEEEEE' }}
22+
{{ currentMonth.dayNames[day._meta!.dayOfWeek - 1] }}
2023
</th>
2124
<th>
2225
<button mat-icon-button class="fs-calendar-switches" (click)="onMonthForward()">
@@ -32,7 +35,7 @@
3235
<tbody role="rowgroup">
3336
<tr *ngFor="let entry of tableData">
3437
<td colspan="2">{{ entry.name }}</td>
35-
<td *ngFor="let day of entry.data" [class.tooltip]="day.toolTip">
38+
<td *ngFor="let day of entry.data" [class.tooltip]="day.toolTip" [class.weekend]="day._meta?.isWeekendDay && markWeekend">
3639
<span class="tooltiptext" *ngIf="day.toolTip">{{ day.toolTip }}</span>
3740
<div class="box box-base" [style.backgroundColor]="day.colors?.backgroundColor" [style.color]="day.colors?.color">
3841
{{ day.char }}

projects/ng-mat-components/src/fs-calendar/calendar-table/fs-calendar-table.component.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
.fs-calendar-table-helper {
1+
:host {
2+
display: block;
3+
position: relative;
24
padding: 1rem;
35
border-radius: 4px;
6+
// margin: .25rem;
47
}
58

69
table {
@@ -46,7 +49,6 @@ table tr td:not(:first-child) {
4649
.box {
4750
width: 24px;
4851
height: 24px;
49-
// background: lightblue;
5052
border-radius: 4px;
5153
margin: 4px;
5254
font-size: 16px;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
22
import * as dateFns from 'date-fns';
33
import { CalendarMonth, CalendarTableEntry } from '../calendar.models';
44
import { FsCalendarService } from '../services/fs-calendar.service';
@@ -21,13 +21,17 @@ export class FsCalendarTableComponent implements OnInit {
2121
currentMonth: CalendarMonth = this.calendarService.generateMonth(this._yearNumber, this._monthNumber, []);
2222
tableData: CalendarTableEntry[] = [];
2323

24+
markWeekend = true;
25+
2426
get dataSource(): CalendarTableEntry[] {
2527
return this._dataSource;
2628
}
2729
get month(): number {
30+
this.monthChange.emit(this._monthNumber);
2831
return this._monthNumber;
2932
}
3033
get year(): number {
34+
this.yearChange.emit(this._yearNumber);
3135
return this._yearNumber;
3236
}
3337

@@ -42,11 +46,14 @@ export class FsCalendarTableComponent implements OnInit {
4246
this._monthNumber = data;
4347
this.genMonthData();
4448
}
49+
@Output() monthChange = new EventEmitter<number>();
50+
4551
@Input()
4652
set year(data: number) {
4753
this._yearNumber = data;
4854
this.genMonthData();
4955
}
56+
@Output() yearChange = new EventEmitter<number>();
5057

5158
constructor(private calendarService: FsCalendarService) {}
5259

projects/ng-mat-components/src/fs-calendar/calendar.models.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface CalendarMonth {
1717
name: string;
1818
year: number;
1919
days: CalendarExtendedDay[];
20+
dayNames: String[];
2021
}
2122

2223
/**
@@ -29,6 +30,7 @@ export interface CalendarSelectedRange {
2930
type: 'range';
3031
start: Date;
3132
end: Date;
33+
affectedDays: Date[];
3234
}
3335

3436
/**
@@ -140,6 +142,7 @@ export interface CalendarExtendedDay {
140142
kw: number;
141143
type: 'cw' | 'plHolder' | 'day';
142144
dayNumber: string;
145+
dayOfWeek: number;
143146
isWeekendDay: boolean;
144147
};
145148
}

0 commit comments

Comments
 (0)