[OpenLayers-Users] SelectFeature unselecting programmatically
Slawomir Messner
slawomir.messner at staff.uni-marburg.de
Fri Dec 7 04:24:19 PST 2012
Hi,
when you use select/unselect function of SelectFeature control why
this.handlers.feature.lastFeature is set in select function but not in
unset in unselect? It's not just the asymmetry which bother me, it is
also that it would solve an issue in the following scenario:
We have a RemoveFeature control which can also remove vertices of
polygons and lines.
We use a SelectFeature to select a feature. On select it draws edit
vertices on the edges, like the ModifyFeature control and a Feature
handler handles the click on the vertices and removes it form layer and
the geometry. After removing it redraws the feature and all seams well,
the feature is correctly drawn, the vertex is gone and it's selected but
unfortunately clickout doesn't work(it works when you select and
clickout without deleting vertex). After some investigation I found out
that lastFeature of the feature handler in SelectFeature control is the
deleted vertex. So on click outside the feature the handler runs handle
method where it sets lastFeature to null, because it is removed from layer.
if(this.lastFeature && !this.lastFeature.layer) {
// last feature has been destroyed
this.lastFeature = null;
}
so it never gets some lines below:
} else {
if(this.lastFeature && (previouslyIn || click)) {
this.triggerCallback(type, 'out', [this.lastFeature]);
}
}
there it should call the clickout callback. So I tried it with unselect
and than select the feature programmatically after deleting the vertex,
to reset the select, but this didn't help. The only thing that helps is
when I set the lastFeature to null before reselect or to set it to the
feature without . Why not "simple" do something like:
"this.selectFeature.handlers.feature.lastFeature = this.feature"
because it looks ugly, uses some attributes which should be private and
I saw the asymmetry in the code of the SelectFeature and asked myself if
it's a bug or a feature.
Is there something why not change the unselect method of SelectFeature to:
unselect: function(feature) {
var layer = feature.layer;
// Store feature style for restoration later
this.unhighlight(feature);
// if the feature handler isn't involved in the feature
// selection (because the feature is selected programatically)
// we fake the
// feature handler to allow unselecting on clickout
if(this.handlers.feature.lastFeature) {
this.handlers.feature.lastFeature = null;
}
OpenLayers.Util.removeItem(layer.selectedFeatures, feature);
layer.events.triggerEvent("featureunselected", {feature: feature});
this.onUnselect.call(this.scope, feature);
},
It doesn't crash the tests.
Btw:
I think there is a documentation error in OL.Handler.Feauture deactivate
function it should be:
/**
* Method: deactivate
* Turn off the handler. Returns false if the handler was not active.
*
* Returns:
* {Boolean}
*/
Keep up the excellent work guys, we look forward to OL3.
Regards
Slawomir
More information about the Users
mailing list