Add a map property to layers
This commit is contained in:
+55
-1
@@ -5,7 +5,8 @@ import BaseObject from './Object.js';
|
||||
import Collection from './Collection.js';
|
||||
import CollectionEventType from './CollectionEventType.js';
|
||||
import EventType from './events/EventType.js';
|
||||
import LayerGroup from './layer/Group.js';
|
||||
import Layer from './layer/Layer.js';
|
||||
import LayerGroup, {GroupEvent} from './layer/Group.js';
|
||||
import MapBrowserEvent from './MapBrowserEvent.js';
|
||||
import MapBrowserEventHandler from './MapBrowserEventHandler.js';
|
||||
import MapBrowserEventType from './MapBrowserEventType.js';
|
||||
@@ -143,6 +144,36 @@ import {removeNode} from './dom.js';
|
||||
* {@link module:ol/Map~Map#setView}.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {import("./layer/Base.js").default} layer Layer.
|
||||
*/
|
||||
function removeLayerMapProperty(layer) {
|
||||
if (layer instanceof Layer) {
|
||||
layer.setMapInternal(null);
|
||||
return;
|
||||
}
|
||||
if (layer instanceof LayerGroup) {
|
||||
layer.getLayers().forEach(removeLayerMapProperty);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("./layer/Base.js").default} layer Layer.
|
||||
* @param {PluggableMap} map Map.
|
||||
*/
|
||||
function setLayerMapProperty(layer, map) {
|
||||
if (layer instanceof Layer) {
|
||||
layer.setMapInternal(map);
|
||||
return;
|
||||
}
|
||||
if (layer instanceof LayerGroup) {
|
||||
const layers = layer.getLayers().getArray();
|
||||
for (let i = 0, ii = layers.length; i < ii; ++i) {
|
||||
setLayerMapProperty(layers[i], map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @fires import("./MapBrowserEvent.js").MapBrowserEvent
|
||||
* @fires import("./MapEvent.js").MapEvent
|
||||
@@ -524,6 +555,14 @@ class PluggableMap extends BaseObject {
|
||||
layers.push(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("./layer/Group.js").GroupEvent} event The layer add event.
|
||||
* @private
|
||||
*/
|
||||
handleLayerAdd_(event) {
|
||||
setLayerMapProperty(event.layer, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given overlay to the map.
|
||||
* @param {import("./Overlay.js").default} overlay Overlay.
|
||||
@@ -1287,9 +1326,12 @@ class PluggableMap extends BaseObject {
|
||||
}
|
||||
const layerGroup = this.getLayerGroup();
|
||||
if (layerGroup) {
|
||||
this.handleLayerAdd_(new GroupEvent('addlayer', layerGroup));
|
||||
this.layerGroupPropertyListenerKeys_ = [
|
||||
listen(layerGroup, ObjectEventType.PROPERTYCHANGE, this.render, this),
|
||||
listen(layerGroup, EventType.CHANGE, this.render, this),
|
||||
listen(layerGroup, 'addlayer', this.handleLayerAdd_, this),
|
||||
listen(layerGroup, 'removelayer', this.handleLayerRemove_, this),
|
||||
];
|
||||
}
|
||||
this.render();
|
||||
@@ -1370,6 +1412,14 @@ class PluggableMap extends BaseObject {
|
||||
return layers.remove(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("./layer/Group.js").GroupEvent} event The layer remove event.
|
||||
* @private
|
||||
*/
|
||||
handleLayerRemove_(event) {
|
||||
removeLayerMapProperty(event.layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given overlay from the map.
|
||||
* @param {import("./Overlay.js").default} overlay Overlay.
|
||||
@@ -1490,6 +1540,10 @@ class PluggableMap extends BaseObject {
|
||||
* @api
|
||||
*/
|
||||
setLayerGroup(layerGroup) {
|
||||
const oldLayerGroup = this.getLayerGroup();
|
||||
if (oldLayerGroup) {
|
||||
this.handleLayerRemove_(new GroupEvent('removelayer', oldLayerGroup));
|
||||
}
|
||||
this.set(MapProperty.LAYERGROUP, layerGroup);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user