This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
/
calendarHelper.js
336 lines (272 loc) · 10.7 KB
/
calendarHelper.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict';
var angular = require('angular');
angular
.module('mwl.calendar')
.factory('calendarHelper', function(dateFilter, moment, calendarConfig) {
function formatDate(date, format) {
if (calendarConfig.dateFormatter === 'angular') {
return dateFilter(moment(date).toDate(), format);
} else if (calendarConfig.dateFormatter === 'moment') {
return moment(date).format(format);
}
}
function adjustEndDateFromStartDiff(oldStart, newStart, oldEnd) {
if (!oldEnd) {
return oldEnd;
}
var diffInSeconds = moment(newStart).diff(moment(oldStart));
return moment(oldEnd).add(diffInSeconds);
}
function eventIsInPeriod(event, periodStart, periodEnd) {
var eventStart = moment(event.startsAt);
var eventEnd = moment(event.endsAt || event.startsAt);
periodStart = moment(periodStart);
periodEnd = moment(periodEnd);
if (angular.isDefined(event.recursOn)) {
switch (event.recursOn) {
case 'year':
eventStart.set({
year: periodStart.year()
});
break;
case 'month':
eventStart.set({
year: periodStart.year(),
month: periodStart.month()
});
break;
default:
throw new Error('Invalid value (' + event.recursOn + ') given for recurs on. Can only be year or month.');
}
eventEnd = adjustEndDateFromStartDiff(event.startsAt, eventStart, eventEnd);
}
return (eventStart.isAfter(periodStart) && eventStart.isBefore(periodEnd)) ||
(eventEnd.isAfter(periodStart) && eventEnd.isBefore(periodEnd)) ||
(eventStart.isBefore(periodStart) && eventEnd.isAfter(periodEnd)) ||
eventStart.isSame(periodStart) ||
eventEnd.isSame(periodEnd);
}
function filterEventsInPeriod(events, startPeriod, endPeriod) {
return events.filter(function(event) {
return eventIsInPeriod(event, startPeriod, endPeriod);
});
}
function getEventsInPeriod(calendarDate, period, allEvents) {
var startPeriod = moment(calendarDate).startOf(period);
var endPeriod = moment(calendarDate).endOf(period);
return filterEventsInPeriod(allEvents, startPeriod, endPeriod);
}
function getBadgeTotal(events) {
return events.filter(function(event) {
return event.incrementsBadgeTotal !== false;
}).length;
}
function getWeekDayNames() {
var weekdays = [];
var count = 0;
while (count < 7) {
weekdays.push(formatDate(moment().weekday(count++), calendarConfig.dateFormats.weekDay));
}
return weekdays;
}
function getYearView(events, currentDay, cellModifier) {
var view = [];
var eventsInPeriod = getEventsInPeriod(currentDay, 'year', events);
var month = moment(currentDay).startOf('year');
var count = 0;
while (count < 12) {
var startPeriod = month.clone();
var endPeriod = startPeriod.clone().endOf('month');
var periodEvents = filterEventsInPeriod(eventsInPeriod, startPeriod, endPeriod);
var cell = {
label: formatDate(startPeriod, calendarConfig.dateFormats.month),
isToday: startPeriod.isSame(moment().startOf('month')),
events: periodEvents,
date: startPeriod,
badgeTotal: getBadgeTotal(periodEvents)
};
cellModifier({calendarCell: cell});
view.push(cell);
month.add(1, 'month');
count++;
}
return view;
}
function getMonthView(events, currentDay, cellModifier) {
var startOfMonth = moment(currentDay).startOf('month');
var day = startOfMonth.clone().startOf('week');
var endOfMonthView = moment(currentDay).endOf('month').endOf('week');
var eventsInPeriod;
if (calendarConfig.displayAllMonthEvents) {
eventsInPeriod = filterEventsInPeriod(events, day, endOfMonthView);
} else {
eventsInPeriod = filterEventsInPeriod(events, startOfMonth, startOfMonth.clone().endOf('month'));
}
var view = [];
var today = moment().startOf('day');
while (day.isBefore(endOfMonthView)) {
var inMonth = day.month() === moment(currentDay).month();
var monthEvents = [];
if (inMonth || calendarConfig.displayAllMonthEvents) {
monthEvents = filterEventsInPeriod(eventsInPeriod, day, day.clone().endOf('day'));
}
var cell = {
label: day.date(),
date: day.clone(),
inMonth: inMonth,
isPast: today.isAfter(day),
isToday: today.isSame(day),
isFuture: today.isBefore(day),
isWeekend: [0, 6].indexOf(day.day()) > -1,
events: monthEvents,
badgeTotal: getBadgeTotal(monthEvents)
};
cellModifier({calendarCell: cell});
view.push(cell);
day.add(1, 'day');
}
return view;
}
function getWeekView(events, currentDay) {
var startOfWeek = moment(currentDay).startOf('week');
var endOfWeek = moment(currentDay).endOf('week');
var dayCounter = startOfWeek.clone();
var days = [];
var today = moment().startOf('day');
while (days.length < 7) {
days.push({
weekDayLabel: formatDate(dayCounter, calendarConfig.dateFormats.weekDay),
date: dayCounter.clone(),
dayLabel: formatDate(dayCounter, calendarConfig.dateFormats.day),
isPast: dayCounter.isBefore(today),
isToday: dayCounter.isSame(today),
isFuture: dayCounter.isAfter(today),
isWeekend: [0, 6].indexOf(dayCounter.day()) > -1
});
dayCounter.add(1, 'day');
}
var eventsSorted = filterEventsInPeriod(events, startOfWeek, endOfWeek).map(function(event) {
var eventStart = moment(event.startsAt).startOf('day');
var eventEnd = moment(event.endsAt || event.startsAt).startOf('day');
var weekViewStart = moment(startOfWeek).startOf('day');
var weekViewEnd = moment(endOfWeek).startOf('day');
var offset, span;
if (eventStart.isBefore(weekViewStart) || eventStart.isSame(weekViewStart)) {
offset = 0;
} else {
offset = eventStart.diff(weekViewStart, 'days');
}
if (eventEnd.isAfter(weekViewEnd)) {
eventEnd = weekViewEnd;
}
if (eventStart.isBefore(weekViewStart)) {
eventStart = weekViewStart;
}
span = moment(eventEnd).diff(eventStart, 'days') + 1;
event.daySpan = span;
event.dayOffset = offset;
return event;
});
return {days: days, events: eventsSorted};
}
function getDayView(events, currentDay, dayViewStart, dayViewEnd, dayViewSplit) {
var dayStartHour = moment(dayViewStart || '00:00', 'HH:mm').hours();
var dayEndHour = moment(dayViewEnd || '23:00', 'HH:mm').hours();
var hourHeight = (60 / dayViewSplit) * 30;
var calendarStart = moment(currentDay).startOf('day').add(dayStartHour, 'hours');
var calendarEnd = moment(currentDay).startOf('day').add(dayEndHour, 'hours');
var calendarHeight = (dayEndHour - dayStartHour + 1) * hourHeight;
var hourHeightMultiplier = hourHeight / 60;
var buckets = [];
var eventsInPeriod = filterEventsInPeriod(
events,
moment(currentDay).startOf('day').toDate(),
moment(currentDay).endOf('day').toDate()
);
return eventsInPeriod.map(function(event) {
if (moment(event.startsAt).isBefore(calendarStart)) {
event.top = 0;
} else {
event.top = (moment(event.startsAt).startOf('minute').diff(calendarStart.startOf('minute'), 'minutes') * hourHeightMultiplier) - 2;
}
if (moment(event.endsAt || event.startsAt).isAfter(calendarEnd)) {
event.height = calendarHeight - event.top;
} else {
var diffStart = event.startsAt;
if (moment(event.startsAt).isBefore(calendarStart)) {
diffStart = calendarStart.toDate();
}
if (!event.endsAt) {
event.height = 30;
} else {
event.height = moment(event.endsAt || event.startsAt).diff(diffStart, 'minutes') * hourHeightMultiplier;
}
}
if (event.top - event.height > calendarHeight) {
event.height = 0;
}
event.left = 0;
return event;
}).filter(function(event) {
return event.height > 0;
}).map(function(event) {
var cannotFitInABucket = true;
buckets.forEach(function(bucket, bucketIndex) {
var canFitInThisBucket = true;
bucket.forEach(function(bucketItem) {
if (eventIsInPeriod(event, bucketItem.startsAt, bucketItem.endsAt || bucketItem.startsAt) ||
eventIsInPeriod(bucketItem, event.startsAt, event.endsAt || event.startsAt)) {
canFitInThisBucket = false;
}
});
if (canFitInThisBucket && cannotFitInABucket) {
cannotFitInABucket = false;
event.left = bucketIndex * 150;
buckets[bucketIndex].push(event);
}
});
if (cannotFitInABucket) {
event.left = buckets.length * 150;
buckets.push([event]);
}
return event;
});
}
function getWeekViewWithTimes(events, currentDay, dayViewStart, dayViewEnd, dayViewSplit) {
var weekView = getWeekView(events, currentDay);
var newEvents = [];
weekView.days.forEach(function(day) {
var dayEvents = weekView.events.filter(function(event) {
return moment(event.startsAt).startOf('day').isSame(moment(day.date).startOf('day'));
});
var newDayEvents = getDayView(
dayEvents,
day.date,
dayViewStart,
dayViewEnd,
dayViewSplit
);
newEvents = newEvents.concat(newDayEvents);
});
weekView.events = newEvents;
return weekView;
}
function getDayViewHeight(dayViewStart, dayViewEnd, dayViewSplit) {
var dayViewStartM = moment(dayViewStart || '00:00', 'HH:mm');
var dayViewEndM = moment(dayViewEnd || '23:00', 'HH:mm');
var hourHeight = (60 / dayViewSplit) * 30;
return ((dayViewEndM.diff(dayViewStartM, 'hours') + 1) * hourHeight) + 2;
}
return {
getWeekDayNames: getWeekDayNames,
getYearView: getYearView,
getMonthView: getMonthView,
getWeekView: getWeekView,
getDayView: getDayView,
getWeekViewWithTimes: getWeekViewWithTimes,
getDayViewHeight: getDayViewHeight,
adjustEndDateFromStartDiff: adjustEndDateFromStartDiff,
formatDate: formatDate,
eventIsInPeriod: eventIsInPeriod //expose for testing only
};
});