Refactor L.Path _update and _project into L.Renderer so that no event handling is needed (#5054)

* Refactor L.Path _update and _project into L.Renderer so that no event handling is needed

* Refactor away L.Path's _update event logic
This commit is contained in:
Iván Sánchez Ortega 2016-10-27 09:38:28 +02:00 committed by Per Liedman
parent 9fd0ba15b7
commit 4b8762edf9
4 changed files with 23 additions and 12 deletions

View File

@ -35,8 +35,6 @@ L.Canvas = L.Renderer.extend({
onAdd: function () {
L.Renderer.prototype.onAdd.call(this);
this._layers = this._layers || {};
// Redraw vectors since canvas is cleared upon removal,
// in case of removing the renderer itself from the map.
this._draw();

View File

@ -76,19 +76,10 @@ L.Path = L.Layer.extend({
this._renderer._initPath(this);
this._reset();
this._renderer._addPath(this);
this._renderer.on('update', this._update, this);
},
onRemove: function () {
this._renderer._removePath(this);
this._renderer.off('update', this._update, this);
},
getEvents: function () {
return {
zoomend: this._project,
viewreset: this._reset
};
},
// @method redraw(): this

View File

@ -32,6 +32,7 @@ L.Renderer = L.Layer.extend({
initialize: function (options) {
L.setOptions(this, options);
L.stamp(this);
this._layers = this._layers || {};
},
onAdd: function () {
@ -45,17 +46,20 @@ L.Renderer = L.Layer.extend({
this.getPane().appendChild(this._container);
this._update();
this.on('update', this._updatePaths, this);
},
onRemove: function () {
L.DomUtil.remove(this._container);
this.off('update', this._updatePaths, this);
},
getEvents: function () {
var events = {
viewreset: this._reset,
zoom: this._onZoom,
moveend: this._update
moveend: this._update,
zoomend: this._onZoomEnd
};
if (this._zoomAnimated) {
events.zoomanim = this._onAnimZoom;
@ -91,6 +95,22 @@ L.Renderer = L.Layer.extend({
_reset: function () {
this._update();
this._updateTransform(this._center, this._zoom);
for (var id in this._layers) {
this._layers[id]._reset();
}
},
_onZoomEnd: function () {
for (var id in this._layers) {
this._layers[id]._project();
}
},
_updatePaths: function () {
for (var id in this._layers) {
this._layers[id]._update();
}
},
_update: function () {

View File

@ -99,6 +99,7 @@ L.SVG = L.Renderer.extend({
}
this._updateStyle(layer);
this._layers[L.stamp(layer)] = layer;
},
_addPath: function (layer) {
@ -109,6 +110,7 @@ L.SVG = L.Renderer.extend({
_removePath: function (layer) {
L.DomUtil.remove(layer._path);
layer.removeInteractiveTarget(layer._path);
delete this._layers[L.stamp(layer)];
},
_updatePath: function (layer) {