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