Browse Source

new code for event mutation, handles props differently

Adam Shaw 8 years ago
parent
commit
0ec7d96592

+ 14 - 1
src/common/ParsableModelMixin.js

@@ -5,7 +5,8 @@ var ParsableModelMixin = {
 
 
 
 
 	/*
 	/*
-	Returns true/false for success
+	Returns true/false for success.
+	Meant to be only called ONCE, at object creation.
 	*/
 	*/
 	applyProps: function(rawProps) {
 	applyProps: function(rawProps) {
 		var standardPropMap = this.standardPropMap;
 		var standardPropMap = this.standardPropMap;
@@ -33,14 +34,26 @@ var ParsableModelMixin = {
 
 
 	/*
 	/*
 	If subclasses override, they must call this supermethod and return the boolean response.
 	If subclasses override, they must call this supermethod and return the boolean response.
+	Meant to be only called ONCE, at object creation.
 	*/
 	*/
 	applyManualStandardProps: function(rawProps) {
 	applyManualStandardProps: function(rawProps) {
 		return true;
 		return true;
 	},
 	},
 
 
 
 
+	/*
+	Can be called even after initial object creation.
+	*/
 	applyMiscProps: function(rawProps) {
 	applyMiscProps: function(rawProps) {
 		// subclasses can implement
 		// subclasses can implement
+	},
+
+
+	/*
+	TODO: why is this a method when defineStandardProps is static
+	*/
+	isStandardProp: function(propName) {
+		return propName in this.standardPropMap;
 	}
 	}
 
 
 };
 };

+ 5 - 0
src/models/event/EventDateProfile.js

@@ -48,6 +48,11 @@ var EventDateProfile = Class.extend({
 });
 });
 
 
 
 
+EventDateProfile.isStandardProp = function(propName) {
+	return propName === 'start' || propName === 'date' || propName === 'end' || propName === 'allDay';
+};
+
+
 /*
 /*
 Needs an EventSource object
 Needs an EventSource object
 */
 */

+ 2 - 2
src/models/event/EventDef.js

@@ -51,7 +51,7 @@ var EventDef = FC.EventDef = Class.extend(ParsableModelMixin, {
 
 
 		EventDef.copyVerbatimStandardProps(this, copy);
 		EventDef.copyVerbatimStandardProps(this, copy);
 
 
-		copy.className = this.className; // should clone?
+		copy.className = this.className.slice(); // copy
 		copy.miscProps = $.extend({}, this.miscProps);
 		copy.miscProps = $.extend({}, this.miscProps);
 
 
 		return copy;
 		return copy;
@@ -137,7 +137,7 @@ var EventDef = FC.EventDef = Class.extend(ParsableModelMixin, {
 
 
 		obj._id = this.uid;
 		obj._id = this.uid;
 		obj.source = this.source;
 		obj.source = this.source;
-		obj.className = this.className; // should clone?
+		obj.className = this.className.slice(); // copy
 		obj.allDay = this.isAllDay();
 		obj.allDay = this.isAllDay();
 
 
 		if (this.rawId != null) {
 		if (this.rawId != null) {

+ 64 - 20
src/models/event/EventDefMutation.js

@@ -5,9 +5,12 @@ var EventDefMutation = FC.EventDefMutation = Class.extend({
 	// callers should use setDateMutation for setting.
 	// callers should use setDateMutation for setting.
 	dateMutation: null,
 	dateMutation: null,
 
 
-	// hack to get updateEvent/createFromRawProps to work.
+	// hacks to get updateEvent/createFromRawProps to work.
 	// not undo-able and not considered in isEmpty.
 	// not undo-able and not considered in isEmpty.
-	rawProps: null, // raw (pre-parse-like)
+	eventDefId: null, // standard manual props
+	className: null, // "
+	verbatimStandardProps: null,
+	miscProps: null,
 
 
 
 
 	/*
 	/*
@@ -27,8 +30,28 @@ var EventDefMutation = FC.EventDefMutation = Class.extend({
 		}
 		}
 
 
 		// can't undo
 		// can't undo
-		if (this.rawProps) {
-			eventDef.applyProps(this.rawProps);
+		// TODO: more DRY with EventDef::applyManualStandardProps
+		if (this.eventDefId != null) {
+			eventDef.id = EventDef.normalizeId((eventDef.rawId = this.eventDefId));
+		}
+
+		// can't undo
+		// TODO: more DRY with EventDef::applyManualStandardProps
+		if (this.className) {
+			eventDef.className = this.className;
+		}
+
+		// can't undo
+		if (this.verbatimStandardProps) {
+			SingleEventDef.copyVerbatimStandardProps(
+				this.verbatimStandardProps, // src
+				eventDef // dest
+			);
+		}
+
+		// can't undo
+		if (this.miscProps) {
+			eventDef.applyMiscProps(this.miscProps);
 		}
 		}
 
 
 		if (origDateProfile) {
 		if (origDateProfile) {
@@ -59,38 +82,59 @@ var EventDefMutation = FC.EventDefMutation = Class.extend({
 });
 });
 
 
 
 
-EventDefMutation.createFromRawProps = function(eventInstance, newRawProps, largeUnit) {
+EventDefMutation.createFromRawProps = function(eventInstance, rawProps, largeUnit) {
 	var eventDef = eventInstance.def;
 	var eventDef = eventInstance.def;
-	var applicableRawProps = {};
+	var dateProps = {};
+	var standardProps = {};
+	var miscProps = {};
+	var verbatimStandardProps = {};
+	var eventDefId = null;
+	var className = null;
 	var propName;
 	var propName;
-	var newDateProfile;
+	var dateProfile;
 	var dateMutation;
 	var dateMutation;
 	var defMutation;
 	var defMutation;
 
 
-	for (propName in newRawProps) {
-		if (
-			// ignore object-type custom properties and any date-related properties,
-			// as well as any other internal property
-			typeof newRawProps[propName] !== 'object' &&
-			propName !== 'start' && propName !== 'end' && propName !== 'allDay' &&
-			propName !== 'source' && propName !== '_id'
-		) {
-			applicableRawProps[propName] = newRawProps[propName];
+	for (propName in rawProps) {
+		if (EventDateProfile.isStandardProp(propName)) {
+			dateProps[propName] = rawProps[propName];
+		}
+		else if (eventDef.isStandardProp(propName)) {
+			standardProps[propName] = rawProps[propName];
+		}
+		else if (eventDef.miscProps[propName] !== rawProps[propName]) { // only if changed
+			miscProps[propName] = rawProps[propName];
 		}
 		}
 	}
 	}
 
 
-	newDateProfile = EventDateProfile.parse(newRawProps, eventDef.source);
+	dateProfile = EventDateProfile.parse(dateProps, eventDef.source);
 
 
-	if (newDateProfile) { // no failure?
+	if (dateProfile) { // no failure?
 		dateMutation = EventDefDateMutation.createFromDiff(
 		dateMutation = EventDefDateMutation.createFromDiff(
 			eventInstance.dateProfile,
 			eventInstance.dateProfile,
-			newDateProfile,
+			dateProfile,
 			largeUnit
 			largeUnit
 		);
 		);
 	}
 	}
 
 
+	if (standardProps.id !== eventDef.id) {
+		eventDefId = standardProps.id; // only apply if there's a change
+	}
+
+	if (!isArraysEqual(standardProps.className, eventDef.className)) {
+		className = standardProps.className; // only apply if there's a change
+	}
+
+	EventDef.copyVerbatimStandardProps(
+		standardProps, // src
+		verbatimStandardProps // dest
+	);
+
 	defMutation = new EventDefMutation();
 	defMutation = new EventDefMutation();
-	defMutation.rawProps = applicableRawProps;
+	defMutation.eventDefId = eventDefId;
+	defMutation.className = className;
+	defMutation.verbatimStandardProps = verbatimStandardProps;
+	defMutation.miscProps = miscProps;
 
 
 	if (dateMutation) {
 	if (dateMutation) {
 		defMutation.dateMutation = dateMutation;
 		defMutation.dateMutation = dateMutation;

+ 18 - 0
src/util.js

@@ -799,6 +799,24 @@ function removeExact(array, exactVal) {
 FC.removeExact = removeExact;
 FC.removeExact = removeExact;
 
 
 
 
+function isArraysEqual(a0, a1) {
+	var len = a0.length;
+	var i;
+
+	if (len == null || len !== a1.length) { // not array? or not same length?
+		return false;
+	}
+
+	for (i = 0; i < len; i++) {
+		if (a0[i] !== a1[i]) {
+			return false;
+		}
+	}
+
+	return true;
+}
+
+
 
 
 function firstDefined() {
 function firstDefined() {
 	for (var i=0; i<arguments.length; i++) {
 	for (var i=0; i<arguments.length; i++) {