From 53037d475af3908487141c1d4762d8aece38b281 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 29 Jul 2015 11:56:36 -0700 Subject: [PATCH] Deduplicate track/untrack document event listener logic --- src/standard/gestures.html | 52 +++++++++++++++----------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/src/standard/gestures.html b/src/standard/gestures.html index ed721f334f..b0fdc3f1dc 100644 --- a/src/standard/gestures.html +++ b/src/standard/gestures.html @@ -137,6 +137,18 @@ return ta; } + function trackDocument(stateObj, movefn, upfn) { + stateObj.movefn = movefn; + stateObj.upfn = upfn; + document.addEventListener('mousemove', movefn); + document.addEventListener('mouseup', upfn); + } + + function untrackDocument(stateObj) { + document.removeEventListener('mousemove', stateObj.movefn); + document.removeEventListener('mouseup', stateObj.upfn); + } + var Gestures = { gestures: {}, recognizers: [], @@ -381,19 +393,7 @@ }, reset: function() { - this.untrackDocument(); - }, - - trackDocument: function(movefn, upfn) { - this.info.movefn = movefn; - this.info.upfn = upfn; - document.addEventListener('mousemove', movefn); - document.addEventListener('mouseup', upfn); - }, - - untrackDocument: function() { - document.removeEventListener('mousemove', this.info.movefn); - document.removeEventListener('mouseup', this.info.upfn); + untrackDocument(this.info); }, mousedown: function(e) { @@ -405,16 +405,16 @@ var movefn = function movefn(e) { if (!hasLeftMouseButton(e)) { self.fire('up', t, e); - self.untrackDocument(); + untrackDocument(self.info); } }; var upfn = function upfn(e) { if (hasLeftMouseButton(e)) { self.fire('up', t, e); } - self.untrackDocument(); + untrackDocument(self.info); }; - this.trackDocument(movefn, upfn); + trackDocument(this.info, movefn, upfn); this.fire('down', t, e); }, touchstart: function(e) { @@ -461,18 +461,6 @@ prevent: false }, - trackDocument: function(movefn, upfn) { - this.info.movefn = movefn; - this.info.upfn = upfn; - document.addEventListener('mousemove', movefn); - document.addEventListener('mouseup', upfn); - }, - - untrackDocument: function() { - document.removeEventListener('mousemove', this.info.movefn); - document.removeEventListener('mouseup', this.info.upfn); - }, - reset: function() { this.info.state = 'start'; this.info.started = false; @@ -480,7 +468,7 @@ this.info.x = 0; this.info.y = 0; this.info.prevent = false; - this.untrackDocument(); + untrackDocument(this.info); }, hasMovedEnough: function(x, y) { @@ -510,7 +498,7 @@ if (!hasLeftMouseButton(e)) { // always fire "end" self.info.state = 'end'; - self.untrackDocument(); + untrackDocument(self.info); } self.fire(t, e); self.info.started = true; @@ -523,10 +511,10 @@ } // remove the temporary listeners - self.untrackDocument(); + untrackDocument(self.info); }; // add temporary document listeners as mouse retargets - this.trackDocument(movefn, upfn); + trackDocument(this.info, movefn, upfn); this.info.x = e.clientX; this.info.y = e.clientY; },