|
| 1 | +import { Component, ChangeDetectionStrategy, OnInit } from '@angular/core'; |
| 2 | +import { |
| 3 | + CalendarEvent, |
| 4 | + CalendarMonthViewDay, |
| 5 | + CalendarView |
| 6 | +} from 'angular-calendar'; |
| 7 | +import { colors } from '../demo-utils/colors'; |
| 8 | +import { isSameMinute, startOfDay } from 'date-fns'; |
| 9 | + |
| 10 | +interface EventGroupMeta { |
| 11 | + type: string; |
| 12 | +} |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: 'mwl-demo-component', |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | + templateUrl: './template.html', |
| 18 | + styles: [ |
| 19 | + ` |
| 20 | + .cell-totals { |
| 21 | + margin: 5px; |
| 22 | + text-align: center; |
| 23 | + } |
| 24 | + .badge { |
| 25 | + margin-right: 5px; |
| 26 | + } |
| 27 | + ` |
| 28 | + ] |
| 29 | +}) |
| 30 | +export class DemoComponent implements OnInit { |
| 31 | + view: CalendarView = CalendarView.Month; |
| 32 | + |
| 33 | + viewDate: Date = new Date(); |
| 34 | + |
| 35 | + events: CalendarEvent[] = [ |
| 36 | + { |
| 37 | + title: 'Event 1', |
| 38 | + color: colors.yellow, |
| 39 | + start: startOfDay(new Date()), |
| 40 | + meta: { |
| 41 | + type: 'warning' |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + title: 'Event 2', |
| 46 | + color: colors.yellow, |
| 47 | + start: startOfDay(new Date()), |
| 48 | + meta: { |
| 49 | + type: 'warning' |
| 50 | + } |
| 51 | + }, |
| 52 | + { |
| 53 | + title: 'Event 3', |
| 54 | + color: colors.blue, |
| 55 | + start: startOfDay(new Date()), |
| 56 | + meta: { |
| 57 | + type: 'info' |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + title: 'Event 4', |
| 62 | + color: colors.red, |
| 63 | + start: startOfDay(new Date()), |
| 64 | + meta: { |
| 65 | + type: 'danger' |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + title: 'Event 5', |
| 70 | + color: colors.red, |
| 71 | + start: startOfDay(new Date()), |
| 72 | + meta: { |
| 73 | + type: 'danger' |
| 74 | + } |
| 75 | + } |
| 76 | + ]; |
| 77 | + |
| 78 | + groupedSimilarEvents: CalendarEvent[] = []; |
| 79 | + |
| 80 | + ngOnInit() { |
| 81 | + // group any events together that have the same type and dates |
| 82 | + // use for when you have a lot of events on the week or day view at the same time |
| 83 | + this.groupedSimilarEvents = []; |
| 84 | + const processedEvents = new Set(); |
| 85 | + this.events.forEach(event => { |
| 86 | + if (processedEvents.has(event)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + const similarEvents = this.events.filter(otherEvent => { |
| 90 | + return ( |
| 91 | + otherEvent !== event && |
| 92 | + !processedEvents.has(otherEvent) && |
| 93 | + isSameMinute(otherEvent.start, event.start) && |
| 94 | + (isSameMinute(otherEvent.end, event.end) || |
| 95 | + (!otherEvent.end && !event.end)) && |
| 96 | + otherEvent.color.primary === event.color.primary && |
| 97 | + otherEvent.color.secondary === event.color.secondary |
| 98 | + ); |
| 99 | + }); |
| 100 | + processedEvents.add(event); |
| 101 | + similarEvents.forEach(otherEvent => { |
| 102 | + processedEvents.add(otherEvent); |
| 103 | + }); |
| 104 | + if (similarEvents.length > 0) { |
| 105 | + this.groupedSimilarEvents.push({ |
| 106 | + title: `${similarEvents.length + 1} events`, |
| 107 | + color: event.color, |
| 108 | + start: event.start, |
| 109 | + end: event.end, |
| 110 | + meta: { |
| 111 | + groupedEvents: [event, ...similarEvents] |
| 112 | + } |
| 113 | + }); |
| 114 | + } else { |
| 115 | + this.groupedSimilarEvents.push(event); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + beforeMonthViewRender({ |
| 121 | + body |
| 122 | + }: { |
| 123 | + body: Array<CalendarMonthViewDay<EventGroupMeta>>; |
| 124 | + }): void { |
| 125 | + // month view has a different UX from the week and day view so we only really need to group by the type |
| 126 | + body.forEach(cell => { |
| 127 | + const groups = {}; |
| 128 | + cell.events.forEach((event: CalendarEvent<EventGroupMeta>) => { |
| 129 | + groups[event.meta.type] = groups[event.meta.type] || []; |
| 130 | + groups[event.meta.type].push(event); |
| 131 | + }); |
| 132 | + cell['eventGroups'] = Object.entries(groups); |
| 133 | + }); |
| 134 | + } |
| 135 | +} |
0 commit comments