Skip to content

Commit

Permalink
rewrite to gcal plugin; leverages new event-source features
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Feb 11, 2011
1 parent 6b63916 commit 075df1d
Showing 1 changed file with 93 additions and 61 deletions.
154 changes: 93 additions & 61 deletions src/gcal/gcal.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,102 @@
/*
* 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($) {

$.fullCalendar.gcalFeed = function(feedUrl, options) {
feedUrl = feedUrl.replace(/\/basic$/, '/full');
options = options || {};
return function(start, end, callback) {
var params = {
'start-min': $.fullCalendar.formatDate(start, 'u'),
'start-max': $.fullCalendar.formatDate(end, 'u'),
'singleevents': true,
'max-results': 9999
};
var ctz = options.currentTimezone;
if (ctz) {
params.ctz = ctz = ctz.replace(' ', '_');

var fc = $.fullCalendar;
var formatDate = fc.formatDate;
var parseISO8601 = fc.parseISO8601;
var addDays = fc.addDays;
var applyAll = fc.applyAll;


fc.sourceNormalizers.push(function(sourceOptions) {
if (sourceOptions.dataType == 'gcal' ||
sourceOptions.dataType === undefined &&
(sourceOptions.url || '').indexOf('http://www.google.com/calendar/feeds/') == 0) {
sourceOptions.dataType = 'gcal';
if (sourceOptions.editable === undefined) {
sourceOptions.editable = false;
}
$.getJSON(feedUrl + "?alt=json-in-script&callback=?", params, function(data) {
var events = [];
if (data.feed.entry) {
$.each(data.feed.entry, function(i, entry) {
var startStr = entry['gd$when'][0]['startTime'],
start = $.fullCalendar.parseISO8601(startStr, true),
end = $.fullCalendar.parseISO8601(entry['gd$when'][0]['endTime'], true),
allDay = startStr.indexOf('T') == -1,
url;
$.each(entry.link, function() {
if (this.type == 'text/html') {
url = this.href;
if (ctz) {
url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz;
}
}
});


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,
'max-results': 9999
});

var ctz = sourceOptions.currentTimezone;
if (ctz) {
data.ctz = ctz = ctz.replace(' ', '_');
}

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 = [];
if (data.feed.entry) {
$.each(data.feed.entry, function(i, entry) {
var startStr = entry['gd$when'][0]['startTime'];
var start = parseISO8601(startStr, true);
var end = parseISO8601(entry['gd$when'][0]['endTime'], true);
var allDay = startStr.indexOf('T') == -1;
var url;
$.each(entry.link, function(i, link) {
if (link.type == 'text/html') {
url = link.href;
if (ctz) {
url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz;
}
});
if (allDay) {
$.fullCalendar.addDays(end, -1); // make inclusive
}
events.push({
id: entry['gCal$uid']['value'],
title: entry['title']['$t'],
url: url,
start: start,
end: end,
allDay: allDay,
location: entry['gd$where'][0]['valueString'],
description: entry['content']['$t'],
className: options.className,
editable: options.editable || false
});
});
}
callback(events);
});
if (allDay) {
addDays(end, -1); // make inclusive
}
events.push({
id: entry['gCal$uid']['value'],
title: entry['title']['$t'],
url: url,
start: start,
end: end,
allDay: allDay,
location: entry['gd$where'][0]['valueString'],
description: entry['content']['$t']
});
});
}
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);

0 comments on commit 075df1d

Please sign in to comment.