From ba8354b593fefff9079523b3ebc5214a90a72b35 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 1 Nov 2007 20:53:06 +0000 Subject: [PATCH] Make the drag handler only call done if it actually dragged - thanks for the review Eric (closes #1118). git-svn-id: http://svn.openlayers.org/trunk/openlayers@5097 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Handler/Drag.js | 14 +++++++++++--- tests/Handler/test_Drag.html | 10 +++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Handler/Drag.js b/lib/OpenLayers/Handler/Drag.js index 5c339e969d..00a70a70a9 100644 --- a/lib/OpenLayers/Handler/Drag.js +++ b/lib/OpenLayers/Handler/Drag.js @@ -16,7 +16,9 @@ * when the drag begins, with each move, and when the drag is done. In * addition, controls can have callbacks keyed to 'up' and 'out' if they * care to differentiate between the types of events that correspond with - * the end of a drag sequence. + * the end of a drag sequence. If no drag actually occurs (no mouse move) + * the 'down' and 'up' callbacks will be called, but not the 'done' + * callback. * * Create a new drag handler with the constructor. * @@ -207,13 +209,16 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, { */ mouseup: function (evt) { if (this.started) { + var dragged = (this.start != this.last); this.started = false; this.dragging = false; // TBD replace with CSS classes this.map.div.style.cursor = ""; this.up(evt); this.callback("up", [evt.xy]); - this.callback("done", [evt.xy]); + if(dragged) { + this.callback("done", [evt.xy]); + } document.onselectstart = this.oldOnselectstart; } return true; @@ -231,16 +236,19 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, { */ mouseout: function (evt) { if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) { + var dragged = (this.start != this.last); this.started = false; this.dragging = false; // TBD replace with CSS classes this.map.div.style.cursor = ""; this.out(evt); this.callback("out", []); + if(dragged) { + this.callback("done", [evt.xy]); + } if(document.onselectstart) { document.onselectstart = this.oldOnselectstart; } - this.callback("done", [evt.xy]); } return true; }, diff --git a/tests/Handler/test_Drag.html b/tests/Handler/test_Drag.html index f681a21e62..c01716fde7 100644 --- a/tests/Handler/test_Drag.html +++ b/tests/Handler/test_Drag.html @@ -87,7 +87,7 @@ } function test_Handler_Drag_callbacks(t) { - t.plan(33); + t.plan(34); var map = new OpenLayers.Map('map', {controls: []}); @@ -187,6 +187,14 @@ OpenLayers.Event.isLeftClick = oldIsLeftClick; handler.checkModifiers = oldCheckModifiers; + // test mouseup before mousemove + var realUp = testEvents.up; + testEvents.up = testEvents.down; + // this will fail with notice about the done callback being called + // if done is called when it shouldn't be + map.events.triggerEvent("mouseup", testEvents.up); + testEvents.up = realUp; + // test mousemove handler.started = false; map.events.triggerEvent("mousemove", {xy: {x: null, y: null}});