forked from mds4mstuke/ics-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathics-generator.js
84 lines (74 loc) · 2.4 KB
/
ics-generator.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
// ----------------------------------------------------------------------------
// @author: stuke
// @date: Sept 14, 2015
// @description: Generate ICS files mostly compliant to RFC-5545 spec.
// Implementing specific portions (no VTODO, VJOURNAL, VFREEBUSY, VALARM)
// ----------------------------------------------------------------------------
const params = IcsUtils.filterPropParams("calendar");
IcsGenerator = function(options) {
IcsUtils.checkOptions(options, params);
for (var paramKey in params) {
if (paramKey == "events") {
this.events = !!options.events ? _.map(options.events, function(event) { return new IcsEvent(event); }) : params.events.default;
} else {
this[paramKey] = options[paramKey] || params[paramKey].default;
}
if (!this[paramKey]) { delete this[paramKey]; }
}
};
_.extend(IcsGenerator.prototype, {
valueOf: function() {
var self = this;
var out = {};
var keys = Object.getOwnPropertyNames(self);
_.each(keys, function(key) {
if (key == "events") {
out[key] = _.map(self.events, function(event) { return event.valueOf(); });
} else {
out[key] = self[key];
}
});
return out;
},
////////////
// getters
////////////
getProperty: function(property) {
if (property == "events") {
return _.map(this.events, function(event) { return event.valueOf(); });
} else {
return this[property];
}
},
////////////
// setters
////////////
setProperty: function(property, value) {
try {
IcsUtils.checkOptions({property: value}, params);
if (property == "events") {
this.events = _.map(value, function(event) { return new IcsEvent(event); })
} else {
this[property] = value;
}
return true;
} catch (e) {
return false;
}
},
createEvent: function(event) {
return new IcsEvent(event);
},
createAttendee: function(attendee) {
return new IcsAttendee(attendee);
},
addEvent: function(event) {
return this.events.push(new IcsEvent(event));
},
//////////////
// generator
//////////////
toIcsString: function() {
return IcsUtils.parseAndWrap(this.valueOf());
}
});