Skip to content

Commit cb73eff

Browse files
author
Matt Lewis
committed
feat(week-view): expose the view period on beforeViewRender
BREAKING CHANGE: for people extending the root week view component, eventRows is now named view.eventRows Closes #418
1 parent a6619bf commit cb73eff

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

src/modules/week/calendar-week-view.component.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import {
1717
WeekDay,
1818
CalendarEvent,
1919
WeekViewEvent,
20-
WeekViewEventRow
20+
WeekViewEventRow,
21+
WeekView,
22+
ViewPeriod,
23+
MonthViewDay
2124
} from 'calendar-utils';
2225
import { ResizeEvent } from 'angular-resizable-element';
2326
import addDays from 'date-fns/add_days';
@@ -33,6 +36,11 @@ export interface WeekViewEventResize {
3336
edge: string;
3437
}
3538

39+
export interface CalendarWeekViewBeforeRenderEvent {
40+
header: WeekDay[];
41+
period: ViewPeriod;
42+
}
43+
3644
/**
3745
* Shows all events on a given week. Example usage:
3846
*
@@ -54,7 +62,7 @@ export interface WeekViewEventResize {
5462
(dayHeaderClicked)="dayHeaderClicked.emit($event)"
5563
(eventDropped)="eventTimesChanged.emit($event)">
5664
</mwl-calendar-week-view-header>
57-
<div *ngFor="let eventRow of eventRows" #eventRowContainer class="cal-events-row">
65+
<div *ngFor="let eventRow of view.eventRows" #eventRowContainer class="cal-events-row">
5866
<div
5967
*ngFor="let weekEvent of eventRow.row"
6068
#event
@@ -194,7 +202,7 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
194202
* If you add the `cssClass` property to a day in the header it will add that class to the cell element in the template
195203
*/
196204
@Output()
197-
beforeViewRender: EventEmitter<{ header: WeekDay[] }> = new EventEmitter();
205+
beforeViewRender = new EventEmitter<CalendarWeekViewBeforeRenderEvent>();
198206

199207
/**
200208
* @hidden
@@ -204,7 +212,7 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
204212
/**
205213
* @hidden
206214
*/
207-
eventRows: WeekViewEventRow[] = [];
215+
view: WeekView;
208216

209217
/**
210218
* @hidden
@@ -401,24 +409,32 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
401409
excluded: this.excludeDays,
402410
weekendDays: this.weekendDays
403411
});
404-
this.beforeViewRender.emit({
405-
header: this.days
406-
});
412+
this.emitBeforeViewRender();
407413
}
408414

409415
private refreshBody(): void {
410-
this.eventRows = this.utils.getWeekView({
416+
this.view = this.utils.getWeekView({
411417
events: this.events,
412418
viewDate: this.viewDate,
413419
weekStartsOn: this.weekStartsOn,
414420
excluded: this.excludeDays,
415421
precision: this.precision,
416422
absolutePositionedEvents: true
417-
}).eventRows;
423+
});
424+
this.emitBeforeViewRender();
418425
}
419426

420427
private refreshAll(): void {
421428
this.refreshHeader();
422429
this.refreshBody();
423430
}
431+
432+
private emitBeforeViewRender(): void {
433+
if (this.days && this.view) {
434+
this.beforeViewRender.emit({
435+
header: this.days,
436+
period: this.view.period
437+
});
438+
}
439+
}
424440
}

src/modules/week/calendar-week.module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { CalendarWeekViewHeaderComponent } from './calendar-week-view-header.com
77
import { CalendarWeekViewEventComponent } from './calendar-week-view-event.component';
88
import { CalendarCommonModule } from '../common/calendar-common.module';
99

10-
export { CalendarWeekViewComponent } from './calendar-week-view.component';
10+
export {
11+
CalendarWeekViewComponent,
12+
CalendarWeekViewBeforeRenderEvent
13+
} from './calendar-week-view.component';
1114
export {
1215
WeekViewEvent as CalendarWeekViewEvent,
1316
WeekViewEventRow as CalendarWeekViewEventRow,

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

+44-3
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ describe('calendarWeekView component', () => {
180180
};
181181
fixture.componentInstance.events.push(event);
182182
fixture.componentInstance.refresh.next(true);
183-
expect(fixture.componentInstance.eventRows[0].row[0].event).to.deep.equal(
184-
event
185-
);
183+
expect(
184+
fixture.componentInstance.view.eventRows[0].row[0].event
185+
).to.deep.equal(event);
186186
fixture.destroy();
187187
});
188188

@@ -837,4 +837,45 @@ describe('calendarWeekView component', () => {
837837
stub.restore();
838838
expect(stub).to.have.been.calledOnce; // tslint:disable-line
839839
});
840+
841+
it('should only call the beforeViewRender output once when refreshing the view', () => {
842+
const fixture: ComponentFixture<
843+
CalendarWeekViewComponent
844+
> = TestBed.createComponent(CalendarWeekViewComponent);
845+
fixture.componentInstance.refresh = new Subject();
846+
fixture.componentInstance.ngOnInit();
847+
fixture.componentInstance.viewDate = new Date('2016-06-27');
848+
const beforeViewRenderCalled = sinon.spy();
849+
// use subscription to test that it was only called a max of one times
850+
const subscription = fixture.componentInstance.beforeViewRender.subscribe(
851+
beforeViewRenderCalled
852+
);
853+
fixture.componentInstance.refresh.next(true);
854+
expect(beforeViewRenderCalled).to.have.callCount(1);
855+
subscription.unsubscribe();
856+
fixture.destroy();
857+
});
858+
859+
it('should expose the view period on the beforeViewRender output', () => {
860+
const fixture: ComponentFixture<
861+
CalendarWeekViewComponent
862+
> = TestBed.createComponent(CalendarWeekViewComponent);
863+
const beforeViewRenderCalled = sinon.spy();
864+
fixture.componentInstance.beforeViewRender
865+
.take(1)
866+
.subscribe(beforeViewRenderCalled);
867+
fixture.componentInstance.ngOnInit();
868+
fixture.componentInstance.viewDate = new Date('2016-06-27');
869+
fixture.componentInstance.ngOnChanges({ viewDate: {} });
870+
expect(
871+
beforeViewRenderCalled.getCall(0).args[0].period.start instanceof Date
872+
).to.equal(true);
873+
expect(
874+
beforeViewRenderCalled.getCall(0).args[0].period.end instanceof Date
875+
).to.equal(true);
876+
expect(
877+
Array.isArray(beforeViewRenderCalled.getCall(0).args[0].period.events)
878+
).to.equal(true);
879+
fixture.destroy();
880+
});
840881
});

0 commit comments

Comments
 (0)