From efe0c6f6ea5f6f497cc7d6c4c7be963a56407071 Mon Sep 17 00:00:00 2001 From: Mattias Bengtsson Date: Thu, 14 Feb 2013 03:04:10 +0100 Subject: [PATCH 1/3] Fire zoomlevelschange event when zoomlevels are updated. This is triggered when you remove a layer from a map with greater zoom level coverage than the remainding layers or when you add a tilelayer with greater zoomlevel coverage than the previous set of layers had. --- src/map/Map.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/map/Map.js b/src/map/Map.js index 5367ba33..ee74f193 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -271,6 +271,10 @@ L.Map = L.Class.extend({ return Math.min(z1, z2); }, + getZoomLevels: function () { + return this.getMaxZoom() - this.getMinZoom() + 1; + }, + getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number bounds = L.latLngBounds(bounds); @@ -533,7 +537,8 @@ L.Map = L.Class.extend({ _updateZoomLevels: function () { var i, minZoom = Infinity, - maxZoom = -Infinity; + maxZoom = -Infinity, + oldZoomLevels = this.getZoomLevels(); for (i in this._zoomBoundLayers) { if (this._zoomBoundLayers.hasOwnProperty(i)) { @@ -553,6 +558,10 @@ L.Map = L.Class.extend({ this._layersMaxZoom = maxZoom; this._layersMinZoom = minZoom; } + + if (oldZoomLevels !== this.getZoomLevels()) { + this.fire("zoomlevelschange"); + } }, // map events From 3a834d35cdcc733dc96db1cd78fb4e4fadceeb2d Mon Sep 17 00:00:00 2001 From: Mattias Bengtsson Date: Thu, 14 Feb 2013 03:07:32 +0100 Subject: [PATCH 2/3] Add tests for the zoomlevelschanged-event. --- spec/suites/map/MapSpec.js | 102 +++++++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 9 deletions(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 5f10ba25..4826920f 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -1,10 +1,14 @@ describe("Map", function () { + var map, + spy; + beforeEach(function () { + map = L.map(document.createElement('div')); + spy = jasmine.createSpy(); + }); + describe("#whenReady", function () { describe("when the map has not yet been 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); expect(spy).not.toHaveBeenCalled(); @@ -15,9 +19,6 @@ describe("Map", function () { describe("when the map has already been loaded", function () { it("calls the callback immediately", function () { - var map = L.map(document.createElement('div')), - spy = jasmine.createSpy(); - map.setView([0, 0], 1); map.whenReady(spy); @@ -29,9 +30,6 @@ describe("Map", function () { describe("#getBounds", function () { it("is safe to call from within a moveend callback during initial " + "load (#1027)", function () { - var c = document.createElement('div'), - map = L.map(c); - map.on("moveend", function () { map.getBounds(); }); @@ -51,4 +49,90 @@ describe("Map", function () { 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(); + + }); + }); + }); + + }); }); From cbee6851c870a8a9047b2d5d193628ffe0e2904f Mon Sep 17 00:00:00 2001 From: Mattias Bengtsson Date: Thu, 14 Feb 2013 12:00:08 +0100 Subject: [PATCH 3/3] Don't expose getZoomLevels. --- src/map/Map.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/map/Map.js b/src/map/Map.js index ee74f193..61881df0 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -271,10 +271,6 @@ L.Map = L.Class.extend({ return Math.min(z1, z2); }, - getZoomLevels: function () { - return this.getMaxZoom() - this.getMinZoom() + 1; - }, - getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number bounds = L.latLngBounds(bounds); @@ -534,11 +530,15 @@ L.Map = L.Class.extend({ L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset)); }, + _getZoomSpan: function () { + return this.getMaxZoom() - this.getMinZoom(); + }, + _updateZoomLevels: function () { var i, minZoom = Infinity, maxZoom = -Infinity, - oldZoomLevels = this.getZoomLevels(); + oldZoomSpan = this._getZoomSpan(); for (i in this._zoomBoundLayers) { if (this._zoomBoundLayers.hasOwnProperty(i)) { @@ -559,7 +559,7 @@ L.Map = L.Class.extend({ this._layersMinZoom = minZoom; } - if (oldZoomLevels !== this.getZoomLevels()) { + if (oldZoomSpan !== this._getZoomSpan()) { this.fire("zoomlevelschange"); } },