Browse Source

allow open-ended UnzonedRanges. other date utils, make use of

Adam Shaw 8 years ago
parent
commit
f6573a95c4

+ 35 - 16
src/Calendar.constraints.js

@@ -291,28 +291,47 @@ Calendar.prototype.eventRangeToEventFootprints = function(eventRange) {
 };
 
 
+/*
+Parses footprints directly.
+Very similar to EventDateProfile::parse :(
+*/
+Calendar.prototype.parseFootprints = function(rawInput) {
+	var start, end;
+
+	if (rawInput.start) {
+		start = calendar.moment(rawInput.start);
+
+		if (!start.isValid()) {
+			start = null;
+		}
+	}
+
+	if (rawInput.end) {
+		end = calendar.moment(rawInput.end);
+
+		if (!end.isValid()) {
+			end = null;
+		}
+	}
+
+	return [
+		new ComponentFootprint(
+			new UnzonedRange(start, end),
+			(start && !start.hasTime()) || (end && !end.hasTime()) // isAllDay
+		)
+	];
+};
+
+
 // Footprint Utils
 // ----------------------------------------------------------------------------------------
 
-/*
-Allow footprints that have undefined range, with implies ALL times.
-TODO: use date range utils
-*/
+
 Calendar.prototype.footprintContainsFootprint = function(outerFootprint, innerFootprint) {
-	return !outerFootprint.unzonedRange || (
-		innerFootprint.unzonedRange.startMs >= outerFootprint.unzonedRange.startMs &&
-		innerFootprint.unzonedRange.endMs <= outerFootprint.unzonedRange.endMs
-	);
+	return outerFootprint.unzonedRange.contains(innerFootprint.unzonedRange);
 };
 
 
-/*
-Allow footprints that have undefined range, with implies ALL times.
-TODO: use date range utils
-*/
 Calendar.prototype.footprintsIntersect = function(footprint0, footprint1) {
-	return !footprint0.unzonedRange || !footprint1.unzonedRange || (
-		footprint0.unzonedRange.startMs < footprint1.unzonedRange.endMs &&
-		footprint0.unzonedRange.endMs > footprint1.unzonedRange.startMs
-	);
+	return footprint0.unzonedRange.intersectsWith(footprint1.unzonedRange);
 };

+ 3 - 0
src/Calendar.moment.js

@@ -133,6 +133,9 @@ Calendar.mixin({
 	},
 
 
+	/*
+	Assumes the footprint is non-open-ended.
+	*/
 	footprintToDateProfile: function(componentFootprint, ignoreEnd) {
 		var start = FC.moment.utc(componentFootprint.unzonedRange.startMs);
 		var end;

+ 1 - 0
src/common/Grid.events.js

@@ -654,6 +654,7 @@ Grid.mixin({
 	// returns the zoned start/end dates for the event that would result from the hypothetical drop. end might be null.
 	// Returning a null value signals an invalid drop hit.
 	// DOES NOT consider overlap/constraint.
+	// Assumes both footprints are non-open-ended.
 	computeExternalDrop: function(componentFootprint, meta) {
 		var calendar = this.view.calendar;
 		var start = FC.moment.utc(componentFootprint.unzonedRange.startMs).stripZone();

+ 1 - 0
src/common/Grid.js

@@ -493,6 +493,7 @@ var Grid = FC.Grid = ChronoComponent.extend({
 
 	// Given two spans, must return the combination of the two.
 	// TODO: do this separation of concerns (combining VS validation) for event dnd/resize too.
+	// Assumes both footprints are non-open-ended.
 	computeSelectionFootprint: function(footprint0, footprint1) {
 		var ms = [
 			footprint0.unzonedRange.startMs,

+ 3 - 0
src/models/ComponentFootprint.js

@@ -14,6 +14,9 @@ var ComponentFootprint = FC.ComponentFootprint = Class.extend({
 	},
 
 
+	/*
+	Only works for non-open-ended ranges.
+	*/
 	toLegacy: function(calendar) {
 		var start = calendar.moment(this.unzonedRange.startMs);
 		var end = calendar.moment(this.unzonedRange.endMs);

+ 56 - 9
src/models/UnzonedRange.js

@@ -1,8 +1,11 @@
 
 var UnzonedRange = FC.UnzonedRange = Class.extend({
 
-	startMs: null,
-	endMs: null,
+	startMs: null, // if null, no start constraint
+	endMs: null, // if null, no end constraint
+
+	// TODO: move these into footprint.
+	// Especially, doesn't make sense for null startMs/endMs.
 	isStart: true,
 	isEnd: true,
 
@@ -16,16 +19,39 @@ var UnzonedRange = FC.UnzonedRange = Class.extend({
 			endInput = endInput.clone().stripZone();
 		}
 
-		this.startMs = startInput.valueOf();
-		this.endMs = endInput.valueOf();
+		if (startInput) {
+			this.startMs = startInput.valueOf();
+		}
+
+		if (endInput) {
+			this.endMs = endInput.valueOf();
+		}
 	},
 
 	constrainTo: function(constraintRange) {
-		var startMs = Math.max(this.startMs, constraintRange.startMs);
-		var endMs = Math.min(this.endMs, constraintRange.endMs);
+		var startMs = this.startMs;
+		var endMs = this.endMs;
 		var newRange = null;
 
-		if (startMs < endMs) {
+		if (constraintRange.startMs !== null) {
+			if (startMs === null) {
+				startMs = constraintRange.startMs;
+			}
+			else {
+				startMs = Math.max(startMs, constraintRange.startMs);
+			}
+		}
+
+		if (constraintRange.endMs !== null) {
+			if (endMs === null) {
+				endMs = constraintRange.endMs;
+			}
+			else {
+				endMs = Math.min(this.endMs, constraintRange.endMs);
+			}
+		}
+
+		if (startMs === null || endMs === null || startMs < endMs) {
 			newRange = new UnzonedRange(startMs, endMs);
 			newRange.isStart = this.isStart && startMs === this.startMs;
 			newRange.isEnd = this.isEnd && endMs === this.endMs;
@@ -34,14 +60,31 @@ var UnzonedRange = FC.UnzonedRange = Class.extend({
 		return newRange;
 	},
 
+
+	contains: function(innerFootprint) {
+		return (this.startMs === null || (innerFootprint.startMs !== null && innerFootprint.startMs >= this.startMs)) &&
+			(this.endMs === null || (innerFootprint.endMs !== null && innerFootprint.endMs <= this.endMs));
+	},
+
+
+	intersectsWith: function(otherFootprint) {
+		return (this.endMs === null || otherFootprint.startMs === null || this.endMs > otherFootprint.startMS) &&
+			(this.startMs === null || otherFootprint.endMs === null || this.startMs < otherFootprint.endMs);
+	},
+
+
 	// hopefully we'll remove these...
 
 	getStart: function() {
-		return FC.moment.utc(this.startMs).stripZone();
+		if (this.startMs !== null) {
+			return FC.moment.utc(this.startMs).stripZone();
+		}
 	},
 
 	getEnd: function() {
-		return FC.moment.utc(this.endMs).stripZone();
+		if (this.endMs !== null) {
+			return FC.moment.utc(this.endMs).stripZone();
+		}
 	},
 
 	getRange: function() {
@@ -54,6 +97,7 @@ var UnzonedRange = FC.UnzonedRange = Class.extend({
 /*
 SIDEEFFECT: will mutate eventRanges.
 Will return a new array result.
+Only works for non-open-ended ranges.
 */
 function invertUnzonedRanges(ranges, constraintRange) {
 	var invertedRanges = [];
@@ -90,6 +134,9 @@ function invertUnzonedRanges(ranges, constraintRange) {
 }
 
 
+/*
+Only works for non-open-ended ranges.
+*/
 function compareUnzonedRanges(range1, range2) {
 	return range1.startMs - range2.startMs; // earlier ranges go first
 }