forked from kewisch/ical.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_parser.js
163 lines (144 loc) · 4.08 KB
/
component_parser.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http:https://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch, 2011-2015 */
/**
* This symbol is further described later on
* @ignore
*/
ICAL.ComponentParser = (function() {
/**
* @classdesc
* The ComponentParser is used to process a String or jCal Object,
* firing callbacks for various found components, as well as completion.
*
* @example
* var options = {
* // when false no events will be emitted for type
* parseEvent: true,
* parseTimezone: true
* };
*
* var parser = new ICAL.ComponentParser(options);
*
* parser.onevent(eventComponent) {
* //...
* }
*
* // ontimezone, etc...
*
* parser.oncomplete = function() {
*
* };
*
* parser.process(stringOrComponent);
*
* @class
* @alias ICAL.ComponentParser
* @param {Object=} options Component parser options
* @param {Boolean} options.parseEvent Whether events should be parsed
* @param {Boolean} options.parseTimezeone Whether timezones should be parsed
*/
function ComponentParser(options) {
if (typeof(options) === 'undefined') {
options = {};
}
var key;
for (key in options) {
/* istanbul ignore else */
if (options.hasOwnProperty(key)) {
this[key] = options[key];
}
}
}
ComponentParser.prototype = {
/**
* When true, parse events
*
* @type {Boolean}
*/
parseEvent: true,
/**
* When true, parse timezones
*
* @type {Boolean}
*/
parseTimezone: true,
/* SAX like events here for reference */
/**
* Fired when parsing is complete
* @callback
*/
oncomplete: /* istanbul ignore next */ function() {},
/**
* Fired if an error occurs during parsing.
*
* @callback
* @param {Error} err details of error
*/
onerror: /* istanbul ignore next */ function(err) {},
/**
* Fired when a top level component (VTIMEZONE) is found
*
* @callback
* @param {ICAL.Timezone} component Timezone object
*/
ontimezone: /* istanbul ignore next */ function(component) {},
/**
* Fired when a top level component (VEVENT) is found.
*
* @callback
* @param {ICAL.Event} component Top level component
*/
onevent: /* istanbul ignore next */ function(component) {},
/**
* Process a string or parse ical object. This function itself will return
* nothing but will start the parsing process.
*
* Events must be registered prior to calling this method.
*
* @param {ICAL.Component|String|Object} ical The component to process,
* either in its final form, as a jCal Object, or string representation
*/
process: function(ical) {
//TODO: this is sync now in the future we will have a incremental parser.
if (typeof(ical) === 'string') {
ical = ICAL.parse(ical);
}
if (!(ical instanceof ICAL.Component)) {
ical = new ICAL.Component(ical);
}
var components = ical.getAllSubcomponents();
var i = 0;
var len = components.length;
var component;
for (; i < len; i++) {
component = components[i];
switch (component.name) {
case 'vtimezone':
if (this.parseTimezone) {
var tzid = component.getFirstPropertyValue('tzid');
if (tzid) {
this.ontimezone(new ICAL.Timezone({
tzid: tzid,
component: component
}));
}
}
break;
case 'vevent':
if (this.parseEvent) {
this.onevent(new ICAL.Event(component));
}
break;
default:
continue;
}
}
//XXX: ideally we should do a "nextTick" here
// so in all cases this is actually async.
this.oncomplete();
}
};
return ComponentParser;
}());