Add layer also for programmatically selected features
This commit is contained in:
@@ -664,6 +664,25 @@ class PluggableMap extends BaseObject {
|
|||||||
return features;
|
return features;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all layers from all layer groups.
|
||||||
|
* @return {Array<import("./layer/Layer.js").default>} Layers.
|
||||||
|
*/
|
||||||
|
getAllLayers() {
|
||||||
|
const layers = [];
|
||||||
|
function addLayersFrom(layerGroup) {
|
||||||
|
layerGroup.forEach(function (layer) {
|
||||||
|
if (layer instanceof LayerGroup) {
|
||||||
|
addLayersFrom(layer.getLayers());
|
||||||
|
} else {
|
||||||
|
layers.push(layer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
addLayersFrom(this.getLayers());
|
||||||
|
return layers;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detect layers that have a color value at a pixel on the viewport, and
|
* Detect layers that have a color value at a pixel on the viewport, and
|
||||||
* execute a callback with each matching layer. Layers included in the
|
* execute a callback with each matching layer. Layers included in the
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import CollectionEventType from '../CollectionEventType.js';
|
|||||||
import Event from '../events/Event.js';
|
import Event from '../events/Event.js';
|
||||||
import GeometryType from '../geom/GeometryType.js';
|
import GeometryType from '../geom/GeometryType.js';
|
||||||
import Interaction from './Interaction.js';
|
import Interaction from './Interaction.js';
|
||||||
|
import VectorLayer from '../layer/Vector.js';
|
||||||
import {TRUE} from '../functions.js';
|
import {TRUE} from '../functions.js';
|
||||||
import {clear} from '../obj.js';
|
import {clear} from '../obj.js';
|
||||||
import {createEditingStyle} from '../style/Style.js';
|
import {createEditingStyle} from '../style/Style.js';
|
||||||
@@ -308,10 +309,8 @@ class Select extends Interaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the associated {@link module:ol/layer/Vector~Vector vectorlayer} of
|
* Returns the associated {@link module:ol/layer/Vector~Vector vector layer} of
|
||||||
* the (last) selected feature. Note that this will not work with any
|
* a selected feature.
|
||||||
* programmatic method like pushing features to
|
|
||||||
* {@link module:ol/interaction/Select~Select#getFeatures collection}.
|
|
||||||
* @param {import("../Feature.js").FeatureLike} feature Feature
|
* @param {import("../Feature.js").FeatureLike} feature Feature
|
||||||
* @return {import('../layer/Vector.js').default} Layer.
|
* @return {import('../layer/Vector.js').default} Layer.
|
||||||
* @api
|
* @api
|
||||||
@@ -378,6 +377,24 @@ class Select extends Interaction {
|
|||||||
if (this.style_) {
|
if (this.style_) {
|
||||||
this.applySelectedStyle_(feature);
|
this.applySelectedStyle_(feature);
|
||||||
}
|
}
|
||||||
|
if (!this.getLayer(feature)) {
|
||||||
|
const layer = /** @type {VectorLayer} */ (
|
||||||
|
this.getMap()
|
||||||
|
.getAllLayers()
|
||||||
|
.find(function (layer) {
|
||||||
|
if (
|
||||||
|
layer instanceof VectorLayer &&
|
||||||
|
layer.getSource() &&
|
||||||
|
layer.getSource().hasFeature(feature)
|
||||||
|
) {
|
||||||
|
return layer;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
if (layer) {
|
||||||
|
this.addFeatureLayerAssociation_(feature, layer);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -475,8 +492,8 @@ class Select extends Interaction {
|
|||||||
*/
|
*/
|
||||||
function (feature, layer) {
|
function (feature, layer) {
|
||||||
if (this.filter_(feature, layer)) {
|
if (this.filter_(feature, layer)) {
|
||||||
selected.push(feature);
|
|
||||||
this.addFeatureLayerAssociation_(feature, layer);
|
this.addFeatureLayerAssociation_(feature, layer);
|
||||||
|
selected.push(feature);
|
||||||
return !this.multi_;
|
return !this.multi_;
|
||||||
}
|
}
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
@@ -511,8 +528,8 @@ class Select extends Interaction {
|
|||||||
function (feature, layer) {
|
function (feature, layer) {
|
||||||
if (this.filter_(feature, layer)) {
|
if (this.filter_(feature, layer)) {
|
||||||
if ((add || toggle) && !includes(features.getArray(), feature)) {
|
if ((add || toggle) && !includes(features.getArray(), feature)) {
|
||||||
selected.push(feature);
|
|
||||||
this.addFeatureLayerAssociation_(feature, layer);
|
this.addFeatureLayerAssociation_(feature, layer);
|
||||||
|
selected.push(feature);
|
||||||
} else if (
|
} else if (
|
||||||
(remove || toggle) &&
|
(remove || toggle) &&
|
||||||
includes(features.getArray(), feature)
|
includes(features.getArray(), feature)
|
||||||
|
|||||||
@@ -233,6 +233,19 @@ describe('ol/Map', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#getAllLayers()', function () {
|
||||||
|
it('returns all layers, also from inside groups', function () {
|
||||||
|
const map = new Map({});
|
||||||
|
const layer = new TileLayer();
|
||||||
|
const group = new LayerGroup({layers: [layer]});
|
||||||
|
map.addLayer(group);
|
||||||
|
|
||||||
|
const allLayers = map.getAllLayers();
|
||||||
|
expect(allLayers.length).to.be(1);
|
||||||
|
expect(allLayers[0]).to.be(layer);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#setLayers()', function () {
|
describe('#setLayers()', function () {
|
||||||
it('adds an array of layers to the map', function () {
|
it('adds an array of layers to the map', function () {
|
||||||
const map = new Map({});
|
const map = new Map({});
|
||||||
|
|||||||
@@ -375,6 +375,13 @@ describe('ol.interaction.Select', function () {
|
|||||||
// Select again to make sure the style change does not break selection
|
// Select again to make sure the style change does not break selection
|
||||||
simulateEvent('singleclick', 10, -20);
|
simulateEvent('singleclick', 10, -20);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns a layer from a programmatically selected feature', function () {
|
||||||
|
const feature = source.getFeatures()[0];
|
||||||
|
interaction.getFeatures().push(feature);
|
||||||
|
const layerWithSelectedFeature = interaction.getLayer(feature);
|
||||||
|
expect(layerWithSelectedFeature).to.equal(layer);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#setActive()', function () {
|
describe('#setActive()', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user