-
Notifications
You must be signed in to change notification settings - Fork 15
/
jquery.microdata.vevent.js
73 lines (69 loc) · 2.43 KB
/
jquery.microdata.vevent.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
/* -*- mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
'use strict';
// http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html#conversion-to-icalendar
jQuery.microdata.ical = function(selector) {
var $ = jQuery;
var veventURI = 'http://microformats.org/profile/hcalendar#vevent';
var $events = selector ?
$(selector).filter(function() {
var $this = $(this);
return $this.itemScope() && $this.itemType().contains(veventURI);
}) :
$(document).items(veventURI);
if ($events.length == 0)
return;
var output = '';
// http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html#add-an-icalendar-line
function addLine(type, value, annotation) {
var line = '';
line += type.toUpperCase();
if (annotation)
line += ';'+annotation;
line += ':';
line += value.replace(/([\\,;])/g, '\\$1').replace(/\r\n|\r|\n/g, '\\n');
var maxLen = 75;
while (line.length > maxLen) {
output += line.substr(0, maxLen);
line = line.substr(maxLen);
output += '\r\n ';
maxLen = 74;
}
output += line+'\r\n';
}
addLine('BEGIN', 'VCALENDAR');
addLine('PRODID', 'jQuery Microdata');
addLine('VERSION', '2.0');
$events.each(function() {
var $event = $(this);
addLine('BEGIN', 'VEVENT');
function zp(n) { return (n < 10 ? '0' : '') + n; }
var stamp = new Date();
var stampString = '' + stamp.getUTCFullYear() + zp(stamp.getUTCMonth() + 1) +
zp(stamp.getUTCDate()) + 'T' + zp(stamp.getUTCHours()) +
zp(stamp.getUTCMinutes()) + zp(stamp.getUTCSeconds()) + 'Z';
addLine('DTSTAMP', stampString, 'VALUE=DATE-TIME');
if ($event.itemId())
addLine('UID', $event.itemId());
$event.properties().each(function() {
var $elem = $(this);
if ($elem.itemScope())
return;
$.each($elem.itemProp(), function() {
var name = this;
if ($elem.get(0).tagName.toUpperCase() == 'TIME') {
var value = $elem.itemValue().replace(/[-:]/g, '');
if ($.microdata.isValidDateString($elem.itemValue())) {
addLine(name, value, 'VALUE=DATE');
} else if ($.microdata.isValidGlobalDateAndTimeString($elem.itemValue())) {
addLine(name, value, 'VALUE=DATE-TIME');
}
} else {
addLine(name, $elem.itemValue());
}
});
});
addLine('END', 'VEVENT');
});
addLine('END', 'VCALENDAR');
return output;
};