Skip features in select interaction

This commit is contained in:
Antoine Abt
2014-03-04 09:31:56 +01:00
parent c2c3798087
commit c8de4d908d
2 changed files with 102 additions and 48 deletions

View File

@@ -32,8 +32,8 @@ var select = new ol.interaction.Select({
color: 'rgba(255,0,0,0.3)' color: 'rgba(255,0,0,0.3)'
}), }),
stroke: new ol.style.Stroke({ stroke: new ol.style.Stroke({
color: 'rgba(255,0,0,1)', color: 'rgba(255,0,0,1)',
size: 2 size: 2
}), }),
text: new ol.style.Text({ text: new ol.style.Text({
font: '12px Calibri,sans-serif', font: '12px Calibri,sans-serif',

View File

@@ -1,10 +1,13 @@
goog.provide('ol.interaction.Select'); goog.provide('ol.interaction.Select');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.functions'); goog.require('goog.functions');
goog.require('ol.Feature');
goog.require('ol.FeatureOverlay'); goog.require('ol.FeatureOverlay');
goog.require('ol.events.condition'); goog.require('ol.events.condition');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
goog.require('ol.layer.Vector');
@@ -87,6 +90,12 @@ ol.interaction.Select = function(options) {
style: options.style style: options.style
}); });
/**
* @private
* @type {Object}
*/
this.featuresLayerHash_ = {};
}; };
goog.inherits(ol.interaction.Select, ol.interaction.Interaction); goog.inherits(ol.interaction.Select, ol.interaction.Interaction);
@@ -111,58 +120,103 @@ ol.interaction.Select.prototype.handleMapBrowserEvent =
var add = this.addCondition_(mapBrowserEvent); var add = this.addCondition_(mapBrowserEvent);
var remove = this.removeCondition_(mapBrowserEvent); var remove = this.removeCondition_(mapBrowserEvent);
var toggle = this.toggleCondition_(mapBrowserEvent); var toggle = this.toggleCondition_(mapBrowserEvent);
var set = !add && !remove && !toggle;
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var features = this.featureOverlay_.getFeatures(); var feature = map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
if (set) { /**
// Replace the currently selected feature(s) with the feature at the pixel, * @param {ol.Feature} feature Feature.
// or clear the selected feature(s) if there is no feature at the pixel. * @param {ol.layer.Layer} layer Layer.
/** @type {ol.Feature|undefined} */ */
var feature = map.forEachFeatureAtPixel(mapBrowserEvent.pixel, function(feature, layer) {
/** this.addFeature_(feature, layer, add, remove, toggle);
* @param {ol.Feature} feature Feature. return feature;
* @param {ol.layer.Layer} layer Layer. }, this, this.layerFilter_);
*/ if (!goog.isDef(feature) && !add && !remove) {
function(feature, layer) { this.removeAllFeatures_();
return feature; }
}, undefined, this.layerFilter_); return false;
if (goog.isDef(feature)) { };
if (features.getLength() == 1) {
if (features.getAt(0) !== feature) {
features.setAt(0, feature); /**
* @param {?ol.Feature|undefined} feature Feature.
* @param {ol.layer.Layer} layer Layer.
* @param {Boolean} add Add
* @param {Boolean} remove Remove
* @param {Boolean} toggle Toggle
* @private
* @todo stability experimental
*/
ol.interaction.Select.prototype.addFeature_ = function(feature, layer, add,
remove, toggle) {
var features = this.featureOverlay_.getFeatures(),
hash = this.featuresLayerHash_,
uid, index = -1,
i, ii;
if ((!goog.isDef(feature) || goog.isNull(feature)) && !add) {
this.removeAllFeatures_();
return;
}
goog.asserts.assertInstanceof(feature, ol.Feature);
uid = goog.getUid(feature);
index = features.getArray().indexOf(feature);
if (index == -1) {
if (!add && !remove && (features.getLength() > 0)) {
for (ii = features.getLength() - 1, i = ii; i >= 0; i--) {
if (features.getAt(i) != feature) {
this.removeFeature_(/** @type {ol.Feature} */ (features.getAt(i)));
} }
} else {
if (features.getLength() != 1) {
features.clear();
}
features.push(feature);
}
} else {
if (features.getLength() !== 0) {
features.clear();
} }
} }
} else { } else {
// Modify the currently selected feature(s). if (toggle || remove) {
map.forEachFeatureAtPixel(mapBrowserEvent.pixel, this.removeFeature_(/** @type {ol.Feature} */ (features.getAt(index)));
/** return;
* @param {ol.Feature} feature Feature. }
* @param {ol.layer.Layer} layer Layer. }
*/ if (remove) {
function(feature, layer) { return;
var index = goog.array.indexOf(features.getArray(), feature); }
if (index == -1) { if (index >= 0) {
if (add || toggle) { goog.array.insert(hash[uid], layer);
features.push(feature); return;
} }
} else { features.push(feature);
if (remove || toggle) { goog.asserts.assertInstanceof(layer, ol.layer.Vector);
features.removeAt(index); layer.getSkippedFeatures().push(feature);
} hash[uid] = [layer];
} };
}, undefined, this.layerFilter_);
/**
* @param {ol.Feature} feature Feature.
* @private
* @todo stability experimental
*/
ol.interaction.Select.prototype.removeFeature_ = function(feature) {
var features = this.featureOverlay_.getFeatures(),
hash = this.featuresLayerHash_,
uid = goog.getUid(feature),
i, ii, layer;
features.remove(feature);
for (i = 0, ii = hash[uid].length; i < ii; i++) {
layer = hash[uid][i];
goog.asserts.assertInstanceof(layer, ol.layer.Vector);
layer.getSkippedFeatures().remove(feature);
}
delete hash[uid];
};
/**
* @private
* @todo stability experimental
*/
ol.interaction.Select.prototype.removeAllFeatures_ = function() {
var i, ii,
features = this.featureOverlay_.getFeatures();
for (ii = features.getLength() - 1, i = ii; i >= 0; i--) {
this.removeFeature_(/** @type {ol.Feature} */ (features.getAt(i)));
} }
return false;
}; };