-
Notifications
You must be signed in to change notification settings - Fork 1
/
icalendar.js
129 lines (123 loc) · 4.51 KB
/
icalendar.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
/* Construct an iCalendar with an event object.
@param event (object) the event details
@return (string) the iCalendar definition */
var makeICalendar = exports.makeICalendar = function (event) {
var limit75 = function(text) {
var out = '';
while (text.length > 75) {
out += text.substr(0, 75) + '\n';
text = ' ' + text.substr(75);
}
out += text;
return out;
};
return 'BEGIN:VCALENDAR\n' +
'VERSION:2.0\n' +
'PRODID:jquery.icalendar\n' +
'METHOD:PUBLISH\n' +
'BEGIN:VEVENT\n' +
'UID:' + new Date().getTime() + '@' + 'eventharvy' + '\n' +
'DTSTAMP:' + formatDateTime(new Date()) + '\n' +
(event.url ? limit75('URL:' + event.url) + '\n' : '') +
(event.contact ? limit75('MAILTO:' + event.contact) + '\n' : '') +
limit75('TITLE:' + event.title) + '\n' +
'DTSTART:' + formatDateTime(event.start) + '\n' +
'DTEND:' + formatDateTime(event.end) + '\n' +
(event.summary ? limit75('SUMMARY:' + event.summary) + '\n' : '') +
(event.description ? limit75('DESCRIPTION:' + event.description) + '\n' : '') +
(event.location ? limit75('LOCATION:' + event.location) + '\n' : '') +
(event.recurrence ? makeRecurrence(event.recurrence) + '\n' : '') +
'END:VEVENT\n' +
'END:VCALENDAR';
};
/* Format a date/time for iCalendar: yyyymmddThhmmss[Z].
@param dateTime (Date) the date/time to format
@param local (boolean) true if this should be a local date/time
@return (string) the formatted date/time */
var formatDateTime = function (dateTime, local) {
return (!dateTime ? '' : (local ?
'' + dateTime.getFullYear() + ensureTwo(dateTime.getMonth() + 1) +
ensureTwo(dateTime.getDate()) + 'T' + ensureTwo(dateTime.getHours()) +
ensureTwo(dateTime.getMinutes()) + ensureTwo(dateTime.getSeconds()) :
'' + dateTime.getUTCFullYear() + ensureTwo(dateTime.getUTCMonth() + 1) +
ensureTwo(dateTime.getUTCDate()) + 'T' + ensureTwo(dateTime.getUTCHours()) +
ensureTwo(dateTime.getUTCMinutes()) + ensureTwo(dateTime.getUTCSeconds()) + 'Z'));
};
/* Ensure a string has at least two digits.
@param value (number) the number to convert
@return (string) the string equivalent */
var ensureTwo = function(value) {
return (value < 10 ? '0' : '') + value;
};
/* Construct an iCalendar recurrence definition. ;
@param recur (object) the recurrence details
@return (string) the iCalendar definition */
var makeRecurrence = function (recur) {
if (!recur) {
return '';
}
var def = '';
if (recur.dates) {
def = 'RDATE;VALUE=DATE:';
if (!isArray(recur.dates)) {
recur.dates = [recur.dates];
}
for (var i = 0; i < recur.dates.length; i++) {
def += (i > 0 ? ',' : '') + formatDate(recur.dates[i]);
}
}
else if (recur.times) {
def = 'RDATE;VALUE=DATE-TIME:';
if (!isArray(recur.times)) {
recur.times = [recur.times];
}
for (var i = 0; i < recur.times.length; i++) {
def += (i > 0 ? ',' : '') + formatDateTime(recur.times[i]);
}
}
else if (recur.periods) {
def = 'RDATE;VALUE=PERIOD:';
if (!isArray(recur.periods[0])) {
recur.periods = [recur.periods];
}
for (var i = 0; i < recur.periods.length; i++) {
def += (i > 0 ? ',' : '') + formatDateTime(recur.periods[i][0]) +
'/' + (recur.periods[i][1].constructor != Date ? recur.periods[i][1] :
formatDateTime(recur.periods[i][1]));
}
}
else {
def = 'RRULE:FREQ=' + (recur.freq || 'daily').toUpperCase() +
(recur.interval ? ';INTERVAL=' + recur.interval : '') +
(recur.until ? ';UNTIL=' + formatDateTime(recur.until) :
(recur.count ? ';COUNT=' + recur.count : '')) +
(recur.weekStart != null ? ';WKST=' +
['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'][recur.weekStart] : '');
if (recur.by) {
if (!isArray(recur.by)) {
recur.by = [recur.by];
}
for (var i = 0; i < recur.by.length; i++) {
if (!isArray(recur.by[i].values)) {
recur.by[i].values = [recur.by[i].values];
}
def += ';BY' + recur.by[i].type.toUpperCase() + '=' +
recur.by[i].values.join(',');
}
}
}
return def;
};
/* Determine whether an object is an array.
@param a (object) the object to test
@return (boolean) true if it is an array, or false if not */
var isArray = function (a) {
return (a && a.constructor == Array);
};
/* Format a date for iCalendar: yyyymmdd.
@param date (Date) the date to format
@return (string) the formatted date */
var formatDate = function (date, local) {
return (!date ? '' : '' + date.getFullYear() +
ensureTwo(date.getMonth() + 1) + ensureTwo(date.getDate()));
};