make the drag handler and drag feature control tidy up after themselves - as a bonus, give the drag feature control an onStart method. Thanks for the review crschmidt (closes #950).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4147 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-08-30 23:25:49 +00:00
parent 17a4129b55
commit cecf760f29
4 changed files with 31 additions and 7 deletions

View File

@@ -24,6 +24,19 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
*/ */
geometryTypes: null, geometryTypes: null,
/**
* APIProperty: onStart
* {Function} Define this function if you want to know when a drag starts.
* The function should expect to receive two arguments: the feature
* that is about to be dragged and the pixel location of the mouse.
*
* Parameters:
* feature - {OpenLayers.Feature.Vector} The feature that is about to be
* dragged.
* pixel - {OpenLayers.Pixel} The pixel location of the mouse.
*/
onStart: function(feature, pixel) {},
/** /**
* APIProperty: onDrag * APIProperty: onDrag
* {Function} Define this function if you want to know about each move of a * {Function} Define this function if you want to know about each move of a
@@ -154,6 +167,9 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
// the return from the handlers is unimportant in this case // the return from the handlers is unimportant in this case
this.dragHandler.deactivate(); this.dragHandler.deactivate();
this.featureHandler.deactivate(); this.featureHandler.deactivate();
this.feature = null;
this.dragging = false;
this.lastPixel = null;
return OpenLayers.Control.prototype.deactivate.apply(this, arguments); return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
}, },
@@ -189,8 +205,8 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
* pixel - {OpenLayers.Pixel} Location of the mouse event. * pixel - {OpenLayers.Pixel} Location of the mouse event.
*/ */
downFeature: function(pixel) { downFeature: function(pixel) {
this.dragHandler.dragging = true;
this.lastPixel = pixel; this.lastPixel = pixel;
this.onStart(this.feature, pixel);
}, },
/** /**

View File

@@ -282,7 +282,10 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
deactivate: function() { deactivate: function() {
var deactivated = false; var deactivated = false;
if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
this.started = false;
this.dragging = false; this.dragging = false;
this.start = null;
this.last = null;
deactivated = true; deactivated = true;
} }
return deactivated; return deactivated;

View File

@@ -109,14 +109,13 @@
} }
map.events.triggerEvent("mousemove"); map.events.triggerEvent("mousemove");
t.ok(!control.dragHandler.dragging,
"control is not dragging before the mousedown");
// simulate a mousedown on a feature // simulate a mousedown on a feature
control.onStart = function(feature, pixel) {
t.eq(feature, "foo", "onStart called with the correct feature");
t.eq(pixel, "bar", "onStart called with the correct pixel");
}
map.events.triggerEvent("mousedown", {xy: "bar", which: 1}); map.events.triggerEvent("mousedown", {xy: "bar", which: 1});
t.ok(control.dragHandler.dragging,
"drag is dragging after the mousedown");
t.eq(control.lastPixel, "bar", t.eq(control.lastPixel, "bar",
"mousedown sets the lastPixel correctly"); "mousedown sets the lastPixel correctly");
} }

View File

@@ -306,7 +306,7 @@
} }
function test_Handler_Drag_deactivate(t) { function test_Handler_Drag_deactivate(t) {
t.plan(3); t.plan(6);
var map = new OpenLayers.Map('map'); var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control(); var control = new OpenLayers.Control();
map.addControl(control); map.addControl(control);
@@ -320,8 +320,14 @@
deactivated = handler.deactivate(); deactivated = handler.deactivate();
t.ok(deactivated, t.ok(deactivated,
"deactivate returns true if the handler was active already"); "deactivate returns true if the handler was active already");
t.ok(!handler.started,
"deactivate sets started to false");
t.ok(!handler.dragging, t.ok(!handler.dragging,
"deactivate sets dragging to false"); "deactivate sets dragging to false");
t.ok(handler.start == null,
"deactivate sets start to null");
t.ok(handler.last == null,
"deactivate sets start to null");
} }