move Layer-related Map code from Map.js to Layer.js

This commit is contained in:
Vladimir Agafonkin 2013-12-06 13:56:38 +02:00
parent d55ffb07ec
commit 7f03570b8d
3 changed files with 77 additions and 78 deletions

View File

@ -17,8 +17,9 @@ var deps = {
'geo/crs/CRS.Simple.js',
'geo/crs/CRS.EPSG3857.js',
'geo/crs/CRS.EPSG4326.js',
'layer/Layer.js',
'map/Map.js'],
'map/Map.js',
'layer/Layer.js'
],
desc: 'The core of the library, including OOP, events, DOM facilities, basic units, projections (EPSG:3857 and EPSG:4326) and the base Map class.'
},

View File

@ -68,3 +68,77 @@ L.Layer = L.Class.extend({
return this._map._panes[this.options.pane];
}
});
L.Map.addInitHook(function () {
this._layers = {};
this._zoomBoundLayers = {};
this._addLayers(this.options.layers);
});
L.Map.include({
addLayer: function (layer) {
layer.addTo(this);
return this;
},
removeLayer: function (layer) {
layer.removeFrom(this);
return this;
},
hasLayer: function (layer) {
return !layer || L.stamp(layer) in this._layers;
},
eachLayer: function (method, context) {
for (var i in this._layers) {
method.call(context, this._layers[i]);
}
return this;
},
_addLayers: function (layers) {
layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
for (var i = 0, len = layers.length; i < len; i++) {
this.addLayer(layers[i]);
}
},
_addZoomLimit: function (layer) {
if (isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
this._zoomBoundLayers[L.stamp(layer)] = layer;
this._updateZoomLevels();
}
},
_removeZoomLimit: function (layer) {
var id = L.stamp(layer);
if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
},
_updateZoomLevels: function () {
var minZoom = Infinity,
maxZoom = -Infinity,
oldZoomSpan = this._getZoomSpan();
for (var i in this._zoomBoundLayers) {
var options = this._zoomBoundLayers[i].options;
minZoom = options.minZoom === undefined ? minZoom : Math.min(minZoom, options.minZoom);
maxZoom = options.maxZoom === undefined ? maxZoom : Math.max(maxZoom, options.maxZoom);
}
this._layersMaxZoom = maxZoom === -Infinity ? undefined : maxZoom;
this._layersMinZoom = minZoom === Infinity ? undefined : minZoom;
if (oldZoomSpan !== this._getZoomSpan()) {
this.fire('zoomlevelschange');
}
}
});

View File

@ -42,12 +42,7 @@ L.Map = L.Class.extend({
this._handlers = [];
this._layers = {};
this._zoomBoundLayers = {};
this.callInitHooks();
this._addLayers(options.layers);
},
@ -150,27 +145,6 @@ L.Map = L.Class.extend({
return this.panTo(newCenter, options);
},
addLayer: function (layer) {
layer.addTo(this);
return this;
},
removeLayer: function (layer) {
layer.removeFrom(this);
return this;
},
hasLayer: function (layer) {
return !layer || L.stamp(layer) in this._layers;
},
eachLayer: function (method, context) {
for (var i in this._layers) {
method.call(context, this._layers[i]);
}
return this;
},
invalidateSize: function (options) {
if (!this._loaded) { return this; }
@ -482,14 +456,6 @@ L.Map = L.Class.extend({
return L.DomUtil.create('div', className, container || this._panes.objectsPane);
},
_addLayers: function (layers) {
layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
for (var i = 0, len = layers.length; i < len; i++) {
this.addLayer(layers[i]);
}
},
// private methods that modify map state
@ -542,48 +508,6 @@ L.Map = L.Class.extend({
return this.getMaxZoom() - this.getMinZoom();
},
_addZoomLimit: function (layer) {
if (isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
this._zoomBoundLayers[L.stamp(layer)] = layer;
this._updateZoomLevels();
}
},
_removeZoomLimit: function (layer) {
var id = L.stamp(layer);
if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
},
_updateZoomLevels: function () {
var minZoom = Infinity,
maxZoom = -Infinity,
oldZoomSpan = this._getZoomSpan(),
i;
for (i in this._zoomBoundLayers) {
var options = this._zoomBoundLayers[i].options;
minZoom = isNaN(options.minZoom) ? minZoom : Math.min(minZoom, options.minZoom);
maxZoom = isNaN(options.maxZoom) ? maxZoom : Math.max(maxZoom, options.maxZoom);
}
if (i === undefined) {
// we have no zoom restricting layers
this._layersMaxZoom = this._layersMinZoom = undefined;
} else {
this._layersMaxZoom = maxZoom;
this._layersMinZoom = minZoom;
}
if (oldZoomSpan !== this._getZoomSpan()) {
this.fire('zoomlevelschange');
}
},
_panInsideMaxBounds: function () {
this.panInsideBounds(this.options.maxBounds);
},