-
Notifications
You must be signed in to change notification settings - Fork 174
/
full-calendar.component.ts
204 lines (177 loc) · 7.45 KB
/
full-calendar.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
Component,
ContentChild,
TemplateRef,
ElementRef,
Input,
AfterViewInit,
DoCheck,
AfterContentChecked,
OnDestroy,
ViewEncapsulation,
ChangeDetectorRef,
} from '@angular/core';
import { Calendar, CalendarOptions } from '@fullcalendar/core';
import { CustomRendering, CustomRenderingStore } from '@fullcalendar/core/internal';
import { OPTION_INPUT_NAMES, OPTION_IS_DEEP } from './options';
import { CalendarOption, CalendarTemplateRef } from './private-types';
import { deepCopy, shallowCopy, mapHash } from './utils/obj';
import { deepEqual } from './utils/fast-deep-equal';
@Component({
selector: 'full-calendar',
templateUrl: './full-calendar.component.html',
encapsulation: ViewEncapsulation.None // the styles are root-level, not scoped within the component
})
export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterContentChecked, OnDestroy {
@Input() options?: CalendarOptions;
@Input() deepChangeDetection?: boolean;
/*
Options as individual Inputs
NOTE: keep in sync with OPTION_INPUT_NAMES
*/
@Input() events?: CalendarOption<'events'> | null | undefined;
@Input() eventSources?: CalendarOption<'eventSources'> | null | undefined;
@Input() resources?: CalendarOption<'resources'> | null | undefined;
/*
Templates
*/
@ContentChild('dayHeaderContent', { static: true }) dayHeaderContent?: CalendarTemplateRef<'dayHeaderContent'>;
@ContentChild('dayCellContent', { static: true }) dayCellContent?: CalendarTemplateRef<'dayCellContent'>;
@ContentChild('weekNumberContent', { static: true }) weekNumberContent?: CalendarTemplateRef<'weekNumberContent'>;
@ContentChild('nowIndicatorContent', { static: true }) nowIndicatorContent?: CalendarTemplateRef<'nowIndicatorContent'>;
@ContentChild('eventContent', { static: true }) eventContent?: CalendarTemplateRef<'eventContent'>;
@ContentChild('slotLaneContent', { static: true }) slotLaneContent?: CalendarTemplateRef<'slotLaneContent'>;
@ContentChild('slotLabelContent', { static: true }) slotLabelContent?: CalendarTemplateRef<'slotLabelContent'>;
@ContentChild('allDayContent', { static: true }) allDayContent?: CalendarTemplateRef<'allDayContent'>;
@ContentChild('moreLinkContent', { static: true }) moreLinkContent?: CalendarTemplateRef<'moreLinkContent'>;
@ContentChild('noEventsContent', { static: true }) noEventsContent?: CalendarTemplateRef<'noEventsContent'>;
@ContentChild('resourceAreaHeaderContent', { static: true }) resourceAreaHeaderContent?: CalendarTemplateRef<'resourceAreaHeaderContent'>;
@ContentChild('resourceGroupLabelContent', { static: true }) resourceGroupLabelContent?: CalendarTemplateRef<'resourceGroupLabelContent'>;
@ContentChild('resourceLabelContent', { static: true }) resourceLabelContent?: CalendarTemplateRef<'resourceLabelContent'>;
@ContentChild('resourceLaneContent', { static: true }) resourceLaneContent?: CalendarTemplateRef<'resourceLaneContent'>;
@ContentChild('resourceGroupLaneContent', { static: true }) resourceGroupLaneContent?: CalendarTemplateRef<'resourceGroupLaneContent'>;
private calendar: Calendar | null = null;
private optionSnapshot: Record<string, any> = {}; // for diffing
private handleCustomRendering: (customRendering: CustomRendering<any>) => void
private customRenderingMap = new Map<string, CustomRendering<any>>()
private customRenderingArray?: CustomRendering<any>[]
public templateMap: { [templateName: string]: TemplateRef<any> } = {}
constructor(
private element: ElementRef,
changeDetector: ChangeDetectorRef
) {
const customRenderingStore = new CustomRenderingStore();
customRenderingStore.subscribe((customRenderingMap) => {
this.customRenderingMap = customRenderingMap;
this.customRenderingArray = undefined; // clear cache
changeDetector.detectChanges();
});
this.handleCustomRendering = customRenderingStore.handle.bind(customRenderingStore);
this.templateMap = this as any; // alias to this
}
ngAfterViewInit() {
const { deepChangeDetection } = this;
const options = {
...this.options,
...this.buildInputOptions(),
};
// initialize snapshot
this.optionSnapshot = mapHash(options, (optionVal: any, optionName: string) => (
(deepChangeDetection && OPTION_IS_DEEP[optionName])
? deepCopy(optionVal)
: optionVal
));
this.calendar = new Calendar(this.element.nativeElement, {
...options,
...this.buildExtraOptions(),
});
this.calendar.render();
}
/*
allows us to manually detect complex input changes, internal mutations to certain options.
called before ngOnChanges. called much more often than ngOnChanges.
*/
ngDoCheck() {
if (this.calendar) { // not the initial render
const { deepChangeDetection, optionSnapshot } = this;
const newOptions = {
...this.options,
...this.buildInputOptions(),
};
const newProcessedOptions: Record<string, any> = {};
let anyChanges = false;
// detect adds and updates (and update snapshot)
for (const optionName in newOptions) {
if (newOptions.hasOwnProperty(optionName)) {
let optionVal = newOptions[optionName as keyof CalendarOptions];
if (deepChangeDetection && OPTION_IS_DEEP[optionName]) {
if (!deepEqual(optionSnapshot[optionName], optionVal)) {
optionSnapshot[optionName] = deepCopy(optionVal);
anyChanges = true;
// trick FC into knowing about a nested change.
// TODO: future versions won't need this.
// can't use the previously-made deep copy because it blows away prototype-association.
optionVal = shallowCopy(optionVal);
}
} else {
if (optionSnapshot[optionName] !== optionVal) {
optionSnapshot[optionName] = optionVal;
anyChanges = true;
}
}
newProcessedOptions[optionName] = optionVal;
}
}
const oldOptionNames = Object.keys(optionSnapshot);
// detect removals (and update snapshot)
for (const optionName of oldOptionNames) {
if (!(optionName in newOptions)) { // doesn't exist in new options?
delete optionSnapshot[optionName];
anyChanges = true;
}
}
if (anyChanges) {
this.calendar.pauseRendering();
this.calendar.resetOptions({
...newProcessedOptions,
...this.buildExtraOptions(),
});
}
}
}
ngAfterContentChecked() {
if (this.calendar) { // too defensive?
this.calendar.resumeRendering();
}
}
ngOnDestroy() {
if (this.calendar) { // too defensive?
this.calendar.destroy();
this.calendar = null;
}
}
get customRenderings(): CustomRendering<any>[] {
return this.customRenderingArray ||
(this.customRenderingArray = [...this.customRenderingMap.values()]);
}
public getApi(): Calendar {
return this.calendar!;
}
private buildInputOptions(): CalendarOptions {
const options: CalendarOptions = {}
for (const inputName of OPTION_INPUT_NAMES) {
const inputValue = (this as any)[inputName];
if (inputValue != null) { // exclude both null and undefined
(options as any)[inputName] = inputValue;
}
}
return options;
}
private buildExtraOptions(): CalendarOptions {
return {
handleCustomRendering: this.handleCustomRendering,
customRenderingMetaMap: this.templateMap,
customRenderingReplacesEl: true,
};
}
}