-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.icalendar.js
793 lines (752 loc) · 29.3 KB
/
jquery.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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* http://keith-wood.name/icalendar.html
iCalendar processing for jQuery v1.1.1.
Written by Keith Wood (kbwood{at}iinet.com.au) October 2008.
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
Please attribute the author if you use it. */
(function($) { // Hide scope, no $ conflict
var PROP_NAME = 'icalendar';
var FLASH_ID = 'icalendar-flash-copy';
/* iCalendar sharing manager. */
function iCalendar() {
this._defaults = {
sites: [], // List of site IDs to use, empty for all
icons: 'icalendar.png', // Horizontal amalgamation of all site icons
iconSize: 16, // The size of the individual icons
target: '_blank', // The name of the target window for the iCalendar links
compact: false, // True if a compact presentation should be used, false for full
popup: false, // True to have it popup on demand, false to show always
popupText: 'Send to my calendar...', // Text for the popup trigger
tipPrefix: '', // Additional text to show in the tool tip for each icon
echoUrl: '', // The URL to echo back iCalendar content, or blank for clipboard
echoField: '', // The ID of a field to copy the iCalendar definition into, or blank for clipboard
start: null, // The start date/time of the event
end: null, // The end date/time of the event
title: '', // The title of the event
summary: '', // The summary of the event
description: '', // The description of the event
location: '', // The location of the event
url: '', // A URL with more information about the event
contact: '', // An e-mail address for further contact about the event
recurrence: null, // Details about a recurring event, an object with attributes:
// dates (Date or Date[]) or times (Date or Date[]) or
// periods (Date[2] or Date[][2] or [][Date, string]) or
// freq (string - secondly, minutely, hourly, daily, weekly, monthly, yearly),
// interval (number), until (Date), count (number), weekStart (number),
// by (object or object[] - type (string - second, minute, day, monthday, yearday,
// weekno, month, setpos), values (number or number[] or string or string[]))
// Confirmation message for clipboard copy
copyConfirm: 'The event will be copied to your clipboard. Continue?',
// Success message during clipboard copy
copySucceeded: 'The event has been copied to your clipboard',
// Failure message during clipboard copy
copyFailed: 'Failed to copy the event to the clipboard\n',
copyFlash: 'clipboard.swf', // The URL for the Flash clipboard copy module
// Clipboard not supported message
copyUnavailable: 'The clipboard is unavailable, please copy the event details from below:\n'
};
this._sites = { // The definitions of the available iCalendar sites
'google': {display: 'Google', icon: 0, override: null,
url: 'http://www.google.com/calendar/event?action=TEMPLATE' +
'&text={t}&dates={s}/{e}&details={d}&location={l}&sprop=website:{u}'},
'icalendar': {display: 'iCalendar', icon: 1, override: null, url: 'echo'},
'outlook': {display: 'Outlook', icon: 2, override: null, url: 'echo'},
'yahoo': {display: 'Yahoo', icon: 3, override: yahooOverride,
url: 'http://calendar.yahoo.com/?v=60&view=d&type=20' +
'&title={t}&st={s}&dur={p}&desc={d}&in_loc={l}&url={u}&rpat={r}'}
};
}
var FREQ_SETTINGS = [{method: 'Seconds', factor: 1},
{method: 'Minutes', factor: 60}, {method: 'Hours', factor: 3600},
{method: 'Date', factor: 86400}, {method: 'Month', factor: 1},
{method: 'FullYear', factor: 12}, {method: 'Date', factor: 604800}];
var SE = 0;
var MI = 1;
var HR = 2;
var DY = 3;
var MO = 4;
var YR = 5;
var WK = 6;
$.extend(iCalendar.prototype, {
/* Class name added to elements to indicate already configured with iCalendar. */
markerClassName: 'hasICalendar',
/* Override the default settings for all iCalendar instances.
@param settings (object) the new settings to use as defaults
@return void */
setDefaults: function(settings) {
extendRemove(this._defaults, settings || {});
return this;
},
/* Add a new iCalendar site to the list.
@param id (string) the ID of the new site
@param display (string) the display name for this site
@param icon (url) the location of an icon for this site (16x16), or
(number) the index of the icon within the combined image
@param url (url) the submission URL for this site
with {t} marking where the event title should be inserted,
{s} indicating the event start date/time insertion point,
{e} indicating the event end date/time insertion point,
{p} indicating the event period (duration) insertion point,
{d} indicating the event description insertion point,
{l} indicating the event location insertion point,
{u} indicating the event URL insertion point,
{c} indicating the event contact insertion point,
{r} indicating the event recurrence insertion point
@param override (function, optional) a function to override default settings
@return void */
addSite: function(id, display, icon, url, override) {
this._sites[id] = {display: display, icon: icon, override: override, url: url};
return this;
},
/* Return the list of defined sites.
@return object[] - indexed by site id (string), each object contains
display (string) the display name,
icon (string) the location of the icon, or
(number) the icon's index in the combined image
url (string) the submission URL for the site */
getSites: function() {
return this._sites;
},
/* Attach the iCalendar widget to a div. */
_attachICalendar: function(target, settings) {
target = $(target);
if (target.hasClass(this.markerClassName)) {
return;
}
target.addClass(this.markerClassName);
this._updateICalendar(target, settings);
},
/* Reconfigure the settings for an iCalendar div. */
_changeICalendar: function(target, settings) {
target = $(target);
if (!target.hasClass(this.markerClassName)) {
return;
}
this._updateICalendar(target, settings);
},
/* Construct the requested iCalendar links. */
_updateICalendar: function(target, settings) {
settings = extendRemove($.extend({}, this._defaults,
$.data(target[0], PROP_NAME) || {}), settings);
$.data(target[0], PROP_NAME, settings);
var sites = settings.sites || this._defaults.sites;
if (sites.length == 0) { // default to all sites
$.each(this._sites, function(id) {
sites[sites.length] = id;
});
}
var addSite = function(site, calId) {
var inserts = {t: encodeURIComponent(settings.title),
d: encodeURIComponent(settings.description),
s: $.icalendar.formatDateTime(settings.start),
e: $.icalendar.formatDateTime(settings.end),
p: $.icalendar.calculateDuration(settings.start, settings.end),
l: encodeURIComponent(settings.location),
u: encodeURIComponent(settings.url),
c: encodeURIComponent(settings.contact),
r: makeRecurrence(settings.recurrence)};
if (site.override) {
site.override.apply(target, [inserts, settings]);
}
var url = site.url;
$.each(inserts, function(n, v) {
var re = new RegExp('\\{' + n + '\\}', 'g');
url = url.replace(re, v);
});
var url = (site.url == 'echo' ? '#' : url);
var item = $('<li></li>');
var anchor = $('<a href="' + url + '" title="' + settings.tipPrefix + site.display + '"' +
(site.url == 'echo' ? '' : ' target="' + settings._target + '"') + '></a>');
if (site.url == 'echo') {
anchor.click(function() {
return $.icalendar._echo(target[0], calId);
});
}
var html = '';
if (site.icon != null) {
if (typeof site.icon == 'number') {
html += '<span style="background: ' +
'transparent url(' + settings.icons + ') no-repeat -' +
(site.icon * settings.iconSize) + 'px 0px;' +
($.browser.mozilla && $.browser.version < '1.9' ?
' padding-left: ' + settings.iconSize + 'px; padding-bottom: ' +
Math.max(0, (settings.iconSize / 2) - 5) + 'px;' : '') + '"></span>';
}
else {
html += '<img src="' + site.icon + '"' +
(($.browser.mozilla && $.browser.version < '1.9') ||
($.browser.msie && $.browser.version < '7.0') ?
' style="vertical-align: bottom;"' :
($.browser.msie ? ' style="vertical-align: middle;"' :
($.browser.opera || $.browser.safari ?
' style="vertical-align: baseline;"' : ''))) + '/>';
}
html += (settings.compact ? '' : ' ');
}
anchor.html(html + (settings.compact ? '' : site.display));
item.append(anchor);
return item;
};
var list = $('<ul class="icalendar_list' +
(settings.compact ? ' icalendar_compact' : '') + '"></ul>');
var allSites = this._sites;
$.each(sites, function(index, id) {
list.append(addSite(allSites[id], id));
});
target.empty().append(list);
if (settings.popup) {
list.before('<span class="icalendar_popup_text">' +
settings.popupText + '</span>').
wrap('<div class="icalendar_popup"></div>');
target.click(function() {
var target = $(this);
var offset = target.offset();
$('.icalendar_popup', target).css('left', offset.left).
css('top', offset.top + target.outerHeight()).toggle();
});
}
},
/* Remove the iCalendar widget from a div. */
_destroyICalendar: function(target) {
target = $(target);
if (!target.hasClass(this.markerClassName)) {
return;
}
target.removeClass(this.markerClassName).empty();
$.removeData(target[0], PROP_NAME);
},
/* Echo the iCalendar text back to the user either as a
downloadable file or via the clipboard.
@param target (element) the owning division
@param calId (string) the ID of the site to send the calendar to */
_echo: function(target, calId) {
var settings = $.data(target, PROP_NAME);
var event = makeICalendar(settings);
if (settings.echoUrl) {
window.location.href = settings.echoUrl + '?content=' + escape(event);
}
else if (settings.echoField) {
$(settings.echoField).val(event);
}
else if (!settings.copyFlash) {
alert(settings.copyUnavailable + event);
}
else if (confirm(settings.copyConfirm)) {
var error = '';
if (error = copyViaFlash(event, settings.copyFlash)) {
alert(settings.copyFailed + error);
}
else {
alert(settings.copySucceeded);
}
}
return false; // Don't follow link
},
/* Ensure a string has at least two digits.
@param value (number) the number to convert
@return (string) the string equivalent */
_ensureTwo: function(value) {
return (value < 10 ? '0' : '') + value;
},
/* Format a date for iCalendar: yyyymmdd.
@param date (Date) the date to format
@return (string) the formatted date */
formatDate: function(date, local) {
return (!date ? '' : '' + date.getFullYear() +
this._ensureTwo(date.getMonth() + 1) + this._ensureTwo(date.getDate()));
},
/* 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 */
formatDateTime: function(dateTime, local) {
return (!dateTime ? '' : (local ?
'' + dateTime.getFullYear() + this._ensureTwo(dateTime.getMonth() + 1) +
this._ensureTwo(dateTime.getDate()) + 'T' + this._ensureTwo(dateTime.getHours()) +
this._ensureTwo(dateTime.getMinutes()) + this._ensureTwo(dateTime.getSeconds()) :
'' + dateTime.getUTCFullYear() + this._ensureTwo(dateTime.getUTCMonth() + 1) +
this._ensureTwo(dateTime.getUTCDate()) + 'T' + this._ensureTwo(dateTime.getUTCHours()) +
this._ensureTwo(dateTime.getUTCMinutes()) + this._ensureTwo(dateTime.getUTCSeconds()) + 'Z'));
},
/* Calculate the duration between two date/times.
@param start (Date) the starting date/time
@param end (Date) the ending date/time
@return (string) the formatted duration or blank if invalid parameters */
calculateDuration: function(start, end) {
if (!start || !end) {
return '';
}
var seconds = Math.abs(end.getTime() - start.getTime()) / 1000;
var days = Math.floor(seconds / 86400);
seconds -= days * 86400;
var hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
var minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
return (start.getTime() > end.getTime() ? '-' : '') +
'P' + (days > 0 ? days + 'D' : '') +
(hours || minutes || seconds ? 'T' + hours + 'H' : '') +
(minutes || seconds ? minutes + 'M' : '') + (seconds ? seconds + 'S' : '');
},
/* Calculate the end date/time given a start and a duration.
@param start (Date) the starting date/time
@param duration (string) the description of the duration
@return (Date) the ending date/time
@throws error if an invalid duration is found */
addDuration: function(start, duration) {
if (!duration) {
return start;
}
var end = new Date(start.getTime());
var matches = DURATION.exec(duration);
if (!matches) {
throw 'Invalid duration';
}
if (matches[2] && (matches[3] || matches[5] || matches[6] || matches[7])) {
throw 'Invalid duration - week must be on its own'; // Week must be on its own
}
if (!matches[4] && (matches[5] || matches[6] || matches[7])) {
throw 'Invalid duration - missing time marker'; // Missing T with hours/minutes/seconds
}
var sign = (matches[1] == '-' ? -1 : +1);
var apply = function(value, factor, method) {
value = parseInt(value);
if (!isNaN(value)) {
end['setUTC' + method](end['getUTC' + method]() + sign * value * factor);
}
};
if (matches[2]) {
apply(matches[2], 7, 'Date');
}
else {
apply(matches[3], 1, 'Date');
apply(matches[5], 1, 'Hours');
apply(matches[6], 1, 'Minutes');
apply(matches[7], 1, 'Seconds');
}
return end;
},
/* Parse the iCalendar data into a JavaScript object model.
@param content (string) the original iCalendar data
@return (object) the iCalendar JavaScript model
@throws errors if the iCalendar structure is incorrect */
parse: function(content) {
var cal = {};
var timezones = {};
var lines = unfoldLines(content);
parseGroup(lines, 0, cal, timezones);
if (!cal.vcalendar) {
throw 'Invalid iCalendar data';
}
return cal.vcalendar;
},
/* Calculate the week of the year for a given date
according to the ISO 8601 definition.
@param date (Date) the date to calculate the week for
@param weekStart (number) the day on which a week starts:
0 = Sun, 1 = Mon, ... (optional, defaults to 1)
@return (number) the week for these parameters (1-53) */
getWeekOfYear: function(date, weekStart) {
return getWeekOfYear(date, weekStart);
},
_parseParams: function(owner, params) {
return parseParams(owner, params);
}
});
/* jQuery extend now ignores nulls! */
function extendRemove(target, props) {
$.extend(target, props);
for (var name in props) {
if (props[name] == null) {
target[name] = null;
}
}
return target;
}
/* Attach the iCalendar functionality to a jQuery selection.
@param command (string) the command to run (optional, default 'attach')
@param options (object) the new settings to use for these iCalendar instances
@return (jQuery object) jQuery for chaining further calls */
$.fn.icalendar = function(options) {
var otherArgs = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
if (typeof options == 'string') {
$.icalendar['_' + options + 'ICalendar'].
apply($.icalendar, [this].concat(otherArgs));
}
else {
$.icalendar._attachICalendar(this, options || {});
}
});
};
/* Initialise the iCalendar functionality. */
$.icalendar = new iCalendar(); // singleton instance
/* Override some substitution values for Yahoo.
@param inserts (object) current values (updated)
@param settings (object) current instance settings */
function yahooOverride(inserts, settings) {
var twoDigits = function(value) {
return (value < 10 ? '0' : '') + value;
};
var dur = (settings.end ? (settings.end.getTime() - settings.start.getTime()) / 60000 : 0);
inserts.p = (dur ? twoDigits(Math.floor(dur / 60)) + '' + twoDigits(dur % 60) : ''); // hhmm
if (inserts.r) {
var byDay = (settings.recurrence.by && settings.recurrence.by[0].type == 'day' ?
settings.recurrence.by[0].values.join('').toLowerCase() : '');
var freq = {daily: 'dy', weekly: 'wk', monthly: 'mh', yearly: 'yr'}[settings.recurrence.freq];
inserts.r = (byDay || freq ? twoDigits(settings.recurrence.interval || 1) + (byDay || freq) : '');
}
}
/* Construct an iCalendar with an event object.
@param event (object) the event details
@return (string) the iCalendar definition */
function makeICalendar(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() + '@' +
(window.location.href.replace(/^[^\/]*\/\/([^\/]*)\/.*$/, '$1') || 'localhost') + '\n' +
'DTSTAMP:' + $.icalendar.formatDateTime(new Date()) + '\n' +
(event.url ? limit75('URL:' + event.url) + '\n' : '') +
(event.contact ? limit75('MAILTO:' + event.contact) + '\n' : '') +
limit75('TITLE:' + event.title) + '\n' +
'DTSTART:' + $.icalendar.formatDateTime(event.start) + '\n' +
'DTEND:' + $.icalendar.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';
}
/* Construct an iCalendar recurrence definition.
@param recur (object) the recurrence details
@return (string) the iCalendar definition */
function makeRecurrence(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 ? ',' : '') + $.icalendar.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 ? ',' : '') + $.icalendar.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 ? ',' : '') + $.icalendar.formatDateTime(recur.periods[i][0]) +
'/' + (recur.periods[i][1].constructor != Date ? recur.periods[i][1] :
$.icalendar.formatDateTime(recur.periods[i][1]));
}
}
else {
def = 'RRULE:FREQ=' + (recur.freq || 'daily').toUpperCase() +
(recur.interval ? ';INTERVAL=' + recur.interval : '') +
(recur.until ? ';UNTIL=' + $.icalendar.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;
}
/* Copy the given text to the system clipboard via Flash.
@param text (string) the text to copy
@param url (string) the URL for the Flash clipboard copy module
@return (string) '' if successful, error message if not */
function copyViaFlash(text, url) {
$('#' + FLASH_ID).remove();
try {
$('body').append('<div id="' + FLASH_ID + '"><embed src="' + url +
'" FlashVars="clipboard=' + encodeURIComponent(text) +
'" width="0" height="0" type="application/x-shockwave-flash"></embed></div>');
return '';
}
catch(e) {
return e;
}
}
/* Pattern for folded lines: start with a whitespace character */
var FOLDED = /^\s(.*)$/;
/* Pattern for an individual entry: name:value */
var ENTRY = /^([A-Za-z0-9-]+)((?:;[A-Za-z0-9-]+=(?:"[^"]+"|[^";:,]+)(?:,(?:"[^"]+"|[^";:,]+))*)*):(.*)$/;
/* Pattern for an individual parameter: name=value[,value] */
var PARAM = /;([A-Za-z0-9-]+)=((?:"[^"]+"|[^";:,]+)(?:,(?:"[^"]+"|[^";:,]+))*)/g;
/* Pattern for an individual parameter value: value | "value" */
var PARAM_VALUE = /,?("[^"]+"|[^";:,]+)/g;
/* Pattern for a date only field: yyyymmdd */
var DATEONLY = /^(\d{4})(\d\d)(\d\d)$/;
/* Pattern for a date/time field: yyyymmddThhmmss[Z] */
var DATETIME = /^(\d{4})(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)(Z?)$/;
/* Pattern for a date/time range field: yyyymmddThhmmss[Z]/yyyymmddThhmmss[Z] */
var DATETIME_RANGE = /^(\d{4})(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)(Z?)\/(\d{4})(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)(Z?)$/;
/* Pattern for a timezone offset field: +hhmm */
var TZ_OFFSET = /^([+-])(\d\d)(\d\d)$/;
/* Pattern for a duration: [+-]PnnW or [+-]PnnDTnnHnnMnnS */
var DURATION = /^([+-])?P(\d+W)?(\d+D)?(T)?(\d+H)?(\d+M)?(\d+S)?$/;
/* Reserved names not suitable for attrbiute names. */
var RESERVED_NAMES = ['class'];
/* iCalendar lines are split so the max length is no more than 75.
Split lines start with a whitespace character.
@param content (string) the original iCalendar data
@return (string[]) the restored iCalendar data */
function unfoldLines(content) {
var lines = content.replace(/\r\n/g, '\n').split('\n');
for (var i = lines.length - 1; i > 0; i--) {
var matches = FOLDED.exec(lines[i]);
if (matches) {
lines[i - 1] += matches[1];
lines[i] = '';
}
}
return $.map(lines, function(line, i) { // Remove blank lines
return (line ? line : null);
});
}
/* Parse a group in the file, delimited by BEGIN:xxx and END:xxx.
Recurse if an embedded group encountered.
@param lines (string[]) the iCalendar data
@param index (number) the current position within the data
@param owner (object) the current owner for the new group
@param timezones (object) collection of defined timezones
@return (number) the updated position after processing this group
@throws errors if group structure is incorrect */
function parseGroup(lines, index, owner, timezones) {
if (index >= lines.length || lines[index].indexOf('BEGIN:') != 0) {
throw 'Missing group start';
}
var group = {};
var name = lines[index].substr(6);
addEntry(owner, name.toLowerCase(), group);
index++;
while (index < lines.length && lines[index].indexOf('END:') != 0) {
if (lines[index].indexOf('BEGIN:') == 0) { // Recurse for embedded group
index = parseGroup(lines, index, group, timezones);
}
else {
var entry = parseEntry(lines[index]);
addEntry(group, entry._name, (entry._simple ? entry._value : entry));
}
index++;
}
if (name == 'VTIMEZONE') { // Save timezone offset
var matches = TZ_OFFSET.exec(group.standard.tzoffsetto);
if (matches) {
timezones[group.tzid] = (matches[1] == '-' ? -1 : +1) *
(parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10));
}
}
else {
for (var name2 in group) {
resolveTimezones(group[name2], timezones);
}
}
if (lines[index] != 'END:' + name) {
throw 'Missing group end ' + name;
}
return index;
}
/* Resolve timezone references for dates.
@param value (any) the current value to check - updated if appropriate
@param timezones (object) collection of defined timezones */
function resolveTimezones(value, timezones) {
if (!value) {
return;
}
if (value.tzid && value._value) {
var offset = timezones[value.tzid];
var offsetDate = function(date, tzid) {
date.setMinutes(date.getMinutes() - offset);
date._type = tzid;
};
if (isArray(value._value)) {
for (var i = 0; i < value._value.length; i++) {
offsetDate(value._value[i], value.tzid);
}
}
else if (value._value.start && value._value.end) {
offsetDate(value._value.start, value.tzid);
offsetDate(value._value.end, value.tzid);
}
else {
offsetDate(value._value, value.tzid);
}
}
else if (isArray(value)) {
for (var i = 0; i < value.length; i++) {
resolveTimezones(value[i], timezones);
}
}
}
/* Add a new entry to an object, making multiple entries into an array.
@param owner (object) the owning object for the new entry
@param name (string) the name of the new entry
@param value (string or object) the new entry value */
function addEntry(owner, name, value) {
if (typeof value == 'string') {
value = value.replace(/\\n/g, '\n');
}
if ($.inArray(name, RESERVED_NAMES) > -1) {
name += '_';
}
if (owner[name]) { // Turn multiple values into an array
if (!isArray(owner[name]) || owner['_' + name + 'IsArray']) {
owner[name] = [owner[name]];
}
owner[name][owner[name].length] = value;
if (owner['_' + name + 'IsArray']) {
owner['_' + name + 'IsArray'] = undefined;
}
}
else {
owner[name] = value;
if (isArray(value)) {
owner['_' + name + 'IsArray'] = true;
}
}
}
/* Parse an individual entry.
The format is: <name>[;<param>=<pvalue>]...:<value>
@param line (string) the line to parse
@return (object) the parsed entry with _name and _value
attributes, _simple to indicate whether or not
other parameters, and other parameters as necessary */
function parseEntry(line) {
var entry = {};
var matches = ENTRY.exec(line);
if (!matches) {
throw 'Missing entry name: ' + line;
}
entry._name = matches[1].toLowerCase();
entry._value = checkDate(matches[3]);
entry._simple = true;
parseParams(entry, matches[2]);
return entry;
}
/* Parse parameters for an individual entry.
The format is: <param>=<pvalue>[;...]
@param owner (object) the owning object for the parameters,
updated with parameters as attributes, and
_simple to indicate whether or not other parameters
@param params (string or string[]) the parameters to parse */
function parseParams(owner, params) {
var param = PARAM.exec(params);
while (param) {
var values = [];
var value = PARAM_VALUE.exec(param[2]);
while (value) {
values.push(checkDate(value[1].replace(/^"(.*)"$/, '$1')));
value = PARAM_VALUE.exec(param[2]);
}
owner[param[1].toLowerCase()] = (values.length > 1 ? values : values[0]);
owner._simple = false;
param = PARAM.exec(params);
}
}
/* Convert a value into a Date object or array of Date objects if appropriate.
@param value (string) the value to check
@return (string or Date) the converted value (if appropriate) */
function checkDate(value) {
var matches = DATETIME.exec(value);
if (matches) {
return makeDate(matches);
}
matches = DATETIME_RANGE.exec(value);
if (matches) {
return {start: makeDate(matches), end: makeDate(matches.slice(7))};
}
matches = DATEONLY.exec(value);
if (matches) {
return makeDate(matches.concat([0, 0, 0, '']));
}
return value;
}
/* Create a date value from matches on a string.
@param matches (string[]) the component parts of the date
@return (Date) the corresponding date */
function makeDate(matches) {
var date = new Date(matches[1], matches[2] - 1, matches[3],
matches[4], matches[5], matches[6]);
date._type = (matches[7] ? 'UTC' : 'float');
return utcDate(date);
}
/* Standardise a date to UTC.
@param date (Date) the date to standardise
@return (Date) the equivalent UTC date */
function utcDate(date) {
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return date;
}
/* Calculate the week of the year for a given date
according to the ISO 8601 definition.
@param date (Date) the date to calculate the week for
@param weekStart (number) the day on which a week starts:
0 = Sun, 1 = Mon, ... (optional, defaults to 1)
@return (number) the week for these parameters (1-53) */
function getWeekOfYear(date, weekStart) {
weekStart = (weekStart || weekStart == 0 ? weekStart : 1);
var checkDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(),
(date.getTimezoneOffset() / -60));
var firstDay = new Date(checkDate.getFullYear(), 1 - 1, 4); // First week always contains 4 Jan
var firstDOW = firstDay.getDay(); // Day of week: Sun = 0, Mon = 1, ...
firstDay.setDate(4 + weekStart - firstDOW - (weekStart > firstDOW ? 7 : 0)); // Preceding week start
if (checkDate < firstDay) { // Adjust first three days in year if necessary
checkDate.setDate(checkDate.getDate() - 3); // Generate for previous year
return getWeekOfYear(checkDate, weekStart);
} else if (checkDate > new Date(checkDate.getFullYear(), 12 - 1, 28)) { // Check last three days in year
var firstDay2 = new Date(checkDate.getFullYear() + 1, 1 - 1, 4); // Find first week in next year
firstDOW = firstDay2.getDay();
firstDay2.setDate(4 + weekStart - firstDOW - (weekStart > firstDOW ? 7 : 0));
if (checkDate >= firstDay2) { // Adjust if necessary
return 1;
}
}
return Math.floor(((checkDate - firstDay) /
(FREQ_SETTINGS[DY].factor * 1000)) / 7) + 1; // Weeks to given date
}
/* 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 */
function isArray(a) {
return (a && a.constructor == Array);
}
})(jQuery);