Use add/remove events
This commit is contained in:
@@ -9,6 +9,7 @@ goog.provide('ol.Map');
|
||||
goog.provide('ol.MapProperty');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.dispose');
|
||||
goog.require('goog.dom.ViewportSizeMonitor');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.BrowserEvent');
|
||||
@@ -232,6 +233,16 @@ ol.Map = function(target, opt_values, opt_viewportSizeMonitor) {
|
||||
goog.inherits(ol.Map, ol.Object);
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.addLayer = function(layer) {
|
||||
var layerRenderer = this.createLayerRenderer(layer);
|
||||
this.setLayerRenderer(layer, layerRenderer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
@@ -563,23 +574,12 @@ ol.Map.prototype.handleCenterChanged = function() {
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.handleLayerAdd = function(layer) {
|
||||
var projection = this.getProjection();
|
||||
var storeProjection = layer.getStore().getProjection();
|
||||
var layerRenderer = this.createLayerRenderer(layer);
|
||||
this.setLayerRenderer(layer, layerRenderer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.handleLayerRemove = function(layer) {
|
||||
this.removeLayerRenderer(layer);
|
||||
ol.Map.prototype.handleLayersAdd = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.elem;
|
||||
this.addLayer(layer);
|
||||
};
|
||||
|
||||
|
||||
@@ -587,33 +587,9 @@ ol.Map.prototype.handleLayerRemove = function(layer) {
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.handleLayersInsertAt = function(collectionEvent) {
|
||||
var layers = /** @type {ol.Collection} */ collectionEvent.target;
|
||||
var layer = /** @type {ol.Layer} */ layers.getAt(collectionEvent.index);
|
||||
this.handleLayerAdd(layer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.handleLayersRemoveAt = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.prev;
|
||||
this.handleLayerRemove(layer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.handleLayersSetAt = function(collectionEvent) {
|
||||
var prevLayer = /** @type {ol.Layer} */ collectionEvent.prev;
|
||||
this.handleLayerRemove(prevLayer);
|
||||
var layers = /** @type {ol.Collection} */ collectionEvent.target;
|
||||
var layer = /** @type {ol.Layer} */ layers.getAt(collectionEvent.index);
|
||||
this.handleLayerAdd(layer);
|
||||
ol.Map.prototype.handleLayersRemove = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.elem;
|
||||
this.removeLayer(layer);
|
||||
};
|
||||
|
||||
|
||||
@@ -631,14 +607,12 @@ ol.Map.prototype.handleLayersChanged = function() {
|
||||
}
|
||||
var layers = this.getLayers();
|
||||
if (goog.isDefAndNotNull(layers)) {
|
||||
layers.forEach(this.handleLayerAdd, this);
|
||||
layers.forEach(this.addLayer, this);
|
||||
this.layersListenerKeys_ = [
|
||||
goog.events.listen(layers, ol.CollectionEventType.INSERT_AT,
|
||||
this.handleLayersInsertAt, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.REMOVE_AT,
|
||||
this.handleLayersRemoveAt, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.SET_AT,
|
||||
this.handleLayersSetAt, false, this)
|
||||
goog.events.listen(layers, ol.CollectionEventType.ADD,
|
||||
this.handleLayersAdd, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.REMOVE,
|
||||
this.handleLayersRemove, false, this)
|
||||
];
|
||||
}
|
||||
};
|
||||
@@ -753,6 +727,15 @@ ol.Map.prototype.renderInternal = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @protected
|
||||
*/
|
||||
ol.Map.prototype.removeLayer = function(layer) {
|
||||
goog.dispose(this.removeLayerRenderer(layer));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @return {ol.LayerRenderer} Layer renderer.
|
||||
|
||||
@@ -140,7 +140,7 @@ ol.view.Attribution.prototype.getElement = function() {
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @protected
|
||||
*/
|
||||
ol.view.Attribution.prototype.handleLayerAdd = function(layer) {
|
||||
ol.view.Attribution.prototype.addLayer = function(layer) {
|
||||
|
||||
var layerKey = goog.getUid(layer);
|
||||
|
||||
@@ -171,7 +171,7 @@ ol.view.Attribution.prototype.handleLayerLoad = function(event) {
|
||||
* @param {ol.Layer} layer Layer.
|
||||
* @protected
|
||||
*/
|
||||
ol.view.Attribution.prototype.handleLayerRemove = function(layer) {
|
||||
ol.view.Attribution.prototype.removeLayer = function(layer) {
|
||||
|
||||
var layerKey = goog.getUid(layer);
|
||||
|
||||
@@ -213,10 +213,9 @@ ol.view.Attribution.prototype.handleLayerVisibleChanged = function(event) {
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.view.Attribution.prototype.handleLayersInsertAt = function(collectionEvent) {
|
||||
var layers = /** @type {ol.Collection} */ collectionEvent.target;
|
||||
var layer = /** @type {ol.Layer} */ layers.getAt(collectionEvent.index);
|
||||
this.handleLayerAdd(layer);
|
||||
ol.view.Attribution.prototype.handleLayersAdd = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.elem;
|
||||
this.addLayer(layer);
|
||||
};
|
||||
|
||||
|
||||
@@ -224,22 +223,9 @@ ol.view.Attribution.prototype.handleLayersInsertAt = function(collectionEvent) {
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.view.Attribution.prototype.handleLayersRemoveAt = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.prev;
|
||||
this.handleLayerRemove(layer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.CollectionEvent} collectionEvent Collection event.
|
||||
* @protected
|
||||
*/
|
||||
ol.view.Attribution.prototype.handleLayersSetAt = function(collectionEvent) {
|
||||
var prevLayer = /** @type {ol.Layer} */ collectionEvent.prev;
|
||||
this.handleLayerRemove(prevLayer);
|
||||
var layers = /** @type {ol.Collection} */ collectionEvent.target;
|
||||
var layer = /** @type {ol.Layer} */ layers.getAt(collectionEvent.index);
|
||||
this.handleLayerAdd(layer);
|
||||
ol.view.Attribution.prototype.handleLayersRemove = function(collectionEvent) {
|
||||
var layer = /** @type {ol.Layer} */ collectionEvent.elem;
|
||||
this.removeLayer(layer);
|
||||
};
|
||||
|
||||
|
||||
@@ -274,14 +260,12 @@ ol.view.Attribution.prototype.handleMapLayersChanged = function() {
|
||||
var map = this.getMap();
|
||||
var layers = map.getLayers();
|
||||
if (goog.isDefAndNotNull(layers)) {
|
||||
layers.forEach(this.handleLayerAdd, this);
|
||||
layers.forEach(this.addLayer, this);
|
||||
this.layersListenerKeys_ = [
|
||||
goog.events.listen(layers, ol.CollectionEventType.INSERT_AT,
|
||||
this.handleLayersInsertAt, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.REMOVE_AT,
|
||||
this.handleLayersRemoveAt, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.SET_AT,
|
||||
this.handleLayersSetAt, false, this)
|
||||
goog.events.listen(layers, ol.CollectionEventType.ADD,
|
||||
this.handleLayersAdd, false, this),
|
||||
goog.events.listen(layers, ol.CollectionEventType.REMOVE,
|
||||
this.handleLayersRemove, false, this)
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -237,6 +237,17 @@ ol.webgl.Map = function(target, opt_values) {
|
||||
goog.inherits(ol.webgl.Map, ol.Map);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.webgl.Map.prototype.addLayer = function(layer) {
|
||||
goog.base(this, 'addLayer', layer);
|
||||
if (layer.getVisible()) {
|
||||
this.render();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -394,17 +405,6 @@ ol.webgl.Map.prototype.handleCenterChanged = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.webgl.Map.prototype.handleLayerAdd = function(layer) {
|
||||
goog.base(this, 'handleLayerAdd', layer);
|
||||
if (layer.getVisible()) {
|
||||
this.render();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {goog.events.Event} event Event.
|
||||
* @protected
|
||||
@@ -414,17 +414,6 @@ ol.webgl.Map.prototype.handleLayerRendererChange = function(event) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.webgl.Map.prototype.handleLayerRemove = function(layer) {
|
||||
goog.base(this, 'handleLayerRemove', layer);
|
||||
if (layer.getVisible()) {
|
||||
this.render();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -573,6 +562,17 @@ ol.webgl.Map.prototype.renderInternal = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.webgl.Map.prototype.removeLayer = function(layer) {
|
||||
goog.base(this, 'removeLayer', layer);
|
||||
if (layer.getVisible()) {
|
||||
this.render();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user