Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
/*!
2
 * FullCalendar v2.2.7 Google Calendar Plugin
3
 * Docs & License: http://arshaw.com/fullcalendar/
4
 * (c) 2013 Adam Shaw
5
 */
6
 
7
(function(factory) {
8
	if (typeof define === 'function' && define.amd) {
9
		define([ 'jquery' ], factory);
10
	}
11
	else {
12
		factory(jQuery);
13
	}
14
})(function($) {
15
 
16
 
17
var API_BASE = 'https://www.googleapis.com/calendar/v3/calendars';
18
var fc = $.fullCalendar;
19
var applyAll = fc.applyAll;
20
 
21
 
22
fc.sourceNormalizers.push(function(sourceOptions) {
23
	var googleCalendarId = sourceOptions.googleCalendarId;
24
	var url = sourceOptions.url;
25
	var match;
26
 
27
	// if the Google Calendar ID hasn't been explicitly defined
28
	if (!googleCalendarId && url) {
29
 
30
		// detect if the ID was specified as a single string.
31
		// will match calendars like "asdf1234@calendar.google.com" in addition to person email calendars.
32
		if ((match = /^[^\/]+@([^\/\.]+\.)*(google|googlemail|gmail)\.com$/.test(url))) {
33
			googleCalendarId = url;
34
		}
35
		// try to scrape it out of a V1 or V3 API feed URL
36
		else if (
37
			(match = /^https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/([^\/]*)/.exec(url)) ||
38
			(match = /^https?:\/\/www.google.com\/calendar\/feeds\/([^\/]*)/.exec(url))
39
		) {
40
			googleCalendarId = decodeURIComponent(match[1]);
41
		}
42
 
43
		if (googleCalendarId) {
44
			sourceOptions.googleCalendarId = googleCalendarId;
45
		}
46
	}
47
 
48
 
49
	if (googleCalendarId) { // is this a Google Calendar?
50
 
51
		// make each Google Calendar source uneditable by default
52
		if (sourceOptions.editable == null) {
53
			sourceOptions.editable = false;
54
		}
55
 
56
		// We want removeEventSource to work, but it won't know about the googleCalendarId primitive.
57
		// Shoehorn it into the url, which will function as the unique primitive. Won't cause side effects.
58
		// This hack is obsolete since 2.2.3, but keep it so this plugin file is compatible with old versions.
59
		sourceOptions.url = googleCalendarId;
60
	}
61
});
62
 
63
 
64
fc.sourceFetchers.push(function(sourceOptions, start, end, timezone) {
65
	if (sourceOptions.googleCalendarId) {
66
		return transformOptions(sourceOptions, start, end, timezone, this); // `this` is the calendar
67
	}
68
});
69
 
70
 
71
function transformOptions(sourceOptions, start, end, timezone, calendar) {
72
	var url = API_BASE + '/' + encodeURIComponent(sourceOptions.googleCalendarId) + '/events?callback=?'; // jsonp
73
	var apiKey = sourceOptions.googleCalendarApiKey || calendar.options.googleCalendarApiKey;
74
	var success = sourceOptions.success;
75
	var data;
76
	var timezoneArg; // populated when a specific timezone. escaped to Google's liking
77
 
78
	function reportError(message, apiErrorObjs) {
79
		var errorObjs = apiErrorObjs || [ { message: message } ]; // to be passed into error handlers
80
		var consoleObj = window.console;
81
		var consoleWarnFunc = consoleObj ? (consoleObj.warn || consoleObj.log) : null;
82
 
83
		// call error handlers
84
		(sourceOptions.googleCalendarError || $.noop).apply(calendar, errorObjs);
85
		(calendar.options.googleCalendarError || $.noop).apply(calendar, errorObjs);
86
 
87
		// print error to debug console
88
		if (consoleWarnFunc) {
89
			consoleWarnFunc.apply(consoleObj, [ message ].concat(apiErrorObjs || []));
90
		}
91
	}
92
 
93
	if (!apiKey) {
94
		reportError("Specify a googleCalendarApiKey. See http://fullcalendar.io/docs/google_calendar/");
95
		return {}; // an empty source to use instead. won't fetch anything.
96
	}
97
 
98
	// The API expects an ISO8601 datetime with a time and timezone part.
99
	// Since the calendar's timezone offset isn't always known, request the date in UTC and pad it by a day on each
100
	// side, guaranteeing we will receive all events in the desired range, albeit a superset.
101
	// .utc() will set a zone and give it a 00:00:00 time.
102
	if (!start.hasZone()) {
103
		start = start.clone().utc().add(-1, 'day');
104
	}
105
	if (!end.hasZone()) {
106
		end = end.clone().utc().add(1, 'day');
107
	}
108
 
109
	// when sending timezone names to Google, only accepts underscores, not spaces
110
	if (timezone && timezone != 'local') {
111
		timezoneArg = timezone.replace(' ', '_');
112
	}
113
 
114
	data = $.extend({}, sourceOptions.data || {}, {
115
		key: apiKey,
116
		timeMin: start.format(),
117
		timeMax: end.format(),
118
		timeZone: timezoneArg,
119
		singleEvents: true,
120
		maxResults: 9999
121
	});
122
 
123
	return $.extend({}, sourceOptions, {
124
		googleCalendarId: null, // prevents source-normalizing from happening again
125
		url: url,
126
		data: data,
127
		startParam: false, // `false` omits this parameter. we already included it above
128
		endParam: false, // same
129
		timezoneParam: false, // same
130
		success: function(data) {
131
			var events = [];
132
			var successArgs;
133
			var successRes;
134
 
135
			if (data.error) {
136
				reportError('Google Calendar API: ' + data.error.message, data.error.errors);
137
			}
138
			else if (data.items) {
139
				$.each(data.items, function(i, entry) {
140
					var url = entry.htmlLink;
141
 
142
					// make the URLs for each event show times in the correct timezone
143
					if (timezoneArg) {
144
						url = injectQsComponent(url, 'ctz=' + timezoneArg);
145
					}
146
 
147
					events.push({
148
						id: entry.id,
149
						title: entry.summary,
150
						start: entry.start.dateTime || entry.start.date, // try timed. will fall back to all-day
151
						end: entry.end.dateTime || entry.end.date, // same
152
						url: url,
153
						location: entry.location,
154
						description: entry.description
155
					});
156
				});
157
 
158
				// call the success handler(s) and allow it to return a new events array
159
				successArgs = [ events ].concat(Array.prototype.slice.call(arguments, 1)); // forward other jq args
160
				successRes = applyAll(success, this, successArgs);
161
				if ($.isArray(successRes)) {
162
					return successRes;
163
				}
164
			}
165
 
166
			return events;
167
		}
168
	});
169
}
170
 
171
 
172
// Injects a string like "arg=value" into the querystring of a URL
173
function injectQsComponent(url, component) {
174
	// inject it after the querystring but before the fragment
175
	return url.replace(/(\?.*?)?(#|$)/, function(whole, qs, hash) {
176
		return (qs ? qs + '&' : '?') + component + hash;
177
	});
178
}
179
 
180
 
181
});