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.

137 lines
3.9 KiB

8 years ago
/**
* 边滚动边加载的列表控件
8 years ago
*
* Created by GUY on 2017/5/23.
* @class BI.ListView
* @extends BI.Widget
*/
BI.ListView = BI.inherit(BI.Widget, {
props: function () {
return {
baseCls: "bi-list-view",
overscanHeight: 100,
blockSize: 10,
scrollTop: 0,
el: {},
3 years ago
items: [],
itemFormatter: function (item, index) {
return item;
},
8 years ago
};
},
init: function () {
this.renderedIndex = -1;
this.cache = {};
},
render: function () {
var self = this, o = this.options;
8 years ago
return {
type: "bi.vertical",
items: [BI.extend({
type: "bi.vertical",
scrolly: false,
ref: function (_ref) {
self.container = _ref;
},
8 years ago
}, o.el)],
element: this,
7 years ago
};
8 years ago
},
4 years ago
// mounted之后绑定事件
8 years ago
mounted: function () {
var self = this, o = this.options;
o.items = BI.isFunction(o.items) ? this.__watch(o.items, function (context, newValue) {
self.populate(newValue);
}) : o.items;
8 years ago
this._populate();
this.element.scroll(function (e) {
o.scrollTop = self.element.scrollTop();
self._calculateBlocksToRender();
});
var lastWidth = this.element.width(),
lastHeight = this.element.height();
8 years ago
BI.ResizeDetector.addResizeListener(this, function () {
var width = self.element.width(),
height = self.element.height();
if (width !== lastWidth || height !== lastHeight) {
lastWidth = width;
lastHeight = height;
6 years ago
self._calculateBlocksToRender();
}
8 years ago
});
},
_renderMoreIf: function () {
var self = this, o = this.options;
var height = this.element.height();
var minContentHeight = o.scrollTop + height + o.overscanHeight;
var index = (this.cache[this.renderedIndex] && (this.cache[this.renderedIndex].index + o.blockSize)) || 0;
var cnt = this.renderedIndex + 1;
8 years ago
var lastHeight;
function getElementHeight() {
8 years ago
return self.container.element.height();
}
lastHeight = getElementHeight();
while ((lastHeight) < minContentHeight && index < o.items.length) {
8 years ago
var items = o.items.slice(index, index + o.blockSize);
3 years ago
this.container.addItems(items.map(function (item, i) {
return o.itemFormatter(item, index + i);
}), this);
8 years ago
var addedHeight = getElementHeight() - lastHeight;
this.cache[cnt] = {
index: index,
scrollTop: lastHeight,
height: addedHeight,
8 years ago
};
this.renderedIndex = cnt;
cnt++;
index += o.blockSize;
lastHeight = getElementHeight();
8 years ago
}
},
_calculateBlocksToRender: function () {
this._renderMoreIf();
},
_populate: function (items) {
var o = this.options;
if (items && this.options.items !== items) {
this.options.items = items;
}
this._calculateBlocksToRender();
this.element.scrollTop(o.scrollTop);
},
restore: function () {
this.renderedIndex = -1;
this.container.empty();
this.cache = {};
},
scrollTo: function (scrollTop) {
this.options.scrollTop = scrollTop;
this._calculateBlocksToRender();
this.element.scrollTop(scrollTop);
},
8 years ago
populate: function (items) {
if (items && this.options.items !== items) {
this.restore();
}
7 years ago
this._populate(items);
8 years ago
},
destroyed: function () {
this.restore();
},
8 years ago
});
7 years ago
BI.shortcut("bi.list_view", BI.ListView);
8 years ago