Skip to content

Commit a65d2d4

Browse files
committed
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.
1 parent dad58ba commit a65d2d4

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

src/ol/interaction/selectinteraction.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,36 @@ ol.interaction.Select = function(opt_options) {
154154
this.filter_ = options.filter ? options.filter :
155155
goog.functions.TRUE;
156156

157+
var featureOverlay = new ol.layer.Vector({
158+
source: new ol.source.Vector({
159+
useSpatialIndex: false,
160+
features: options.features,
161+
wrapX: options.wrapX
162+
}),
163+
style: options.style ? options.style :
164+
ol.interaction.Select.getDefaultStyleFunction(),
165+
updateWhileAnimating: true,
166+
updateWhileInteracting: true
167+
});
168+
169+
/**
170+
* @private
171+
* @type {ol.layer.Vector}
172+
*/
173+
this.featureOverlay_ = featureOverlay;
174+
157175
var layerFilter;
158176
if (options.layers) {
159177
if (goog.isFunction(options.layers)) {
160-
layerFilter = options.layers;
178+
layerFilter =
179+
/**
180+
* @param {ol.layer.Layer} layer Layer.
181+
* @return {boolean} Include.
182+
*/
183+
function(layer) {
184+
goog.asserts.assertFunction(options.layers);
185+
return layer === featureOverlay || options.layers(layer);
186+
};
161187
} else {
162188
var layers = options.layers;
163189
layerFilter =
@@ -166,7 +192,7 @@ ol.interaction.Select = function(opt_options) {
166192
* @return {boolean} Include.
167193
*/
168194
function(layer) {
169-
return ol.array.includes(layers, layer);
195+
return layer === featureOverlay || ol.array.includes(layers, layer);
170196
};
171197
}
172198
} else {
@@ -187,22 +213,6 @@ ol.interaction.Select = function(opt_options) {
187213
*/
188214
this.featureLayerAssociation_ = {};
189215

190-
/**
191-
* @private
192-
* @type {ol.layer.Vector}
193-
*/
194-
this.featureOverlay_ = new ol.layer.Vector({
195-
source: new ol.source.Vector({
196-
useSpatialIndex: false,
197-
features: options.features,
198-
wrapX: options.wrapX
199-
}),
200-
style: options.style ? options.style :
201-
ol.interaction.Select.getDefaultStyleFunction(),
202-
updateWhileAnimating: true,
203-
updateWhileInteracting: true
204-
});
205-
206216
var features = this.featureOverlay_.getSource().getFeaturesCollection();
207217
goog.events.listen(features, ol.CollectionEventType.ADD,
208218
this.addFeature_, false, this);
@@ -318,9 +328,11 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
318328
* @param {ol.layer.Layer} layer Layer.
319329
*/
320330
function(feature, layer) {
321-
if (!ol.array.includes(features.getArray(), feature)) {
331+
if (layer !== this.featureOverlay_) {
322332
if (add || toggle) {
323-
if (this.filter_(feature, layer)) {
333+
if (this.filter_(feature, layer) &&
334+
!ol.array.includes(features.getArray(), feature) &&
335+
!ol.array.includes(selected, feature)) {
324336
selected.push(feature);
325337
this.addFeatureLayerAssociation_(feature, layer);
326338
}

0 commit comments

Comments
 (0)