From ee8e892bb418a42afba898e96457bec92498ccc2 Mon Sep 17 00:00:00 2001 From: Xavier Mamano Date: Wed, 15 Feb 2012 17:55:37 +0100 Subject: [PATCH 1/2] Move some touch logic to the `handler` base class. --- lib/OpenLayers/Handler.js | 36 +++++++++++++++++++ lib/OpenLayers/Handler/Click.js | 27 +------------- lib/OpenLayers/Handler/Drag.js | 20 +---------- lib/OpenLayers/Handler/Feature.js | 20 +---------- lib/OpenLayers/Handler/Point.js | 20 +---------- tests/Handler/Click.html | 58 +++++++++++++++++++++++++++++++ tests/Handler/Drag.html | 7 +++- tests/Handler/Feature.html | 24 ++++++------- tests/Handler/Point.html | 29 ++++++++-------- 9 files changed, 130 insertions(+), 111 deletions(-) diff --git a/lib/OpenLayers/Handler.js b/lib/OpenLayers/Handler.js index 58c71b45a4..4a7aa20d36 100644 --- a/lib/OpenLayers/Handler.js +++ b/lib/OpenLayers/Handler.js @@ -85,6 +85,14 @@ OpenLayers.Handler = OpenLayers.Class({ * the OpenLayers code. */ evt: null, + + /** + * Property: touch + * {Boolean} Indcates the support of touch events. When touch events are + * starded touch will be true and all mouse related listeners will do + * nothing. + */ + touch: false, /** * Constructor: OpenLayers.Handler @@ -186,10 +194,38 @@ OpenLayers.Handler = OpenLayers.Class({ this.unregister(events[i], this[events[i]]); } } + this.touch = false; this.active = false; return true; }, + /** + * Method: startTouch + * Start touch events, this method must be called by subclasses in + * "touchstart" method. When touch events are starded will be + * true and all mouse related listeners will do nothing. + * + * Returns: + * {Boolean} The touch events are started. + */ + startTouch: function() { + if (this.touch) { + return false; + } else { + this.touch = true; + var events = [ + "mousedown", "mouseup", "mousemove", "click", "dblclick", + "mouseout" + ]; + for (var i=0, len=events.length; i. */ timerId: null, - - /** - * Property: touch - * {Boolean} When a touchstart event is fired, touch will be true and all - * mouse related listeners will do nothing. - */ - touch: false, /** * Property: down @@ -155,10 +148,7 @@ OpenLayers.Handler.Click = OpenLayers.Class(OpenLayers.Handler, { * {Boolean} Continue propagating this event. */ touchstart: function(evt) { - if (!this.touch) { - this.unregisterMouseListeners(); - this.touch = true; - } + this.startTouch(); this.down = this.getEventInfo(evt); this.last = this.getEventInfo(evt); return true; @@ -195,20 +185,6 @@ OpenLayers.Handler.Click = OpenLayers.Class(OpenLayers.Handler, { } return true; }, - - /** - * Method: unregisterMouseListeners - * In a touch environment, we don't want to handle mouse events. - */ - unregisterMouseListeners: function() { - this.map.events.un({ - mousedown: this.mousedown, - mouseup: this.mouseup, - click: this.click, - dblclick: this.dblclick, - scope: this - }); - }, /** * Method: mousedown @@ -516,7 +492,6 @@ OpenLayers.Handler.Click = OpenLayers.Class(OpenLayers.Handler, { this.down = null; this.first = null; this.last = null; - this.touch = false; deactivated = true; } return deactivated; diff --git a/lib/OpenLayers/Handler/Drag.js b/lib/OpenLayers/Handler/Drag.js index a2adf66ed0..573cb12cfb 100644 --- a/lib/OpenLayers/Handler/Drag.js +++ b/lib/OpenLayers/Handler/Drag.js @@ -50,13 +50,6 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, { */ dragging: false, - /** - * Property: touch - * {Boolean} When a touchstart event is fired, touch will be true and all - * mouse related listeners will do nothing. - */ - touch: false, - /** * Property: last * {} The last pixel location of the drag. @@ -344,17 +337,7 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, { * {Boolean} Let the event propagate. */ touchstart: function(evt) { - if (!this.touch) { - this.touch = true; - // unregister mouse listeners - this.map.events.un({ - mousedown: this.mousedown, - mouseup: this.mouseup, - mousemove: this.mousemove, - click: this.click, - scope: this - }); - } + this.startTouch(); return this.dragstart(evt); }, @@ -508,7 +491,6 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, { deactivate: function() { var deactivated = false; if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { - this.touch = false; this.started = false; this.dragging = false; this.start = null; diff --git a/lib/OpenLayers/Handler/Feature.js b/lib/OpenLayers/Handler/Feature.js index 402b8d66a8..411a7c246e 100644 --- a/lib/OpenLayers/Handler/Feature.js +++ b/lib/OpenLayers/Handler/Feature.js @@ -59,13 +59,6 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, { * {} The location of the last mouseup. */ up: null, - - /** - * Property: touch - * {Boolean} When a touchstart event is fired, touch will be true and all - * mouse related listeners will do nothing. - */ - touch: false, /** * Property: clickTolerance @@ -139,17 +132,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, { * {Boolean} Let the event propagate. */ touchstart: function(evt) { - if(!this.touch) { - this.touch = true; - this.map.events.un({ - mousedown: this.mousedown, - mouseup: this.mouseup, - mousemove: this.mousemove, - click: this.click, - dblclick: this.dblclick, - scope: this - }); - } + this.startTouch(); return OpenLayers.Event.isMultiTouch(evt) ? true : this.mousedown(evt); }, @@ -395,7 +378,6 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, { this.lastFeature = null; this.down = null; this.up = null; - this.touch = false; this.map.events.un({ "removelayer": this.handleMapEvents, "changelayer": this.handleMapEvents, diff --git a/lib/OpenLayers/Handler/Point.js b/lib/OpenLayers/Handler/Point.js index 0b9ddb9dd4..70e36e6d6e 100644 --- a/lib/OpenLayers/Handler/Point.js +++ b/lib/OpenLayers/Handler/Point.js @@ -113,12 +113,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, { */ pixelTolerance: 5, - /** - * Property: touch - * {Boolean} Indcates the support of touch events. - */ - touch: false, - /** * Property: lastTouchPx * {} The last pixel used to know the distance between @@ -216,7 +210,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, { this.layer.destroy(false); } this.layer = null; - this.touch = false; return true; }, @@ -383,18 +376,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, { * {Boolean} Allow event propagation */ touchstart: function(evt) { - if (!this.touch) { - this.touch = true; - // unregister mouse listeners - this.map.events.un({ - mousedown: this.mousedown, - mouseup: this.mouseup, - mousemove: this.mousemove, - click: this.click, - dblclick: this.dblclick, - scope: this - }); - } + this.startTouch(); this.lastTouchPx = evt.xy; return this.down(evt); }, diff --git a/tests/Handler/Click.html b/tests/Handler/Click.html index 41172cd896..be508cac75 100644 --- a/tests/Handler/Click.html +++ b/tests/Handler/Click.html @@ -669,6 +669,64 @@ map.destroy(); } + function test_touchstart(t) { + // a test to verify that the touchstart function does + // unregister the mouse listeners when it's called the + // first time + + t.plan(7); + + // set up + + var map = new OpenLayers.Map("map", { + controls: [] + }); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Click(control, {}); + control.handler = handler; + map.addControl(control); + handler.activate(); + + function allRegistered() { + var eventTypes = ['mousedown', 'mouseup', 'click', 'dblclick'], + eventType, + listeners, + listener, + flag; + for(var i=0, ilen=eventTypes.length; i diff --git a/tests/Handler/Drag.html b/tests/Handler/Drag.html index 4be1df9e6d..4122493b3e 100644 --- a/tests/Handler/Drag.html +++ b/tests/Handler/Drag.html @@ -294,7 +294,7 @@ // "touchend" events set expected states in the drag handler. // We also verify that we stop event bubbling as appropriate. - t.plan(14); + t.plan(19); // set up @@ -324,6 +324,11 @@ t.eq(h.start.y, 0, '[touchstart] start.y is correct'); t.eq(log.length, 1, '[touchstart] one item in log'); t.ok(log[0] === e, "touchstart", '[touchstart] event is stopped'); + t.eq(m.events.listeners.mousedown.length, 0,"mousedown is not registered"); + t.eq(m.events.listeners.mouseup.length, 0,"mouseup is not registered"); + t.eq(m.events.listeners.mousemove.length, 0,"mousemove is not registered"); + t.eq(m.events.listeners.click.length, 0,"click is not registered"); + t.eq(m.events.listeners.mouseout.length, 0,"mouseout is not registered"); e = {xy: new Px(1, 1)}; m.events.triggerEvent('touchmove', e); diff --git a/tests/Handler/Feature.html b/tests/Handler/Feature.html index 7c768e1a69..21cbf7ad4f 100644 --- a/tests/Handler/Feature.html +++ b/tests/Handler/Feature.html @@ -280,9 +280,10 @@ handler.mousedown = function() {}; // mock mousedown handler.activate(); + var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick']; + function allRegistered() { - var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick'], - eventType, + var eventType, listeners, listener, flag; @@ -305,21 +306,18 @@ } function noneRegistered() { - var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick'], - eventType, - listeners, - listener; + var eventType, + times, + flag = false; for(var i=0, ilen=eventTypes.length; i Date: Thu, 25 Apr 2013 16:25:56 +0200 Subject: [PATCH 2/2] Take into account the comments of bartvde --- lib/OpenLayers/Handler.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/OpenLayers/Handler.js b/lib/OpenLayers/Handler.js index 4a7aa20d36..0d43187e10 100644 --- a/lib/OpenLayers/Handler.js +++ b/lib/OpenLayers/Handler.js @@ -88,8 +88,8 @@ OpenLayers.Handler = OpenLayers.Class({ /** * Property: touch - * {Boolean} Indcates the support of touch events. When touch events are - * starded touch will be true and all mouse related listeners will do + * {Boolean} Indicates the support of touch events. When touch events are + * started touch will be true and all mouse related listeners will do * nothing. */ touch: false, @@ -202,16 +202,11 @@ OpenLayers.Handler = OpenLayers.Class({ /** * Method: startTouch * Start touch events, this method must be called by subclasses in - * "touchstart" method. When touch events are starded will be + * "touchstart" method. When touch events are started will be * true and all mouse related listeners will do nothing. - * - * Returns: - * {Boolean} The touch events are started. */ startTouch: function() { - if (this.touch) { - return false; - } else { + if (!this.touch) { this.touch = true; var events = [ "mousedown", "mouseup", "mousemove", "click", "dblclick", @@ -222,7 +217,6 @@ OpenLayers.Handler = OpenLayers.Class({ this.unregister(events[i], this[events[i]]); } } - return true; } },