Merge pull request #1376 from moonlite/zoomlevelschange-event

Zoomlevelschange event
This commit is contained in:
Vladimir Agafonkin 2013-02-14 03:35:57 -08:00
commit a948379db8
2 changed files with 103 additions and 10 deletions

View File

@ -1,10 +1,14 @@
describe("Map", function () { describe("Map", function () {
var map,
spy;
beforeEach(function () {
map = L.map(document.createElement('div'));
spy = jasmine.createSpy();
});
describe("#whenReady", function () { describe("#whenReady", function () {
describe("when the map has not yet been loaded", function () { describe("when the map has not yet been loaded", function () {
it("calls the callback when the map is loaded", function () { it("calls the callback when the map is loaded", function () {
var map = L.map(document.createElement('div')),
spy = jasmine.createSpy();
map.whenReady(spy); map.whenReady(spy);
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
@ -15,9 +19,6 @@ describe("Map", function () {
describe("when the map has already been loaded", function () { describe("when the map has already been loaded", function () {
it("calls the callback immediately", function () { it("calls the callback immediately", function () {
var map = L.map(document.createElement('div')),
spy = jasmine.createSpy();
map.setView([0, 0], 1); map.setView([0, 0], 1);
map.whenReady(spy); map.whenReady(spy);
@ -29,9 +30,6 @@ describe("Map", function () {
describe("#getBounds", function () { describe("#getBounds", function () {
it("is safe to call from within a moveend callback during initial " it("is safe to call from within a moveend callback during initial "
+ "load (#1027)", function () { + "load (#1027)", function () {
var c = document.createElement('div'),
map = L.map(c);
map.on("moveend", function () { map.on("moveend", function () {
map.getBounds(); map.getBounds();
}); });
@ -51,4 +49,90 @@ describe("Map", function () {
expect(map.getMaxZoom()).toBe(10); expect(map.getMaxZoom()).toBe(10);
}); });
}); });
describe("#addLayer", function () {
describe("When the first layer is added to a map", function () {
it("should fire a zoomlevelschange event", function () {
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
expect(spy).toHaveBeenCalled();
});
});
describe("when a new layer with greater zoomlevel coverage than the current layer is added to a map", function () {
it("Should fire a zoomlevelschange event ",
function () {
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 15 }).addTo(map);
expect(spy).toHaveBeenCalled();
});
});
describe("when a new layer with the same or lower zoomlevel coverage as the current layer is added to a map", function () {
it("Shouldn't fire a zoomlevelschange event ",
function () {
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
expect(spy).not.toHaveBeenCalled();
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 5 }).addTo(map);
expect(spy).not.toHaveBeenCalled();
});
});
});
describe("#removeLayer", function () {
describe("when the last tile layer on a map is removed", function () {
it("should fire a zoomlevelschange event ", function () {
map.whenReady(function(){
var tl = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 })
.addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(tl);
expect(spy).toHaveBeenCalled();
});
});
});
describe("when a tile layer is removed from a map and it had greater zoom level coverage than the remainding layer", function () {
it("should fire a zoomlevelschange event ", function () {
map.whenReady(function(){
var tl = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 })
.addTo(map),
t2 = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 15 })
.addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(t2);
expect(spy).toHaveBeenCalled();
});
});
});
describe("when a tile layer is removed from a map it and it had lesser or the sa,e zoom level coverage as the remainding layer(s)", function () {
it("shouldn't fire a zoomlevelschange event ", function () {
map.whenReady(function(){
var tl = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 })
.addTo(map),
t2 = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 })
.addTo(map),
t3 = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 5 })
.addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(t2);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(t3);
expect(spy).not.toHaveBeenCalled();
});
});
});
});
}); });

View File

@ -532,10 +532,15 @@ L.Map = L.Class.extend({
L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset)); L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
}, },
_getZoomSpan: function () {
return this.getMaxZoom() - this.getMinZoom();
},
_updateZoomLevels: function () { _updateZoomLevels: function () {
var i, var i,
minZoom = Infinity, minZoom = Infinity,
maxZoom = -Infinity; maxZoom = -Infinity,
oldZoomSpan = this._getZoomSpan();
for (i in this._zoomBoundLayers) { for (i in this._zoomBoundLayers) {
if (this._zoomBoundLayers.hasOwnProperty(i)) { if (this._zoomBoundLayers.hasOwnProperty(i)) {
@ -555,6 +560,10 @@ L.Map = L.Class.extend({
this._layersMaxZoom = maxZoom; this._layersMaxZoom = maxZoom;
this._layersMinZoom = minZoom; this._layersMinZoom = minZoom;
} }
if (oldZoomSpan !== this._getZoomSpan()) {
this.fire("zoomlevelschange");
}
}, },
// map events // map events