Răsfoiți Sursa

split apart day drag listeners: select & dayClick

Adam Shaw 9 ani în urmă
părinte
comite
572b799d66
1 a modificat fișierele cu 73 adăugiri și 48 ștergeri
  1. 73 48
      src/common/Grid.js

+ 73 - 48
src/common/Grid.js

@@ -28,7 +28,8 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 	// TODO: port isTimeScale into same system?
 	largeUnit: null,
 
-	dayDragListener: null,
+	dayClickListener: null,
+	daySelectListener: null,
 	segDragListener: null,
 	segResizeListener: null,
 	externalDragListener: null,
@@ -39,7 +40,8 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 		this.isRTL = view.opt('isRTL');
 		this.elsByFill = {};
 
-		this.dayDragListener = this.buildDayDragListener();
+		this.dayClickListener = this.buildDayClickListener();
+		this.daySelectListener = this.buildDaySelectListener();
 		this.initMouseIgnoring();
 	},
 
@@ -263,9 +265,14 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 	// Process a mousedown on an element that represents a day. For day clicking and selecting.
 	dayMousedown: function(ev) {
 		if (!this.isIgnoringMouse) {
-			this.dayDragListener.startInteraction(ev, {
-				//distance: 5, // needs more work if we want dayClick to fire correctly
-			});
+
+			this.dayClickListener.startInteraction(ev);
+
+			if (this.view.opt('selectable')) {
+				this.daySelectListener.startInteraction(ev, {
+					//distance: 5, // needs more work
+				});
+			}
 		}
 	},
 
@@ -284,28 +291,64 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 			selectLongPressDelay = view.opt('longPressDelay'); // fallback
 		}
 
-		this.dayDragListener.startInteraction(ev, {
-			delay: selectLongPressDelay
-		});
+		this.dayClickListener.startInteraction(ev);
+
+		if (view.opt('selectable')) {
+			this.daySelectListener.startInteraction(ev, {
+				delay: selectLongPressDelay
+			});
+		}
 	},
 
 
-	// Creates a listener that tracks the user's drag across day elements.
-	// For day clicking and selecting.
-	buildDayDragListener: function() {
+	// Creates a listener that tracks the user's drag across day elements, for day clicking.
+	buildDayClickListener: function() {
 		var _this = this;
 		var view = this.view;
-		var isSelectable = view.opt('selectable');
 		var dayClickHit; // null if invalid dayClick
+
+		var dragListener = new HitDragListener(this, {
+			scroll: view.opt('dragScroll'),
+			interactionStart: function() {
+				dayClickHit = dragListener.origHit;
+			},
+			hitOver: function(hit, isOrig, origHit) {
+				// if user dragged to another cell at any point, it can no longer be a dayClick
+				if (!isOrig) {
+					dayClickHit = null;
+				}
+			},
+			hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits
+				dayClickHit = null;
+			},
+			interactionEnd: function(ev, isCancelled) {
+				if (
+					!isCancelled &&
+					dayClickHit &&
+					!_this.isIgnoringMouse // see hack in dayTouchStart
+				) {
+					view.triggerDayClick(
+						_this.getHitSpan(dayClickHit),
+						_this.getHitEl(dayClickHit),
+						ev
+					);
+				}
+			}
+		});
+
+		return dragListener;
+	},
+
+
+	// Creates a listener that tracks the user's drag across day elements, for day selecting.
+	buildDaySelectListener: function() {
+		var _this = this;
+		var view = this.view;
 		var selectionSpan; // null if invalid selection
 
-		// this listener tracks a mousedown on a day element, and a subsequent drag.
-		// if the drag ends on the same day, it is a 'dayClick'.
-		// if 'selectable' is enabled, this listener also detects selections.
 		var dragListener = new HitDragListener(this, {
 			scroll: view.opt('dragScroll'),
 			interactionStart: function() {
-				dayClickHit = dragListener.origHit; // for dayClick, where no dragging happens
 				selectionSpan = null;
 			},
 			dragStart: function() {
@@ -314,27 +357,20 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 			hitOver: function(hit, isOrig, origHit) {
 				if (origHit) { // click needs to have started on a hit
 
-					// if user dragged to another cell at any point, it can no longer be a dayClick
-					if (!isOrig) {
-						dayClickHit = null;
-					}
+					selectionSpan = _this.computeSelection(
+						_this.getHitSpan(origHit),
+						_this.getHitSpan(hit)
+					);
 
-					if (isSelectable) {
-						selectionSpan = _this.computeSelection(
-							_this.getHitSpan(origHit),
-							_this.getHitSpan(hit)
-						);
-						if (selectionSpan) {
-							_this.renderSelection(selectionSpan);
-						}
-						else if (selectionSpan === false) {
-							disableCursor();
-						}
+					if (selectionSpan) {
+						_this.renderSelection(selectionSpan);
+					}
+					else if (selectionSpan === false) {
+						disableCursor();
 					}
 				}
 			},
 			hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits
-				dayClickHit = null;
 				selectionSpan = null;
 				_this.unrenderSelection();
 			},
@@ -342,21 +378,9 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 				enableCursor();
 			},
 			interactionEnd: function(ev, isCancelled) {
-				if (!isCancelled) {
-					if (
-						dayClickHit &&
-						!_this.isIgnoringMouse // see hack in dayTouchStart
-					) {
-						view.triggerDayClick(
-							_this.getHitSpan(dayClickHit),
-							_this.getHitEl(dayClickHit),
-							ev
-						);
-					}
-					if (selectionSpan) {
-						// the selection will already have been rendered. just report it
-						view.reportSelection(selectionSpan, ev);
-					}
+				if (!isCancelled && selectionSpan) {
+					// the selection will already have been rendered. just report it
+					view.reportSelection(selectionSpan, ev);
 				}
 			}
 		});
@@ -369,7 +393,8 @@ var Grid = FC.Grid = Class.extend(ListenerMixin, MouseIgnorerMixin, {
 	// Useful for when public API methods that result in re-rendering are invoked during a drag.
 	// Also useful for when touch devices misbehave and don't fire their touchend.
 	clearDragListeners: function() {
-		this.dayDragListener.endInteraction();
+		this.dayClickListener.endInteraction();
+		this.daySelectListener.endInteraction();
 
 		if (this.segDragListener) {
 			this.segDragListener.endInteraction(); // will clear this.segDragListener