-
Notifications
You must be signed in to change notification settings - Fork 35
/
jquery.addtocal.js
292 lines (261 loc) · 10.3 KB
/
jquery.addtocal.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
/*
* jQuery.addtocal v0.1.2
* http://github.com/tardate/jquery.addtocalendar
*
* Copyright (c) 2010 Paul GALLAGHER http://tardate.com
* Dual licensed under the MIT or GPL Version 2 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($) {
$.widget("tardate.AddToCal",
{
options: {
/*
* calendars is a collection of all the supported calendars and the method for formating
* the calendar link in each case. If you want to use a calendar system not supported here, you can
* extend or modify this array as required in widget setup.
*/
calendars : [
{value: 1,
label:"Add to Google Calendar",
enabled : function(addtocal) { return true; },
formatlink : function(eventDetails) {
return "http://www.google.com/calendar/event?action=TEMPLATE&trp=false" +
"&text=" + eventDetails.title +
"&dates=" + eventDetails.start +
"/" + eventDetails.end +
"&location=" + eventDetails.location +
"&details=" + eventDetails.details +
"&sprop=" + eventDetails.url;
} },
{value: 2, label:"Add to Live Calendar",
enabled : function(addtocal) { return true; },
formatlink : function(eventDetails) {
return "http://calendar.live.com/calendar/calendar.aspx?rru=addevent" +
"&dtstart=" + eventDetails.start +
"&dtend=" + eventDetails.end +
"&summary=" + eventDetails.title +
"&location=" + eventDetails.location;
} },
{value: 3, label:"Add to Yahoo! Calendar",
enabled : function(addtocal) { return true; },
formatlink : function(eventDetails) {
var minsDuration = ( Date.parse(eventDetails.end) - Date.parse(eventDetails.start) ) / 60 / 1000;
var durationString = (minsDuration / 60).toPaddedString(2) + (minsDuration%60).toPaddedString(2);
return "http://calendar.yahoo.com/?v=60" +
"&DUR=" + durationString +
"&TITLE=" + eventDetails.title +
"&ST=" + eventDetails.start +
"&in_loc=" + eventDetails.location +
"&DESC=" + eventDetails.details +
"&URL=" + eventDetails.url;
} },
{value: 4, label:"Add to 30boxes",
enabled : function(addtocal) { return addtocal.options.icalEnabled; },
formatlink : function(eventDetails) {
return ( eventDetails.webcalurl ?
"http://30boxes.com/add.php?webcal=" + encodeURIComponent( eventDetails.webcalurl ) : null );
} },
{value: 5, label:"iCal",
enabled : function(addtocal) { return addtocal.options.icalEnabled; },
formatlink : function(eventDetails) {
return (eventDetails.icalurl ? eventDetails.icalurl : null);
} },
{value: 6, label:"vCalendar",
enabled : function(addtocal) { return addtocal.options.vcalEnabled; },
formatlink : function(eventDetails) {
return ( eventDetails.vcalurl ? eventDetails.vcalurl : null );
} }
],
/* icalEnabled: set if iCal links are to be supported (requires you to provide an iCal format resource) */
icalEnabled: true,
/* vcalEnabled: set if vCalendar links are to be supported (requires you to provide an vCalendar format resource) */
vcalEnabled: true,
/* getEventDetails is the most critical function to provide.
* It is called when a user selects a calendar to add an event to.
* The element parameter is the jQuery object for the event invoked.
* You must return an object packed with the relevant event details.
* How you determine the event attributes will depend on your page.
* The example below illustrates how to handle two formats of event markup.
*/
getEventDetails: function( element ) {
return {
webcalurl: 'webcal://site.ics',
icalurl: 'http://site.ics',
vcalurl: 'http://site.vcs',
start: new Date(),
end: new Date(),
title: null, details: null,
location: null, url: null};
},
/*
* sanitizeEventDetails cleans up and normalises the event details provided by getEventDetails
*/
sanitizeEventDetails: function( eventDetails ) {
eventDetails.title = ( eventDetails.title ? encodeURIComponent( eventDetails.title ) : '' );
eventDetails.start = ( typeof eventDetails.start.toRFC3339UTCString == 'function' ?
eventDetails.start.toRFC3339UTCString(true,true) : eventDetails.start );
eventDetails.end = ( typeof eventDetails.end.toRFC3339UTCString == 'function' ?
eventDetails.end.toRFC3339UTCString(true,true) : eventDetails.end );
eventDetails.location = ( eventDetails.location ? encodeURIComponent( eventDetails.location ) : '' );
eventDetails.details = ( eventDetails.details ? encodeURIComponent( eventDetails.details ) : '' );
// avoid 414 error due to overlong url
var MAX_DETAILS_LENGTH = 1550;
if (eventDetails.details.length > MAX_DETAILS_LENGTH) {
eventDetails.details = eventDetails.details.substr(0, MAX_DETAILS_LENGTH);
eventDetails.details.replace(/%[^0-9]*$/, "");
eventDetails.details += "...";
}
eventDetails.url = ( eventDetails.url ? encodeURIComponent( eventDetails.url ) : '' );
return eventDetails;
},
/* records the currently selected calendar service */
selectedCalendarTarget: null,
/* positioning of the addtocal widget */
appendTo: "body",
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
/* main method called on selection of calendar service */
select: function(event, ui) {
var eventDetails = ui.sanitizeEventDetails( ui.getEventDetails($(this)) );
var calendar_provider = $.grep(ui.calendars, function(element, index){
return (element.value == ui.selectedCalendarTarget);
});
var link = calendar_provider[0].formatlink(eventDetails);
if(link) window.open(link);
}
},
source:[],
_create: function() {
var self = this,
doc = this.element[ 0 ].ownerDocument;
this.element
.addClass( "ui-addtocal" )
.bind( "click.addtocal", function( event ) {
event.preventDefault();
event.stopPropagation();
self.toggleMenu();
});
this._initSource();
this.menu = $( "<ul></ul>" )
.addClass( "ui-addtocal" )
.appendTo( $( this.options.appendTo || "body", doc )[0] )
.menu({
selected: function( event, ui ) {
var item = ui.item.data( "item.addtocal" ),
previous = self.previous;
// only trigger when focus was lost (click on menu)
if ( self.element[0] !== doc.activeElement ) {
self.element.focus();
self.previous = previous;
}
self.options.selectedCalendarTarget = item.value;
self._trigger( "select", event, self.options );
self.close( event );
self.selectedItem = item;
}
})
.zIndex( this.element.zIndex() + 1 )
.css({ top: 0, left: 0 })
.hide()
.data( "menu" );
if ( $.fn.bgiframe ) {
this.menu.element.bgiframe();
}
//Close the popup if click elsewhere in the window
$(document).bind("click", function(event, ui) { self.close( event ); });
},
destroy: function() {
this.element
.removeClass( "ui-addtocal" );
this.menu.element.remove();
$.Widget.prototype.destroy.call( this );
},
_setOption: function( key, value ) {
$.Widget.prototype._setOption.apply( this, arguments );
if ( key === "appendTo" ) {
this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] )
}
},
_initSource: function() {
var self = this;
self.source=[];
$.each( this.options.calendars, function(index, value) {
if(value.enabled(self)) self.source.push( {value: value.value, label: value.label } );
});
},
toggleMenu: function( event ) {
var content = this.source;
if ( content.length && ! ( this.menu.element.is(":visible") ) ) {
$('.ui-addtocal').AddToCal( 'close' );
content = this._normalize( content );
this._suggest( content );
this._trigger( "open" );
} else {
this.close();
}
},
close: function( event ) {
clearTimeout( this.closing );
if ( this.menu.element.is(":visible") ) {
this._trigger( "close", event );
this.menu.element.hide();
this.menu.deactivate();
}
},
_normalize: function( items ) {
// assume all items have the right format when the first item is complete
if ( items.length && items[0].label && items[0].value ) {
return items;
}
return $.map( items, function(item) {
if ( typeof item === "string" ) {
return {
label: item,
value: item
};
}
return $.extend({
label: item.label || item.value,
value: item.value || item.label
}, item );
});
},
_suggest: function( items ) {
var ul = this.menu.element
.empty()
.zIndex( this.element.zIndex() + 1 ),
menuWidth,
textWidth;
this._renderMenu( ul, items );
// TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
this.menu.deactivate();
this.menu.refresh();
this.menu.element.show().position( $.extend({
of: this.element
}, this.options.position ));
menuWidth = ul.width( "" ).outerWidth();
textWidth = this.element.outerWidth();
ul.outerWidth( Math.max( menuWidth, textWidth ) );
},
_renderMenu: function( ul, items ) {
var self = this;
$.each( items, function( index, item ) {
self._renderItem( ul, item );
});
},
_renderItem: function( ul, item) {
return $( "<li></li>" )
.data( "item.addtocal", item )
.append( $( "<a></a>" ).text( item.label ) )
.appendTo( ul );
},
widget: function() {
return this.menu.element;
}
});
}(jQuery));