Ver Fonte

parsing event definitions

Adam Shaw há 8 anos atrás
pai
commit
1f50ed8ccf
5 ficheiros alterados com 122 adições e 4 exclusões
  1. 1 0
      src.json
  2. 1 1
      src/EventManager.js
  3. 99 0
      src/models/EventDefinition.js
  4. 19 2
      src/models/EventDefinitionCollection.js
  5. 2 1
      src/util.js

+ 1 - 0
src.json

@@ -43,6 +43,7 @@
     "defaults.js",
     "locale.js",
     "Header.js",
+    "models/EventDefinition.js",
     "models/EventDefinitionCollection.js",
     "EventManager.js",
     "basic/BasicView.js",

+ 1 - 1
src/EventManager.js

@@ -44,7 +44,7 @@ function EventManager() { // assumed to be a calendar
 	var rangeStart, rangeEnd;
 	var pendingSourceCnt = 0; // outstanding fetch requests, max one per source
 	var cache = []; // holds events that have already been expanded
-	var eventDefCollection = new EventDefinitionCollection();
+	var eventDefCollection = new EventDefinitionCollection(t);
 
 
 	$.each(

+ 99 - 0
src/models/EventDefinition.js

@@ -0,0 +1,99 @@
+
+var EventDefinition = Class.extend({
+
+	source: null,
+	id: null,
+	title: null,
+	rendering: null,
+	miscProps: null,
+
+	constructor: function(rawProps, source, calendar) {
+		this.source = source;
+		this.id = rawProps.id || ('_fc' + (++EventDefinition.uuid));
+		this.title = rawProps.title || '';
+		this.rendering = rawProps.rendering || null;
+
+		this.assignMiscProps(rawProps);
+	},
+
+	assignMiscProps: function(rawProps) {
+		var miscProps = {};
+		var name;
+
+		for (name in rawProps) {
+			if (!this.isStandardProp(name)) {
+				miscProps[name] = rawProps[name];
+			}
+		}
+
+		this.miscProps = miscProps;
+	},
+
+	isStandardProp: function(name) {
+		return name === 'id' || name === 'title' || name === 'rendering';
+	},
+
+	buildPeriod: function(start, end) {
+	}
+});
+
+EventDefinition.uuid = 0;
+
+
+
+function isEventInputRecurring(eventInput) {
+	var start = eventInput.start || eventInput.date;
+	var end = eventInput.end;
+
+	return eventInput.dow ||
+		(isTimeString(start) || moment.isDuration(start)) ||
+		(end && (isTimeString(start) || moment.isDuration(start)));
+}
+
+
+
+var RecurringEventDefinition = EventDefinition.extend({
+
+	startTime: null,
+	endTime: null,
+	dow: null,
+
+	constructor: function(rawProps, source, calendar) {
+		EventDefinition.apply(this, arguments);
+
+		this.startTime = moment.duration(rawProps.start);
+		this.endTime = rawProps.end ? moment.duration(rawProps.end) : null;
+		this.dow = rawProps.dow;
+	},
+
+	isStandardProp: function(name) {
+		return EventDefinition.prototype.isStandardProp(name) ||
+			name === 'start' || name === 'end' || name === 'dow';
+	},
+
+	buildPeriod: function(start, end) {
+	}
+});
+
+
+
+var SingleEventDefinition = EventDefinition.extend({
+
+	start: null,
+	end: null,
+
+	constructor: function(rawProps, source, calendar) {
+		EventDefinition.apply(this, arguments);
+
+		this.start = calendar.moment(rawProps.start || rawProps.date); // 'date' is an alias
+		this.end = rawProps.end ? calendar.moment(rawProps.end) : null;
+	},
+
+	isStandardProp: function(name) {
+		return EventDefinition.prototype.isStandardProp(name) ||
+			name === 'start' || name === 'end' || name === 'date'; // 'date' is an alias
+	},
+
+	buildPeriod: function() {
+	}
+});

+ 19 - 2
src/models/EventDefinitionCollection.js

@@ -1,12 +1,29 @@
 
 var EventDefinitionCollection = Class.extend({
 
+	calendar: null,
+	models: null,
+
+	constructor: function(calendar) {
+		this.calendar = calendar;
+		this.models = [];
+	},
+
 	addRaw: function(eventInput, source) {
-		console.log('addRaw', JSON.stringify(eventInput), typeof source);
+		var eventDef;
+
+		if (isEventInputRecurring(eventInput)) {
+			eventDef = new RecurringEventDefinition(eventInput, source, this.calendar);
+		}
+		else {
+			eventDef = new SingleEventDefinition(eventInput, source, this.calendar);
+		}
+
+		this.models.push(eventDef);
 	},
 
 	clear: function() {
-
+		this.models = [];
 	},
 
 	clearBySource: function() {

+ 2 - 1
src/util.js

@@ -779,7 +779,8 @@ function isNativeDate(input) {
 
 // Returns a boolean about whether the given input is a time string, like "06:40:00" or "06:00"
 function isTimeString(str) {
-	return /^\d+\:\d+(?:\:\d+\.?(?:\d{3})?)?$/.test(str);
+	return typeof str === 'string' &&
+		/^\d+\:\d+(?:\:\d+\.?(?:\d{3})?)?$/.test(str);
 }