[Pkg-javascript-commits] [leaflet] 245/301: Add debounceMoveend option to invalidateSize

Jonas Smedegaard js at moszumanska.debian.org
Mon Jan 27 22:22:54 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet.

commit 33263e537dde232da3e8bf4c316f210436c0b506
Author: John Firebaugh <john.firebaugh at gmail.com>
Date:   Wed Nov 13 10:50:26 2013 -0800

    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 00da85d..0bb2a27 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 e8c4057..029eea9 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) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet.git



More information about the Pkg-javascript-commits mailing list