Bladeren bron

adjust getPeerEvents to work with spans

Adam Shaw 10 jaren geleden
bovenliggende
commit
1700087d57
3 gewijzigde bestanden met toevoegingen van 42 en 48 verwijderingen
  1. 20 36
      src/EventManager.js
  2. 8 5
      src/common/Grid.events.js
  3. 14 7
      src/common/Grid.js

+ 20 - 36
src/EventManager.js

@@ -26,7 +26,6 @@ function EventManager(options) { // assumed to be a calendar
 	t.mutateEvent = mutateEvent;
 	t.normalizeEventDates = normalizeEventDates;
 	t.normalizeEventTimes = normalizeEventTimes;
-	t.eventToRange = eventToRange;
 	
 	
 	// imports
@@ -578,15 +577,6 @@ function EventManager(options) { // assumed to be a calendar
 	}
 
 
-	// Generates the unzoned start/end dates an event appears to occupy
-	function eventToRange(event) {
-		return {
-			start: event.start.clone().stripZone(),
-			end: t.getEventEnd(event).stripZone()
-		};
-	}
-
-
 	// If the given event is a recurring event, break it down into an array of individual instances.
 	// If not a recurring event, return an array with the single original event.
 	// If given a falsy input (probably because of a failed buildEventFromInput call), returns an empty array.
@@ -895,19 +885,13 @@ function EventManager(options) { // assumed to be a calendar
 	/* Overlapping / Constraining
 	-----------------------------------------------------------------------------------------*/
 
-	t.isEventLocationAllowed = isEventLocationAllowed;
-	t.isExternalLocationAllowed = isExternalLocationAllowed;
-	t.isSelectionRangeAllowed = isSelectionRangeAllowed;
-
-
-	// Determines if an event location (zoned start/end/allDay) is valid
-	function isEventLocationAllowed(eventLocation, event) {
-		return isEventRangeAllowed(eventToRange(eventLocation), event);
-	}
+	t.isEventSpanAllowed = isEventSpanAllowed;
+	t.isExternalSpanAllowed = isExternalSpanAllowed;
+	t.isSelectionSpanAllowed = isSelectionSpanAllowed;
 
 
-	// Determines if an event range (unzoned start/end) is valid
-	function isEventRangeAllowed(range, event) {
+	// Determines if the given event can be relocated to the given span (unzoned start/end with other misc data)
+	function isEventSpanAllowed(span, event) {
 		var source = event.source || {};
 		var constraint = firstDefined(
 			event.constraint,
@@ -919,12 +903,12 @@ function EventManager(options) { // assumed to be a calendar
 			source.overlap,
 			options.eventOverlap
 		);
-		return isRangeAllowed(range, constraint, overlap, event);
+		return isSpanAllowed(span, constraint, overlap, event);
 	}
 
 
-	// Determines if an drop location (zoned start/end/allDay) for an external event is valid
-	function isExternalLocationAllowed(eventLocation, eventProps) {
+	// Determines if an external event can be relocated to the given span (unzoned start/end with other misc data)
+	function isExternalSpanAllowed(eventSpan, eventLocation, eventProps) {
 		var eventInput;
 		var event;
 
@@ -935,25 +919,25 @@ function EventManager(options) { // assumed to be a calendar
 		}
 
 		if (event) {
-			return isEventLocationAllowed(eventLocation, event);
+			return isEventSpanAllowed(eventSpan, event);
 		}
 		else { // treat it as a selection
 
-			return isSelectionRangeAllowed(eventToRange(eventLocation));
+			return isSelectionSpanAllowed(eventSpan);
 		}
 	}
 
 
-	// Determines if a range (unzoned start/end) is valid
-	function isSelectionRangeAllowed(range) {
-		return isRangeAllowed(range, options.selectConstraint, options.selectOverlap);
+	// Determines the given span (unzoned start/end with other misc data) can be selected.
+	function isSelectionSpanAllowed(span) {
+		return isSpanAllowed(span, options.selectConstraint, options.selectOverlap);
 	}
 
 
-	// Returns true if the given range (caused by an event drop/resize or a selection) is allowed to exist
+	// Returns true if the given span (caused by an event drop/resize or a selection) is allowed to exist
 	// according to the constraint/overlap settings.
 	// `event` is not required if checking a selection.
-	function isRangeAllowed(range, constraint, overlap, event) {
+	function isSpanAllowed(span, constraint, overlap, event) {
 		var constraintEvents;
 		var anyContainment;
 		var peerEvents;
@@ -969,7 +953,7 @@ function EventManager(options) { // assumed to be a calendar
 
 			anyContainment = false;
 			for (i = 0; i < constraintEvents.length; i++) {
-				if (eventContainsRange(constraintEvents[i], range)) {
+				if (eventContainsRange(constraintEvents[i], span)) {
 					anyContainment = true;
 					break;
 				}
@@ -980,13 +964,13 @@ function EventManager(options) { // assumed to be a calendar
 			}
 		}
 
-		peerEvents = t.getPeerEvents(event, range);
+		peerEvents = t.getPeerEvents(span, event);
 
 		for (i = 0; i < peerEvents.length; i++)  {
 			peerEvent = peerEvents[i];
 
 			// there needs to be an actual intersection before disallowing anything
-			if (eventIntersectsRange(peerEvent, range)) {
+			if (eventIntersectsRange(peerEvent, span)) {
 
 				// evaluate overlap for the given range and short-circuit if necessary
 				if (overlap === false) {
@@ -1066,8 +1050,8 @@ function EventManager(options) { // assumed to be a calendar
 
 
 // Returns a list of events that the given event should be compared against when being considered for a move to
-// the specified range. Attached to the Calendar's prototype because EventManager is a mixin for a Calendar.
-Calendar.prototype.getPeerEvents = function(event, range) {
+// the specified span. Attached to the Calendar's prototype because EventManager is a mixin for a Calendar.
+Calendar.prototype.getPeerEvents = function(span, event) {
 	var cache = this.getEventCache();
 	var peerEvents = [];
 	var i, otherEvent;

+ 8 - 5
src/common/Grid.events.js

@@ -291,7 +291,7 @@ Grid.mixin({
 					event
 				);
 
-				if (dropLocation && !calendar.isEventLocationAllowed(dropLocation, event)) {
+				if (dropLocation &&!calendar.isEventSpanAllowed(_this.eventToSpan(dropLocation), event)) {
 					disableCursor();
 					dropLocation = null;
 				}
@@ -504,7 +504,7 @@ Grid.mixin({
 			dropLocation.end = dropLocation.start.clone().add(meta.duration);
 		}
 
-		if (!calendar.isExternalLocationAllowed(dropLocation, meta.eventProps)) {
+		if (!calendar.isExternalSpanAllowed(this.eventToSpan(dropLocation), dropLocation, meta.eventProps)) {
 			return null;
 		}
 
@@ -565,7 +565,7 @@ Grid.mixin({
 					_this.computeEventEndResize(origHitSpan, hitSpan, event);
 
 				if (resizeLocation) {
-					if (!calendar.isEventLocationAllowed(resizeLocation, event)) {
+					if (!calendar.isEventSpanAllowed(_this.eventToSpan(resizeLocation), event)) {
 						disableCursor();
 						resizeLocation = null;
 					}
@@ -825,9 +825,12 @@ Grid.mixin({
 	},
 
 
-	// Convert an event's zoned dates into a visible unzoned range. Convenience.
+	// Generates the unzoned start/end dates an event appears to occupy
 	eventToRange: function(event) {
-		return this.view.calendar.eventToRange(event);
+		return {
+			start: event.start.clone().stripZone(),
+			end: this.view.calendar.getEventEnd(event).stripZone()
+		};
 	},
 
 

+ 14 - 7
src/common/Grid.js

@@ -365,17 +365,24 @@ var Grid = FC.Grid = Class.extend({
 	// Will return false if the selection is invalid and this should be indicated to the user.
 	// Will return null/undefined if a selection invalid but no error should be reported.
 	computeSelection: function(span0, span1) {
-		var dates = [ span0.start, span0.end, span1.start, span1.end ];
-		var combinedSpan;
-
-		dates.sort(compareNumbers); // sorts chronologically. works with Moments
-		combinedSpan = { start: dates[0].clone(), end: dates[3].clone() };
+		var span = this.computeSelectionSpan(span0, span1);
 
-		if (!this.view.calendar.isSelectionRangeAllowed(combinedSpan)) {
+		if (span && !this.view.calendar.isSelectionSpanAllowed(span)) {
 			return false;
 		}
 
-		return combinedSpan;
+		return span;
+	},
+
+
+	// Given two spans, must return the combination of the two.
+	// TODO: do this separation of concerns (combining VS validation) for event dnd/resize too.
+	computeSelectionSpan: function(span0, span1) {
+		var dates = [ span0.start, span0.end, span1.start, span1.end ];
+
+		dates.sort(compareNumbers); // sorts chronologically. works with Moments
+
+		return { start: dates[0].clone(), end: dates[3].clone() };
 	},