add Layer getEvents and manage listeners on map automatically

This commit is contained in:
Vladimir Agafonkin 2013-12-05 21:46:08 +02:00
parent 64320fe3fa
commit 6e0e006209
6 changed files with 38 additions and 39 deletions

View File

@ -16,7 +16,7 @@ L.ImageOverlay = L.Layer.extend({
}, },
onAdd: function (map) { onAdd: function (map) {
this._animated = this._map.options.zoomAnimation && L.Browser.any3d; this._animated = map.options.zoomAnimation && L.Browser.any3d;
if (!this._image) { if (!this._image) {
this._initImage(); this._initImage();
@ -24,15 +24,11 @@ L.ImageOverlay = L.Layer.extend({
this.getPane().appendChild(this._image); this.getPane().appendChild(this._image);
map.on(this._getEvents(), this);
this._reset(); this._reset();
}, },
onRemove: function (map) { onRemove: function () {
L.DomUtil.remove(this._image); L.DomUtil.remove(this._image);
map.off(this._getEvents(), this);
}, },
setOpacity: function (opacity) { setOpacity: function (opacity) {
@ -66,7 +62,7 @@ L.ImageOverlay = L.Layer.extend({
return this.options.attribution; return this.options.attribution;
}, },
_getEvents: function () { getEvents: function () {
var events = {viewreset: this._reset}; var events = {viewreset: this._reset};
if (this._animated) { if (this._animated) {

View File

@ -23,15 +23,23 @@ L.Layer = L.Class.extend({
}, },
_layerAdd: function () { _layerAdd: function () {
// check in case layer gets added and then removed before the map is ready var map = this._map;
if (!this._map) { return; }
// 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.fire('add');
this._map.fire('layeradd', {layer: this}); map.fire('layeradd', {layer: this});
}, },
removeFrom: function (map) { removeFrom: function (map) {
var id = L.stamp(this); var id = L.stamp(this);
if (!map._layers[id]) { return this; } if (!map._layers[id]) { return this; }
@ -39,6 +47,10 @@ L.Layer = L.Class.extend({
this.onRemove(map); this.onRemove(map);
} }
if (this.getEvents) {
map.off(this.getEvents(), this);
}
delete map._layers[id]; delete map._layers[id];
if (map._loaded) { if (map._loaded) {

View File

@ -46,7 +46,6 @@ L.Popup = L.Layer.extend({
} }
this.getPane().appendChild(this._container); this.getPane().appendChild(this._container);
map.on(this._getEvents(), this);
this.update(); this.update();
@ -74,8 +73,6 @@ L.Popup = L.Layer.extend({
L.DomUtil.remove(this._container); L.DomUtil.remove(this._container);
} }
map.off(this._getEvents(), this);
map.fire('popupclose', {popup: this}); map.fire('popupclose', {popup: this});
if (this._source) { if (this._source) {
@ -120,7 +117,7 @@ L.Popup = L.Layer.extend({
this._adjustPan(); this._adjustPan();
}, },
_getEvents: function () { getEvents: function () {
var events = {viewreset: this._updatePosition}, var events = {viewreset: this._updatePosition},
options = this.options; options = this.options;

View File

@ -25,28 +25,27 @@ L.Marker = L.Layer.extend({
onAdd: function (map) { onAdd: function (map) {
this._animated = map.options.zoomAnimation && map.options.markerZoomAnimation; this._animated = map.options.zoomAnimation && map.options.markerZoomAnimation;
map.on('viewreset', this.update, this);
this._initIcon(); this._initIcon();
this.update(); this.update();
if (this._animated) {
map.on('zoomanim', this._animateZoom, this);
}
}, },
onRemove: function (map) { onRemove: function () {
if (this.dragging) { if (this.dragging) {
this.dragging.disable(); this.dragging.disable();
} }
this._removeIcon(); this._removeIcon();
this._removeShadow(); this._removeShadow();
},
map.off({ getEvents: function () {
'viewreset': this.update, var events = {viewreset: this.update};
'zoomanim': this._animateZoom
}, this); if (this._animated) {
events.zoomanim = this._animateZoom;
}
return events;
}, },
getLatLng: function () { getLatLng: function () {
@ -83,6 +82,7 @@ L.Marker = L.Layer.extend({
}, },
update: function () { update: function () {
if (this._icon) { if (this._icon) {
var pos = this._map.latLngToLayerPoint(this._latlng).round(); var pos = this._map.latLngToLayerPoint(this._latlng).round();
this._setPos(pos); this._setPos(pos);

View File

@ -37,8 +37,6 @@ L.GridLayer = L.Layer.extend({
this._update = L.Util.limitExecByInterval(this._update, this.options.updateInterval, this); this._update = L.Util.limitExecByInterval(this._update, this.options.updateInterval, this);
} }
map.on(this._getEvents(), this);
this._reset(); this._reset();
this._update(); this._update();
}, },
@ -51,7 +49,6 @@ L.GridLayer = L.Layer.extend({
this._clearBgBuffer(); this._clearBgBuffer();
L.DomUtil.remove(this._container); L.DomUtil.remove(this._container);
map.off(this._getEvents(), this);
map._removeZoomLimit(this); map._removeZoomLimit(this);
this._container = null; this._container = null;
@ -107,7 +104,7 @@ L.GridLayer = L.Layer.extend({
return this; return this;
}, },
_getEvents: function () { getEvents: function () {
var events = { var events = {
viewreset: this._reset, viewreset: this._reset,
moveend: this._update moveend: this._update

View File

@ -37,7 +37,7 @@ L.Path = L.Layer.extend({
L.setOptions(this, options); L.setOptions(this, options);
}, },
onAdd: function (map) { onAdd: function () {
if (!this._container) { if (!this._container) {
this._initElements(); this._initElements();
@ -52,14 +52,9 @@ L.Path = L.Layer.extend({
if (this._container) { if (this._container) {
this._map._pathRoot.appendChild(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); L.DomUtil.remove(this._container);
// TODO move to Path.VML // TODO move to Path.VML
@ -68,11 +63,13 @@ L.Path = L.Layer.extend({
this._stroke = null; this._stroke = null;
this._fill = null; this._fill = null;
} }
},
map.off({ getEvents: function () {
return {
viewreset: this.projectLatlngs, viewreset: this.projectLatlngs,
moveend: this._updatePath moveend: this._updatePath
}, this); };
}, },
/* /*