Skip to content

Commit 581b9a8

Browse files
author
Matt Lewis
committed
feat(weekendDays): allow weekend days to be customised
Closes #255
1 parent 4fbe62e commit 581b9a8

9 files changed

+79
-17
lines changed

demos/demo-modules/i18n/component.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, ChangeDetectionStrategy } from '@angular/core';
2-
import { CalendarEvent, CalendarDateFormatter } from 'angular-calendar';
2+
import { CalendarEvent, CalendarDateFormatter, DAYS_OF_WEEK } from 'angular-calendar';
33
import { CustomDateFormatter } from './custom-date-formatter.provider';
44

55
@Component({
@@ -21,7 +21,12 @@ export class DemoComponent {
2121

2222
locale: string = 'fr';
2323

24-
weekStartsOn: number = 1;
24+
weekStartsOn: number = DAYS_OF_WEEK.MONDAY;
25+
26+
weekendDays: number[] = [
27+
DAYS_OF_WEEK.FRIDAY,
28+
DAYS_OF_WEEK.SATURDAY
29+
];
2530

2631
}
2732

demos/demo-modules/i18n/template.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
[viewDate]="viewDate"
1111
[events]="events"
1212
[locale]="locale"
13-
[weekStartsOn]="weekStartsOn">
13+
[weekStartsOn]="weekStartsOn"
14+
[weekendDays]="weekendDays">
1415
</mwl-calendar-month-view>
1516
<mwl-calendar-week-view
1617
*ngSwitchCase="'week'"
1718
[viewDate]="viewDate"
1819
[events]="events"
1920
[locale]="locale"
20-
[weekStartsOn]="weekStartsOn">
21+
[weekStartsOn]="weekStartsOn"
22+
[weekendDays]="weekendDays">
2123
</mwl-calendar-week-view>
2224
<mwl-calendar-day-view
2325
*ngSwitchCase="'day'"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"dependencies": {
144144
"angular-draggable-droppable": "^1.0.1",
145145
"angular-resizable-element": "^1.1.1",
146-
"calendar-utils": "0.0.53",
146+
"calendar-utils": "0.0.54",
147147
"date-fns": "^1.28.5",
148148
"positioning": "^1.0.4"
149149
}

src/components/month/calendarMonthView.component.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ export class CalendarMonthViewComponent implements OnChanges, OnInit, OnDestroy
147147
*/
148148
@Input() openDayEventsTemplate: TemplateRef<any>;
149149

150+
/**
151+
* An array of day indexes (0 = sunday, 1 = monday etc) that indicate which days are weekends
152+
*/
153+
@Input() weekendDays: number[];
154+
150155
/**
151156
* Called when the day cell is clicked
152157
*/
@@ -211,11 +216,11 @@ export class CalendarMonthViewComponent implements OnChanges, OnInit, OnDestroy
211216
*/
212217
ngOnChanges(changes: any): void {
213218

214-
if (changes.viewDate || changes.excludeDays) {
219+
if (changes.viewDate || changes.excludeDays || changes.weekendDays) {
215220
this.refreshHeader();
216221
}
217222

218-
if (changes.viewDate || changes.events || changes.excludeDays) {
223+
if (changes.viewDate || changes.events || changes.excludeDays || changes.weekendDays) {
219224
this.refreshBody();
220225
}
221226

@@ -266,7 +271,8 @@ export class CalendarMonthViewComponent implements OnChanges, OnInit, OnDestroy
266271
this.columnHeaders = this.utils.getWeekViewHeader({
267272
viewDate: this.viewDate,
268273
weekStartsOn: this.weekStartsOn,
269-
excluded: this.excludeDays
274+
excluded: this.excludeDays,
275+
weekendDays: this.weekendDays
270276
});
271277
}
272278

@@ -275,7 +281,8 @@ export class CalendarMonthViewComponent implements OnChanges, OnInit, OnDestroy
275281
events: this.events,
276282
viewDate: this.viewDate,
277283
weekStartsOn: this.weekStartsOn,
278-
excluded: this.excludeDays
284+
excluded: this.excludeDays,
285+
weekendDays: this.weekendDays
279286
});
280287
if (this.dayModifier) {
281288
this.view.days.forEach(day => this.dayModifier(day));

src/components/week/calendarWeekView.component.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
147147
*/
148148
@Input() allowDragOutside: boolean = false;
149149

150+
/**
151+
* An array of day indexes (0 = sunday, 1 = monday etc) that indicate which days are weekends
152+
*/
153+
@Input() weekendDays: number[];
154+
150155
/**
151156
* Called when a header week day is clicked
152157
*/
@@ -216,7 +221,7 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
216221
*/
217222
ngOnChanges(changes: any): void {
218223

219-
if (changes.viewDate || changes.excludeDays) {
224+
if (changes.viewDate || changes.excludeDays || changes.weekendDays) {
220225
this.refreshHeader();
221226
}
222227

@@ -349,7 +354,8 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
349354
this.days = this.utils.getWeekViewHeader({
350355
viewDate: this.viewDate,
351356
weekStartsOn: this.weekStartsOn,
352-
excluded: this.excludeDays
357+
excluded: this.excludeDays,
358+
weekendDays: this.weekendDays
353359
});
354360
}
355361

src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ export * from './calendar.module';
99
export * from './components/day/calendarDayView.component';
1010
export * from './components/month/calendarMonthView.component';
1111
export * from './components/week/calendarWeekView.component';
12-
export { CalendarEvent, EventAction as CalendarEventAction, MonthViewDay as CalendarMonthViewDay } from 'calendar-utils';
12+
export {
13+
CalendarEvent,
14+
EventAction as CalendarEventAction,
15+
MonthViewDay as CalendarMonthViewDay,
16+
DAYS_OF_WEEK
17+
} from 'calendar-utils';

test/calendarMonthView.component.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
CalendarDateFormatter,
1717
CalendarModule,
1818
MOMENT,
19-
CalendarMonthViewDay
19+
CalendarMonthViewDay,
20+
DAYS_OF_WEEK
2021
} from './../src';
2122
import { CalendarMonthViewComponent } from './../src/components/month/calendarMonthView.component';
2223
import { Subject } from 'rxjs/Subject';
@@ -527,4 +528,23 @@ describe('calendarMonthView component', () => {
527528
fixture.destroy();
528529
});
529530

531+
it('should allow the weekend days to be customised', () => {
532+
const fixture: ComponentFixture<CalendarMonthViewComponent> = TestBed.createComponent(CalendarMonthViewComponent);
533+
fixture.componentInstance.viewDate = new Date('2017-06-25');
534+
fixture.componentInstance.weekendDays = [
535+
DAYS_OF_WEEK.FRIDAY,
536+
DAYS_OF_WEEK.SATURDAY
537+
];
538+
fixture.componentInstance.ngOnChanges({viewDate: {}, weekendDays: {}});
539+
fixture.detectChanges();
540+
expect(fixture.componentInstance.view.days[0].isWeekend).to.equal(false);
541+
expect(fixture.componentInstance.view.days[5].isWeekend).to.equal(true);
542+
expect(fixture.componentInstance.view.days[6].isWeekend).to.equal(true);
543+
const headerCells: HTMLElement[] = fixture.nativeElement.querySelectorAll('.cal-header .cal-cell');
544+
expect(headerCells[0].classList.contains('cal-weekend')).to.equal(false);
545+
expect(headerCells[5].classList.contains('cal-weekend')).to.equal(true);
546+
expect(headerCells[6].classList.contains('cal-weekend')).to.equal(true);
547+
fixture.destroy();
548+
});
549+
530550
});

test/calendarWeekView.component.spec.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
CalendarDateFormatter,
1515
CalendarModule,
1616
MOMENT,
17-
CalendarEventTimesChangedEvent
17+
CalendarEventTimesChangedEvent,
18+
DAYS_OF_WEEK
1819
} from '../src';
1920
import { CalendarWeekViewComponent } from '../src/components/week/calendarWeekView.component';
2021
import { DragAndDropModule } from 'angular-draggable-droppable';
@@ -600,4 +601,20 @@ describe('calendarWeekView component', () => {
600601
});
601602
});
602603

604+
it('should allow the weekend days to be customised', () => {
605+
const fixture: ComponentFixture<CalendarWeekViewComponent> = TestBed.createComponent(CalendarWeekViewComponent);
606+
fixture.componentInstance.viewDate = new Date('2017-06-25');
607+
fixture.componentInstance.weekendDays = [
608+
DAYS_OF_WEEK.FRIDAY,
609+
DAYS_OF_WEEK.SATURDAY
610+
];
611+
fixture.componentInstance.ngOnChanges({viewDate: {}, weekendDays: {}});
612+
fixture.detectChanges();
613+
const headerCells: HTMLElement[] = fixture.nativeElement.querySelectorAll('.cal-header');
614+
expect(headerCells[0].classList.contains('cal-weekend')).to.equal(false);
615+
expect(headerCells[5].classList.contains('cal-weekend')).to.equal(true);
616+
expect(headerCells[6].classList.contains('cal-weekend')).to.equal(true);
617+
fixture.destroy();
618+
});
619+
603620
});

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,9 @@ cachedir@^1.1.0:
825825
dependencies:
826826
os-homedir "^1.0.1"
827827

828-
829-
version "0.0.53"
830-
resolved "https://registry.yarnpkg.com/calendar-utils/-/calendar-utils-0.0.53.tgz#64284dfc2d01d8e58d672ab0d8fb13ba38bb6f88"
828+
829+
version "0.0.54"
830+
resolved "https://registry.yarnpkg.com/calendar-utils/-/calendar-utils-0.0.54.tgz#deb7a2d53711bd86c10a58a1e96bb733d7b61f9b"
831831

832832
833833
version "1.0.0"

0 commit comments

Comments
 (0)