Просмотр исходного кода

fixes for Google Calendar V3 API migration

Adam Shaw 11 лет назад
Родитель
Сommit
facadd0341
3 измененных файлов с 76 добавлено и 7 удалено
  1. 8 0
      changelog.md
  2. 18 7
      src/gcal/gcal.js
  3. 50 0
      tests/automated/events-gcal.js

+ 8 - 0
changelog.md

@@ -1,4 +1,12 @@
 
+v2.2.2 (2014-11-19)
+-------------------
+
+- Fixes to Google Calendar API V3 code
+	- wouldn't recognize a lone-string Google Calendar ID if periods before the @ symbol
+	- removeEventSource wouldn't work when given a Google Calendar ID
+
+
 v2.2.1 (2014-11-19)
 -------------------
 

+ 18 - 7
src/gcal/gcal.js

@@ -20,30 +20,41 @@ var applyAll = fc.applyAll;
 
 
 fc.sourceNormalizers.push(function(sourceOptions) {
+	var googleCalendarId = sourceOptions.googleCalendarId;
 	var url = sourceOptions.url;
 	var match;
 
 	// if the Google Calendar ID hasn't been explicitly defined
-	if (!sourceOptions.googleCalendarId && url) {
+	if (!googleCalendarId && url) {
 
 		// detect if the ID was specified as a single string
-		if ((match = /^[\w-]+@[\w-\.]+\.calendar\.google\.com$/.test(url))) {
-			sourceOptions.googleCalendarId = url;
+		if ((match = /^[^\/]+@([^\/]+\.)?calendar\.google\.com$/.test(url))) {
+			googleCalendarId = url;
 		}
 		// try to scrape it out of a V1 or V3 API feed URL
 		else if (
 			(match = /^https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/([^\/]*)/.exec(url)) ||
 			(match = /^https?:\/\/www.google.com\/calendar\/feeds\/([^\/]*)/.exec(url))
 		) {
-			sourceOptions.googleCalendarId = decodeURIComponent(match[1]);
+			googleCalendarId = decodeURIComponent(match[1]);
+		}
+
+		if (googleCalendarId) {
+			sourceOptions.googleCalendarId = googleCalendarId;
 		}
 	}
 
-	// make each google calendar source uneditable by default
-	if (sourceOptions.googleCalendarId) {
+
+	if (googleCalendarId) { // is this a Google Calendar?
+
+		// make each Google Calendar source uneditable by default
 		if (sourceOptions.editable == null) {
 			sourceOptions.editable = false;
 		}
+
+		// We want removeEventSource to work, but it won't know about the googleCalendarId primitive.
+		// Shoehorn it into the url, which will function as the unique primitive. Won't cause side effects.
+		sourceOptions.url = googleCalendarId;
 	}
 });
 
@@ -56,7 +67,7 @@ fc.sourceFetchers.push(function(sourceOptions, start, end, timezone) {
 
 
 function transformOptions(sourceOptions, start, end, timezone, calendar) {
-	var url = API_BASE + '/' + encodeURI(sourceOptions.googleCalendarId) + '/events?callback=?'; // jsonp
+	var url = API_BASE + '/' + encodeURIComponent(sourceOptions.googleCalendarId) + '/events?callback=?'; // jsonp
 	var apiKey = sourceOptions.googleCalendarApiKey || calendar.options.googleCalendarApiKey;
 	var success = sourceOptions.success;
 	var data;

+ 50 - 0
tests/automated/events-gcal.js

@@ -183,6 +183,28 @@ describe('Google Calendar plugin', function() {
 		$('#cal').fullCalendar(options);
 	});
 
+	it('detects a gcal when `events` is the actual calendar ID, with complicated characters (1)', function(done) {
+		options.googleCalendarApiKey = API_KEY;
+		options.events = '[email protected]';
+		options.eventAfterAllRender = function() {
+			expect(currentWarnArgs.length).toBe(2);
+			expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
+			done();
+		};
+		$('#cal').fullCalendar(options);
+	});
+
+	it('detects a gcal when `events` is the actual calendar ID, with complicated characters (2)', function(done) {
+		options.googleCalendarApiKey = API_KEY;
+		options.events = '[email protected]';
+		options.eventAfterAllRender = function() {
+			expect(currentWarnArgs.length).toBe(2);
+			expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
+			done();
+		};
+		$('#cal').fullCalendar(options);
+	});
+
 	it('works with requesting an HTTP V1 API feed URL', function(done) {
 		options.googleCalendarApiKey = API_KEY;
 		options.events = 'http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic';
@@ -217,4 +239,32 @@ describe('Google Calendar plugin', function() {
 		$('#cal').fullCalendar(options);
 	});
 
+	describe('removeEventSource', function() {
+		it('works when specifying only the Google Calendar ID', function(done) {
+			var CALENDAR_ID = '[email protected]';
+			var called = false;
+
+			options.googleCalendarApiKey = API_KEY;
+			options.eventSources = [ { googleCalendarId: CALENDAR_ID } ];
+			options.eventAfterAllRender = function() {
+				var events;
+
+				if (called) { return; } // only the first time
+				called = true;
+
+				events = $('#cal').fullCalendar('clientEvents');
+				expect(events.length).toBe(4); // 4 holidays in November 2014
+
+				setTimeout(function() {
+					$('#cal').fullCalendar('removeEventSource', CALENDAR_ID);
+					events = $('#cal').fullCalendar('clientEvents');
+					expect(events.length).toBe(0);
+					done();
+				}, 0);
+			};
+
+			$('#cal').fullCalendar(options);
+		});
+	});
+
 });