Add debounceMoveend option to invalidateSize

This commit is contained in:
John Firebaugh 2013-11-13 10:50:26 -08:00
parent 248c5e7c8f
commit 33263e537d
2 changed files with 56 additions and 3 deletions

View File

@ -396,7 +396,8 @@ describe("Map", function () {
describe("#invalidateSize", function () { describe("#invalidateSize", function () {
var container, var container,
orig_width = 100; orig_width = 100,
clock;
beforeEach(function () { beforeEach(function () {
container = map.getContainer(); container = map.getContainer();
@ -404,10 +405,12 @@ describe("Map", function () {
document.body.appendChild(container); document.body.appendChild(container);
map.setView([0, 0], 0); map.setView([0, 0], 0);
map.invalidateSize({pan: false}); map.invalidateSize({pan: false});
clock = sinon.useFakeTimers();
}); });
afterEach(function () { afterEach(function () {
document.body.removeChild(container); document.body.removeChild(container);
clock.restore();
}); });
it("pans by the right amount when growing in 1px increments", function () { 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); 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();
});
}); });
}); });

View File

@ -288,7 +288,14 @@ L.Map = L.Class.extend({
this._rawPanBy(offset); 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', { return this.fire('resize', {
@ -685,7 +692,7 @@ L.Map = L.Class.extend({
_onResize: function () { _onResize: function () {
L.Util.cancelAnimFrame(this._resizeRequest); L.Util.cancelAnimFrame(this._resizeRequest);
this._resizeRequest = L.Util.requestAnimFrame( this._resizeRequest = L.Util.requestAnimFrame(
this.invalidateSize, this, false, this._container); function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container);
}, },
_onMouseClick: function (e) { _onMouseClick: function (e) {