From 1f0c21ac6002e091548ba168f73811c2ad8cfab1 Mon Sep 17 00:00:00 2001 From: acanimal Date: Thu, 26 Mar 2015 09:35:39 +0100 Subject: [PATCH] Add "filter" option to Select interaction --- externs/olx.js | 11 ++++++++++- src/ol/interaction/selectinteraction.js | 26 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index bb863598be..5686485536 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2584,7 +2584,8 @@ olx.interaction.PointerOptions.prototype.handleUpEvent; * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * removeCondition: (ol.events.ConditionType|undefined), * toggleCondition: (ol.events.ConditionType|undefined), - * multi: (boolean|undefined)}} + * multi: (boolean|undefined), + * filter: (ol.interaction.SelectFilterFunction|undefined)}} * @api */ olx.interaction.SelectOptions; @@ -2670,6 +2671,14 @@ olx.interaction.SelectOptions.prototype.toggleCondition; */ olx.interaction.SelectOptions.prototype.multi; +/** + * A function that takes an {@link ol.Feature} and an {@link ol.layer.Layer} and + * returns `true` if the feature may be selected or `false` otherwise. + * @type {ol.interaction.SelectFilterFunction|undefined} + * @api + */ +olx.interaction.SelectOptions.prototype.filter; + /** * Namespace. diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index c205f65f23..9e1c16a638 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -27,6 +27,15 @@ ol.SelectEventType = { }; +/** + * A function that takes an {@link ol.Feature} and an {@link ol.layer.Layer} + * and returns `true` if the feature may be selected or `false` otherwise. + * @typedef {function(ol.Feature, ol.layer.Layer): boolean} + * @api + */ +ol.interaction.SelectFilterFunction; + + /** * @classdesc @@ -115,6 +124,13 @@ ol.interaction.Select = function(opt_options) { */ this.multi_ = goog.isDef(options.multi) ? options.multi : false; + /** + * @private + * @type {ol.interaction.SelectFilterFunction} + */ + this.filter_ = goog.isDef(options.filter) ? options.filter : + goog.functions.TRUE; + var layerFilter; if (goog.isDef(options.layers)) { if (goog.isFunction(options.layers)) { @@ -198,7 +214,9 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { * @param {ol.layer.Layer} layer Layer. */ function(feature, layer) { - selected.push(feature); + if (this.filter_(feature, layer)) { + selected.push(feature); + } return !this.multi_; }, this, this.layerFilter_); if (selected.length > 0 && features.getLength() == 1 && @@ -223,14 +241,16 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { var index = goog.array.indexOf(features.getArray(), feature); if (index == -1) { if (add || toggle) { - selected.push(feature); + if (this.filter_(feature, layer)) { + selected.push(feature); + } } } else { if (remove || toggle) { deselected.push(feature); } } - }, undefined, this.layerFilter_); + }, this, this.layerFilter_); var i; for (i = deselected.length - 1; i >= 0; --i) { features.remove(deselected[i]);