add onEnter and onLeave callback functions to the DragFeature control, p=adube, r=me (closes #3034)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11071 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2011-02-08 20:47:23 +00:00
parent 198b3a9c7f
commit 9495c8cb70
2 changed files with 42 additions and 5 deletions

View File

@@ -65,6 +65,28 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
*/
onComplete: function(feature, pixel) {},
/**
* APIProperty: onEnter
* {Function} Define this function if you want to know when the mouse
* goes over a feature and thereby makes this feature a candidate
* for dragging.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that is ready
* to be dragged.
*/
onEnter: function(feature) {},
/**
* APIProperty: onLeave
* {Function} Define this function if you want to know when the mouse
* goes out of the feature that was dragged.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
*/
onLeave: function(feature) {},
/**
* APIProperty: documentDrag
* {Boolean} If set to true, mouse dragging will continue even if the
@@ -192,6 +214,7 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
this.handlers.drag.activate();
this.over = true;
OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over");
this.onEnter(feature);
} else {
if(this.feature.id == feature.id) {
this.over = true;
@@ -269,6 +292,7 @@ OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
OpenLayers.Element.removeClass(
this.map.viewPortDiv, this.displayClass + "Over"
);
this.onLeave(feature);
this.feature = null;
} else {
if(this.feature.id == feature.id) {

View File

@@ -71,11 +71,14 @@
}
function test_Control_DragFeature_over(t) {
t.plan(3);
t.plan(5);
var log = [];
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.DragFeature(layer);
var control = new OpenLayers.Control.DragFeature(layer, {
onEnter: function(f) { log.push({feature: f}); }
});
map.addControl(control);
control.activate();
@@ -94,6 +97,10 @@
"control gets the proper feature from the feature handler");
t.ok(control.handlers.drag.active,
"drag handler activated when over a feature");
t.eq(log.length, 1,
"onEnter called exactly once");
t.eq(log[0].feature.id, feature.id,
"onEnter called with expected feature");
}
function test_Control_DragFeature_down(t) {
@@ -241,11 +248,14 @@
}
function test_Control_DragFeature_out(t) {
t.plan(2);
t.plan(4);
var log = [];
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.DragFeature(layer);
var control = new OpenLayers.Control.DragFeature(layer, {
onLeave: function(f) { log.push({feature: f}); }
});
map.addControl(control);
control.activate();
@@ -268,7 +278,10 @@
map.events.triggerEvent("mousemove", {type: "mousemove"});
t.ok(control.feature == null,
"feature is set to null on mouse out");
t.eq(log.length, 1,
"onLeave called exactly once");
t.eq(log[0].feature.id, feature.id,
"onLeave called with expected feature");
}
</script>