瀏覽代碼

use ListenerMixin to make things cleaner

Adam Shaw 10 年之前
父節點
當前提交
1dc69b828e
共有 5 個文件被更改,包括 28 次插入44 次删除
  1. 14 22
      src/common/DragListener.js
  2. 6 6
      src/common/Grid.js
  3. 3 5
      src/common/MouseFollower.js
  4. 3 4
      src/common/Popover.js
  5. 2 7
      src/common/View.js

+ 14 - 22
src/common/DragListener.js

@@ -3,7 +3,7 @@
 ----------------------------------------------------------------------------------------------------------------------*/
 // TODO: use Emitter
 
-var DragListener = FC.DragListener = Class.extend({
+var DragListener = FC.DragListener = Class.extend(ListenerMixin, {
 
 	options: null,
 
@@ -14,10 +14,6 @@ var DragListener = FC.DragListener = Class.extend({
 	originX: null,
 	originY: null,
 
-	// handler attached to the document, bound to the DragListener's `this`
-	mousemoveProxy: null,
-	mouseupProxy: null,
-
 	// for IE8 bug-fighting behavior, for now
 	subjectEl: null, // the element being draged. optional
 	subjectHref: null,
@@ -27,7 +23,6 @@ var DragListener = FC.DragListener = Class.extend({
 	scrollTopVel: null, // pixels per second
 	scrollLeftVel: null, // pixels per second
 	scrollIntervalId: null, // ID of setTimeout for scrolling animation loop
-	scrollHandlerProxy: null, // this-scoped function for handling when scrollEl is scrolled
 
 	scrollSensitivity: 30, // pixels from edge for scrolling to start
 	scrollSpeed: 200, // pixels per second, at maximum speed
@@ -69,16 +64,19 @@ var DragListener = FC.DragListener = Class.extend({
 				if (!scrollParent.is(window) && !scrollParent.is(document)) {
 					this.scrollEl = scrollParent;
 
-					// scope to `this`, and use `debounce` to make sure rapid calls don't happen
-					this.scrollHandlerProxy = debounce(proxy(this, 'scrollHandler'), 100);
-					this.scrollEl.on('scroll', this.scrollHandlerProxy);
+					this.listenTo(
+						this.scrollEl,
+						'scroll',
+						debounce(proxy(this, 'scrollHandler'), 100) // make sure rapid calls don't happen
+					);
 				}
 			}
 
-			$(document)
-				.on('mousemove', this.mousemoveProxy = proxy(this, 'mousemove'))
-				.on('mouseup', this.mouseupProxy = proxy(this, 'mouseup'))
-				.on('selectstart', this.preventDefault); // prevents native selection in IE<=8
+			this.listenTo($(document), {
+				mousemove: this.mousemove,
+				mouseup: this.mouseup,
+				selectstart: this.preventDefault // prevents native selection in IE<=8
+			});
 
 			if (ev) {
 				this.originX = ev.pageX;
@@ -200,17 +198,11 @@ var DragListener = FC.DragListener = Class.extend({
 
 			// remove the scroll handler if there is a scrollEl
 			if (this.scrollEl) {
-				this.scrollEl.off('scroll', this.scrollHandlerProxy);
-				this.scrollHandlerProxy = null;
+				this.stopListeningTo(this.scrollEl, 'scroll');
 			}
 
-			$(document)
-				.off('mousemove', this.mousemoveProxy)
-				.off('mouseup', this.mouseupProxy)
-				.off('selectstart', this.preventDefault);
-
-			this.mousemoveProxy = null;
-			this.mouseupProxy = null;
+			// stops listening to mousemove, mouseup, selectstart
+			this.stopListeningTo($(document));
 
 			this.isListening = false;
 			this.listenStop(ev);

+ 6 - 6
src/common/Grid.js

@@ -2,7 +2,7 @@
 /* An abstract class comprised of a "grid" of areas that each represent a specific datetime
 ----------------------------------------------------------------------------------------------------------------------*/
 
-var Grid = FC.Grid = Class.extend({
+var Grid = FC.Grid = Class.extend(ListenerMixin, {
 
 	view: null, // a View object
 	isRTL: null, // shortcut to the view's isRTL option
@@ -13,8 +13,6 @@ var Grid = FC.Grid = Class.extend({
 	el: null, // the containing element
 	elsByFill: null, // a hash of jQuery element sets used for rendering each fill. Keyed by fill name.
 
-	externalDragStartProxy: null, // binds the Grid's scope to externalDragStart (in DayGrid.events)
-
 	// derived from options
 	eventTimeFormat: null,
 	displayEventTime: null,
@@ -33,7 +31,6 @@ var Grid = FC.Grid = Class.extend({
 		this.isRTL = view.opt('isRTL');
 
 		this.elsByFill = {};
-		this.externalDragStartProxy = proxy(this, 'externalDragStart');
 	},
 
 
@@ -225,13 +222,16 @@ var Grid = FC.Grid = Class.extend({
 
 	// Binds DOM handlers to elements that reside outside the grid, such as the document
 	bindGlobalHandlers: function() {
-		$(document).on('dragstart sortstart', this.externalDragStartProxy); // jqui
+		this.listenTo($(document), {
+			dragstart: this.externalDragStart, // jqui
+			sortstart: this.externalDragStart // jqui
+		});
 	},
 
 
 	// Unbinds DOM handlers from elements that reside outside the grid
 	unbindGlobalHandlers: function() {
-		$(document).off('dragstart sortstart', this.externalDragStartProxy); // jqui
+		this.stopListeningTo($(document));
 	},
 
 

+ 3 - 5
src/common/MouseFollower.js

@@ -2,7 +2,7 @@
 /* Creates a clone of an element and lets it track the mouse as it moves
 ----------------------------------------------------------------------------------------------------------------------*/
 
-var MouseFollower = Class.extend({
+var MouseFollower = Class.extend(ListenerMixin, {
 
 	options: null,
 
@@ -22,8 +22,6 @@ var MouseFollower = Class.extend({
 	topDelta: null,
 	leftDelta: null,
 
-	mousemoveProxy: null, // document mousemove handler, bound to the MouseFollower's `this`
-
 	isFollowing: false,
 	isHidden: false,
 	isAnimating: false, // doing the revert animation?
@@ -49,7 +47,7 @@ var MouseFollower = Class.extend({
 				this.updatePosition();
 			}
 
-			$(document).on('mousemove', this.mousemoveProxy = proxy(this, 'mousemove'));
+			this.listenTo($(document), 'mousemove', this.mousemove);
 		}
 	},
 
@@ -74,7 +72,7 @@ var MouseFollower = Class.extend({
 		if (this.isFollowing && !this.isAnimating) { // disallow more than one stop animation at a time
 			this.isFollowing = false;
 
-			$(document).off('mousemove', this.mousemoveProxy);
+			this.stopListeningTo($(document), 'mousemove');
 
 			if (shouldRevert && revertDuration && !this.isHidden) { // do a revert animation?
 				this.isAnimating = true;

+ 3 - 4
src/common/Popover.js

@@ -13,12 +13,11 @@ Options:
 	- hide (callback)
 */
 
-var Popover = Class.extend({
+var Popover = Class.extend(ListenerMixin, {
 
 	isHidden: true,
 	options: null,
 	el: null, // the container element for the popover. generated by this object
-	documentMousedownProxy: null, // document mousedown handler bound to `this`
 	margin: 10, // the space required between the popover and the edges of the scroll container
 
 
@@ -72,7 +71,7 @@ var Popover = Class.extend({
 		});
 
 		if (options.autoHide) {
-			$(document).on('mousedown', this.documentMousedownProxy = proxy(this, 'documentMousedown'));
+			this.listenTo($(document), 'mousedown', this.documentMousedown);
 		}
 	},
 
@@ -95,7 +94,7 @@ var Popover = Class.extend({
 			this.el = null;
 		}
 
-		$(document).off('mousedown', this.documentMousedownProxy);
+		this.stopListeningTo($(document), 'mousedown');
 	},
 
 

+ 2 - 7
src/common/View.js

@@ -41,9 +41,6 @@ var View = FC.View = Class.extend({
 	nextDayThreshold: null,
 	isHiddenDayHash: null,
 
-	// document handlers, bound to `this` object
-	documentMousedownProxy: null, // TODO: doesn't work with touch
-
 	// now indicator
 	isNowIndicatorRendered: null,
 	initialNowDate: null, // result first getNow call
@@ -66,8 +63,6 @@ var View = FC.View = Class.extend({
 
 		this.eventOrderSpecs = parseFieldSpecs(this.opt('eventOrder'));
 
-		this.documentMousedownProxy = proxy(this, 'documentMousedown');
-
 		this.initialize();
 	},
 
@@ -393,13 +388,13 @@ var View = FC.View = Class.extend({
 
 	// Binds DOM handlers to elements that reside outside the view container, such as the document
 	bindGlobalHandlers: function() {
-		$(document).on('mousedown', this.documentMousedownProxy);
+		this.listenTo($(document), 'mousedown', this.documentMousedown);
 	},
 
 
 	// Unbinds DOM handlers from elements that reside outside the view container
 	unbindGlobalHandlers: function() {
-		$(document).off('mousedown', this.documentMousedownProxy);
+		this.stopListeningTo($(document), 'mousedown');
 	},