Skip to content

Commit 5ca90c9

Browse files
committed
fix(week-view): handle excluding non consecutive days
Fixes #973
1 parent ad0a521 commit 5ca90c9

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

projects/angular-calendar/src/modules/common/util.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,25 @@ export function getWeekViewPeriod(
147147
let viewStart = daysInWeek
148148
? dateAdapter.startOfDay(viewDate)
149149
: dateAdapter.startOfWeek(viewDate, { weekStartsOn });
150-
if (excluded.indexOf(dateAdapter.getDay(viewStart)) > -1) {
151-
viewStart = dateAdapter.subDays(
152-
addDaysWithExclusions(dateAdapter, viewStart, 1, excluded),
153-
1
154-
);
150+
const endOfWeek = dateAdapter.endOfWeek(viewDate, { weekStartsOn });
151+
while (
152+
excluded.indexOf(dateAdapter.getDay(viewStart)) > -1 &&
153+
viewStart < endOfWeek
154+
) {
155+
viewStart = dateAdapter.addDays(viewStart, 1);
155156
}
156157
if (daysInWeek) {
157158
const viewEnd = dateAdapter.endOfDay(
158159
addDaysWithExclusions(dateAdapter, viewStart, daysInWeek - 1, excluded)
159160
);
160161
return { viewStart, viewEnd };
161162
} else {
162-
let viewEnd = dateAdapter.endOfWeek(viewDate, { weekStartsOn });
163-
if (excluded.indexOf(dateAdapter.getDay(viewEnd)) > -1) {
164-
viewEnd = dateAdapter.addDays(
165-
addDaysWithExclusions(dateAdapter, viewEnd, -1, excluded),
166-
1
167-
);
163+
let viewEnd = endOfWeek;
164+
while (
165+
excluded.indexOf(dateAdapter.getDay(viewEnd)) > -1 &&
166+
viewEnd > viewStart
167+
) {
168+
viewEnd = dateAdapter.subDays(viewEnd, 1);
168169
}
169170
return { viewStart, viewEnd };
170171
}

projects/angular-calendar/test/calendar-week-view.component.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ describe('calendarWeekView component', () => {
9999
fixture.destroy();
100100
});
101101

102+
it('should support excluding non consecutive days', () => {
103+
const fixture: ComponentFixture<
104+
CalendarWeekViewComponent
105+
> = TestBed.createComponent(CalendarWeekViewComponent);
106+
fixture.componentInstance.viewDate = new Date('2016-06-29');
107+
fixture.componentInstance.excludeDays = [4, 6];
108+
fixture.componentInstance.ngOnChanges({ viewDate: {} });
109+
expect(fixture.componentInstance.days.length).to.equal(5);
110+
fixture.destroy();
111+
});
112+
113+
it('should support excluding all but 1 day', () => {
114+
const fixture: ComponentFixture<
115+
CalendarWeekViewComponent
116+
> = TestBed.createComponent(CalendarWeekViewComponent);
117+
fixture.componentInstance.viewDate = new Date('2016-06-29');
118+
fixture.componentInstance.excludeDays = [0, 1, 2, 3, 4, 5];
119+
fixture.componentInstance.ngOnChanges({ viewDate: {} });
120+
expect(fixture.componentInstance.days.length).to.equal(1);
121+
fixture.destroy();
122+
});
123+
102124
it('should generate the week view with default colors for events', () => {
103125
const fixture: ComponentFixture<
104126
CalendarWeekViewComponent

0 commit comments

Comments
 (0)