Added a bunch of tests and fixed some bugs those exposed.

Also don't try to automatically adjust zoom. Leave that to the
layer control for now and wait for a setBaseLayer / switchLayer
or something instead.
This commit is contained in:
Mattias Bengtsson 2012-11-19 04:36:13 +01:00
parent 6f1532f402
commit c0135a219d
5 changed files with 103 additions and 14 deletions

View File

@ -15,6 +15,8 @@
<div id="map"></div>
<script type="text/javascript">
// Test that changing between layers with differing zoomlevels also updates
// the zoomlevels in the map + also
function getCloudMadeUrl(styleId) {
return 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/' + styleId + '/256/{z}/{x}/{y}.png';

View File

@ -11,11 +11,13 @@ describe("Control.Layers", function () {
layers = L.control.layers(baseLayers).addTo(map),
spy = jasmine.createSpy();
map.on('baselayerchange', spy);
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]);
map.on('baselayerchange', spy)
.whenReady(function(){
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]);
expect(spy).toHaveBeenCalled();
expect(spy.mostRecentCall.args[0].layer).toBe(baseLayers["Layer 1"]);
expect(spy).toHaveBeenCalled();
expect(spy.mostRecentCall.args[0].layer).toBe(baseLayers["Layer 1"]);
});
});
it("is not fired on input that doesn't change the base layer", function () {

View File

@ -1 +1,76 @@
describe('TileLayer', noSpecs);
describe('TileLayer', function () {
describe("#getMaxZoom, #getMinZoom", function () {
var map;
beforeEach(function () {
map = L.map(document.createElement('div'));
});
describe("when a tilelayer is added to a map with no other layers", function () {
it("should have the same zoomlevels as the tilelayer", function () {
var maxZoom = 10,
minZoom = 5;
map.setView([0, 0], 1);
L.tileLayer("{z}{x}{y}", {
maxZoom: maxZoom,
minZoom: minZoom
}).addTo(map);
expect(map.getMaxZoom() === maxZoom).toBeTruthy();
expect(map.getMinZoom() === minZoom).toBeTruthy();
});
});
describe("when a tilelayer is added to a map that already has a tilelayer", function () {
it("should have its zoomlevels updated to fit the new layer", function () {
map.setView([0, 0], 1);
L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 15 }).addTo(map);
expect(map.getMinZoom()).toBe(10);
expect(map.getMaxZoom()).toBe(15);
L.tileLayer("{z}{x}{y}", { minZoom:5, maxZoom: 10 }).addTo(map);
expect(map.getMinZoom()).toBe(5); // changed
expect(map.getMaxZoom()).toBe(15); // unchanged
L.tileLayer("{z}{x}{y}",{ minZoom:10, maxZoom: 20 }).addTo(map);
expect(map.getMinZoom()).toBe(5); // unchanged
expect(map.getMaxZoom()).toBe(20); // changed
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 25 }).addTo(map);
expect(map.getMinZoom()).toBe(0); // changed
expect(map.getMaxZoom()).toBe(25); // changed
});
});
describe("when a tilelayer is removed from a map", function () {
it("it should have its zoomlevels updated to only fit the layers it currently has", function () {
var tiles = [ L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 15 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:5, maxZoom: 10 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 20 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 25 }).addTo(map)
];
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(25);
map.removeLayer(tiles[0]);
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(25);
map.removeLayer(tiles[3]);
expect(map.getMinZoom()).toBe(5);
expect(map.getMaxZoom()).toBe(20);
map.removeLayer(tiles[2]);
expect(map.getMinZoom()).toBe(5);
expect(map.getMaxZoom()).toBe(10);
map.removeLayer(tiles[1]);
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(Infinity);
});
});
});
});

View File

@ -197,6 +197,7 @@ L.Control.Layers = L.Control.extend({
}
if (baseLayer) {
this._map.setZoom(this._map.getZoom());
this._map.fire('baselayerchange', {layer: baseLayer});
}
},

View File

@ -151,9 +151,6 @@ L.Map = L.Class.extend({
if(layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))){
this._zoomBoundLayers[id] = layer;
this._updateZoomLevels();
if(this._loaded){
this.setZoom(this.getZoom());
}
}
// TODO looks ugly, refactor!!!
@ -542,13 +539,25 @@ L.Map = L.Class.extend({
},
_updateZoomLevels: function () {
var i;
this._layersMinZoom = 0;
this._layersMaxZoom = Infinity;
var i,
minZoom = Infinity,
maxZoom = -Infinity;
for (i in this._zoomBoundLayers) {
var l = this._zoomBoundLayers[i];
this._layersMinZoom = Math.max(this._layersMinZoom, l.options.minZoom || 0);
this._layersMaxZoom = Math.min(this._layersMaxZoom, l.options.maxZoom || Infinity);
var layer = this._zoomBoundLayers[i];
if(!isNaN(layer.options.minZoom)){
minZoom = Math.min(minZoom, layer.options.minZoom);
}
if(!isNaN(layer.options.maxZoom)){
maxZoom = Math.max(maxZoom, layer.options.maxZoom);
}
}
if(i === undefined){ // we have no tilelayers
this._layersMaxZoom = this._layersMinZoom = undefined;
} else {
this._layersMaxZoom = maxZoom;
this._layersMinZoom = minZoom;
}
},