fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.5 KiB

8 years ago
/**
* 虚拟列表
8 years ago
*
8 years ago
* Created by GUY on 2017/5/22.
8 years ago
* @class BI.VirtualList
* @extends BI.Widget
*/
BI.VirtualList = BI.inherit(BI.Widget, {
props: function () {
return {
8 years ago
baseCls: "bi-virtual-list",
overscanHeight: 100,
blockSize: 10,
scrollTop: 0,
items: []
8 years ago
};
},
8 years ago
init: function () {
8 years ago
var self = this;
8 years ago
this.renderedIndex = -1;
this.cache = {};
},
8 years ago
render: function () {
var self = this, o = this.options;
return {
8 years ago
type: "bi.vertical",
8 years ago
items: [{
type: "bi.layout",
ref: function () {
8 years ago
self.topBlank = this;
}
}, {
type: "bi.vertical",
scrolly: false,
ref: function () {
self.container = this;
}
}, {
type: "bi.layout",
ref: function () {
self.bottomBlank = this;
}
}],
element: this
7 years ago
};
8 years ago
},
4 years ago
// mounted之后绑定事件
8 years ago
mounted: function () {
8 years ago
var self = this, o = this.options;
this._populate();
this.element.scroll(function (e) {
o.scrollTop = self.element.scrollTop();
self._calculateBlocksToRender();
});
BI.ResizeDetector.addResizeListener(this, function () {
8 years ago
self._calculateBlocksToRender();
8 years ago
});
},
_renderMoreIf: function () {
8 years ago
var self = this, o = this.options;
8 years ago
var height = this.element.height();
var minContentHeight = o.scrollTop + height + o.overscanHeight;
var index = (this.renderedIndex + 1) * o.blockSize, cnt = this.renderedIndex + 1;
8 years ago
var lastHeight;
8 years ago
var getElementHeight = function () {
return self.container.element.height() + self.topBlank.element.height() + self.bottomBlank.element.height();
};
while ((lastHeight = getElementHeight()) < minContentHeight && index < o.items.length) {
8 years ago
var items = o.items.slice(index, index + o.blockSize);
6 years ago
this.container.addItems(items, this);
8 years ago
var addedHeight = getElementHeight() - lastHeight;
8 years ago
this.tree.set(cnt, addedHeight);
this.renderedIndex = cnt;
cnt++;
index += o.blockSize;
8 years ago
}
},
8 years ago
_calculateBlocksToRender: function () {
var o = this.options;
this._renderMoreIf();
8 years ago
var height = this.element.height();
var minContentHeightFrom = o.scrollTop - o.overscanHeight;
var minContentHeightTo = o.scrollTop + height + o.overscanHeight;
var start = this.tree.greatestLowerBound(minContentHeightFrom);
var end = this.tree.leastUpperBound(minContentHeightTo);
var needDestroyed = [], needMount = [];
8 years ago
for (var i = 0; i < start; i++) {
4 years ago
var index = i * o.blockSize;
if (!this.cache[i]) {
this.cache[i] = {};
}
8 years ago
if (!this.cache[i].destroyed) {
for (var j = index; j < index + o.blockSize && j < o.items.length; j++) {
needDestroyed.push(this.container._children[j]);
this.container._children[j] = null;
}
this.cache[i].destroyed = true;
}
}
for (var i = end + 1; i <= this.renderedIndex; i++) {
4 years ago
var index = i * o.blockSize;
if (!this.cache[i]) {
this.cache[i] = {};
}
8 years ago
if (!this.cache[i].destroyed) {
for (var j = index; j < index + o.blockSize && j < o.items.length; j++) {
needDestroyed.push(this.container._children[j]);
this.container._children[j] = null;
}
this.cache[i].destroyed = true;
}
}
5 years ago
var firstFragment = BI.Widget._renderEngine.createFragment(),
lastFragment = BI.Widget._renderEngine.createFragment();
8 years ago
var currentFragment = firstFragment;
for (var i = (start < 0 ? 0 : start); i <= end && i <= this.renderedIndex; i++) {
4 years ago
var index = i * o.blockSize;
if (!this.cache[i]) {
this.cache[i] = {};
}
8 years ago
if (!this.cache[i].destroyed) {
currentFragment = lastFragment;
}
if (this.cache[i].destroyed === true) {
for (var j = index; j < index + o.blockSize && j < o.items.length; j++) {
var w = this.container._addElement(j, o.items[j], this);
needMount.push(w);
8 years ago
currentFragment.appendChild(w.element[0]);
}
this.cache[i].destroyed = false;
}
}
this.container.element.prepend(firstFragment);
this.container.element.append(lastFragment);
5 years ago
this.topBlank.setHeight(this.tree.sumTo(Math.max(-1, start - 1)));
this.bottomBlank.setHeight(this.tree.sumTo(this.renderedIndex) - this.tree.sumTo(Math.min(end, this.renderedIndex)));
BI.each(needMount, function (i, child) {
child && child._mount();
});
8 years ago
BI.each(needDestroyed, function (i, child) {
child && child._destroy();
});
8 years ago
},
8 years ago
_populate: function (items) {
8 years ago
var o = this.options;
8 years ago
if (items && this.options.items !== items) {
this.options.items = items;
}
8 years ago
this.tree = BI.PrefixIntervalTree.empty(Math.ceil(o.items.length / o.blockSize));
4 years ago
8 years ago
this._calculateBlocksToRender();
5 years ago
try {
this.element.scrollTop(o.scrollTop);
} catch (e) {
}
8 years ago
},
8 years ago
_clearChildren: function () {
BI.each(this.container._children, function (i, cell) {
cell && cell._destroy();
8 years ago
});
this.container._children = {};
this.container.attr("items", []);
},
8 years ago
restore: function () {
this.renderedIndex = -1;
8 years ago
this._clearChildren();
8 years ago
this.cache = {};
8 years ago
this.options.scrollTop = 0;
// 依赖于cache的占位元素也要初始化
this.topBlank.setHeight(0);
this.bottomBlank.setHeight(0);
8 years ago
},
populate: function (items) {
8 years ago
if (items && this.options.items !== items) {
this.restore();
}
this._populate(items);
8 years ago
},
destroyed: function () {
this.cache = {};
this.renderedIndex = -1;
8 years ago
}
});
7 years ago
BI.shortcut("bi.virtual_list", BI.VirtualList);
8 years ago