forked from kewisch/ical.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.js
567 lines (484 loc) · 15.3 KB
/
event.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* 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.Event = (function() {
/**
* @classdesc
* ICAL.js is organized into multiple layers. The bottom layer is a raw jCal
* object, followed by the component/property layer. The highest level is the
* event representation, which this class is part of. See the
* {@tutorial layers} guide for more details.
*
* @class
* @alias ICAL.Event
* @param {ICAL.Component=} component The ICAL.Component to base this event on
* @param {Object} options Options for this event
* @param {Boolean} options.strictExceptions
* When true, will verify exceptions are related by their UUID
* @param {Array<ICAL.Component|ICAL.Event>} options.exceptions
* Exceptions to this event, either as components or events. If not
* specified exceptions will automatically be set in relation of
* component's parent
*/
function Event(component, options) {
if (!(component instanceof ICAL.Component)) {
options = component;
component = null;
}
if (component) {
this.component = component;
} else {
this.component = new ICAL.Component('vevent');
}
this._rangeExceptionCache = Object.create(null);
this.exceptions = Object.create(null);
this.rangeExceptions = [];
if (options && options.strictExceptions) {
this.strictExceptions = options.strictExceptions;
}
if (options && options.exceptions) {
options.exceptions.forEach(this.relateException, this);
} else if (this.component.parent && !this.isRecurrenceException()) {
this.component.parent.getAllSubcomponents('vevent').forEach(function(event) {
if (event.hasProperty('recurrence-id')) {
this.relateException(event);
}
}, this);
}
}
Event.prototype = {
THISANDFUTURE: 'THISANDFUTURE',
/**
* List of related event exceptions.
*
* @type {ICAL.Event[]}
*/
exceptions: null,
/**
* When true, will verify exceptions are related by their UUID.
*
* @type {Boolean}
*/
strictExceptions: false,
/**
* Relates a given event exception to this object. If the given component
* does not share the UID of this event it cannot be related and will throw
* an exception.
*
* If this component is an exception it cannot have other exceptions
* related to it.
*
* @param {ICAL.Component|ICAL.Event} obj Component or event
*/
relateException: function(obj) {
if (this.isRecurrenceException()) {
throw new Error('cannot relate exception to exceptions');
}
if (obj instanceof ICAL.Component) {
obj = new ICAL.Event(obj);
}
if (this.strictExceptions && obj.uid !== this.uid) {
throw new Error('attempted to relate unrelated exception');
}
var id = obj.recurrenceId.toString();
// we don't sort or manage exceptions directly
// here the recurrence expander handles that.
this.exceptions[id] = obj;
// index RANGE=THISANDFUTURE exceptions so we can
// look them up later in getOccurrenceDetails.
if (obj.modifiesFuture()) {
var item = [
obj.recurrenceId.toUnixTime(), id
];
// we keep them sorted so we can find the nearest
// value later on...
var idx = ICAL.helpers.binsearchInsert(
this.rangeExceptions,
item,
compareRangeException
);
this.rangeExceptions.splice(idx, 0, item);
}
},
/**
* Checks if this record is an exception and has the RANGE=THISANDFUTURE
* value.
*
* @return {Boolean} True, when exception is within range
*/
modifiesFuture: function() {
if (!this.component.hasProperty('recurrence-id')) {
return false;
}
var range = this.component.getFirstProperty('recurrence-id').getParameter('range');
return range === this.THISANDFUTURE;
},
/**
* Finds the range exception nearest to the given date.
*
* @param {ICAL.Time} time usually an occurrence time of an event
* @return {?ICAL.Event} the related event/exception or null
*/
findRangeException: function(time) {
if (!this.rangeExceptions.length) {
return null;
}
var utc = time.toUnixTime();
var idx = ICAL.helpers.binsearchInsert(
this.rangeExceptions,
[utc],
compareRangeException
);
idx -= 1;
// occurs before
if (idx < 0) {
return null;
}
var rangeItem = this.rangeExceptions[idx];
/* istanbul ignore next: sanity check only */
if (utc < rangeItem[0]) {
return null;
}
return rangeItem[1];
},
/**
* This object is returned by {@link ICAL.Event#getOccurrenceDetails getOccurrenceDetails}
*
* @typedef {Object} occurrenceDetails
* @memberof ICAL.Event
* @property {ICAL.Time} recurrenceId The passed in recurrence id
* @property {ICAL.Event} item The occurrence
* @property {ICAL.Time} startDate The start of the occurrence
* @property {ICAL.Time} endDate The end of the occurrence
*/
/**
* Returns the occurrence details based on its start time. If the
* occurrence has an exception will return the details for that exception.
*
* NOTE: this method is intend to be used in conjunction
* with the {@link ICAL.Event#iterator iterator} method.
*
* @param {ICAL.Time} occurrence time occurrence
* @return {ICAL.Event.occurrenceDetails} Information about the occurrence
*/
getOccurrenceDetails: function(occurrence) {
var id = occurrence.toString();
var utcId = occurrence.convertToZone(ICAL.Timezone.utcTimezone).toString();
var item;
var result = {
//XXX: Clone?
recurrenceId: occurrence
};
if (id in this.exceptions) {
item = result.item = this.exceptions[id];
result.startDate = item.startDate;
result.endDate = item.endDate;
result.item = item;
} else if (utcId in this.exceptions) {
item = this.exceptions[utcId];
result.startDate = item.startDate;
result.endDate = item.endDate;
result.item = item;
} else {
// range exceptions (RANGE=THISANDFUTURE) have a
// lower priority then direct exceptions but
// must be accounted for first. Their item is
// always the first exception with the range prop.
var rangeExceptionId = this.findRangeException(
occurrence
);
var end;
if (rangeExceptionId) {
var exception = this.exceptions[rangeExceptionId];
// range exception must modify standard time
// by the difference (if any) in start/end times.
result.item = exception;
var startDiff = this._rangeExceptionCache[rangeExceptionId];
if (!startDiff) {
var original = exception.recurrenceId.clone();
var newStart = exception.startDate.clone();
// zones must be same otherwise subtract may be incorrect.
original.zone = newStart.zone;
startDiff = newStart.subtractDate(original);
this._rangeExceptionCache[rangeExceptionId] = startDiff;
}
var start = occurrence.clone();
start.zone = exception.startDate.zone;
start.addDuration(startDiff);
end = start.clone();
end.addDuration(exception.duration);
result.startDate = start;
result.endDate = end;
} else {
// no range exception standard expansion
end = occurrence.clone();
end.addDuration(this.duration);
result.endDate = end;
result.startDate = occurrence;
result.item = this;
}
}
return result;
},
/**
* Builds a recur expansion instance for a specific point in time (defaults
* to startDate).
*
* @param {ICAL.Time} startTime Starting point for expansion
* @return {ICAL.RecurExpansion} Expansion object
*/
iterator: function(startTime) {
return new ICAL.RecurExpansion({
component: this.component,
dtstart: startTime || this.startDate
});
},
/**
* Checks if the event is recurring
*
* @return {Boolean} True, if event is recurring
*/
isRecurring: function() {
var comp = this.component;
return comp.hasProperty('rrule') || comp.hasProperty('rdate');
},
/**
* Checks if the event describes a recurrence exception. See
* {@tutorial terminology} for details.
*
* @return {Boolean} True, if the event describes a recurrence exception
*/
isRecurrenceException: function() {
return this.component.hasProperty('recurrence-id');
},
/**
* Returns the types of recurrences this event may have.
*
* Returned as an object with the following possible keys:
*
* - YEARLY
* - MONTHLY
* - WEEKLY
* - DAILY
* - MINUTELY
* - SECONDLY
*
* @return {Object.<ICAL.Recur.frequencyValues, Boolean>}
* Object of recurrence flags
*/
getRecurrenceTypes: function() {
var rules = this.component.getAllProperties('rrule');
var i = 0;
var len = rules.length;
var result = Object.create(null);
for (; i < len; i++) {
var value = rules[i].getFirstValue();
result[value.freq] = true;
}
return result;
},
/**
* The uid of this event
* @type {String}
*/
get uid() {
return this._firstProp('uid');
},
set uid(value) {
this._setProp('uid', value);
},
/**
* The start date
* @type {ICAL.Time}
*/
get startDate() {
return this._firstProp('dtstart');
},
set startDate(value) {
this._setTime('dtstart', value);
},
/**
* The end date. This can be the result directly from the property, or the
* end date calculated from start date and duration. Setting the property
* will remove any duration properties.
* @type {ICAL.Time}
*/
get endDate() {
var endDate = this._firstProp('dtend');
if (!endDate) {
var duration = this._firstProp('duration');
endDate = this.startDate.clone();
if (duration) {
endDate.addDuration(duration);
} else if (endDate.isDate) {
endDate.day += 1;
}
}
return endDate;
},
set endDate(value) {
if (this.component.hasProperty('duration')) {
this.component.removeProperty('duration');
}
this._setTime('dtend', value);
},
/**
* The duration. This can be the result directly from the property, or the
* duration calculated from start date and end date. Setting the property
* will remove any `dtend` properties.
* @type {ICAL.Duration}
*/
get duration() {
var duration = this._firstProp('duration');
if (!duration) {
return this.endDate.subtractDateTz(this.startDate);
}
return duration;
},
set duration(value) {
if (this.component.hasProperty('dtend')) {
this.component.removeProperty('dtend');
}
this._setProp('duration', value);
},
/**
* The location of the event.
* @type {String}
*/
get location() {
return this._firstProp('location');
},
set location(value) {
return this._setProp('location', value);
},
/**
* The attendees in the event
* @type {ICAL.Property[]}
* @readonly
*/
get attendees() {
//XXX: This is way lame we should have a better
// data structure for this later.
return this.component.getAllProperties('attendee');
},
/**
* The event summary
* @type {String}
*/
get summary() {
return this._firstProp('summary');
},
set summary(value) {
this._setProp('summary', value);
},
/**
* The event description.
* @type {String}
*/
get description() {
return this._firstProp('description');
},
set description(value) {
this._setProp('description', value);
},
/**
* The event color from [rfc7986](https://datatracker.ietf.org/doc/html/rfc7986)
* @type {String}
*/
get color() {
return this._firstProp('color');
},
set color(value) {
this._setProp('color', value);
},
/**
* The organizer value as an uri. In most cases this is a mailto: uri, but
* it can also be something else, like urn:uuid:...
* @type {String}
*/
get organizer() {
return this._firstProp('organizer');
},
set organizer(value) {
this._setProp('organizer', value);
},
/**
* The sequence value for this event. Used for scheduling
* see {@tutorial terminology}.
* @type {Number}
*/
get sequence() {
return this._firstProp('sequence');
},
set sequence(value) {
this._setProp('sequence', value);
},
/**
* The recurrence id for this event. See {@tutorial terminology} for details.
* @type {ICAL.Time}
*/
get recurrenceId() {
return this._firstProp('recurrence-id');
},
set recurrenceId(value) {
this._setTime('recurrence-id', value);
},
/**
* Set/update a time property's value.
* This will also update the TZID of the property.
*
* TODO: this method handles the case where we are switching
* from a known timezone to an implied timezone (one without TZID).
* This does _not_ handle the case of moving between a known
* (by TimezoneService) timezone to an unknown timezone...
*
* We will not add/remove/update the VTIMEZONE subcomponents
* leading to invalid ICAL data...
* @private
* @param {String} propName The property name
* @param {ICAL.Time} time The time to set
*/
_setTime: function(propName, time) {
var prop = this.component.getFirstProperty(propName);
if (!prop) {
prop = new ICAL.Property(propName);
this.component.addProperty(prop);
}
// utc and local don't get a tzid
if (
time.zone === ICAL.Timezone.localTimezone ||
time.zone === ICAL.Timezone.utcTimezone
) {
// remove the tzid
prop.removeParameter('tzid');
} else {
prop.setParameter('tzid', time.zone.tzid);
}
prop.setValue(time);
},
_setProp: function(name, value) {
this.component.updatePropertyWithValue(name, value);
},
_firstProp: function(name) {
return this.component.getFirstPropertyValue(name);
},
/**
* The string representation of this event.
* @return {String}
*/
toString: function() {
return this.component.toString();
}
};
function compareRangeException(a, b) {
if (a[0] > b[0]) return 1;
if (b[0] > a[0]) return -1;
return 0;
}
return Event;
}());