Merge branch 'invalidateSize'

This commit is contained in:
Vladimir Agafonkin 2013-11-13 21:50:02 +02:00
commit 3618f404f5
2 changed files with 54 additions and 5 deletions

View File

@ -396,7 +396,8 @@ describe("Map", function () {
describe("#invalidateSize", function () {
var container,
origWidth = 100;
origWidth = 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();
});
});
});

View File

@ -290,9 +290,12 @@ L.Map = L.Class.extend({
this.fire('move');
// make sure moveend is not fired too often on resize
clearTimeout(this._sizeTimer);
this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
if (options.debounceMoveend) {
clearTimeout(this._sizeTimer);
this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
} else {
this.fire('moveend');
}
}
return this.fire('resize', {
@ -690,7 +693,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) {