Skip to content

Commit

Permalink
feat(day-view): make previous and next view helpers respect excludeDays
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlewis92 committed Jun 18, 2018
1 parent d2223d5 commit 50159cc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 43 deletions.
15 changes: 0 additions & 15 deletions demos/demo-modules/exclude-days/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ViewEncapsulation
} from '@angular/core';
import { CalendarEvent } from 'angular-calendar';
import { subDays, addDays } from 'date-fns';
import { colors } from '../demo-utils/colors';

@Component({
Expand Down Expand Up @@ -34,18 +33,4 @@ export class DemoComponent {

// exclude weekends
excludeDays: number[] = [0, 6];

skipWeekends(direction: 'back' | 'forward'): void {
if (this.view === 'day') {
if (direction === 'back') {
while (this.excludeDays.indexOf(this.viewDate.getDay()) > -1) {
this.viewDate = subDays(this.viewDate, 1);
}
} else if (direction === 'forward') {
while (this.excludeDays.indexOf(this.viewDate.getDay()) > -1) {
this.viewDate = addDays(this.viewDate, 1);
}
}
}
}
}
4 changes: 2 additions & 2 deletions demos/demo-modules/exclude-days/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mwlCalendarPreviousView
[view]="view"
[(viewDate)]="viewDate"
(viewDateChange)="skipWeekends('back')">
[excludeDays]="excludeDays">
Previous
</div>
<div
Expand All @@ -20,7 +20,7 @@
mwlCalendarNextView
[view]="view"
[(viewDate)]="viewDate"
(viewDateChange)="skipWeekends('forward')">
[excludeDays]="excludeDays">
Next
</div>
</div>
Expand Down
17 changes: 16 additions & 1 deletion src/modules/common/calendar-next-view.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export class CalendarNextViewDirective {
*/
@Input() viewDate: Date;

/**
* Days to skip when going forward by 1 day
*/
@Input() excludeDays: number[];

/**
* Called when the view date is changed
*/
Expand All @@ -52,6 +57,16 @@ export class CalendarNextViewDirective {
month: this.dateAdapter.addMonths
}[this.view];

this.viewDateChange.emit(addFn(this.viewDate, 1));
let newDate = addFn(this.viewDate, 1);

while (
this.view === CalendarView.Day &&
this.excludeDays &&
this.excludeDays.indexOf(newDate.getDay()) > -1
) {
newDate = this.dateAdapter.addDays(newDate, 1);
}

this.viewDateChange.emit(newDate);
}
}
17 changes: 16 additions & 1 deletion src/modules/common/calendar-previous-view.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export class CalendarPreviousViewDirective {
*/
@Input() viewDate: Date;

/**
* Days to skip when going back by 1 day
*/
@Input() excludeDays: number[];

/**
* Called when the view date is changed
*/
Expand All @@ -52,6 +57,16 @@ export class CalendarPreviousViewDirective {
month: this.dateAdapter.subMonths
}[this.view];

this.viewDateChange.emit(subFn(this.viewDate, 1));
let newDate = subFn(this.viewDate, 1);

while (
this.view === CalendarView.Day &&
this.excludeDays &&
this.excludeDays.indexOf(newDate.getDay()) > -1
) {
newDate = this.dateAdapter.subDays(newDate, 1);
}

this.viewDateChange.emit(newDate);
}
}
35 changes: 22 additions & 13 deletions test/calendar-next-view.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { expect } from 'chai';
import { CalendarModule, DateAdapter } from '../src';
import { adapterFactory } from '../src/date-adapters/date-fns';

@Component({
template:
'<button mwlCalendarNextView [view]="view" [(viewDate)]="viewDate">Next</button>'
'<button mwlCalendarNextView [view]="view" [(viewDate)]="viewDate" [excludeDays]="excludeDays">Next</button>'
})
class TestComponent {
public view: string;
public viewDate: Date;
view: string;
viewDate: Date;
excludeDays: number[];
}

describe('mwlCalendarNextView directive', () => {
Expand All @@ -27,9 +28,7 @@ describe('mwlCalendarNextView directive', () => {
});

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

it('should increase the view date by 1 week', () => {
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
TestComponent
);
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'week';
fixture.componentInstance.viewDate = new Date('2017-01-28');
fixture.detectChanges();
Expand All @@ -57,9 +54,7 @@ describe('mwlCalendarNextView directive', () => {
});

it('should increase the view date by 1 day', () => {
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
TestComponent
);
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'day';
fixture.componentInstance.viewDate = new Date('2017-01-28');
fixture.detectChanges();
Expand All @@ -70,4 +65,18 @@ describe('mwlCalendarNextView directive', () => {
);
fixture.destroy();
});

it('should increase the view date by 1 day, skipping weekends', () => {
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'day';
fixture.componentInstance.viewDate = new Date('2018-06-15');
fixture.componentInstance.excludeDays = [0, 6];
fixture.detectChanges();
fixture.nativeElement.querySelector('button').click();
fixture.detectChanges();
expect(fixture.componentInstance.viewDate).to.deep.equal(
new Date('2018-06-18')
);
fixture.destroy();
});
});
31 changes: 20 additions & 11 deletions test/calendar-previous-view.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { expect } from 'chai';
import { CalendarModule, DateAdapter } from '../src';
import { adapterFactory } from '../src/date-adapters/date-fns';

@Component({
template:
'<button mwlCalendarPreviousView [view]="view" [(viewDate)]="viewDate">Previous</button>'
'<button mwlCalendarPreviousView [view]="view" [(viewDate)]="viewDate" [excludeDays]="excludeDays">Previous</button>'
})
class TestComponent {
public view: string;
public viewDate: Date;
excludeDays: number[];
}

describe('calendarPreviousView directive', () => {
Expand All @@ -27,9 +28,7 @@ describe('calendarPreviousView directive', () => {
});

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

it('should decrease the view date by 1 week', () => {
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
TestComponent
);
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'week';
fixture.componentInstance.viewDate = new Date('2017-01-28');
fixture.detectChanges();
Expand All @@ -57,9 +54,7 @@ describe('calendarPreviousView directive', () => {
});

it('should decrease the view date by 1 day', () => {
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
TestComponent
);
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'day';
fixture.componentInstance.viewDate = new Date('2017-01-28');
fixture.detectChanges();
Expand All @@ -70,4 +65,18 @@ describe('calendarPreviousView directive', () => {
);
fixture.destroy();
});

it('should decrease the view date by 1 day, skipping weekends', () => {
const fixture = TestBed.createComponent<TestComponent>(TestComponent);
fixture.componentInstance.view = 'day';
fixture.componentInstance.viewDate = new Date('2018-06-18');
fixture.componentInstance.excludeDays = [0, 6];
fixture.detectChanges();
fixture.nativeElement.querySelector('button').click();
fixture.detectChanges();
expect(fixture.componentInstance.viewDate).to.deep.equal(
new Date('2018-06-15')
);
fixture.destroy();
});
});

0 comments on commit 50159cc

Please sign in to comment.