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

generic internal event source matching util. use to solve removeEventSource multiple bug

Adam Shaw 9 лет назад
Родитель
Сommit
e4e81d983c
3 измененных файлов с 97 добавлено и 26 удалено
  1. 52 25
      src/EventManager.js
  2. 1 1
      tests/automated/getEventSourceById.js
  3. 44 0
      tests/automated/removeEventSource.js

+ 52 - 25
src/EventManager.js

@@ -282,18 +282,6 @@ function EventManager(options) { // assumed to be a calendar
 	-----------------------------------------------------------------------------*/
 	-----------------------------------------------------------------------------*/
 
 
 
 
-	function getEventSources() {
-		return sources.slice(1); // returns a shallow copy of sources with stickySource removed
-	}
-
-
-	function getEventSourceById(id) {
-		return $.grep(sources, function(src) {
-			return src.id && src.id === id;
-		})[0];
-	}
-
-
 	function addEventSource(sourceInput) {
 	function addEventSource(sourceInput) {
 		var source = buildEventSource(sourceInput);
 		var source = buildEventSource(sourceInput);
 		if (source) {
 		if (source) {
@@ -348,30 +336,69 @@ function EventManager(options) { // assumed to be a calendar
 	}
 	}
 
 
 
 
-	function removeEventSource(targetSource) {
+	function removeEventSource(matchInput) {
+		var targetSources = getEventSourcesByMatch(matchInput); // allow for multiple matches
+		var i;
 
 
 		// cancel pending requests
 		// cancel pending requests
-		for (var i = 0; i < sources.length; i++) {
-			if (isSourcesEqual(sources[i], targetSource)) {
-				rejectEventSource(sources[i]);
-			}
+		for (i = 0; i < targetSources.length; i++) {
+			rejectEventSource(targetSources[i]);
 		}
 		}
 
 
-		// remove from source list
-		sources = $.grep(sources, function(src) {
-			return !isSourcesEqual(src, targetSource);
+		// remove from persisted source list
+		sources = $.grep(sources, function(source) {
+			for (i = 0; i < targetSources.length; i++) {
+				if (source === targetSources[i]) {
+					return false; // exclude
+				}
+			}
+			return true; // include
 		});
 		});
 
 
-		// remove all client events from that source
-		cache = $.grep(cache, function(e) {
-			return !isSourcesEqual(e.source, targetSource);
-		});
+		cache = excludeEventsBySources(cache, targetSources);
 
 
 		reportEvents(cache);
 		reportEvents(cache);
 	}
 	}
 
 
 
 
-	function isSourcesEqual(source1, source2) {
+	function getEventSources() {
+		return sources.slice(1); // returns a shallow copy of sources with stickySource removed
+	}
+
+
+	function getEventSourceById(id) {
+		return $.grep(sources, function(source) {
+			return source.id && source.id === id;
+		})[0];
+	}
+
+
+	// matchInput can either by a real event source object, an ID, or the function/URL for the source.
+	// returns an array of matching source objects.
+	function getEventSourcesByMatch(matchInput) {
+		var i, source;
+
+		// given an proper event source object
+		for (i = 0; i < sources.length; i++) {
+			source = sources[i];
+			if (source === matchInput) {
+				return [ source ];
+			}
+		}
+
+		// an ID match
+		source = getEventSourceById(matchInput);
+		if (source) {
+			return [ source ];
+		}
+
+		return $.grep(sources, function(source) {
+			return isSourcesEquivalent(matchInput, source);
+		});
+	}
+
+
+	function isSourcesEquivalent(source1, source2) {
 		return source1 && source2 && getSourcePrimitive(source1) == getSourcePrimitive(source2);
 		return source1 && source2 && getSourcePrimitive(source1) == getSourcePrimitive(source2);
 	}
 	}
 
 

+ 1 - 1
tests/automated/getEventSourceById.js

@@ -31,7 +31,7 @@ describe('getEventSource', function() {
 		};
 		};
 	});
 	});
 
 
-	iit('retreives the queried event source', function(done) {
+	it('retreives the queried event source', function(done) {
 		var eventSource1;
 		var eventSource1;
 		var eventSource2;
 		var eventSource2;
 
 

+ 44 - 0
tests/automated/removeEventSource.js

@@ -92,6 +92,50 @@ describe('removeEventSource', function() {
 		$('#cal').fullCalendar('addEventSource', source2);
 		$('#cal').fullCalendar('addEventSource', source2);
 	});
 	});
 
 
+	describe('when multiple sources share the same fetching function', function() {
+		var fetchFunc = function(start, end, timezone, callback) {
+			callback([{
+				title: 'event',
+				start: '2014-08-01T02:00:00'
+			}]);
+		};
+		beforeEach(function() {
+			options.eventSources = [
+				{ events: fetchFunc, className: 'event1', id: 'source1' },
+				{ events: fetchFunc, className: 'event2', id: 'source2' }
+			];
+		});
+
+		describe('when called with the raw function', function() {
+			it('removes events from all matching sources', function() {
+
+				$('#cal').fullCalendar(options);
+				expect($('.fc-event').length).toBe(2);
+				expect($('.event1').length).toBe(1);
+				expect($('.event2').length).toBe(1);
+
+				$('#cal').fullCalendar('removeEventSource', fetchFunc);
+				expect($('.fc-event').length).toBe(0);
+			});
+		});
+
+		describe('when called with proper source object', function() {
+			it('removes events only from the specific source', function() {
+
+				$('#cal').fullCalendar(options);
+				expect($('.fc-event').length).toBe(2);
+				expect($('.event1').length).toBe(1);
+				expect($('.event2').length).toBe(1);
+
+				var source = $('#cal').fullCalendar('getEventSourceById', 'source2');
+				$('#cal').fullCalendar('removeEventSource', source);
+				expect($('.fc-event').length).toBe(1);
+				expect($('.event1').length).toBe(1);
+				expect($('.event2').length).toBe(0);
+			});
+		});
+	});
+
 	function testInput(eventInput) {
 	function testInput(eventInput) {
 
 
 		it('correctly removes events provided via `events` at initialization', function(done) {
 		it('correctly removes events provided via `events` at initialization', function(done) {