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) {
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) {

View File

@ -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) {

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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);
};
},
/*