Skip to content

Commit ccffe05

Browse files
committed
fix(week-view): allow resizing events with no end date
BREAKING CHANGE: events with no end date that are resized now assume to have the start date as the end date Fixes #614
1 parent 56dc132 commit ccffe05

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
368368
weekEvent
369369
);
370370

371+
const resizingBeforeStart = currentResize.edge === 'left';
371372
let daysDiff: number;
372-
if (currentResize.edge === 'left') {
373+
if (resizingBeforeStart) {
373374
daysDiff = weekEvent.offset - currentResize.originalOffset;
374375
} else {
375376
daysDiff = weekEvent.span - currentResize.originalSpan;
@@ -379,10 +380,10 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
379380
weekEvent.span = currentResize.originalSpan;
380381

381382
let newStart: Date = weekEvent.event.start;
382-
let newEnd: Date = weekEvent.event.end;
383-
if (currentResize.edge === 'left') {
383+
let newEnd: Date = weekEvent.event.end || weekEvent.event.start;
384+
if (resizingBeforeStart) {
384385
newStart = this.dateAdapter.addDays(newStart, daysDiff);
385-
} else if (newEnd) {
386+
} else {
386387
newEnd = this.dateAdapter.addDays(newEnd, daysDiff);
387388
}
388389

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

+63
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,69 @@ describe('calendarWeekView component', () => {
485485
});
486486
});
487487

488+
it('should resize events with no end date', () => {
489+
const fixture: ComponentFixture<
490+
CalendarWeekViewComponent
491+
> = TestBed.createComponent(CalendarWeekViewComponent);
492+
fixture.componentInstance.viewDate = new Date('2016-06-27');
493+
fixture.componentInstance.events = [
494+
{
495+
title: 'foo',
496+
color: { primary: '', secondary: '' },
497+
start: moment('2016-06-27')
498+
.add(4, 'hours')
499+
.toDate(),
500+
resizable: {
501+
afterEnd: true
502+
}
503+
}
504+
];
505+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
506+
fixture.detectChanges();
507+
document.body.appendChild(fixture.nativeElement);
508+
const event: HTMLElement = fixture.nativeElement.querySelector(
509+
'.cal-event-container'
510+
);
511+
const dayWidth: number = event.parentElement.offsetWidth / 7;
512+
const rect: ClientRect = event.getBoundingClientRect();
513+
let resizeEvent: CalendarEventTimesChangedEvent;
514+
fixture.componentInstance.eventTimesChanged.subscribe(e => {
515+
resizeEvent = e;
516+
});
517+
triggerDomEvent('mousedown', document.body, {
518+
clientX: rect.right,
519+
clientY: rect.top
520+
});
521+
fixture.detectChanges();
522+
triggerDomEvent('mousemove', document.body, {
523+
clientX: rect.right + dayWidth,
524+
clientY: rect.top
525+
});
526+
fixture.detectChanges();
527+
expect(Math.round(event.getBoundingClientRect().left)).to.equal(
528+
Math.round(rect.left)
529+
);
530+
expect(Math.round(event.getBoundingClientRect().width)).to.equal(
531+
Math.round(rect.width + dayWidth)
532+
);
533+
triggerDomEvent('mouseup', document.body, {
534+
clientX: rect.right + dayWidth,
535+
clientY: rect.top
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(4, 'hours')
546+
.add(1, 'day')
547+
.toDate()
548+
});
549+
});
550+
488551
it('should allow 2 events next to each other to be resized at the same time', () => {
489552
const fixture: ComponentFixture<
490553
CalendarWeekViewComponent

0 commit comments

Comments
 (0)