From 33263e537dde232da3e8bf4c316f210436c0b506 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 13 Nov 2013 10:50:26 -0800 Subject: [PATCH] Add debounceMoveend option to invalidateSize --- spec/suites/map/MapSpec.js | 48 +++++++++++++++++++++++++++++++++++++- src/map/Map.js | 11 +++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 00da85d8..0bb2a278 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -396,7 +396,8 @@ describe("Map", function () { describe("#invalidateSize", function () { var container, - orig_width = 100; + orig_width = 100, + clock; beforeEach(function () { container = map.getContainer(); @@ -404,10 +405,12 @@ describe("Map", function () { document.body.appendChild(container); map.setView([0, 0], 0); map.invalidateSize({pan: false}); + clock = sinon.useFakeTimers(); }); afterEach(function () { document.body.removeChild(container); + clock.restore(); }); it("pans by the right amount when growing in 1px increments", function () { @@ -447,5 +450,48 @@ describe("Map", function () { expect(map._getMapPanePos().x).to.be(0); }); + + it("emits no move event if the size has not changed", function () { + var spy = sinon.spy(); + map.on("move", spy); + + map.invalidateSize(); + + expect(spy.called).not.to.be.ok(); + }); + + it("emits a move event if the size has changed", function () { + var spy = sinon.spy(); + map.on("move", spy); + + container.style.width = (orig_width + 5) + "px"; + map.invalidateSize(); + + expect(spy.called).to.be.ok(); + }); + + it("emits a moveend event if the size has changed", function () { + var spy = sinon.spy(); + map.on("moveend", spy); + + container.style.width = (orig_width + 5) + "px"; + map.invalidateSize(); + + expect(spy.called).to.be.ok(); + }); + + it("debounces the moveend event if the debounceMoveend option is given", function () { + var spy = sinon.spy(); + map.on("moveend", spy); + + container.style.width = (orig_width + 5) + "px"; + map.invalidateSize({debounceMoveend: true}); + + expect(spy.called).not.to.be.ok(); + + clock.tick(200); + + expect(spy.called).to.be.ok(); + }); }); }); diff --git a/src/map/Map.js b/src/map/Map.js index e8c40571..029eea97 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -288,7 +288,14 @@ L.Map = L.Class.extend({ this._rawPanBy(offset); } - this.fire('move').fire('moveend'); + this.fire('move'); + + if (options.debounceMoveend) { + clearTimeout(this._sizeTimer); + this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200); + } else { + this.fire('moveend'); + } } return this.fire('resize', { @@ -685,7 +692,7 @@ L.Map = L.Class.extend({ _onResize: function () { L.Util.cancelAnimFrame(this._resizeRequest); this._resizeRequest = L.Util.requestAnimFrame( - this.invalidateSize, this, false, this._container); + function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container); }, _onMouseClick: function (e) {