Adam Shaw 8 лет назад
Родитель
Сommit
ecadc15c4d

+ 2 - 1
src/Calendar.js

@@ -336,8 +336,9 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 	},
 	},
 
 
 
 
+	// returns an EventInstanceDataSource
 	requestEvents: function(start, end) {
 	requestEvents: function(start, end) {
-		this.eventManager.requestEvents(
+		return this.eventManager.requestEvents(
 			start,
 			start,
 			end,
 			end,
 			this.opt('timezone'),
 			this.opt('timezone'),

+ 1 - 1
src/Calendar.options.js

@@ -69,7 +69,7 @@ Calendar.mixin({
 				return; // optionsModel already reacts to this
 				return; // optionsModel already reacts to this
 			}
 			}
 			else if (optionName === 'timezone') {
 			else if (optionName === 'timezone') {
-				this.view.flash('bindingEvents'); // refetches and reprocesses events
+				this.view.flash('eventDataSource'); // refetches and reprocesses events
 				return;
 				return;
 			}
 			}
 		}
 		}

+ 3 - 36
src/View.js

@@ -180,7 +180,7 @@ var View = FC.View = InteractiveDateComponent.extend({
 	// -----------------------------------------------------------------------------------------------------------------
 	// -----------------------------------------------------------------------------------------------------------------
 
 
 
 
-	// returns existing state at time of request
+	// returns an EventInstanceDataSource
 	requestEvents: function(dateProfile) {
 	requestEvents: function(dateProfile) {
 		var calendar = this.calendar;
 		var calendar = this.calendar;
 		var forceAllDay = dateProfile.isRangeAllDay && !this.usesMinMaxTime;
 		var forceAllDay = dateProfile.isRangeAllDay && !this.usesMinMaxTime;
@@ -192,23 +192,6 @@ var View = FC.View = InteractiveDateComponent.extend({
 	},
 	},
 
 
 
 
-	bindEventChanges: function() {
-		this.listenTo(this.calendar.eventManager, 'receive', this._handleEventsChanged);
-	},
-
-
-	_handleEventsChanged: function(changeset) {
-		this.startBatchRender();
-		this.handleEventsChanged(changeset);
-		this.stopBatchRender();
-	},
-
-
-	unbindEventChanges: function() {
-		this.stopListeningTo(this.calendar.eventManager);
-	},
-
-
 	// Date High-level Rendering
 	// Date High-level Rendering
 	// -----------------------------------------------------------------------------------------------------------------
 	// -----------------------------------------------------------------------------------------------------------------
 
 
@@ -746,22 +729,6 @@ View.watch('businessHours', [ 'businessHourGenerator', 'dateProfile' ], function
 });
 });
 
 
 
 
-View.watch('bindingEvents', [ 'dateProfile' ], function(deps) {
-	this.handleEventsBound();
-	this.requestEvents(deps.dateProfile);
-
-	if (this.calendar.eventManager.isFinalized()) {
-		this._handleEventsChanged(
-			new EventInstanceChangeset(
-				false, // isClear
-				null, // removals
-				this.calendar.eventManager.getEventInstanceRepo() // additions
-			)
-		);
-	}
-
-	this.bindEventChanges();
-}, function() {
-	this.unbindEventChanges();
-	this.handleEventsUnbound();
+View.watch('eventDataSource', [ 'dateProfile' ], function(deps) {
+	return this.requestEvents(deps.dateProfile);
 });
 });

+ 64 - 26
src/agenda/AgendaView.js

@@ -279,41 +279,45 @@ var AgendaView = FC.AgendaView = View.extend({
 	------------------------------------------------------------------------------------------------------------------*/
 	------------------------------------------------------------------------------------------------------------------*/
 
 
 
 
-	handleEventsChanged: function(eventChangeset) {
-		var allDayChangeset = new EventInstanceChangeset();
-		var timedChangeset = new EventInstanceChangeset();
+	setEventDataSourceInChildren: function(eventDataSource) {
+		var allDayEventSource = new EventInstanceDataSource();
+		var timedEventSource = new EventInstanceDataSource();
 
 
-		if (eventChangeset.isClear) {
-			allDayChangeset.isClear = true;
-			timedChangeset.isClear = true;
-		}
-
-		// reference dateProfile.isAllDay() instead of the eventDef
-		// because eventDef might have updated
+		function processChangeset(changeset, isInitial) {
+			var split = splitEventChangesetByAllDay(changeset);
 
 
-		eventChangeset.removalsRepo.iterEventInstances(function(instance) {
-			if (instance.dateProfile.isAllDay()) {
-				allDayChangeset.removalsRepo.addEventInstance(instance);
+			if (isInitial || !split.allDay.isEmpty()) {
+				allDayEventSource.addChangeset(split.allDay);
 			}
 			}
-			else {
-				timedChangeset.removalsRepo.addEventInstance(instance);
-			}
-		});
 
 
-		eventChangeset.additionsRepo.iterEventInstances(function(instance) {
-			if (instance.dateProfile.isAllDay()) {
-				allDayChangeset.additionsRepo.addEventInstance(instance);
+			if (isInitial || !split.timed.isEmpty()) {
+				timedEventSource.addChangeset(split.timed);
 			}
 			}
-			else {
-				timedChangeset.additionsRepo.addEventInstance(instance);
-			}
-		});
+		}
 
 
-		this.timeGrid.handleEventsChanged(timedChangeset);
+		// initial event instances. simulate a changeset
+		if (eventDataSource.isFinalized()) {
+			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) {
 		if (this.dayGrid) {
-			this.dayGrid.handleEventsChanged(allDayChangeset);
+			this.dayGrid.set('eventDataSource', allDayEventSource);
 		}
 		}
+		this.timeGrid.set('eventDataSource', timedEventSource);
+	},
+
+
+	unsetEventDataSourceInChildren: function(eventDataSource) {
+		this.stopListeningTo(eventDataSource);
+
+		View.prototype.unsetEventDataSourceInChildren.apply(this, arguments);
 	},
 	},
 
 
 
 
@@ -440,6 +444,40 @@ 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) {
 function groupEventFootprintsByAllDay(eventFootprints) {
 	var allDay = [];
 	var allDay = [];
 	var timed = [];
 	var timed = [];

+ 29 - 41
src/component/DateComponent.js

@@ -257,37 +257,17 @@ var DateComponent = FC.DateComponent = Component.extend({
 	},
 	},
 
 
 
 
-	// Event Data Handling
-	// -----------------------------------------------------------------------------------------------------------------
-
-
-	handleEventsBound: function() {
-		this.callChildren('handleEventsBound', arguments);
-	},
-
-
-	handleEventsChanged: function(changeset) {
-		if (this.hasOwnEventRendering()) {
-
-			if (!this.eventInstanceRepo) {
-				this.eventInstanceRepo = new EventInstanceRepo();
-			}
-
-			changeset.applyToRepo(this.eventInstanceRepo);
+	// Event Data
+	// ---------------------------------------------------------------------------------------------------------------
 
 
-			if (this.has('displayingEvents')) {
-				this.requestEventRender(this.eventInstanceRepo);
-			}
-		}
 
 
-		this.callChildren('handleEventsChanged', arguments);
+	setEventDataSourceInChildren: function(eventDataSource) {
+		this.setInChildren('eventDataSource', eventDataSource);
 	},
 	},
 
 
 
 
-	handleEventsUnbound: function() {
-		this.eventInstanceRepo = null;
-
-		this.callChildren('handleEventsUnbound', arguments);
+	unsetEventDataSourceInChildren: function() {
+		this.unsetInChildren('eventDataSource');
 	},
 	},
 
 
 
 
@@ -295,26 +275,27 @@ var DateComponent = FC.DateComponent = Component.extend({
 	// -----------------------------------------------------------------------------------------------------------------
 	// -----------------------------------------------------------------------------------------------------------------
 
 
 
 
-	hasOwnEventRendering: function() {
-		return this.eventRenderer || this.renderEvents; // or legacy function
-	},
-
+	startDisplayingEvents: function(eventDataSource) {
+		var _this = this;
 
 
-	startDisplayingEvents: function() {
 		if (this.eventRenderer) {
 		if (this.eventRenderer) {
 			this.eventRenderer.rangeUpdated();
 			this.eventRenderer.rangeUpdated();
 		}
 		}
 
 
-		if (this.eventInstanceRepo) {
-			this.requestEventRender(this.eventInstanceRepo);
+		if (eventDataSource.isFinalized()) {
+			this.requestEventRender(eventDataSource.instanceRepo);
 		}
 		}
+
+		this.listenTo(eventDataSource, 'receive', function(eventInstanceChangeset) {
+			_this.requestEventRender(eventDataSource.instanceRepo);
+		});
 	},
 	},
 
 
 
 
-	stopDisplayingEvents: function() {
-		if (this.eventInstanceRepo) {
-			this.requestEventUnrender();
-		}
+	stopDisplayingEvents: function(eventDataSource) {
+		this.stopListeningTo(eventDataSource);
+
+		this.requestEventUnrender();
 	},
 	},
 
 
 
 
@@ -784,10 +765,17 @@ DateComponent.watch('displayingBusinessHours', [ 'displayingDates', 'businessHou
 });
 });
 
 
 
 
-DateComponent.watch('displayingEvents', [ 'displayingDates' ], function() {
-	this.startDisplayingEvents();
-}, function() {
-	this.stopDisplayingEvents();
+DateComponent.watch('displayingEvents', [ 'displayingDates', 'eventDataSource' ], function(deps) {
+	this.startDisplayingEvents(deps.eventDataSource);
+}, function(deps) {
+	this.stopDisplayingEvents(deps.eventDataSource);
+});
+
+
+DateComponent.watch('settingEventDataSourceInChildren', [ 'eventDataSource' ], function(deps) {
+	this.setEventDataSourceInChildren(deps.eventDataSource);
+}, function(deps) {
+	this.unsetEventDataSourceInChildren(deps.eventDataSource);
 });
 });
 
 
 
 

+ 5 - 0
src/models/EventInstanceChangeset.js

@@ -132,6 +132,11 @@ var EventInstanceChangeset = Class.extend({
 	},
 	},
 
 
 
 
+	isEmpty: function() {
+		return !this.isClear && !this.removalsRepo.cnt && !this.additionsRepo.cnt;
+	},
+
+
 	applyToRepo: function(repo) {
 	applyToRepo: function(repo) {
 		var removalsHash = this.removalsRepo.byDefId;
 		var removalsHash = this.removalsRepo.byDefId;
 		var additionsHash = this.additionsRepo.byDefId;
 		var additionsHash = this.additionsRepo.byDefId;

+ 3 - 7
src/models/EventManager.js

@@ -15,6 +15,7 @@ var EventManager = Class.extend(EmitterMixin, ListenerMixin, {
 	},
 	},
 
 
 
 
+	// returns an EventInstanceDataSource
 	requestEvents: function(start, end, timezone, force) {
 	requestEvents: function(start, end, timezone, force) {
 		if (
 		if (
 			force ||
 			force ||
@@ -25,6 +26,8 @@ var EventManager = Class.extend(EmitterMixin, ListenerMixin, {
 				new EventPeriod(start, end, timezone)
 				new EventPeriod(start, end, timezone)
 			);
 			);
 		}
 		}
+
+		return this.currentPeriod;
 	},
 	},
 
 
 
 
@@ -35,13 +38,6 @@ var EventManager = Class.extend(EmitterMixin, ListenerMixin, {
 	},
 	},
 
 
 
 
-	getEventInstanceRepo: function() {
-		if (this.isFinalized()) {
-			return this.currentPeriod.instanceRepo;
-		}
-	},
-
-
 	isFinalized: function() {
 	isFinalized: function() {
 		return this.currentPeriod && this.currentPeriod.isFinalized();
 		return this.currentPeriod && this.currentPeriod.isFinalized();
 	},
 	},