Include own layer in layerFilter and only select unselected features

When selecting an already selected feature, it will be on the Select
interaction's featureOverlay_. So we need to include that featureOverlay_
in the layer filter, regardless of what the user set as layer filter.

When in toggle mode, we need to make sure that we only select features that
are not already included in the selection.
This commit is contained in:
Andreas Hocevar
2016-01-04 21:36:24 +01:00
parent dad58ba622
commit a65d2d4078

View File

@@ -154,10 +154,36 @@ ol.interaction.Select = function(opt_options) {
this.filter_ = options.filter ? options.filter :
goog.functions.TRUE;
var featureOverlay = new ol.layer.Vector({
source: new ol.source.Vector({
useSpatialIndex: false,
features: options.features,
wrapX: options.wrapX
}),
style: options.style ? options.style :
ol.interaction.Select.getDefaultStyleFunction(),
updateWhileAnimating: true,
updateWhileInteracting: true
});
/**
* @private
* @type {ol.layer.Vector}
*/
this.featureOverlay_ = featureOverlay;
var layerFilter;
if (options.layers) {
if (goog.isFunction(options.layers)) {
layerFilter = options.layers;
layerFilter =
/**
* @param {ol.layer.Layer} layer Layer.
* @return {boolean} Include.
*/
function(layer) {
goog.asserts.assertFunction(options.layers);
return layer === featureOverlay || options.layers(layer);
};
} else {
var layers = options.layers;
layerFilter =
@@ -166,7 +192,7 @@ ol.interaction.Select = function(opt_options) {
* @return {boolean} Include.
*/
function(layer) {
return ol.array.includes(layers, layer);
return layer === featureOverlay || ol.array.includes(layers, layer);
};
}
} else {
@@ -187,22 +213,6 @@ ol.interaction.Select = function(opt_options) {
*/
this.featureLayerAssociation_ = {};
/**
* @private
* @type {ol.layer.Vector}
*/
this.featureOverlay_ = new ol.layer.Vector({
source: new ol.source.Vector({
useSpatialIndex: false,
features: options.features,
wrapX: options.wrapX
}),
style: options.style ? options.style :
ol.interaction.Select.getDefaultStyleFunction(),
updateWhileAnimating: true,
updateWhileInteracting: true
});
var features = this.featureOverlay_.getSource().getFeaturesCollection();
goog.events.listen(features, ol.CollectionEventType.ADD,
this.addFeature_, false, this);
@@ -318,9 +328,11 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
* @param {ol.layer.Layer} layer Layer.
*/
function(feature, layer) {
if (!ol.array.includes(features.getArray(), feature)) {
if (layer !== this.featureOverlay_) {
if (add || toggle) {
if (this.filter_(feature, layer)) {
if (this.filter_(feature, layer) &&
!ol.array.includes(features.getArray(), feature) &&
!ol.array.includes(selected, feature)) {
selected.push(feature);
this.addFeatureLayerAssociation_(feature, layer);
}