From a65d2d40782b1884fdf3b4347410f68eea4bca71 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Mon, 4 Jan 2016 21:36:24 +0100 Subject: [PATCH] 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. --- src/ol/interaction/selectinteraction.js | 52 +++++++++++++++---------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index 7ef21263c6..872dc7f23b 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -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); }