Simplifying layer mapping

By making selectionLayer an object keyed by source layer UIDs,
we save some indexOf lookups.
This commit is contained in:
ahocevar
2013-08-21 18:59:08 +02:00
parent d5c0fdd557
commit a417b75c1f
2 changed files with 27 additions and 25 deletions
+18 -16
View File
@@ -47,7 +47,7 @@ ol.control.Select = function(opt_options) {
this.featureMap_ = []; this.featureMap_ = [];
/** /**
* @type {Array.<ol.layer.Vector>} * @type {Object.<*, ol.layer.Vector>}
* @protected * @protected
*/ */
this.selectionLayers; this.selectionLayers;
@@ -91,7 +91,7 @@ goog.inherits(ol.control.Select, ol.control.Control);
* @private * @private
*/ */
ol.control.Select.prototype.createSelectionLayers_ = function() { ol.control.Select.prototype.createSelectionLayers_ = function() {
this.selectionLayers = []; this.selectionLayers = {};
for (var i = 0, ii = this.layers_.length; i < ii; ++i) { for (var i = 0, ii = this.layers_.length; i < ii; ++i) {
this.featureMap_.push({}); this.featureMap_.push({});
var layer = this.layers_[i]; var layer = this.layers_[i];
@@ -101,7 +101,7 @@ ol.control.Select.prototype.createSelectionLayers_ = function() {
}); });
selectionLayer.setTemporary(true); selectionLayer.setTemporary(true);
selectionLayer.bindTo('visible', layer); selectionLayer.bindTo('visible', layer);
this.selectionLayers.push(selectionLayer); this.selectionLayers[goog.getUid(layer)] = selectionLayer;
} }
}; };
@@ -129,7 +129,7 @@ ol.control.Select.prototype.activate = function() {
this.active_ = true; this.active_ = true;
goog.dom.classes.add(this.element, 'active'); goog.dom.classes.add(this.element, 'active');
var map = this.getMap(); var map = this.getMap();
for (var i = 0, ii = this.selectionLayers.length; i < ii; ++i) { for (var i in this.selectionLayers) {
map.addLayer(this.selectionLayers[i]); map.addLayer(this.selectionLayers[i]);
} }
@@ -151,7 +151,7 @@ ol.control.Select.prototype.deactivate = function() {
this.listenerKeys.length = 0; this.listenerKeys.length = 0;
} }
var map = this.getMap(); var map = this.getMap();
for (var i = 0, ii = this.selectionLayers.length; i < ii; ++i) { for (var i in this.selectionLayers) {
map.removeLayer(this.selectionLayers[i]); map.removeLayer(this.selectionLayers[i]);
} }
goog.dom.classes.remove(this.element, 'active'); goog.dom.classes.remove(this.element, 'active');
@@ -164,15 +164,16 @@ ol.control.Select.prototype.deactivate = function() {
* @param {ol.MapBrowserEvent} evt Event. * @param {ol.MapBrowserEvent} evt Event.
*/ */
ol.control.Select.prototype.handleClick = function(evt) { ol.control.Select.prototype.handleClick = function(evt) {
var layers = goog.array.filter(this.layers_, this.layerFilterFunction, this);
var clear = !ol.interaction.condition.shiftKeyOnly(evt.browserEvent); var clear = !ol.interaction.condition.shiftKeyOnly(evt.browserEvent);
function select(featuresByLayer) { function select(featuresByLayer) {
this.select(featuresByLayer, clear); this.select(featuresByLayer, layers, clear);
} }
var map = this.getMap(); var map = this.getMap();
map.getFeatures({ map.getFeatures({
layers: goog.array.filter(this.layers_, this.layerFilterFunction, this), layers: layers,
pixel: evt.getPixel(), pixel: evt.getPixel(),
success: goog.bind(select, this) success: goog.bind(select, this)
}); });
@@ -183,11 +184,10 @@ ol.control.Select.prototype.handleClick = function(evt) {
* @param {ol.CollectionEvent} evt Event. * @param {ol.CollectionEvent} evt Event.
*/ */
ol.control.Select.prototype.handleLayerCollectionChange = function(evt) { ol.control.Select.prototype.handleLayerCollectionChange = function(evt) {
var layer = evt.elem; var layer = /** @type {ol.layer.Layer} */ (evt.elem);
var index = goog.array.indexOf(this.layers_, layer); var selectionLayer = this.selectionLayers[goog.getUid(layer)];
if (index !== -1) { if (goog.isDef(selectionLayer)) {
this.selectionLayers[index].setVisible( selectionLayer.setVisible(evt.type === ol.CollectionEventType.ADD);
evt.type === ol.CollectionEventType.ADD);
} }
}; };
@@ -198,18 +198,20 @@ ol.control.Select.prototype.handleLayerCollectionChange = function(evt) {
* @return {boolean} Whether to include the layer. * @return {boolean} Whether to include the layer.
*/ */
ol.control.Select.prototype.layerFilterFunction = function(layer, index) { ol.control.Select.prototype.layerFilterFunction = function(layer, index) {
return this.selectionLayers[index].getVisible(); return this.selectionLayers[goog.getUid(layer)].getVisible();
}; };
/** /**
* @param {Array.<Array.<ol.Feature>>} featuresByLayer Features by layer. * @param {Array.<Array.<ol.Feature>>} featuresByLayer Features by layer.
* @param {Array.<ol.layer.Layer>} layers The queried layers.
* @param {boolean} clear Whether the current layer content should be cleared. * @param {boolean} clear Whether the current layer content should be cleared.
*/ */
ol.control.Select.prototype.select = function(featuresByLayer, clear) { ol.control.Select.prototype.select = function(featuresByLayer, layers, clear) {
for (var i = 0, ii = featuresByLayer.length; i < ii; ++i) { for (var i = 0, ii = featuresByLayer.length; i < ii; ++i) {
var layer = this.layers_[i]; var layer = layers[i];
var selectionLayer = this.selectionLayers[i]; var selectionLayer =
this.selectionLayers[goog.getUid(layer)];
var features = featuresByLayer[i]; var features = featuresByLayer[i];
var numFeatures = features.length; var numFeatures = features.length;
var selectedFeatures = []; var selectedFeatures = [];
+9 -9
View File
@@ -97,24 +97,24 @@ describe('ol.control.Select', function() {
describe('#select', function() { describe('#select', function() {
it('toggles selection of features', function() { it('toggles selection of features', function() {
var layer = select.selectionLayers[0]; var layer = select.selectionLayers[goog.getUid(select.layers_[0])];
select.select([features]); select.select([features], select.layers_);
expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(2); expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(2);
select.select([features]); select.select([features], select.layers_);
expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(0); expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(0);
}); });
it('can append features to an existing selection', function() { it('can append features to an existing selection', function() {
var layer = select.selectionLayers[0]; var layer = select.selectionLayers[goog.getUid(select.layers_[0])];
select.select([[features[0]]]); select.select([[features[0]]], select.layers_);
select.select([[features[1]]]); select.select([[features[1]]], select.layers_);
expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(2); expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(2);
}); });
it('can clear a selection before selecting new features', function() { it('can clear a selection before selecting new features', function() {
var layer = select.selectionLayers[0]; var layer = select.selectionLayers[goog.getUid(select.layers_[0])];
select.select([[features[0]]], true); select.select([[features[0]]], select.layers_, true);
select.select([[features[1]]], true); select.select([[features[1]]], select.layers_, true);
expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(1); expect(goog.object.getCount(layer.featureCache_.idLookup_)).to.be(1);
}); });