-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcalendar.ts
More file actions
226 lines (212 loc) · 7.3 KB
/
Copy pathcalendar.ts
File metadata and controls
226 lines (212 loc) · 7.3 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Handles rendering the calendar given a container element, eventSources, and interaction callbacks.
*/
import {
Calendar,
EventApi,
EventClickArg,
EventHoveringArg,
EventSourceInput,
} from "@fullcalendar/core";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
import rrulePlugin from "@fullcalendar/rrule";
import listPlugin from "@fullcalendar/list";
import interactionPlugin from "@fullcalendar/interaction";
import googleCalendarPlugin from "@fullcalendar/google-calendar";
import iCalendarPlugin from "@fullcalendar/icalendar";
// There is an issue with FullCalendar RRule support around DST boundaries which is fixed by this monkeypatch:
// https://github.com/fullcalendar/fullcalendar/issues/5273#issuecomment-1360459342
rrulePlugin.recurringTypes[0].expand = function (errd, fr, de) {
const hours = errd.rruleSet._dtstart.getHours();
return errd.rruleSet
.between(de.toDate(fr.start), de.toDate(fr.end), true)
.map((d: Date) => {
return new Date(
Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
hours,
d.getMinutes()
)
);
});
};
export interface ExtraRenderProps {
eventClick?: (info: EventClickArg) => void;
select?: (
startDate: Date,
endDate: Date,
allDay: boolean,
viewType: string
) => Promise<void>;
modifyEvent?: (event: EventApi, oldEvent: EventApi) => Promise<boolean>;
eventMouseEnter?: (info: EventHoveringArg) => void;
firstDay?: number;
initialView?: { desktop: string; mobile: string };
timeFormat24h?: boolean;
openContextMenuForEvent?: (
event: EventApi,
mouseEvent: MouseEvent
) => Promise<void>;
toggleTask?: (event: EventApi, isComplete: boolean) => Promise<boolean>;
forceNarrow?: boolean;
}
export function renderCalendar(
containerEl: HTMLElement,
eventSources: EventSourceInput[],
settings?: ExtraRenderProps
): Calendar {
const isMobile = window.innerWidth < 500;
const isNarrow = settings?.forceNarrow || isMobile;
const {
eventClick,
select,
modifyEvent,
eventMouseEnter,
openContextMenuForEvent,
toggleTask,
} = settings || {};
const modifyEventCallback =
modifyEvent &&
(async ({
event,
oldEvent,
revert,
}: {
event: EventApi;
oldEvent: EventApi;
revert: () => void;
}) => {
const success = await modifyEvent(event, oldEvent);
if (!success) {
revert();
}
});
const cal = new Calendar(containerEl, {
plugins: [
// View plugins
dayGridPlugin,
timeGridPlugin,
listPlugin,
// Drag + drop and editing
interactionPlugin,
// Remote sources
googleCalendarPlugin,
iCalendarPlugin,
rrulePlugin,
],
googleCalendarApiKey: "AIzaSyDIiklFwJXaLWuT_4y6I9ZRVVsPuf4xGrk",
initialView:
settings?.initialView?.[isNarrow ? "mobile" : "desktop"] ||
(isNarrow ? "timeGrid3Days" : "timeGridWeek"),
nowIndicator: true,
scrollTimeReset: false,
dayMaxEvents: true,
headerToolbar: !isNarrow
? {
left: "prev,next today",
center: "title",
right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek",
}
: !isMobile
? {
right: "today,prev,next",
left: "timeGrid3Days,timeGridDay,listWeek",
}
: false,
footerToolbar: isMobile
? {
right: "today,prev,next",
left: "timeGrid3Days,timeGridDay,listWeek",
}
: false,
views: {
timeGridDay: {
type: "timeGrid",
duration: { days: 1 },
buttonText: isNarrow ? "1" : "day",
},
timeGrid3Days: {
type: "timeGrid",
duration: { days: 3 },
buttonText: "3",
},
},
firstDay: settings?.firstDay,
...(settings?.timeFormat24h && {
eventTimeFormat: {
hour: "numeric",
minute: "2-digit",
hour12: false,
},
slotLabelFormat: {
hour: "numeric",
minute: "2-digit",
hour12: false,
},
}),
eventSources,
eventClick,
selectable: select && true,
selectMirror: select && true,
select:
select &&
(async (info) => {
await select(info.start, info.end, info.allDay, info.view.type);
info.view.calendar.unselect();
}),
editable: modifyEvent && true,
eventDrop: modifyEventCallback,
eventResize: modifyEventCallback,
eventMouseEnter,
eventDidMount: ({ event, el, textColor }) => {
el.addEventListener("contextmenu", (e) => {
e.preventDefault();
openContextMenuForEvent && openContextMenuForEvent(event, e);
});
if (toggleTask) {
if (event.extendedProps.isTask) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked =
event.extendedProps.taskCompleted !== false;
checkbox.onclick = async (e) => {
e.stopPropagation();
if (e.target) {
let ret = await toggleTask(
event,
(e.target as HTMLInputElement).checked
);
if (!ret) {
(e.target as HTMLInputElement).checked = !(
e.target as HTMLInputElement
).checked;
}
}
};
// Make the checkbox more visible against different color events.
if (textColor == "black") {
checkbox.addClass("ofc-checkbox-black");
} else {
checkbox.addClass("ofc-checkbox-white");
}
if (checkbox.checked) {
el.addClass("ofc-task-completed");
}
// Depending on the view, we should put the checkbox in a different spot.
const container =
el.querySelector(".fc-event-time") ||
el.querySelector(".fc-event-title") ||
el.querySelector(".fc-list-event-title");
container?.addClass("ofc-has-checkbox");
container?.prepend(checkbox);
}
}
},
longPressDelay: 250,
});
cal.render();
return cal;
}