Skip to content

Commit b00d57c

Browse files
committed
fix(day-view): allow events with no end date to be resized
BREAKING CHANGE: previously events with no end date that were resized would emit an empty end time, now they will emit a sensible default new end date Fixes #614
1 parent 94a8e79 commit b00d57c

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,15 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
456456
MINUTES_IN_HOUR / (this.hourSegments * this.hourSegmentHeight);
457457
const minutesMoved: number = pixelsMoved * pixelAmountInMinutes;
458458
let newStart: Date = dayEvent.event.start;
459-
let newEnd: Date = dayEvent.event.end;
459+
let newEnd: Date =
460+
dayEvent.event.end ||
461+
this.dateAdapter.addMinutes(
462+
dayEvent.event.start,
463+
30 * pixelAmountInMinutes
464+
);
460465
if (resizingBeforeStart) {
461466
newStart = this.dateAdapter.addMinutes(newStart, minutesMoved);
462-
} else if (newEnd) {
467+
} else {
463468
newEnd = this.dateAdapter.addMinutes(newEnd, minutesMoved);
464469
}
465470

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

+116
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,122 @@ describe('CalendarDayViewComponent component', () => {
490490
});
491491
});
492492

493+
it.only('should resize events with no end date', () => {
494+
const fixture: ComponentFixture<
495+
CalendarDayViewComponent
496+
> = TestBed.createComponent(CalendarDayViewComponent);
497+
fixture.componentInstance.viewDate = new Date('2016-06-27');
498+
fixture.componentInstance.events = [
499+
{
500+
title: 'foo',
501+
color: { primary: '', secondary: '' },
502+
start: moment('2016-06-27')
503+
.add(4, 'hours')
504+
.toDate(),
505+
resizable: {
506+
afterEnd: true
507+
}
508+
}
509+
];
510+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
511+
fixture.detectChanges();
512+
document.body.appendChild(fixture.nativeElement);
513+
const event: HTMLElement = fixture.nativeElement.querySelector(
514+
'.cal-event-container'
515+
);
516+
const rect: ClientRect = event.getBoundingClientRect();
517+
let resizeEvent: CalendarEventTimesChangedEvent;
518+
fixture.componentInstance.eventTimesChanged.subscribe(e => {
519+
resizeEvent = e;
520+
});
521+
triggerDomEvent('mousedown', document.body, {
522+
clientY: rect.bottom,
523+
clientX: rect.left + 10
524+
});
525+
fixture.detectChanges();
526+
triggerDomEvent('mousemove', document.body, {
527+
clientY: rect.bottom + 30,
528+
clientX: rect.left + 10
529+
});
530+
fixture.detectChanges();
531+
expect(event.getBoundingClientRect().bottom).to.equal(rect.bottom + 30);
532+
expect(event.getBoundingClientRect().height).to.equal(60);
533+
triggerDomEvent('mouseup', document.body, {
534+
clientY: rect.top + 30,
535+
clientX: rect.left + 10
536+
});
537+
fixture.detectChanges();
538+
fixture.destroy();
539+
expect(resizeEvent).to.deep.equal({
540+
event: fixture.componentInstance.events[0],
541+
newStart: moment('2016-06-27')
542+
.add(4, 'hours')
543+
.toDate(),
544+
newEnd: moment('2016-06-27')
545+
.add(5, 'hours')
546+
.toDate()
547+
});
548+
});
549+
550+
it.only('should resize events with no end date with a custom amount of segments', () => {
551+
const fixture: ComponentFixture<
552+
CalendarDayViewComponent
553+
> = TestBed.createComponent(CalendarDayViewComponent);
554+
fixture.componentInstance.viewDate = new Date('2016-06-27');
555+
fixture.componentInstance.hourSegments = 4;
556+
fixture.componentInstance.events = [
557+
{
558+
title: 'foo',
559+
color: { primary: '', secondary: '' },
560+
start: moment('2016-06-27')
561+
.add(4, 'hours')
562+
.toDate(),
563+
resizable: {
564+
afterEnd: true
565+
}
566+
}
567+
];
568+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
569+
fixture.detectChanges();
570+
document.body.appendChild(fixture.nativeElement);
571+
const event: HTMLElement = fixture.nativeElement.querySelector(
572+
'.cal-event-container'
573+
);
574+
const rect: ClientRect = event.getBoundingClientRect();
575+
let resizeEvent: CalendarEventTimesChangedEvent;
576+
fixture.componentInstance.eventTimesChanged.subscribe(e => {
577+
resizeEvent = e;
578+
});
579+
triggerDomEvent('mousedown', document.body, {
580+
clientY: rect.bottom,
581+
clientX: rect.left + 10
582+
});
583+
fixture.detectChanges();
584+
triggerDomEvent('mousemove', document.body, {
585+
clientY: rect.bottom + 30,
586+
clientX: rect.left + 10
587+
});
588+
fixture.detectChanges();
589+
expect(event.getBoundingClientRect().bottom).to.equal(rect.bottom + 30);
590+
expect(event.getBoundingClientRect().height).to.equal(60);
591+
triggerDomEvent('mouseup', document.body, {
592+
clientY: rect.top + 30,
593+
clientX: rect.left + 10
594+
});
595+
fixture.detectChanges();
596+
fixture.destroy();
597+
expect(resizeEvent).to.deep.equal({
598+
event: fixture.componentInstance.events[0],
599+
newStart: moment('2016-06-27')
600+
.add(4, 'hours')
601+
.toDate(),
602+
newEnd: moment('2016-06-27')
603+
.add(4, 'hours')
604+
.add(30, 'minutes')
605+
.toDate()
606+
});
607+
});
608+
493609
it('should resize the event and respect the event snap size', () => {
494610
const fixture: ComponentFixture<
495611
CalendarDayViewComponent

0 commit comments

Comments
 (0)