Skip to content

Commit 50159cc

Browse files
committed
feat(day-view): make previous and next view helpers respect excludeDays
1 parent d2223d5 commit 50159cc

6 files changed

+76
-43
lines changed

demos/demo-modules/exclude-days/component.ts

-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ViewEncapsulation
55
} from '@angular/core';
66
import { CalendarEvent } from 'angular-calendar';
7-
import { subDays, addDays } from 'date-fns';
87
import { colors } from '../demo-utils/colors';
98

109
@Component({
@@ -34,18 +33,4 @@ export class DemoComponent {
3433

3534
// exclude weekends
3635
excludeDays: number[] = [0, 6];
37-
38-
skipWeekends(direction: 'back' | 'forward'): void {
39-
if (this.view === 'day') {
40-
if (direction === 'back') {
41-
while (this.excludeDays.indexOf(this.viewDate.getDay()) > -1) {
42-
this.viewDate = subDays(this.viewDate, 1);
43-
}
44-
} else if (direction === 'forward') {
45-
while (this.excludeDays.indexOf(this.viewDate.getDay()) > -1) {
46-
this.viewDate = addDays(this.viewDate, 1);
47-
}
48-
}
49-
}
50-
}
5136
}

demos/demo-modules/exclude-days/template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mwlCalendarPreviousView
77
[view]="view"
88
[(viewDate)]="viewDate"
9-
(viewDateChange)="skipWeekends('back')">
9+
[excludeDays]="excludeDays">
1010
Previous
1111
</div>
1212
<div
@@ -20,7 +20,7 @@
2020
mwlCalendarNextView
2121
[view]="view"
2222
[(viewDate)]="viewDate"
23-
(viewDateChange)="skipWeekends('forward')">
23+
[excludeDays]="excludeDays">
2424
Next
2525
</div>
2626
</div>

src/modules/common/calendar-next-view.directive.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export class CalendarNextViewDirective {
3434
*/
3535
@Input() viewDate: Date;
3636

37+
/**
38+
* Days to skip when going forward by 1 day
39+
*/
40+
@Input() excludeDays: number[];
41+
3742
/**
3843
* Called when the view date is changed
3944
*/
@@ -52,6 +57,16 @@ export class CalendarNextViewDirective {
5257
month: this.dateAdapter.addMonths
5358
}[this.view];
5459

55-
this.viewDateChange.emit(addFn(this.viewDate, 1));
60+
let newDate = addFn(this.viewDate, 1);
61+
62+
while (
63+
this.view === CalendarView.Day &&
64+
this.excludeDays &&
65+
this.excludeDays.indexOf(newDate.getDay()) > -1
66+
) {
67+
newDate = this.dateAdapter.addDays(newDate, 1);
68+
}
69+
70+
this.viewDateChange.emit(newDate);
5671
}
5772
}

src/modules/common/calendar-previous-view.directive.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export class CalendarPreviousViewDirective {
3434
*/
3535
@Input() viewDate: Date;
3636

37+
/**
38+
* Days to skip when going back by 1 day
39+
*/
40+
@Input() excludeDays: number[];
41+
3742
/**
3843
* Called when the view date is changed
3944
*/
@@ -52,6 +57,16 @@ export class CalendarPreviousViewDirective {
5257
month: this.dateAdapter.subMonths
5358
}[this.view];
5459

55-
this.viewDateChange.emit(subFn(this.viewDate, 1));
60+
let newDate = subFn(this.viewDate, 1);
61+
62+
while (
63+
this.view === CalendarView.Day &&
64+
this.excludeDays &&
65+
this.excludeDays.indexOf(newDate.getDay()) > -1
66+
) {
67+
newDate = this.dateAdapter.subDays(newDate, 1);
68+
}
69+
70+
this.viewDateChange.emit(newDate);
5671
}
5772
}

test/calendar-next-view.directive.spec.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Component } from '@angular/core';
2-
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { TestBed } from '@angular/core/testing';
33
import { expect } from 'chai';
44
import { CalendarModule, DateAdapter } from '../src';
55
import { adapterFactory } from '../src/date-adapters/date-fns';
66

77
@Component({
88
template:
9-
'<button mwlCalendarNextView [view]="view" [(viewDate)]="viewDate">Next</button>'
9+
'<button mwlCalendarNextView [view]="view" [(viewDate)]="viewDate" [excludeDays]="excludeDays">Next</button>'
1010
})
1111
class TestComponent {
12-
public view: string;
13-
public viewDate: Date;
12+
view: string;
13+
viewDate: Date;
14+
excludeDays: number[];
1415
}
1516

1617
describe('mwlCalendarNextView directive', () => {
@@ -27,9 +28,7 @@ describe('mwlCalendarNextView directive', () => {
2728
});
2829

2930
it('should increase the view date by 1 month', () => {
30-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
31-
TestComponent
32-
);
31+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
3332
fixture.componentInstance.view = 'month';
3433
fixture.componentInstance.viewDate = new Date('2017-01-28');
3534
fixture.detectChanges();
@@ -42,9 +41,7 @@ describe('mwlCalendarNextView directive', () => {
4241
});
4342

4443
it('should increase the view date by 1 week', () => {
45-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
46-
TestComponent
47-
);
44+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
4845
fixture.componentInstance.view = 'week';
4946
fixture.componentInstance.viewDate = new Date('2017-01-28');
5047
fixture.detectChanges();
@@ -57,9 +54,7 @@ describe('mwlCalendarNextView directive', () => {
5754
});
5855

5956
it('should increase the view date by 1 day', () => {
60-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
61-
TestComponent
62-
);
57+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
6358
fixture.componentInstance.view = 'day';
6459
fixture.componentInstance.viewDate = new Date('2017-01-28');
6560
fixture.detectChanges();
@@ -70,4 +65,18 @@ describe('mwlCalendarNextView directive', () => {
7065
);
7166
fixture.destroy();
7267
});
68+
69+
it('should increase the view date by 1 day, skipping weekends', () => {
70+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
71+
fixture.componentInstance.view = 'day';
72+
fixture.componentInstance.viewDate = new Date('2018-06-15');
73+
fixture.componentInstance.excludeDays = [0, 6];
74+
fixture.detectChanges();
75+
fixture.nativeElement.querySelector('button').click();
76+
fixture.detectChanges();
77+
expect(fixture.componentInstance.viewDate).to.deep.equal(
78+
new Date('2018-06-18')
79+
);
80+
fixture.destroy();
81+
});
7382
});

test/calendar-previous-view.directive.spec.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Component } from '@angular/core';
2-
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { TestBed } from '@angular/core/testing';
33
import { expect } from 'chai';
44
import { CalendarModule, DateAdapter } from '../src';
55
import { adapterFactory } from '../src/date-adapters/date-fns';
66

77
@Component({
88
template:
9-
'<button mwlCalendarPreviousView [view]="view" [(viewDate)]="viewDate">Previous</button>'
9+
'<button mwlCalendarPreviousView [view]="view" [(viewDate)]="viewDate" [excludeDays]="excludeDays">Previous</button>'
1010
})
1111
class TestComponent {
1212
public view: string;
1313
public viewDate: Date;
14+
excludeDays: number[];
1415
}
1516

1617
describe('calendarPreviousView directive', () => {
@@ -27,9 +28,7 @@ describe('calendarPreviousView directive', () => {
2728
});
2829

2930
it('should decrease the view date by 1 month', () => {
30-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
31-
TestComponent
32-
);
31+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
3332
fixture.componentInstance.view = 'month';
3433
fixture.componentInstance.viewDate = new Date('2017-02-28');
3534
fixture.detectChanges();
@@ -42,9 +41,7 @@ describe('calendarPreviousView directive', () => {
4241
});
4342

4443
it('should decrease the view date by 1 week', () => {
45-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
46-
TestComponent
47-
);
44+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
4845
fixture.componentInstance.view = 'week';
4946
fixture.componentInstance.viewDate = new Date('2017-01-28');
5047
fixture.detectChanges();
@@ -57,9 +54,7 @@ describe('calendarPreviousView directive', () => {
5754
});
5855

5956
it('should decrease the view date by 1 day', () => {
60-
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
61-
TestComponent
62-
);
57+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
6358
fixture.componentInstance.view = 'day';
6459
fixture.componentInstance.viewDate = new Date('2017-01-28');
6560
fixture.detectChanges();
@@ -70,4 +65,18 @@ describe('calendarPreviousView directive', () => {
7065
);
7166
fixture.destroy();
7267
});
68+
69+
it('should decrease the view date by 1 day, skipping weekends', () => {
70+
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
71+
fixture.componentInstance.view = 'day';
72+
fixture.componentInstance.viewDate = new Date('2018-06-18');
73+
fixture.componentInstance.excludeDays = [0, 6];
74+
fixture.detectChanges();
75+
fixture.nativeElement.querySelector('button').click();
76+
fixture.detectChanges();
77+
expect(fixture.componentInstance.viewDate).to.deep.equal(
78+
new Date('2018-06-15')
79+
);
80+
fixture.destroy();
81+
});
7382
});

0 commit comments

Comments
 (0)