Merge pull request #1915 from twpayne/select-interaction

Restore select interaction behaviour
This commit is contained in:
Tom Payne
2014-03-28 14:07:26 +01:00
2 changed files with 49 additions and 80 deletions

View File

@@ -1,7 +1,6 @@
goog.require('ol.Collection');
goog.require('ol.Map');
goog.require('ol.View2D');
goog.require('ol.events.condition');
goog.require('ol.interaction');
goog.require('ol.interaction.Modify');
goog.require('ol.interaction.Select');
@@ -15,7 +14,6 @@ goog.require('ol.style.Style');
var raster = new ol.layer.Tile({
style: 'Aerial',
source: new ol.source.MapQuest({
layer: 'sat'
})
@@ -31,8 +29,6 @@ var vector = new ol.layer.Vector({
});
var select = new ol.interaction.Select({
addCondition: ol.events.condition.shiftKeyOnly,
toggleCondition: ol.events.condition.always,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#3399CC',

View File

@@ -1,7 +1,6 @@
goog.provide('ol.interaction.Select');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.functions');
goog.require('ol.Feature');
goog.require('ol.FeatureOverlay');
@@ -113,87 +112,61 @@ ol.interaction.Select.prototype.handleMapBrowserEvent =
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 feature = map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/**
* @param {ol.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
*/
function(feature, layer) {
this.addFeature_(feature, add, remove, toggle);
return feature;
}, this, this.layerFilter_);
if (!goog.isDef(feature) && !add && !remove) {
this.removeAllFeatures_();
}
return false;
};
/**
* @param {?ol.Feature|undefined} feature Feature.
* @param {Boolean} add Add
* @param {Boolean} remove Remove
* @param {Boolean} toggle Toggle
* @private
* @todo stability experimental
*/
ol.interaction.Select.prototype.addFeature_ = function(feature, add,
remove, toggle) {
var features = this.featureOverlay_.getFeatures();
var index = -1;
var i, ii;
if ((!goog.isDef(feature) || goog.isNull(feature)) && !add) {
this.removeAllFeatures_();
return;
}
goog.asserts.assertInstanceof(feature, ol.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)));
}
var skippedFeatures = map.getSkippedFeatures();
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,
/**
* @param {ol.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
*/
function(feature, layer) {
return feature;
}, undefined, this.layerFilter_);
if (goog.isDef(feature) &&
features.getLength() == 1 &&
features.getAt(0) == feature) {
// No change
} else {
if (features.getLength() !== 0) {
features.forEach(function(feature) {
skippedFeatures.remove(feature);
});
features.clear();
}
if (goog.isDef(feature)) {
features.push(feature);
skippedFeatures.push(feature);
}
}
} else {
if (toggle || remove) {
this.removeFeature_(/** @type {ol.Feature} */ (features.getAt(index)));
return;
}
}
if (remove) {
return;
}
if (index == -1) {
features.push(feature);
this.getMap().getSkippedFeatures().push(feature);
}
};
/**
* @param {ol.Feature} feature Feature.
* @private
* @todo stability experimental
*/
ol.interaction.Select.prototype.removeFeature_ = function(feature) {
this.featureOverlay_.getFeatures().remove(feature);
this.getMap().getSkippedFeatures().remove(feature);
};
/**
* @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)));
// 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);
skippedFeatures.push(feature);
}
} else {
if (remove || toggle) {
features.removeAt(index);
skippedFeatures.remove(feature);
}
}
}, undefined, this.layerFilter_);
}
return false;
};