-
Notifications
You must be signed in to change notification settings - Fork 171
/
ouical.js
172 lines (140 loc) · 10.4 KB
/
ouical.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
;(function(exports) {
var MS_IN_MINUTES = 60 * 1000;
var formatTime = function(date) {
return date.toISOString().replace(/-|:|\.\d+/g, '');
};
var calculateEndTime = function(event) {
return event.end ?
formatTime(event.end) :
formatTime(new Date(event.start.getTime() + (event.duration * MS_IN_MINUTES)));
};
var calendarGenerators = {
google: function(event) {
var startTime = formatTime(event.start);
var endTime = calculateEndTime(event);
var href = encodeURI([
'https://www.google.com/calendar/render',
'?action=TEMPLATE',
'&text=' + (event.title || ''),
'&dates=' + (startTime || ''),
'/' + (endTime || ''),
'&details=' + (event.description || ''),
'&location=' + (event.address || ''),
'&sprop=&sprop=name:'
].join(''));
return '<a class="icon-google" target="_blank" href="' +
href + '">Google Calendar</a>';
},
yahoo: function(event) {
var eventDuration = event.end ?
((event.end.getTime() - event.start.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 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 + '">Yahoo! Calendar</a>';
},
ics: function(event, eClass, calendarName) {
var startTime = formatTime(event.start);
var endTime = calculateEndTime(event);
var href = encodeURI(
'data:text/calendar;charset=utf8,' + [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'URL:' + document.URL,
'DTSTART:' + (startTime || ''),
'DTEND:' + (endTime || ''),
'SUMMARY:' + (event.title || ''),
'DESCRIPTION:' + (event.description || ''),
'LOCATION:' + (event.address || ''),
'END:VEVENT',
'END:VCALENDAR'].join('\n'));
return '<a class="' + eClass + '" target="_blank" href="' +
href + '">' + calendarName + ' Calendar</a>';
},
ical: function(event) {
return this.ics(event, 'icon-ical', 'iCal');
},
outlook: function(event) {
return this.ics(event, 'icon-outlook', 'Outlook');
}
};
var generateCalendars = function(event) {
return {
google: calendarGenerators.google(event),
yahoo: calendarGenerators.yahoo(event),
ical: calendarGenerators.ical(event),
outlook: calendarGenerators.outlook(event)
};
};
// Create CSS
var addCSS = function() {
if (!document.getElementById('ouical-css')) {
document.getElementsByTagName('head')[0].appendChild(generateCSS());
}
};
var generateCSS = function() {
var styles = document.createElement('style');
styles.id = 'ouical-css';
styles.innerHTML = "#add-to-calendar-checkbox-label{cursor:pointer}.add-to-calendar-checkbox~a{display:none}.add-to-calendar-checkbox:checked~a{display:block;width:150px;margin-left:20px}input[type=checkbox].add-to-calendar-checkbox{position:absolute;top:-9999px;left:-9999px}.add-to-calendar-checkbox~a:before{width:16px;height:16px;display:inline-block;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAAAQCAYAAACIoli7AA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5PV6Ov2BQFMoFL5INXqtx+upd3d21rXY7Y5AIMBvTp8FCJeXl/nVBKNFRa7Ag+xgsnH2K0p79+474Ix1IJWy5qgXuw40MPb8dwFkOFfngA0nY9zqQe1WnrQtzQRSBgwGEXs2zqUHmXvFvCCFLwP/Lw6PdhQLjVqFVIwSkCRFIgdPVp+sI66d7ury1Xrc7saGhkZ7OBziAEpxGotXYYQg/J4CReZwh3fdriqM2IQkrZN1mg/H9joY+4DMvSyt+eQlTL71uf8a+65VfvVw5nDh5Jpl58NHMK5FCT88diaSGi4DFYnTHvDkgTUyl/8IMABtKh8piZwIuwAAAABJRU5ErkJggg==);margin-right:.5em;content:' '}.icon-ical:before{background-position:-68px 0}.icon-outlook:before{}.icon-yahoo:before{background-position:-36px +4px}.icon-google:before{background-position:-52px 0}";
return styles;
};
// Make sure we have the necessary event data, such as start time and event duration
var validParams = function(params) {
return params.data !== undefined && params.data.start !== undefined &&
(params.data.end !== undefined || params.data.duration !== undefined);
};
var generateMarkup = function(calendars, clazz, calendarId) {
var result = document.createElement('div');
result.innerHTML = '<label for="checkbox-for-' +
calendarId + '" class="add-to-calendar-checkbox">+ Add to my Calendar</label>';
result.innerHTML += '<input name="add-to-calendar-checkbox" class="add-to-calendar-checkbox" id="checkbox-for-' + calendarId + '" type="checkbox">';
Object.keys(calendars).forEach(function(services) {
result.innerHTML += calendars[services];
});
result.className = 'add-to-calendar';
if (clazz !== undefined) {
result.className += (' ' + clazz);
}
addCSS();
result.id = calendarId;
return result;
};
var getClass = function(params) {
if (params.options && params.options.class) {
return params.options.class;
}
};
var getOrGenerateCalendarId = function(params) {
return params.options && params.options.id ?
params.options.id :
Math.floor(Math.random() * 1000000); // Generate a 6-digit random ID
};
exports.createCalendar = function(params) {
if (!validParams(params)) {
console.log('Event details missing.');
return;
}
return generateMarkup(generateCalendars(params.data),
getClass(params),
getOrGenerateCalendarId(params));
};
})(this);