-
Notifications
You must be signed in to change notification settings - Fork 15
/
icalevent.js
100 lines (79 loc) · 2.86 KB
/
icalevent.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
var tzone = require('tzone');
var iCalEvent = function(event){
this.id = '-//iCalEvent.js v0.3//EN';
this.event = {};
if (event) this.create(event);
if (!this.event.uid) this.event.uid = this.createUID();
}
iCalEvent.prototype = {
create: function(event){
for (var key in event) {
if (event.hasOwnProperty(key)) this.set(key, event[key]);
}
},
createUID: function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c){
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
},
format: function(datetime){
function pad(n){
return (n < 10 ? '0' : '') + n;
}
var d = new Date(datetime);
d.setUTCMinutes(d.getUTCMinutes() - this.event.offset);
padded = (d.getUTCFullYear()
+ pad(d.getUTCMonth() + 1)
+ pad(d.getUTCDate()) + 'T'
+ pad(d.getUTCHours())
+ pad(d.getUTCMinutes())
+ pad(d.getUTCSeconds()));
return padded;
},
get: function(key){
if (this.event[key]) return this.event[key];
},
set: function(key, value){
if (this[key]) this[key](value)
else this.event[key] = value;
},
start: function(datetime){
if (!this.event.timezone) {
var d = new Date(datetime);
d.setUTCMinutes(d.getUTCMinutes() - this.event.offset);
this.event.timezone = tzone.getLocation(d);
}
this.event.start = this.format(datetime);
},
end: function(datetime){
this.event.end = this.format(datetime);
},
toFile: function(){
var result = '';
result += 'BEGIN:VCALENDAR\r\n';
result += 'VERSION:2.0\r\n';
result += 'PRODID:' + this.id + '\r\n';
result += 'BEGIN:VEVENT\r\n';
result += 'UID:' + this.event.uid + '\r\n';
result += 'DTSTAMP:' + this.format(new Date()) + '\r\n';
if (this.event.method) result += 'METHOD:' + this.event.method.toUpperCase() + '\r\n';
if (this.event.status) result += 'STATUS:' + this.event.status.toUpperCase() + '\r\n';
if (this.event.start) result += 'DTSTART;TZID=' + this.event.timezone + ':' + this.event.start + '\r\n';
if (this.event.end) result += 'DTEND;TZID=' + this.event.timezone + ':' + this.event.end + '\r\n';
if (this.event.summary) result += 'SUMMARY:' + this.event.summary + '\r\n';
if (this.event.description) result += 'DESCRIPTION:' + this.event.description + '\r\n';
if (this.event.organizer) result += 'ORGANIZER;CN=' + this.event.organizer.name + ':mailto:' + this.event.organizer.email + '\r\n';
if (this.event.location) result += 'LOCATION:' + this.event.location + '\r\n';
if (this.event.url) result += 'URL;VALUE=URI:' + this.event.url + '\r\n';
if (this.event.attendees) {
this.event.attendees.forEach(function(attendee){
result += 'ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CN=' + attendee.name + ':MAILTO:' + attendee.email + '\r\n';
});
}
result += 'END:VEVENT\r\n';
result += 'END:VCALENDAR\r\n';
return result;
}
}
module.exports = iCalEvent;