Browse Source

change how raw eventdef props are parsed

Adam Shaw 8 years ago
parent
commit
457d6d95b5

+ 56 - 96
src/models/event/EventDef.js

@@ -127,40 +127,46 @@ var EventDef = Class.extend({
 	// Standard Prop Parsing System, for the INSTANCE
 	// -----------------------------------------------------------------------------------------------------------------
 
+	standardPropMap: {}, // ::defineStandardProps will always clone
 
-	standardPropMap: {},
-	standardPropHandlers: [],
 
+	/*
+	returns false on failure
+	*/
+	applyRawProps: function(rawProps) {
+		var standardPropMap = this.standardPropMap;
+		var miscProps = {};
+		var propName;
 
-	isStandardProp: function(propName) {
-		return this.standardPropMap[propName] !== undefined;
-	},
-
+		if (rawProps.id == null) {
+			this.id = EventDef.generateId();
+		}
+		else {
+			this.id = EventDef.normalizeId((this.rawId = rawProps.id));
+		}
 
-	applyStandardProps: function(rawProps) {
-		var map = this.standardPropMap;
-		var handlers = this.standardPropHandlers;
-		var propsForHandlers = {};
-		var allSuccessful = true;
-		var propName;
-		var i;
+		// can make DRY with EventSource
+		if ($.isArray(rawProps.className)) {
+			this.className = rawProps.className;
+		}
+		else if (typeof rawProps.className === 'string') {
+			this.className = rawProps.className.split(/\s+/);
+		}
 
 		for (propName in rawProps) {
-			if (map[propName] === true) { // has handler?
-				propsForHandlers[propName] = rawProps[propName];
+			if (propName in standardPropMap) {
+				if (standardPropMap[propName] === true) { // copy verbatim?
+					this[propName] = rawProps[propName];
+				}
 			}
-			else if (map[propName] === false) { // verbatim
-				this[propName] = rawProps[propName];
+			else {
+				miscProps[propName] = rawProps[propName];
 			}
 		}
 
-		for (i = 0; i < handlers.length; i++) {
-			if (handlers[i].call(this, propsForHandlers) === false) {
-				allSuccessful = false; // don't want to break. want to execute all handlers
-			}
-		}
+		this.miscProps = miscProps;
 
-		return allSuccessful;
+		return true;
 	}
 
 });
@@ -183,31 +189,16 @@ EventDef.generateId = function() {
 };
 
 
-// Standard Prop Parsing System, for class self-definition
+// Standard Prop *Parsing* System, for class self-definition
 // ---------------------------------------------------------------------------------------------------------------------
 
 
-EventDef.defineStandardPropHandler = function(propNames, handler) {
+EventDef.defineStandardProps = function(propDefs) {
 	var proto = this.prototype;
-	var map = proto.standardPropMap = $.extend({}, proto.standardPropMap);
-	var i;
 
-	for (i = 0; i < propNames.length; i++) {
-		map[propNames[i]] = true; // true means "uses handler"
-	}
+	proto.standardPropMap = Object.create(proto.standardPropMap);
 
-	proto.standardPropHandlers = proto.standardPropHandlers.concat(handler);
-};
-
-
-EventDef.defineVerbatimStandardProps = function(propNames) {
-	var proto = this.prototype;
-	var map = proto.standardPropMap = $.extend({}, proto.standardPropMap);
-	var i;
-
-	for (i = 0; i < propNames.length; i++) {
-		map[propNames[i]] = false; // false means "copy verbatim"
-	}
+	copyOwnProps(propDefs, proto.standardPropMap);
 };
 
 
@@ -217,8 +208,8 @@ EventDef.copyVerbatimStandardProps = function(src, dest) {
 
 	for (propName in map) {
 		if (
-			src[propName] != null &&
-			!map[propName] // falsy means "copy verbatim"
+			src[propName] != null && // in the src object?
+			map[propName] === true // false means "copy verbatim"
 		) {
 			dest[propName] = src[propName];
 		}
@@ -234,9 +225,6 @@ EventDef.parse = function(rawInput, source) {
 	var def = new this(source);
 	var calendarTransform = source.calendar.opt('eventDataTransform');
 	var sourceTransform = source.eventDataTransform;
-	var rawStandardProps = {};
-	var miscProps = {};
-	var propName;
 
 	if (calendarTransform) {
 		rawInput = calendarTransform(rawInput);
@@ -245,21 +233,10 @@ EventDef.parse = function(rawInput, source) {
 		rawInput = sourceTransform(rawInput);
 	}
 
-	for (propName in rawInput) {
-		if (def.isStandardProp(propName)) {
-			rawStandardProps[propName] = rawInput[propName];
-		}
-		else {
-			miscProps[propName] = rawInput[propName];
-		}
-	}
-
-	if (def.applyStandardProps(rawStandardProps) === false) {
+	if (def.applyRawProps(rawInput) === false) {
 		return false;
 	}
 
-	def.miscProps = miscProps;
-
 	return def;
 };
 
@@ -268,41 +245,24 @@ EventDef.parse = function(rawInput, source) {
 // ---------------------------------------------------------------------------------------------------------------------
 
 
-EventDef.defineStandardPropHandler([
-	'id',
-	'className',
-	'source' // will ignored
-], function(rawProps) {
-
-	if (rawProps.id == null) {
-		this.id = EventDef.generateId();
-	}
-	else {
-		this.id = EventDef.normalizeId((this.rawId = rawProps.id));
-	}
-
-	// can make DRY with EventSource
-	if ($.isArray(rawProps.className)) {
-		this.className = rawProps.className;
-	}
-	else if (typeof rawProps.className === 'string') {
-		this.className = rawProps.className.split(/\s+/);
-	}
+EventDef.defineStandardProps({
+	// not automatically assigned (`false`)
+	id: false,
+	className: false,
+	source: false, // will ignored
+
+	// automatically assigned (`true`)
+	title: true,
+	url: true,
+	rendering: true,
+	constraint: true,
+	overlap: true,
+	editable: true,
+	startEditable: true,
+	durationEditable: true,
+	resourceEditable: true,
+	color: true,
+	backgroundColor: true,
+	borderColor: true,
+	textColor: true,
 });
-
-
-EventDef.defineVerbatimStandardProps([
-	'title',
-	'url',
-	'rendering',
-	'constraint',
-	'overlap',
-	'editable',
-	'startEditable',
-	'durationEditable',
-	'resourceEditable',
-	'color',
-	'backgroundColor',
-	'borderColor',
-	'textColor'
-]);

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

@@ -5,10 +5,9 @@ var EventDefMutation = Class.extend({
 	// callers should use setDateMutation for setting.
 	dateMutation: null,
 
-	// hacks to get updateEvent/createFromRawProps to work.
+	// hack to get updateEvent/createFromRawProps to work.
 	// not undo-able and not considered in isEmpty.
-	standardProps: null, // raw (pre-parse-like)
-	miscProps: null,
+	rawProps: null, // raw (pre-parse-like)
 
 
 	/*
@@ -28,13 +27,8 @@ var EventDefMutation = Class.extend({
 		}
 
 		// can't undo
-		if (this.standardProps) {
-			eventDef.applyStandardProps(this.standardProps);
-		}
-
-		// can't undo
-		if (this.miscProps) {
-			eventDef.miscProps = this.miscProps;
+		if (this.rawProps) {
+			eventDef.applyRawProps(this.rawProps);
 		}
 
 		if (origDateProfile) {
@@ -67,8 +61,7 @@ var EventDefMutation = Class.extend({
 
 EventDefMutation.createFromRawProps = function(eventInstance, newRawProps, largeUnit) {
 	var eventDef = eventInstance.def;
-	var standardProps = {};
-	var miscProps = {};
+	var applicableRawProps = {};
 	var propName;
 	var newDateProfile;
 	var dateMutation;
@@ -82,12 +75,7 @@ EventDefMutation.createFromRawProps = function(eventInstance, newRawProps, large
 			propName !== 'start' && propName !== 'end' && propName !== 'allDay' &&
 			propName !== 'source' && propName !== '_id'
 		) {
-			if (eventDef.isStandardProp(propName)) {
-				standardProps[propName] = newRawProps[propName];
-			}
-			else {
-				miscProps[propName] = newRawProps[propName];
-			}
+			applicableRawProps[propName] = newRawProps[propName];
 		}
 	}
 
@@ -102,8 +90,7 @@ EventDefMutation.createFromRawProps = function(eventInstance, newRawProps, large
 	}
 
 	defMutation = new EventDefMutation();
-	defMutation.standardProps = standardProps;
-	defMutation.miscProps = miscProps;
+	defMutation.rawProps = applicableRawProps;
 
 	if (dateMutation) {
 		defMutation.dateMutation = dateMutation;

+ 26 - 17
src/models/event/RecurringEventDef.js

@@ -79,30 +79,39 @@ var RecurringEventDef = EventDef.extend({
 		}
 
 		return def;
-	}
+	},
 
-});
 
+	/*
+	NOTE: if super-method fails, should still attempt to apply
+	*/
+	applyRawProps: function(rawProps) {
+		var superSuccess = EventDef.prototype.applyRawProps.apply(this, arguments);
 
-// Parsing
-// ---------------------------------------------------------------------------------------------------------------------
+		if (rawProps.start) {
+			this.startTime = moment.duration(rawProps.start);
+		}
 
+		if (rawProps.end) {
+			this.endTime = moment.duration(rawProps.end);
+		}
 
-RecurringEventDef.defineStandardPropHandler([
-	'start',
-	'end',
-	'dow'
-], function(rawProps) {
+		if (rawProps.dow) {
+			this.setDow(rawProps.dow);
+		}
 
-	if (rawProps.start) {
-		this.startTime = moment.duration(rawProps.start);
+		return superSuccess;
 	}
 
-	if (rawProps.end) {
-		this.endTime = moment.duration(rawProps.end);
-	}
+});
 
-	if (rawProps.dow) {
-		this.setDow(rawProps.dow);
-	}
+
+// Parsing
+// ---------------------------------------------------------------------------------------------------------------------
+
+
+RecurringEventDef.defineStandardProps({
+	start: false,
+	end: false,
+	dow: false
 });

+ 23 - 14
src/models/event/SingleEventDef.js

@@ -43,6 +43,24 @@ var SingleEventDef = EventDef.extend({
 			dateProfile.end ? calendar.moment(dateProfile.end) : null,
 			calendar
 		);
+	},
+
+
+	/*
+	NOTE: if super-method fails, should still attempt to apply
+	*/
+	applyRawProps: function(rawProps) {
+		var superSuccess = EventDef.prototype.applyRawProps.apply(this, arguments);
+		var dateProfile = EventDateProfile.parse(rawProps, this.source); // returns null on failure
+
+		if (dateProfile) {
+			this.dateProfile = dateProfile;
+
+			return superSuccess;
+		}
+		else {
+			return false;
+		}
 	}
 
 });
@@ -52,18 +70,9 @@ var SingleEventDef = EventDef.extend({
 // ---------------------------------------------------------------------------------------------------------------------
 
 
-SingleEventDef.defineStandardPropHandler([
-	'start',
-	'date', // alias for 'start'
-	'end',
-	'allDay'
-], function(rawProps) {
-	var dateProfile = EventDateProfile.parse(rawProps, this.source);
-
-	if (dateProfile) { // no failure?
-		this.dateProfile = dateProfile;
-	}
-	else {
-		return false;
-	}
+SingleEventDef.defineStandardProps({
+	start: false,
+	date: false, // alias for 'start'
+	end: false,
+	allDay: false
 });