|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + ChangeDetectorRef, |
| 4 | + Component, |
| 5 | + ViewEncapsulation |
| 6 | +} from '@angular/core'; |
| 7 | +import { |
| 8 | + CalendarEvent, |
| 9 | + CalendarEventTitleFormatter, |
| 10 | + CalendarView |
| 11 | +} from 'angular-calendar'; |
| 12 | +import { DayViewHourSegment } from 'calendar-utils'; |
| 13 | +import { fromEvent } from 'rxjs'; |
| 14 | +import { finalize, takeUntil } from 'rxjs/operators'; |
| 15 | +import { addDays, addMinutes, endOfWeek } from 'date-fns'; |
| 16 | + |
| 17 | +function floorToNearest(amount: number, precision: number) { |
| 18 | + return Math.floor(amount / precision) * precision; |
| 19 | +} |
| 20 | + |
| 21 | +function ceilToNearest(amount: number, precision: number) { |
| 22 | + return Math.ceil(amount / precision) * precision; |
| 23 | +} |
| 24 | + |
| 25 | +export class CustomEventTitleFormatter extends CalendarEventTitleFormatter { |
| 26 | + weekTooltip(event: CalendarEvent, title: string) { |
| 27 | + if (!event.meta.tmpEvent) { |
| 28 | + return super.weekTooltip(event, title); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + dayTooltip(event: CalendarEvent, title: string) { |
| 33 | + if (!event.meta.tmpEvent) { |
| 34 | + return super.dayTooltip(event, title); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// tslint:disable-next-line max-classes-per-file |
| 40 | +@Component({ |
| 41 | + selector: 'mwl-demo-component', |
| 42 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 43 | + templateUrl: 'template.html', |
| 44 | + providers: [ |
| 45 | + { |
| 46 | + provide: CalendarEventTitleFormatter, |
| 47 | + useClass: CustomEventTitleFormatter |
| 48 | + } |
| 49 | + ], |
| 50 | + styles: [ |
| 51 | + ` |
| 52 | + .disable-hover { |
| 53 | + pointer-events: none; |
| 54 | + } |
| 55 | + ` |
| 56 | + ], |
| 57 | + encapsulation: ViewEncapsulation.None |
| 58 | +}) |
| 59 | +export class DemoComponent { |
| 60 | + viewDate = new Date(); |
| 61 | + |
| 62 | + events: CalendarEvent[] = []; |
| 63 | + |
| 64 | + dragToCreateActive = false; |
| 65 | + |
| 66 | + constructor(private cdr: ChangeDetectorRef) {} |
| 67 | + |
| 68 | + startDragToCreate( |
| 69 | + segment: DayViewHourSegment, |
| 70 | + mouseDownEvent: MouseEvent, |
| 71 | + segmentElement: HTMLElement |
| 72 | + ) { |
| 73 | + const dragToSelectEvent: CalendarEvent = { |
| 74 | + id: this.events.length, |
| 75 | + title: 'New event', |
| 76 | + start: segment.date, |
| 77 | + meta: { |
| 78 | + tmpEvent: true |
| 79 | + } |
| 80 | + }; |
| 81 | + this.events = [...this.events, dragToSelectEvent]; |
| 82 | + const segmentPosition = segmentElement.getBoundingClientRect(); |
| 83 | + this.dragToCreateActive = true; |
| 84 | + const endOfView = endOfWeek(this.viewDate); |
| 85 | + |
| 86 | + fromEvent(document, 'mousemove') |
| 87 | + .pipe( |
| 88 | + finalize(() => { |
| 89 | + delete dragToSelectEvent.meta.tmpEvent; |
| 90 | + this.dragToCreateActive = false; |
| 91 | + this.refresh(); |
| 92 | + }), |
| 93 | + takeUntil(fromEvent(document, 'mouseup')) |
| 94 | + ) |
| 95 | + .subscribe((mouseMoveEvent: MouseEvent) => { |
| 96 | + const minutesDiff = ceilToNearest( |
| 97 | + mouseMoveEvent.clientY - segmentPosition.top, |
| 98 | + 30 |
| 99 | + ); |
| 100 | + |
| 101 | + const daysDiff = |
| 102 | + floorToNearest( |
| 103 | + mouseMoveEvent.clientX - segmentPosition.left, |
| 104 | + segmentPosition.width |
| 105 | + ) / segmentPosition.width; |
| 106 | + |
| 107 | + const newEnd = addDays(addMinutes(segment.date, minutesDiff), daysDiff); |
| 108 | + if (newEnd > segment.date && newEnd < endOfView) { |
| 109 | + dragToSelectEvent.end = newEnd; |
| 110 | + } |
| 111 | + this.refresh(); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + private refresh() { |
| 116 | + this.events = [...this.events]; |
| 117 | + this.cdr.detectChanges(); |
| 118 | + } |
| 119 | +} |
0 commit comments