Adam Shaw před 8 roky
rodič
revize
db054f7813
3 změnil soubory, kde provedl 90 přidání a 62 odebrání
  1. 1 0
      src.json
  2. 11 62
      src/agenda/AgendaView.js
  3. 78 0
      src/models/EventInstanceDataSourceSplitter.js

+ 1 - 0
src.json

@@ -58,6 +58,7 @@
     "models/ComponentFootprint.js",
     "models/EventManager.js",
     "models/EventInstanceDataSource.js",
+    "models/EventInstanceDataSourceSplitter.js",
     "models/EventPeriod.js",
     "models/EventInstanceRepo.js",
     "models/EventInstanceChangeset.js",

+ 11 - 62
src/agenda/AgendaView.js

@@ -14,6 +14,8 @@ var AgendaView = FC.AgendaView = View.extend({
 	dayGridClass: DayGrid, // class used to instantiate the dayGrid. subclasses can override
 	dayGrid: null, // the "all-day" subcomponent. if all-day is turned off, this will be null
 
+	eventDataSourceSplitter: null,
+
 	axisWidth: null, // the width of the time axis running down the side
 
 	headContainerEl: null, // div that hold's the timeGrid's rendered date header
@@ -280,42 +282,23 @@ var AgendaView = FC.AgendaView = View.extend({
 
 
 	setEventDataSourceInChildren: function(eventDataSource) {
-		var allDayEventSource = new EventInstanceDataSource();
-		var timedEventSource = new EventInstanceDataSource();
-
-		function processChangeset(changeset, isInitial) {
-			var split = splitEventChangesetByAllDay(changeset);
+		var splitter = this.eventDataSourceSplitter =
+			new EventInstanceDataSourceSplitter(function(eventInstance) {
+				return [ eventInstance.dateProfile.isAllDay() ];
+			});
 
-			if (isInitial || !split.allDay.isEmpty()) {
-				allDayEventSource.addChangeset(split.allDay);
-			}
+		this.timeGrid.set('eventDataSource', splitter.buildSubSource(false));
 
-			if (isInitial || !split.timed.isEmpty()) {
-				timedEventSource.addChangeset(split.timed);
-			}
-		}
-
-		// initial event instances. simulate a changeset
-		if (eventDataSource.isPopulated) {
-			processChangeset(
-				new EventInstanceChangeset(false, null, eventDataSource.instanceRepo),
-				true // isInitial=true
-			);
-		}
-
-		// listen for further changes
-		this.listenTo(eventDataSource, 'receive', processChangeset);
-
-		// finally, install into the children...
 		if (this.dayGrid) {
-			this.dayGrid.set('eventDataSource', allDayEventSource);
+			this.dayGrid.set('eventDataSource', splitter.buildSubSource(true));
 		}
-		this.timeGrid.set('eventDataSource', timedEventSource);
+
+		splitter.addSource(eventDataSource);
 	},
 
 
 	unsetEventDataSourceInChildren: function(eventDataSource) {
-		this.stopListeningTo(eventDataSource);
+		this.eventDataSourceSplitter.removeSource(eventDataSource);
 
 		View.prototype.unsetEventDataSourceInChildren.apply(this, arguments);
 	},
@@ -444,40 +427,6 @@ var agendaDayGridMethods = {
 };
 
 
-function splitEventChangesetByAllDay(eventChangeset) {
-	var allDayChangeset = new EventInstanceChangeset();
-	var timedChangeset = new EventInstanceChangeset();
-
-	if (eventChangeset.isClear) {
-		allDayChangeset.isClear = true;
-		timedChangeset.isClear = true;
-	}
-
-	// reference dateProfile.isAllDay() instead of the eventDef
-	// because eventDef might have updated
-
-	eventChangeset.removalsRepo.iterEventInstances(function(instance) {
-		if (instance.dateProfile.isAllDay()) {
-			allDayChangeset.removalsRepo.addEventInstance(instance);
-		}
-		else {
-			timedChangeset.removalsRepo.addEventInstance(instance);
-		}
-	});
-
-	eventChangeset.additionsRepo.iterEventInstances(function(instance) {
-		if (instance.dateProfile.isAllDay()) {
-			allDayChangeset.additionsRepo.addEventInstance(instance);
-		}
-		else {
-			timedChangeset.additionsRepo.addEventInstance(instance);
-		}
-	});
-
-	return { allDay: allDayChangeset, timed: timedChangeset };
-}
-
-
 function groupEventFootprintsByAllDay(eventFootprints) {
 	var allDay = [];
 	var timed = [];

+ 78 - 0
src/models/EventInstanceDataSourceSplitter.js

@@ -0,0 +1,78 @@
+
+var EventInstanceDataSourceSplitter = Class.extend(EmitterMixin, ListenerMixin, {
+
+	keysFunc: null,
+
+
+	constructor: function(keysFunc) {
+		this.keysFunc = keysFunc;
+	},
+
+
+	buildSubSource: function(key) {
+		var subDataSource = new EventInstanceDataSource();
+
+		this.on('clear', function() {
+			subDataSource.addChangeset(new EventInstanceChangeset(true));
+		});
+
+		this.on('receive:' + key, function(changeset) {
+			subDataSource.addChangeset(changeset);
+		});
+
+		this.on('after:receive', function() {
+			if (!subDataSource.isPopulated) {
+				subDataSource.addChangeset(new EventInstanceChangeset());
+			}
+		})
+
+		return subDataSource;
+	},
+
+
+	addSource: function(dataSource) {
+		this.listenTo(dataSource, 'receive', this.processChangeset);
+
+		if (dataSource.isPopulated) {
+			this.processChangeset(new EventInstanceChangeset(false, null, dataSource.instanceRepo));
+		}
+	},
+
+
+	removeSource: function(dataSource) {
+		this.stopListeningTo(dataSource);
+	},
+
+
+	processChangeset: function(changeset) {
+		var keysFunc = this.keysFunc;
+		var changesetsByKey = {};
+		var key;
+		var getChangeset = function(key) {
+			return (changesetsByKey[key] || (changesetsByKey[key] = new EventInstanceChangeset()));
+		};
+
+		if (changeset.isClear) {
+			this.trigger('clear');
+		}
+
+		changeset.removalsRepo.iterEventInstances(function(eventInstance) {
+			keysFunc(eventInstance).forEach(function(key) {
+				getChangeset(key).removalsRepo.addEventInstance(eventInstance);
+			});
+		});
+
+		changeset.additionsRepo.iterEventInstances(function(eventInstance) {
+			keysFunc(eventInstance).forEach(function(key) {
+				getChangeset(key).additionsRepo.addEventInstance(eventInstance);
+			});
+		});
+
+		for (key in changesetsByKey) {
+			this.trigger('receive:' + key, changesetsByKey[key]);
+		}
+
+		this.trigger('after:receive');
+	}
+
+});