forked from carlsednaoui/add-to-calendar-buttons
-
Notifications
You must be signed in to change notification settings - Fork 17
/
add-to-calendar.js
486 lines (392 loc) · 21.7 KB
/
add-to-calendar.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
;(function(exports) {
/* --------------
config
--------------- */
var MS_IN_MINUTES = 60 * 1000;
var CONFIG = {
selector : ".add-to-calendar",
duration : 60,
texts : {
label : "Add to Calendar",
title : "New event",
download : "Calendar-event.ics",
google : "Google Calendar",
yahoo : "Yahoo! Calendar",
off365 : "Office 365",
ical : "Download iCal",
outlook : "Download Outlook",
ienoblob : "Sorry, your browser does not support downloading Calendar events."
}
};
if (typeof ADDTOCAL_CONFIG != "undefined") {
CONFIG = ADDTOCAL_CONFIG;
}
/* --------------
browser sniffing
--------------- */
// ie < edg (=chromium) doesnt support data-uri:text/calendar
var ieCanDownload = ('msSaveOrOpenBlob' in window.navigator);
var ieMustDownload = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(navigator.userAgent);
/* --------------
generators
--------------- */
var calendarGenerators = {
google: function(event) {
var startTime,endTime;
if (event.allday) {
// google wants 2 consecutive days at 00:00
startTime = formatTime(event.tzstart);
endTime = formatTime(getEndDate(event.tzstart,60*24));
startTime = stripISOTime(startTime);
endTime = stripISOTime(endTime);
} else {
if (event.timezone) {
// google is somehow weird with timezones.
// it works better when giving the local
// time in the given timezone without the zulu,
// and pass timezone as argument.
// but then the dates we have loaded
// need to shift inverse with tzoffset the
// browser gave us.
// so
var shiftstart, shiftend;
shiftstart = new Date(event.start.getTime()-event.start.getTimezoneOffset()*MS_IN_MINUTES);
if (event.end) {
shiftend = new Date(event.end.getTime()-event.end.getTimezoneOffset()*MS_IN_MINUTES);
}
startTime = formatTime(shiftstart);
endTime = formatTime(shiftend);
// strip the zulu and pass the tz as argument later
startTime = startTime.substring(0,startTime.length-1);
endTime = endTime.substring(0,endTime.length-1);
} else {
// use regular times
startTime = formatTime(event.start);
endTime = formatTime(event.end);
}
}
var href = encodeURI([
'https://www.google.com/calendar/render',
'?action=TEMPLATE',
'&text=' + (event.title || ''),
'&dates=' + (startTime || ''),
'/' + (endTime || ''),
(event.timezone)?'&ctz='+event.timezone:'',
'&details=' + (event.description || ''),
'&location=' + (event.address || ''),
'&sprop=&sprop=name:'
].join(''));
return '<a class="icon-google" target="_blank" href="' +
href + '">'+CONFIG.texts.google+'</a>';
},
yahoo: function(event) {
if (event.allday) {
var yahooEventDuration = 'allday';
} else {
var eventDuration = event.tzend ?
((event.tzend.getTime() - event.tzstart.getTime())/ MS_IN_MINUTES) :
event.duration;
// Yahoo dates are crazy, we need to convert the duration from minutes to hh:mm
var yahooHourDuration = eventDuration < 600 ?
'0' + Math.floor((eventDuration / 60)) :
Math.floor((eventDuration / 60)) + '';
var yahooMinuteDuration = eventDuration % 60 < 10 ?
'0' + eventDuration % 60 :
eventDuration % 60 + '';
var yahooEventDuration = yahooHourDuration + yahooMinuteDuration;
}
// Remove timezone from event time
// var st = formatTime(new Date(event.start - (event.start.getTimezoneOffset() * MS_IN_MINUTES))) || '';
var st = formatTime(event.tzstart) || '';
var href = encodeURI([
'http://calendar.yahoo.com/?v=60&view=d&type=20',
'&title=' + (event.title || ''),
'&st=' + st,
'&dur=' + (yahooEventDuration || ''),
'&desc=' + (event.description || ''),
'&in_loc=' + (event.address || '')
].join(''));
return '<a class="icon-yahoo" target="_blank" href="' +
href + '">'+CONFIG.texts.yahoo+'</a>';
},
off365: function(event) {
var startTime = formatTime(event.tzstart);
var endTime = formatTime(event.tzend);
var href = encodeURI([
'https://outlook.office365.com/owa/',
'?path=/calendar/action/compose',
'&rru=addevent',
'&subject=' + (event.title || ''),
'&startdt=' + (startTime || ''),
'&enddt=' + (endTime || ''),
'&body=' + (event.description || ''),
'&location=' + (event.address || ''),
'&allday=' + (event.allday)?'true':'false'
].join(''));
return '<a class="icon-off365" target="_blank" href="' +
href + '">'+CONFIG.texts.off365+'</a>';
},
ics: function(event, eClass, calendarName) {
var startTime,endTime;
if (event.allday) {
// DTSTART and DTEND need to be equal and 0
startTime = formatTime(event.tzstart);
endTime = startTime = stripISOTime(startTime)+'T000000';
} else {
startTime = formatTime(event.tzstart);
endTime = formatTime(event.tzend);
}
var cal = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'URL:' + document.URL,
'DTSTART:' + (startTime || ''),
'DTEND:' + (endTime || ''),
'SUMMARY:' + (event.title || ''),
'DESCRIPTION:' + (event.description || ''),
'LOCATION:' + (event.address || ''),
'UID:' + (event.id || '') + '-' + document.URL,
'END:VEVENT',
'END:VCALENDAR'].join('\n');
if (ieMustDownload) {
return '<a class="' + eClass + '" onclick="ieDownloadCalendar(\'' +
escapeJSValue(cal) + '\')">' + calendarName + '</a>';
}
var href = encodeURI('data:text/calendar;charset=utf8,' + cal);
return '<a class="' + eClass + '" download="'+CONFIG.texts.download+'" href="' +
href + '">' + calendarName + '</a>';
},
ical: function(event) {
return this.ics(event, 'icon-ical', CONFIG.texts.ical);
},
outlook: function(event) {
return this.ics(event, 'icon-outlook', CONFIG.texts.outlook);
}
};
/* --------------
helpers
--------------- */
var changeTimezone = function(date,timezone) {
if (date) {
if (timezone) {
var invdate = new Date(date.toLocaleString('en-US', {
timeZone: timezone
}));
var diff = date.getTime()-invdate.getTime();
return new Date(date.getTime()+diff);
}
return date;
}
return;
}
var formatTime = function(date) {
return date?date.toISOString().replace(/-|:|\.\d+/g, ''):'';
};
var getEndDate = function(start,duration) {
return new Date(start.getTime() + duration * MS_IN_MINUTES);
};
var stripISOTime = function(isodatestr) {
return isodatestr.substr(0,isodatestr.indexOf('T'));
};
var escapeJSValue = function(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\"/g, '"')
.replace(/\'/g, '\\\'')
.replace(/(\r?\n|\r)/gm, '\\n');
};
/* --------------
output handling
--------------- */
var generateMarkup = function(calendars, clazz, calendarId) {
var result = document.createElement('div');
result.innerHTML = '<label for="checkbox-for-' +
calendarId + '" class="add-to-calendar-label">'+CONFIG.texts.label+'</label>';
result.innerHTML += '<input name="add-to-calendar-checkbox" class="add-to-calendar-checkbox" id="checkbox-for-' + calendarId + '" type="checkbox" ' +
' onclick="closeCalenderOnMouseDown(this)">';
var dropdown = document.createElement('div');
dropdown.className = 'add-to-calendar-dropdown';
Object.keys(calendars).forEach(function(services) {
dropdown.innerHTML += calendars[services];
});
result.appendChild(dropdown);
result.className = 'add-to-calendar-widget';
if (clazz !== undefined) {
result.className += (' ' + clazz);
}
addCSS();
result.id = calendarId;
return result;
};
var generateCalendars = function(event) {
return {
google: calendarGenerators.google(event),
yahoo: calendarGenerators.yahoo(event),
off365: calendarGenerators.off365(event),
ical: calendarGenerators.ical(event),
outlook: calendarGenerators.outlook(event)
};
};
var addCSS = function() {
if (!document.getElementById('add-to-calendar-css')) {
document.getElementsByTagName('head')[0].appendChild(generateCSS());
}
};
var generateCSS = function() {
var styles = document.createElement('style');
styles.id = 'add-to-calendar-css';
styles.innerHTML = ".add-to-calendar{position:relative;text-align:left}.add-to-calendar>*{display:none}.add-to-calendar>.add-to-calendar-widget{display:block}.add-to-calendar-label{cursor:pointer}.add-to-calendar-checkbox+div.add-to-calendar-dropdown{display:none;margin-left:20px}.add-to-calendar-checkbox:checked+div.add-to-calendar-dropdown{display:block}input[type=checkbox].add-to-calendar-checkbox{position:absolute;visibility:hidden}.add-to-calendar-checkbox+div.add-to-calendar-dropdown a{cursor:pointer;display:block}.add-to-calendar-checkbox+div.add-to-calendar-dropdown a:before{width:16px;height:16px;display:inline-block;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAAAQCAYAAACIoli7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NT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a+65VfvVw5nDh5Jpl58NHMK5FCT88diaSGi4DFYnTHvDkgTUyl/8IMABtKh8piZwIuwAAAABJRU5ErkJggg==);margin-right:.5em;content:' '}.icon-ical:before{background-position:-68px 0}.icon-yahoo:before{background-position:-36px +4px}.icon-google:before{background-position:-52px 0}.add-to-calendar-widget{font-family:sans-serif;margin:1em 0;position:relative}.add-to-calendar-label{display:inline-block;background-color:#fff;background-image:url(data:image/vndmicrosofticon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/wAAAAAAAAAAVlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/AAAAAFZWVv9WVlb///////////9WVlb///////////9WVlb///////////9WVlb///////////9WVlb/VlZW/wAAAABWVlb/VlZW////////////VlZW////////////VlZW////////////VlZW////////////VlZW/1ZWVv8AAAAAVlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/AAAAAFZWVv9WVlb///////////9WVlb///////////9WVlb///////////9WVlb///////////9WVlb/VlZW/wAAAABWVlb/VlZW////////////VlZW////////////VlZW////////////VlZW////////////VlZW/1ZWVv8AAAAAVlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/AAAAAFZWVv9WVlb///////////9WVlb///////////9WVlb///////////9WVlb///////////9WVlb/VlZW/wAAAABWVlb/VlZW////////////VlZW////////////VlZW////////////VlZW////////////VlZW/1ZWVv8AAAAAVlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/AAAAAFZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/1ZWVv9WVlb/VlZW/wAAAAAAAAAAVlZW/1ZWVv///////////1ZWVv9WVlb/VlZW/1ZWVv9WVlb///////////9WVlb/VlZW/wAAAAAAAAAAAAAAAAAAAABWVlb///////////9WVlb/AAAAAAAAAAAAAAAAVlZW////////////VlZW/wAAAAAAAAAAAAAAAAAAAAAAAAAAVlZW/1ZWVv9WVlb/VlZW/wAAAAAAAAAAAAAAAFZWVv9WVlb/VlZW/1ZWVv8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==);background-position:10px 45%;background-repeat:no-repeat;padding:1em 1em 1em 40px;background-size:20px 20px;border-radius:3px;box-shadow:0 0 0 .5px rgba(50,50,93,.17),0 2px 5px 0 rgba(50,50,93,.1),0 1px 1.5px 0 rgba(0,0,0,.07),0 1px 2px 0 rgba(0,0,0,.08),0 0 0 0 transparent!important}.add-to-calendar-dropdown{position:absolute;z-index:99;background-color:#fff;top:0;left:0;padding:1em;margin:0!important;border-radius:3px;box-shadow:0 0 0 .5px rgba(50,50,93,.17),0 2px 5px 0 rgba(50,50,93,.1),0 1px 1.5px 0 rgba(0,0,0,.07),0 1px 2px 0 rgba(0,0,0,.08),0 0 0 0 transparent!important}.add-to-calendar-dropdown a{display:block;line-height:1.75em;text-decoration:none;color:inherit;opacity:.7}.add-to-calendar-dropdown a:hover{opacity:1}";
return styles;
};
/* --------------
input handling
--------------- */
var sanitizeParams = function(params) {
if (!params.options) {
params.options = {}
}
if (!params.options.id) {
params.options.id = Math.floor(Math.random() * 1000000);
}
if (!params.options.class) {
params.options.class = '';
}
if (!params.data) {
params.data = {};
}
if (!params.data.start) {
params.data.start=new Date();
}
if (params.data.allday) {
delete params.data.end; // may be set later
delete params.data.duration;
}
if (params.data.end) {
delete params.data.duration;
} else {
if (!params.data.duration) {
params.data.duration = CONFIG.duration;
}
}
if (params.data.duration) {
params.data.end = getEndDate(params.data.start,params.data.duration);
}
if (params.data.timezone) {
params.data.tzstart = changeTimezone(params.data.start,params.data.timezone);
params.data.tzend = changeTimezone(params.data.end,params.data.timezone);
} else {
params.data.tzstart = params.data.start;
params.data.tzend = params.data.end;
}
if (!params.data.title) {
params.data.title = CONFIG.texts.title;
}
};
var validParams = function(params) {
return params.data !== undefined && params.data.start !== undefined &&
(params.data.end !== undefined || params.data.allday !== undefined);
};
var parseCalendar = function(elm) {
/*
<div title="Add to Calendar" class="addtocalendar">
<span class="start">12/18/2018 08:00 AM</span>
<span class="end">12/18/2018 10:00 AM</span>
<span class="duration">45</span>
<span class="allday">true</span>
<span class="timezone">America/Los_Angeles</span>
<span class="title">Summary of the event</span>
<span class="description">Description of the event</span>
<span class="location">Location of the event</span>
</div>
*/
var data = {}, node;
node = elm.querySelector('.start');
if (node) data.start = new Date(node.textContent);
node = elm.querySelector('.end');
if (node) data.end = new Date(node.textContent);
node = elm.querySelector('.duration');
if (node) data.duration = 1*node.textContent;
node = elm.querySelector('.allday');
if (node) data.allday = true;
node = elm.querySelector('.title');
if (node) data.title = node.textContent;
node = elm.querySelector('.description');
if (node) data.description = node.textContent;
node = elm.querySelector('.address');
if (node) data.address = node.textContent;
if (!data.address) {
node = elm.querySelector('.location');
if (node) data.address = node.textContent;
}
node = elm.querySelector('.timezone');
if (node) data.timezone = node.textContent;
cal = createCalendar({data:data});
if (cal) elm.appendChild(cal);
return cal;
}
/* --------------
exports
--------------- */
exports.ieDownloadCalendar = function(cal) {
if (ieCanDownload) {
var blob = new Blob([cal], { type: 'text/calendar' });
window.navigator.msSaveOrOpenBlob(blob, CONFIG.texts.download);
} else {
alert(CONFIG.texts.ienoblob);
}
};
exports.closeCalenderOnMouseDown = function(checkbox) {
//console.log('check');
var closeCalendar = function() {
//console.log('click');
setTimeout(function() { checkbox.checked=false; }, 750);
document.removeEventListener("mousedown",closeCalendar);
};
document.addEventListener("mousedown",closeCalendar);
}
exports.addToCalendarData = function(params) {
if (!params) params = {};
sanitizeParams(params);
if (!validParams(params)) {
console.error('Event details missing.');
return;
}
return generateCalendars(params.data);
}
// bwc
exports.createCalendar = function(params) {
return addToCalendar(params);
};
exports.addToCalendar = function(params) {
if (!params) params = {};
if (params instanceof HTMLElement) {
//console.log('HTMLElement');
return parseCalendar(params);
}
if (params instanceof NodeList) {
//console.log('NodeList');
var success = (params.length>0);
Array.prototype.forEach.call(params, function(node) {
success = success && addToCalendar(node);
});
return success;
}
sanitizeParams(params);
if (!validParams(params)) {
console.error('Event details missing.');
return;
}
return generateMarkup(
generateCalendars(params.data),
params.options.class,
params.options.id
);
};
// document.ready
document.addEventListener("DOMContentLoaded", function(event) {
addToCalendar(document.querySelectorAll(CONFIG.selector));
});
})(this);