Cleanup mouse event on touch devices, r=erilem (closes #3215)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11827 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Stéphane Brunner
2011-03-31 09:01:05 +00:00
parent 80ce6e6583
commit 36face3f71
2 changed files with 95 additions and 10 deletions

View File

@@ -219,6 +219,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
this.layer.destroy(false); this.layer.destroy(false);
} }
this.layer = null; this.layer = null;
this.touch = false;
return true; return true;
}, },
@@ -385,9 +386,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation * {Boolean} Allow event propagation
*/ */
mousedown: function(evt) { mousedown: function(evt) {
if (this.touch) {
return;
}
return this.down(evt); return this.down(evt);
}, },
@@ -402,7 +400,18 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation * {Boolean} Allow event propagation
*/ */
touchstart: function(evt) { touchstart: function(evt) {
this.touch = true; 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.lastTouchPx = evt.xy; this.lastTouchPx = evt.xy;
return this.down(evt); return this.down(evt);
}, },
@@ -418,9 +427,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation * {Boolean} Allow event propagation
*/ */
mousemove: function(evt) { mousemove: function(evt) {
if (this.touch) {
return;
}
return this.move(evt); return this.move(evt);
}, },
@@ -450,9 +456,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation * {Boolean} Allow event propagation
*/ */
mouseup: function(evt) { mouseup: function(evt) {
if (this.touch) {
return;
}
return this.up(evt); return this.up(evt);
}, },

View File

@@ -417,6 +417,88 @@
"handler.point is null after destroy"); "handler.point is null after 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(4);
// set up
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
var layer = new OpenLayers.Layer.Vector("foo", {
maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
isBaseLayer: true
});
map.addLayer(layer);
var control = new OpenLayers.Control({});
var handler = new OpenLayers.Handler.Point(control, {});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
handler.activate();
function allRegistered() {
var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick'],
eventType,
listeners,
listener,
flag;
for(var i=0, ilen=eventTypes.length; i<ilen; i++) {
flag = false;
eventType = eventTypes[i];
listeners = map.events.listeners[eventType];
for(var j=0, jlen=listeners.length; j<jlen; j++) {
listener = listeners[j];
if(listener.func === handler[eventType] && listener.obj === handler) {
flag = true;
break;
}
}
if(!flag) {
return false;
}
}
return true;
}
function noneRegistered() {
var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick'],
eventType,
listeners,
listener;
for(var i=0, ilen=eventTypes.length; i<ilen; i++) {
eventType = eventTypes[i];
listeners = map.events.listeners[eventType];
for(var j=0, jlen=listeners.length; j<jlen; j++) {
listener = listeners[j];
if(listener.func === handler[eventType] && listener.obj === handler) {
return false;
}
}
}
return true;
}
// test
t.ok(allRegistered(), 'mouse listeners are registered');
handler.touchstart({xy: new OpenLayers.Pixel(0, 0)});
t.ok(noneRegistered(), 'mouse listeners are unregistered');
t.ok(handler.touch, 'handler.touch is set');
handler.deactivate();
t.ok(!handler.touch, 'handler.touch is not set');
// tear down
map.destroy();
}
// //
// Sequence tests // Sequence tests
// //