From 6e0e00620969bec170fa958f4de9fcaebb36f8b7 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 5 Dec 2013 21:46:08 +0200 Subject: [PATCH] add Layer getEvents and manage listeners on map automatically --- src/layer/ImageOverlay.js | 10 +++------- src/layer/Layer.js | 20 ++++++++++++++++---- src/layer/Popup.js | 5 +---- src/layer/marker/Marker.js | 22 +++++++++++----------- src/layer/tile/GridLayer.js | 5 +---- src/layer/vector/Path.js | 15 ++++++--------- 6 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/layer/ImageOverlay.js b/src/layer/ImageOverlay.js index fa94c0b0..c8ebf75b 100644 --- a/src/layer/ImageOverlay.js +++ b/src/layer/ImageOverlay.js @@ -16,7 +16,7 @@ L.ImageOverlay = L.Layer.extend({ }, onAdd: function (map) { - this._animated = this._map.options.zoomAnimation && L.Browser.any3d; + this._animated = map.options.zoomAnimation && L.Browser.any3d; if (!this._image) { this._initImage(); @@ -24,15 +24,11 @@ L.ImageOverlay = L.Layer.extend({ this.getPane().appendChild(this._image); - map.on(this._getEvents(), this); - this._reset(); }, - onRemove: function (map) { + onRemove: function () { L.DomUtil.remove(this._image); - - map.off(this._getEvents(), this); }, setOpacity: function (opacity) { @@ -66,7 +62,7 @@ L.ImageOverlay = L.Layer.extend({ return this.options.attribution; }, - _getEvents: function () { + getEvents: function () { var events = {viewreset: this._reset}; if (this._animated) { diff --git a/src/layer/Layer.js b/src/layer/Layer.js index 4c9f9b99..25b803ef 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -23,15 +23,23 @@ L.Layer = L.Class.extend({ }, _layerAdd: function () { - // check in case layer gets added and then removed before the map is ready - if (!this._map) { return; } + var map = this._map; + + // check in case layer gets added and then removed before the map is ready + if (!map) { return; } + + this.onAdd(map); + + if (this.getEvents) { + map.on(this.getEvents(), this); + } - this.onAdd(this._map); this.fire('add'); - this._map.fire('layeradd', {layer: this}); + map.fire('layeradd', {layer: this}); }, removeFrom: function (map) { + var id = L.stamp(this); if (!map._layers[id]) { return this; } @@ -39,6 +47,10 @@ L.Layer = L.Class.extend({ this.onRemove(map); } + if (this.getEvents) { + map.off(this.getEvents(), this); + } + delete map._layers[id]; if (map._loaded) { diff --git a/src/layer/Popup.js b/src/layer/Popup.js index 14da2bd1..3d199372 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -46,7 +46,6 @@ L.Popup = L.Layer.extend({ } this.getPane().appendChild(this._container); - map.on(this._getEvents(), this); this.update(); @@ -74,8 +73,6 @@ L.Popup = L.Layer.extend({ L.DomUtil.remove(this._container); } - map.off(this._getEvents(), this); - map.fire('popupclose', {popup: this}); if (this._source) { @@ -120,7 +117,7 @@ L.Popup = L.Layer.extend({ this._adjustPan(); }, - _getEvents: function () { + getEvents: function () { var events = {viewreset: this._updatePosition}, options = this.options; diff --git a/src/layer/marker/Marker.js b/src/layer/marker/Marker.js index 1fa1bd91..1eabdea1 100644 --- a/src/layer/marker/Marker.js +++ b/src/layer/marker/Marker.js @@ -25,28 +25,27 @@ L.Marker = L.Layer.extend({ onAdd: function (map) { this._animated = map.options.zoomAnimation && map.options.markerZoomAnimation; - map.on('viewreset', this.update, this); - this._initIcon(); this.update(); - - if (this._animated) { - map.on('zoomanim', this._animateZoom, this); - } }, - onRemove: function (map) { + onRemove: function () { if (this.dragging) { this.dragging.disable(); } this._removeIcon(); this._removeShadow(); + }, - map.off({ - 'viewreset': this.update, - 'zoomanim': this._animateZoom - }, this); + getEvents: function () { + var events = {viewreset: this.update}; + + if (this._animated) { + events.zoomanim = this._animateZoom; + } + + return events; }, getLatLng: function () { @@ -83,6 +82,7 @@ L.Marker = L.Layer.extend({ }, update: function () { + if (this._icon) { var pos = this._map.latLngToLayerPoint(this._latlng).round(); this._setPos(pos); diff --git a/src/layer/tile/GridLayer.js b/src/layer/tile/GridLayer.js index 0d47a9f5..79955de9 100644 --- a/src/layer/tile/GridLayer.js +++ b/src/layer/tile/GridLayer.js @@ -37,8 +37,6 @@ L.GridLayer = L.Layer.extend({ this._update = L.Util.limitExecByInterval(this._update, this.options.updateInterval, this); } - map.on(this._getEvents(), this); - this._reset(); this._update(); }, @@ -51,7 +49,6 @@ L.GridLayer = L.Layer.extend({ this._clearBgBuffer(); L.DomUtil.remove(this._container); - map.off(this._getEvents(), this); map._removeZoomLimit(this); this._container = null; @@ -107,7 +104,7 @@ L.GridLayer = L.Layer.extend({ return this; }, - _getEvents: function () { + getEvents: function () { var events = { viewreset: this._reset, moveend: this._update diff --git a/src/layer/vector/Path.js b/src/layer/vector/Path.js index ad7fe9f4..263b7a4f 100644 --- a/src/layer/vector/Path.js +++ b/src/layer/vector/Path.js @@ -37,7 +37,7 @@ L.Path = L.Layer.extend({ L.setOptions(this, options); }, - onAdd: function (map) { + onAdd: function () { if (!this._container) { this._initElements(); @@ -52,14 +52,9 @@ L.Path = L.Layer.extend({ if (this._container) { this._map._pathRoot.appendChild(this._container); } - - map.on({ - viewreset: this.projectLatlngs, - moveend: this._updatePath - }, this); }, - onRemove: function (map) { + onRemove: function () { L.DomUtil.remove(this._container); // TODO move to Path.VML @@ -68,11 +63,13 @@ L.Path = L.Layer.extend({ this._stroke = null; this._fill = null; } + }, - map.off({ + getEvents: function () { + return { viewreset: this.projectLatlngs, moveend: this._updatePath - }, this); + }; }, /*