diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index 6a84a8b477..6c0f000ea8 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -8,6 +8,7 @@ goog.require('goog.asserts'); goog.require('goog.events'); goog.require('goog.events.Event'); goog.require('goog.functions'); +goog.require('goog.object'); goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); goog.require('ol.array'); @@ -174,6 +175,14 @@ ol.interaction.Select = function(opt_options) { */ this.layerFilter_ = layerFilter; + /** + * An association between selected feature (key) + * and layer (value) + * @private + * @type {Array.} + */ + this.featureLayerAssociation_ = []; + /** * @private * @type {ol.layer.Vector} @@ -200,6 +209,20 @@ ol.interaction.Select = function(opt_options) { goog.inherits(ol.interaction.Select, ol.interaction.Interaction); +/** + * @param {ol.Feature} feature Feature. + * @param {ol.layer.Layer} layer Layer. + * @private + */ +ol.interaction.Select.prototype.addFeatureLayerAssociation_ = + function(feature, layer) { + var key = goog.getUid(feature); + var obj = {}; + obj[key] = layer; + this.featureLayerAssociation_.push(obj); +}; + + /** * Get the selected features. * @return {ol.Collection.} Features collection. @@ -210,6 +233,27 @@ ol.interaction.Select.prototype.getFeatures = function() { }; +/** + * Returns the associated {@link ol.layer.Vector vectorlayer} of + * the (last) selected feature. + * @param {ol.Feature} feature Feature + * @return {ol.layer.Vector} Layer. + * @api + */ +ol.interaction.Select.prototype.getLayer = function(feature) { + goog.asserts.assertInstanceof(feature, ol.Feature, + 'feature should be an ol.Feature'); + var key = goog.getUid(feature).toString(); + var found = goog.array.find(this.featureLayerAssociation_, function(each) { + if (key == goog.object.getKeys(each)[0]) { + return true; + } + return false; + }); + return /** @type {ol.layer.Vector} */ (goog.object.getValues(found)[0]); +}; + + /** * Handles the {@link ol.MapBrowserEvent map browser event} and may change the * selected state of features. @@ -243,6 +287,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { function(feature, layer) { if (this.filter_(feature, layer)) { selected.push(feature); + this.addFeatureLayerAssociation_(feature, layer); return !this.multi_; } }, this, this.layerFilter_); @@ -256,6 +301,16 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { features.clear(); } features.extend(selected); + // Modify array featureLayerAssociation_ + if (selected.length === 0) { + this.featureLayerAssociation_.length = 0; + } else { + if (deselected.length > 0) { + deselected.forEach(function(feature) { + this.removeFeatureLayerAssociation_(feature); + }, this); + } + } } } else { // Modify the currently selected feature(s). @@ -269,11 +324,13 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { if (add || toggle) { if (this.filter_(feature, layer)) { selected.push(feature); + this.addFeatureLayerAssociation_(feature, layer); } } } else { if (remove || toggle) { deselected.push(feature); + this.removeFeatureLayerAssociation_(feature); } } }, this, this.layerFilter_); @@ -360,3 +417,21 @@ ol.interaction.Select.prototype.removeFeature_ = function(evt) { map.unskipFeature(feature); } }; + + +/** + * @param {ol.Feature} feature Feature. + * @private + */ +ol.interaction.Select.prototype.removeFeatureLayerAssociation_ = + function(feature) { + var key = goog.getUid(feature); + var index = goog.array.findIndex(this.featureLayerAssociation_, + function(each) { + if (key == goog.object.getKeys(each)[0]) { + return true; + } + return false; + }); + this.featureLayerAssociation_.splice(index, 1); +};