Skip to content

Commit 24739ce

Browse files
author
Matt Lewis
committed
feat(day-view): expose the period on the beforeViewRender output
BREAKING CHANGE: the event signature of the beforeViewRender output has changed from $event.body to $event.body.hourGrid Closes #418
1 parent cb73eff commit 24739ce

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

demos/demo-modules/selectable-period/template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*ngSwitchCase="'day'"
2323
[viewDate]="viewDate"
2424
[events]="events"
25-
(beforeViewRender)="beforeDayViewRender($event.body)"
25+
(beforeViewRender)="beforeDayViewRender($event.body.hourGrid)"
2626
(hourSegmentClicked)="hourSegmentClicked($event.date)">
2727
</mwl-calendar-day-view>
28-
</div>
28+
</div>

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

+27-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
DayView,
1717
DayViewHour,
1818
DayViewHourSegment,
19-
DayViewEvent
19+
DayViewEvent,
20+
ViewPeriod
2021
} from 'calendar-utils';
2122
import { Subject } from 'rxjs/Subject';
2223
import { Subscription } from 'rxjs/Subscription';
@@ -28,6 +29,13 @@ import { CalendarEventTimesChangedEvent } from '../common/calendar-event-times-c
2829
import { CalendarUtils } from '../common/calendar-utils.provider';
2930
import { validateEvents } from '../common/util';
3031

32+
export interface CalendarDayViewBeforeRenderEvent {
33+
body: {
34+
hourGrid: DayViewHour[];
35+
};
36+
period: ViewPeriod;
37+
}
38+
3139
/**
3240
* @hidden
3341
*/
@@ -222,32 +230,30 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
222230
* Called when an event title is clicked
223231
*/
224232
@Output()
225-
eventClicked: EventEmitter<{ event: CalendarEvent }> = new EventEmitter<{
233+
eventClicked = new EventEmitter<{
226234
event: CalendarEvent;
227235
}>();
228236

229237
/**
230238
* Called when an hour segment is clicked
231239
*/
232240
@Output()
233-
hourSegmentClicked: EventEmitter<{ date: Date }> = new EventEmitter<{
241+
hourSegmentClicked = new EventEmitter<{
234242
date: Date;
235243
}>();
236244

237245
/**
238246
* Called when an event is resized or dragged and dropped
239247
*/
240248
@Output()
241-
eventTimesChanged: EventEmitter<
242-
CalendarEventTimesChangedEvent
243-
> = new EventEmitter<CalendarEventTimesChangedEvent>();
249+
eventTimesChanged = new EventEmitter<CalendarEventTimesChangedEvent>();
244250

245251
/**
246252
* An output that will be called before the view is rendered for the current day.
247-
* If you add the `cssClass` property to a segment it will add that class to the hour segment in the template
253+
* If you add the `cssClass` property to an hour grid segment it will add that class to the hour segment in the template
248254
*/
249255
@Output()
250-
beforeViewRender: EventEmitter<{ body: DayViewHour[] }> = new EventEmitter();
256+
beforeViewRender = new EventEmitter<CalendarDayViewBeforeRenderEvent>();
251257

252258
/**
253259
* @hidden
@@ -450,9 +456,7 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
450456
minute: this.dayEndMinute
451457
}
452458
});
453-
this.beforeViewRender.emit({
454-
body: this.hours
455-
});
459+
this.emitBeforeViewRender();
456460
}
457461

458462
private refreshView(): void {
@@ -471,10 +475,22 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
471475
eventWidth: this.eventWidth,
472476
segmentHeight: this.hourSegmentHeight
473477
});
478+
this.emitBeforeViewRender();
474479
}
475480

476481
private refreshAll(): void {
477482
this.refreshHourGrid();
478483
this.refreshView();
479484
}
485+
486+
private emitBeforeViewRender(): void {
487+
if (this.hours && this.view) {
488+
this.beforeViewRender.emit({
489+
body: {
490+
hourGrid: this.hours
491+
},
492+
period: this.view.period
493+
});
494+
}
495+
}
480496
}

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ describe('CalendarDayViewComponent component', () => {
318318
> = TestBed.createComponent(CalendarDayViewComponent);
319319
fixture.componentInstance.viewDate = new Date('2016-06-27');
320320
fixture.componentInstance.beforeViewRender.take(1).subscribe(({ body }) => {
321-
body.forEach(hour => {
321+
body.hourGrid.forEach(hour => {
322322
hour.segments.forEach(segment => {
323323
segment.cssClass = 'foo';
324324
});
@@ -1012,4 +1012,45 @@ describe('CalendarDayViewComponent component', () => {
10121012
fixture.nativeElement.querySelector('.cal-hour-segment').style.height
10131013
).to.equal('45px');
10141014
});
1015+
1016+
it('should only call the beforeViewRender output once when refreshing the view', () => {
1017+
const fixture: ComponentFixture<
1018+
CalendarDayViewComponent
1019+
> = TestBed.createComponent(CalendarDayViewComponent);
1020+
fixture.componentInstance.refresh = new Subject();
1021+
fixture.componentInstance.ngOnInit();
1022+
fixture.componentInstance.viewDate = new Date('2016-06-27');
1023+
const beforeViewRenderCalled = sinon.spy();
1024+
// use subscription to test that it was only called a max of one times
1025+
const subscription = fixture.componentInstance.beforeViewRender.subscribe(
1026+
beforeViewRenderCalled
1027+
);
1028+
fixture.componentInstance.refresh.next(true);
1029+
expect(beforeViewRenderCalled).to.have.callCount(1);
1030+
subscription.unsubscribe();
1031+
fixture.destroy();
1032+
});
1033+
1034+
it('should expose the view period on the beforeViewRender output', () => {
1035+
const fixture: ComponentFixture<
1036+
CalendarDayViewComponent
1037+
> = TestBed.createComponent(CalendarDayViewComponent);
1038+
const beforeViewRenderCalled = sinon.spy();
1039+
fixture.componentInstance.beforeViewRender
1040+
.take(1)
1041+
.subscribe(beforeViewRenderCalled);
1042+
fixture.componentInstance.ngOnInit();
1043+
fixture.componentInstance.viewDate = new Date('2016-06-27');
1044+
fixture.componentInstance.ngOnChanges({ viewDate: {} });
1045+
expect(
1046+
beforeViewRenderCalled.getCall(0).args[0].period.start instanceof Date
1047+
).to.equal(true);
1048+
expect(
1049+
beforeViewRenderCalled.getCall(0).args[0].period.end instanceof Date
1050+
).to.equal(true);
1051+
expect(
1052+
Array.isArray(beforeViewRenderCalled.getCall(0).args[0].period.events)
1053+
).to.equal(true);
1054+
fixture.destroy();
1055+
});
10151056
});

0 commit comments

Comments
 (0)