Skip to content

Commit

Permalink
fix(infiniteGrid): add defense code when the user forces the scroll (n…
Browse files Browse the repository at this point in the history
…aver#474)

* fix(infiniteGrid): add defense code when the user forces the scroll

close naver#455

* skip: add test code
  • Loading branch information
sculove authored Mar 10, 2017
1 parent f74fa1d commit 9bbb159
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/infiniteGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
"append": "append",
"prepend": "prepend"
};
var RETRY = 3;
ns.InfiniteGrid = ns.Class.extend(ns.Component, {
_events: function() {
return EVENTS;
Expand Down Expand Up @@ -450,6 +451,7 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
addItems = addItems || [];

this.el.style.height = this._getContainerSize().height + "px";
this._doubleCheckCount = RETRY;

// refresh element
this._topElement = this.getTopElement();
Expand Down Expand Up @@ -486,6 +488,22 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
distance: distance,
croppedCount: options.removedCount
});

// doublecheck!!! (workaround)
if (!options.isAppend) {
if (this._getScrollTop() === 0) {
var self = this;
clearInterval(this._doubleCheckTimer);
this._doubleCheckTimer = setInterval(function() {
if (self._getScrollTop() === 0) {
self.trigger(self._prefix + EVENTS.prepend, {
scrollTop: 0
});
(--self._doubleCheckCount <= 0) && clearInterval(self._doubleCheckTimer);
}
}, 500);
}
}
},

// $elements => $([HTMLElement, HTMLElement, ...])
Expand Down Expand Up @@ -641,6 +659,8 @@ eg.module("infiniteGrid", ["jQuery", eg, window, document], function($, ns, glob
this._prevScrollTop = 0;
this._equalItemSize = 0;
this._resizeTimeout = null;
this._doubleCheckTimer = null;
this._doubleCheckCount = RETRY;
this._resetCols(this._appendCols.length || 0);
this.items = [];
},
Expand Down
26 changes: 26 additions & 0 deletions test/unit/js/infiniteGrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,32 @@ QUnit.test("check item/element order and check removed parts", function(assert)
this.inst.append(getContent("append",30));
});

QUnit.test("check a prepend workaround code (doublecheck)", function(assert) {
// Given
// When
var done = assert.async();
var prependCount = 0;
this.inst.on({
"prepend": function(e) {
prependCount++
},
"layoutComplete": function(e) {
assert.equal(e.isAppend, false, "prepend type");
$(window).scrollTop(0);
}
});

// When
this.inst.prepend(getContent("prepend", 200));

setTimeout(function() {
// Then
assert.equal(prependCount, 3);
done();
}, 2000);
});


QUnit.module("infiniteGrid unit method Test", {
beforeEach : function() {
this.inst = null;
Expand Down

0 comments on commit 9bbb159

Please sign in to comment.