rewrite to gcal plugin; leverages new event-source features

This commit is contained in:
Adam Shaw 2011-02-10 22:42:13 -08:00
parent 6b6391635e
commit 075df1d144

View file

@ -1,51 +1,74 @@
/*
* FullCalendar v@VERSION Google Calendar Extension
*
* Copyright (c) 2010 Adam Shaw
* Dual licensed under the MIT and GPL licenses, located in
* MIT-LICENSE.txt and GPL-LICENSE.txt respectively.
*
* Date: @DATE
*
*/
(function($) { (function($) {
$.fullCalendar.gcalFeed = function(feedUrl, options) {
feedUrl = feedUrl.replace(/\/basic$/, '/full'); var fc = $.fullCalendar;
options = options || {}; var formatDate = fc.formatDate;
var parseISO8601 = fc.parseISO8601;
var addDays = fc.addDays;
var applyAll = fc.applyAll;
return function(start, end, callback) {
var params = { fc.sourceNormalizers.push(function(sourceOptions) {
'start-min': $.fullCalendar.formatDate(start, 'u'), if (sourceOptions.dataType == 'gcal' ||
'start-max': $.fullCalendar.formatDate(end, 'u'), sourceOptions.dataType === undefined &&
(sourceOptions.url || '').indexOf('http://www.google.com/calendar/feeds/') == 0) {
sourceOptions.dataType = 'gcal';
if (sourceOptions.editable === undefined) {
sourceOptions.editable = false;
}
}
});
fc.sourceFetchers.push(function(sourceOptions, start, end) {
if (sourceOptions.dataType == 'gcal') {
return transformOptions(sourceOptions, start, end);
}
});
function transformOptions(sourceOptions, start, end) {
var success = sourceOptions.success;
var data = $.extend({}, sourceOptions.data || {}, {
'start-min': formatDate(start, 'u'),
'start-max': formatDate(end, 'u'),
'singleevents': true, 'singleevents': true,
'max-results': 9999 'max-results': 9999
}; });
var ctz = options.currentTimezone;
var ctz = sourceOptions.currentTimezone;
if (ctz) { if (ctz) {
params.ctz = ctz = ctz.replace(' ', '_'); data.ctz = ctz = ctz.replace(' ', '_');
} }
$.getJSON(feedUrl + "?alt=json-in-script&callback=?", params, function(data) {
return $.extend({}, sourceOptions, {
url: sourceOptions.url.replace(/\/basic$/, '/full') + '?alt=json-in-script&callback=?',
dataType: 'jsonp',
data: data,
startParam: false,
endParam: false,
cacheParam: false,
cache: false, // TODO: when we remove cacheParam, we can also remove this
success: function(data) {
var events = []; var events = [];
if (data.feed.entry) { if (data.feed.entry) {
$.each(data.feed.entry, function(i, entry) { $.each(data.feed.entry, function(i, entry) {
var startStr = entry['gd$when'][0]['startTime'], var startStr = entry['gd$when'][0]['startTime'];
start = $.fullCalendar.parseISO8601(startStr, true), var start = parseISO8601(startStr, true);
end = $.fullCalendar.parseISO8601(entry['gd$when'][0]['endTime'], true), var end = parseISO8601(entry['gd$when'][0]['endTime'], true);
allDay = startStr.indexOf('T') == -1, var allDay = startStr.indexOf('T') == -1;
url; var url;
$.each(entry.link, function() { $.each(entry.link, function(i, link) {
if (this.type == 'text/html') { if (link.type == 'text/html') {
url = this.href; url = link.href;
if (ctz) { if (ctz) {
url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz; url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz;
} }
} }
}); });
if (allDay) { if (allDay) {
$.fullCalendar.addDays(end, -1); // make inclusive addDays(end, -1); // make inclusive
} }
events.push({ events.push({
id: entry['gCal$uid']['value'], id: entry['gCal$uid']['value'],
@ -55,16 +78,25 @@
end: end, end: end,
allDay: allDay, allDay: allDay,
location: entry['gd$where'][0]['valueString'], location: entry['gd$where'][0]['valueString'],
description: entry['content']['$t'], description: entry['content']['$t']
className: options.className,
editable: options.editable || false
}); });
}); });
} }
callback(events); var args = [events].concat(Array.prototype.slice.call(arguments, 1));
}); var res = applyAll(success, this, args);
if ($.isArray(res)) {
return res;
} }
return events;
}
});
} }
fc.gcalFeed = function(url, sourceOptions) {
return $.extend({}, sourceOptions, { url: url, dataType: 'gcal' });
};
})(jQuery); })(jQuery);