From 395f1222a1cca537b204373fce7be51fe6f6e466 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 17 Mar 2014 16:43:41 +0100 Subject: [PATCH] Add remove and toggle options to ol.interaction.Select --- examples/select-features.html | 2 +- src/objectliterals.jsdoc | 13 ++++-- src/ol/interaction/selectinteraction.js | 53 +++++++++++++++++++------ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/examples/select-features.html b/examples/select-features.html index b2d66b34b3..8e3905f067 100644 --- a/examples/select-features.html +++ b/examples/select-features.html @@ -33,7 +33,7 @@

Select features example

-

Example of using the Select interaction. Select features by clicking polygons. Hold the Shift-key to add to the selection.

+

Example of using the Select interaction. Select features by clicking polygons. Hold the Shift-key to toggle the feature in the selection.

See the select-features.js source to see how this is done.

diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index dbf8ab8536..75fd408663 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -515,9 +515,9 @@ /** * @typedef {Object} olx.interaction.SelectOptions * @property {ol.events.ConditionType|undefined} addCondition A conditional - * modifier (e.g. shift key) that determines if the selection is added to - * the current selection. By default, a shift-click adds to the current - * selection. + * modifier (e.g. alt key) that determines if the feature is added to + * the current selection. By default, this is never. Note that the default + * toggle condition allows features to be added. * @property {ol.events.ConditionType|undefined} condition A conditional * modifier (e.g. shift key) that determines if the interaction is active * (i.e. selection occurs) or not. By default, a click with no modifier keys @@ -529,6 +529,13 @@ * @property {Array.|undefined} layers Layers. Zero or more * layers from which features should be selected. * @property {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} style FeatureOverlay style. + * @property {ol.events.ConditionType|undefined} removeCondition A conditional + * modifier (e.g. alt key) that determines if the feature is removed from + * the current selection. By default, this is never. + * @property {ol.events.ConditionType|undefined} toggleCondition A conditional + * modifier (e.g. shift key) that determines if the selection is toggled in + * the current selection. By default, a shift-click toggles the feature in + * the current selection. */ /** diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index 4c9e85d63b..4fd1b5e7c6 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -30,7 +30,21 @@ ol.interaction.Select = function(options) { * @type {ol.events.ConditionType} */ this.addCondition_ = goog.isDef(options.addCondition) ? - options.addCondition : ol.events.condition.shiftKeyOnly; + options.addCondition : ol.events.condition.never; + + /** + * @private + * @type {ol.events.ConditionType} + */ + this.removeCondition_ = goog.isDef(options.removeCondition) ? + options.removeCondition : ol.events.condition.never; + + /** + * @private + * @type {ol.events.ConditionType} + */ + this.toggleCondition_ = goog.isDef(options.toggleCondition) ? + options.toggleCondition : ol.events.condition.shiftKeyOnly; var layerFilter; if (goog.isDef(options.layerFilter)) { @@ -95,20 +109,14 @@ ol.interaction.Select.prototype.handleMapBrowserEvent = return true; } var add = this.addCondition_(mapBrowserEvent); + var remove = this.removeCondition_(mapBrowserEvent); + var toggle = this.toggleCondition_(mapBrowserEvent); + var set = !add && !remove && !toggle; var map = mapBrowserEvent.map; var features = this.featureOverlay_.getFeatures(); - if (add) { - map.forEachFeatureAtPixel(mapBrowserEvent.pixel, - /** - * @param {ol.Feature} feature Feature. - * @param {ol.layer.Layer} layer Layer. - */ - function(feature, layer) { - if (goog.array.indexOf(features.getArray(), feature) == -1) { - features.push(feature); - } - }, undefined, this.layerFilter_); - } else { + if (set) { + // Replace the currently selected feature(s) with the feature at the pixel, + // or clear the selected feature(s) if there is no feature at the pixel. /** @type {ol.Feature|undefined} */ var feature = map.forEachFeatureAtPixel(mapBrowserEvent.pixel, /** @@ -134,6 +142,25 @@ ol.interaction.Select.prototype.handleMapBrowserEvent = features.clear(); } } + } else { + // Modify the currently selected feature(s). + map.forEachFeatureAtPixel(mapBrowserEvent.pixel, + /** + * @param {ol.Feature} feature Feature. + * @param {ol.layer.Layer} layer Layer. + */ + function(feature, layer) { + var index = goog.array.indexOf(features.getArray(), feature); + if (index == -1) { + if (add || toggle) { + features.push(feature); + } + } else { + if (remove || toggle) { + features.removeAt(index); + } + } + }, undefined, this.layerFilter_); } return false; };