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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
odoo.define('/web/static/src/js/libs/fullcalendar.js', function () {
"use strict";
function createYearCalendarView(FullCalendar) {
const {
Calendar,
createElement,
EventApi,
memoizeRendering,
View,
} = FullCalendar;
class YearView extends View {
constructor() {
super(...arguments);
this.months = null;
this.renderSubCalendarsMem = memoizeRendering(
this.renderSubCalendars, this.unrenderSubCalendars);
this.events = [];
}
//----------------------------------------------------------------------
// Getters
//----------------------------------------------------------------------
get currentDate() {
return this.context.calendar.state.currentDate;
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
/**
* @override
*/
destroy() {
this.renderSubCalendarsMem.unrender();
super.destroy();
}
/**
* Removes the selection on sub calendar.
* Selections on sub calendars are not propagated to this view so
* this view cannot manage them.
*/
unselect() {
for (const { calendar } of this.months) {
calendar.unselect();
}
}
/**
* @override
*/
render() {
this.renderSubCalendarsMem(this.context);
super.render(...arguments);
}
/**
* Renders the main layout (the 4x3 month grid)
*/
renderSubCalendars() {
this.el.classList.add('fc-scroller');
if (!this.context.options.selectable) {
this.el.classList.add('fc-readonly-year-view');
}
this.months = [];
for (let monthNumber = 0; monthNumber < 12; monthNumber++) {
const monthDate = new Date(this.currentDate.getFullYear(), monthNumber);
const monthShortName = moment(monthDate).format('MMM').toLowerCase();
const container = createElement('div', { class: 'fc-month-container' });
this.el.appendChild(container);
const el = createElement('div', {
class: `fc-month fc-month-${monthShortName}`,
});
container.appendChild(el);
const calendar = this._createMonthCalendar(el, monthDate);
this.months.push({ el, calendar });
calendar.render();
}
}
/**
* Removes the main layout (the 4x3 month grid).
* Called when view is switched/destroyed.
*/
unrenderSubCalendars() {
for (const { el, calendar } of this.months) {
calendar.destroy();
el.remove();
}
}
/**
* Renders events in sub calendars.
* Called every time event source changed (when changing the date,
* when changing filters, adding/removing filters).
*/
renderEvents() {
// `renderDates` also renders events so if it's called just before
// then do not execute this as it will do a re-render.
if (this.datesRendered) {
this.datesRendered = false;
return;
}
this.events = this._computeEvents();
for (const { calendar } of this.months) {
calendar.refetchEvents();
}
this._setCursorOnEventDates();
}
/**
* Renders dates and events in sub calendars.
* Called when the year of the date changed to render a new
* 4*3 grid of month calendar based on the new year.
*/
renderDates() {
this.events = this._computeEvents();
for (const [monthNumber, { calendar }] of Object.entries(this.months)) {
const monthDate = new Date(this.currentDate.getFullYear(), monthNumber);
calendar.gotoDate(monthDate);
}
this._setCursorOnEventDates();
this.datesRendered = true;
}
//----------------------------------------------------------------------
// Private
//----------------------------------------------------------------------
/**
* @private
*/
_computeEvents() {
const calendar = this.context.calendar;
return calendar.getEvents().map(event => {
const endUTC = calendar.dateEnv.toDate(event._instance.range.end);
const end = new Date(event._instance.range.end);
if (endUTC.getHours() > 0 || endUTC.getMinutes() > 0 ||
endUTC.getSeconds() > 0 || endUTC.getMilliseconds() > 0) {
end.setDate(end.getDate() + 1);
}
// clone event data to not trigger rerendering and issues
const instance = Object.assign({}, event._instance, {
range: { start: new Date(event._instance.range.start), end },
});
const def = Object.assign({}, event._def, {
rendering: 'background',
allDay: true,
});
return new EventApi(this.context.calendar, def, instance);
});
}
/**
* Create a month calendar for the date `monthDate` and mount it on container.
*
* @private
* @param {HTMLElement} container
* @param {Date} monthDate
*/
_createMonthCalendar(container, monthDate) {
return new Calendar(container, Object.assign({}, this.context.options, {
defaultDate: monthDate,
defaultView: 'dayGridMonth',
header: { left: false, center: 'title', right: false },
titleFormat: { month: 'short', year: 'numeric' },
height: 0,
contentHeight: 0,
weekNumbers: false,
showNonCurrentDates: false,
views: {
dayGridMonth: {
columnHeaderText: (date) => moment(date).format("ddd")[0],
},
},
selectMinDistance: 5, // needed to not trigger select when click
dateClick: this._onYearDateClick.bind(this),
datesRender: undefined,
events: (info, successCB) => {
successCB(this.events);
},
windowResize: undefined,
}));
}
/**
* Sets fc-has-event class on every dates that have at least one event.
*
* @private
*/
_setCursorOnEventDates() {
for (const el of this.el.querySelectorAll('.fc-has-event')) {
el.classList.remove('fc-has-event');
}
for (const event of Object.values(this.events)) {
let currentDate = moment(event._instance.range.start);
while (currentDate.isBefore(event._instance.range.end, 'day')) {
const formattedDate = currentDate.format('YYYY-MM-DD');
const el = this.el.querySelector(`.fc-day-top[data-date="${formattedDate}"]`);
if (el) {
el.classList.add('fc-has-event');
}
currentDate.add(1, 'days');
}
}
}
//----------------------------------------------------------------------
// Handlers
//----------------------------------------------------------------------
/**
* @private
* @param {*} info
*/
_onYearDateClick(info) {
const calendar = this.context.calendar;
const events = Object.values(this.events)
.filter(event => {
const startUTC = calendar.dateEnv.toDate(event._instance.range.start);
const endUTC = calendar.dateEnv.toDate(event._instance.range.end);
const start = moment(startUTC);
const end = moment(endUTC);
const inclusivity = start.isSame(end, 'day') ? '[]' : '[)';
return moment(info.date).isBetween(start, end, 'day', inclusivity);
})
.map(event => {
return Object.assign({}, event._def, event._instance.range);
});
const yearDateInfo = Object.assign({}, info, {
view: this,
monthView: info.view,
events,
selectable: this.context.options.selectable,
});
calendar.publiclyTrigger('yearDateClick', [yearDateInfo]);
}
}
return FullCalendar.createPlugin({
views: {
dayGridYear: {
class: YearView,
duration: { years: 1 },
defaults: {
fixedWeekCount: true,
},
},
},
});
}
return {
createYearCalendarView,
};
});
|