Add properties stopClick, stopDown, and stopUp to the feature handler. If

stopClick is true, clicks handled by the feature handler don't propagate to
other click listeners; otherwise handled clicks do propagate. The same kind of
rule applies to stopDown and stopUp. These properties default to true. Thanks
to Attila Csipa for expressing the need for this feature and cooking up the
first patch. r=tschaub. (closes #1266)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5976 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2008-02-03 17:35:39 +00:00
parent f9950ec389
commit cf87ffc26c
2 changed files with 76 additions and 8 deletions

View File

@@ -73,6 +73,33 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
*/ */
geometryTypes: null, geometryTypes: null,
/**
* Property: stopClick
* {Boolean} If stopClick is set to true, handled clicks do not
* propagate to other click listeners. Otherwise, handled clicks
* do propagate. Unhandled clicks always propagate, whatever the
* value of stopClick. Defaults to true.
*/
stopClick: true,
/**
* Property: stopDown
* {Boolean} If stopDown is set to true, handled mousedowns do not
* propagate to other mousedown listeners. Otherwise, handled
* mousedowns do propagate. Unhandled mousedowns always propagate,
* whatever the value of stopDown. Defaults to true.
*/
stopDown: true,
/**
* Property: stopUp
* {Boolean} If stopUp is set to true, handled mouseups do not
* propagate to other mouseup listeners. Otherwise, handled mouseups
* do propagate. Unhandled mouseups always propagate, whatever the
* value of stopUp. Defaults to true.
*/
stopUp: true,
/** /**
* Property: layerIndex * Property: layerIndex
* {Int} * {Int}
@@ -106,7 +133,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
*/ */
mousedown: function(evt) { mousedown: function(evt) {
this.down = evt.xy; this.down = evt.xy;
return !this.handle(evt); return this.handle(evt) ? !this.stopDown : true;
}, },
/** /**
@@ -119,7 +146,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
*/ */
mouseup: function(evt) { mouseup: function(evt) {
this.up = evt.xy; this.up = evt.xy;
return !this.handle(evt); return this.handle(evt) ? !this.stopUp : true;
}, },
/** /**
@@ -134,7 +161,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} * {Boolean}
*/ */
click: function(evt) { click: function(evt) {
return !this.handle(evt); return this.handle(evt) ? !this.stopClick : true;
}, },
/** /**
@@ -191,11 +218,11 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
* evt - {Event} * evt - {Event}
* *
* Returns: * Returns:
* {Boolean} Stop event propagation. * {Boolean} The event occurred over a relevant feature.
*/ */
handle: function(evt) { handle: function(evt) {
var type = evt.type; var type = evt.type;
var stopEvtPropag = false; var handled = false;
var previouslyIn = !!(this.feature); // previously in a feature var previouslyIn = !!(this.feature); // previously in a feature
var click = (type == "click" || type == "dblclick"); var click = (type == "click" || type == "dblclick");
this.feature = this.layer.getFeatureFromEvent(evt); this.feature = this.layer.getFeatureFromEvent(evt);
@@ -212,7 +239,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
this.triggerCallback(type, 'in', [this.feature]); this.triggerCallback(type, 'in', [this.feature]);
} }
this.lastFeature = this.feature; this.lastFeature = this.feature;
stopEvtPropag = true; handled = true;
} else { } else {
// not in to a feature // not in to a feature
if(previouslyIn && inNew || (click && this.lastFeature)) { if(previouslyIn && inNew || (click && this.lastFeature)) {
@@ -225,7 +252,7 @@ OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, {
this.triggerCallback(type, 'out', [this.lastFeature]); this.triggerCallback(type, 'out', [this.lastFeature]);
} }
} }
return stopEvtPropag; return handled;
}, },
/** /**

View File

@@ -121,6 +121,7 @@
feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(0,0)); feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(0,0));
handler.handle("click", {}); handler.handle("click", {});
} }
function test_Handler_Feature_callbacks(t) { function test_Handler_Feature_callbacks(t) {
t.plan(9); t.plan(9);
@@ -238,6 +239,46 @@
"deactivate sets the layer z-index back"); "deactivate sets the layer z-index back");
} }
function test_Handler_Feature_stopHandled(t) {
t.plan(3);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var layer = new OpenLayers.Layer();
map.addLayer(layer);
var handler = new OpenLayers.Handler.Feature(control, layer);
handler.activate();
handler.handle = function(evt) { return /* handled */ true; };
var evtPx = {xy: new OpenLayers.Pixel(Math.random(), Math.random())};
map.events.register("click", map, function(e) {
t.ok(!handler.stopClick, "clicks propagate with stopClick set to false" );
});
map.events.register("mousedown", map, function(e) {
t.ok(!handler.stopDown, "mousedown propagate with stopDown set to false" );
});
map.events.register("mouseup", map, function(e) {
t.ok(!handler.stopUp, "mouseup propagate with stopUp set to false" );
});
// 0 test
map.events.triggerEvent('click', evtPx);
// 0 test
map.events.triggerEvent('mousedown', evtPx);
// 0 test
map.events.triggerEvent('mousedown', evtPx);
// 1 test
handler.stopClick = false;
map.events.triggerEvent('click', evtPx);
// 1 test
handler.stopDown = false;
map.events.triggerEvent('mousedown', evtPx);
// 1 test
handler.stopUp = false;
map.events.triggerEvent('mouseup', evtPx);
// 3 tests total
}
</script> </script>
</head> </head>