Browse Source

性能优化

es6
guy 3 years ago
parent
commit
fcdac98e01
  1. 59
      src/base/grid/grid.js

59
src/base/grid/grid.js

@ -59,6 +59,7 @@ BI.GridView = BI.inherit(BI.Widget, {
items: [this.container] items: [this.container]
}); });
if (o.items.length > 0) { if (o.items.length > 0) {
this._calculateSizeAndPositionData();
this._populate(); this._populate();
} }
}, },
@ -72,6 +73,24 @@ BI.GridView = BI.inherit(BI.Widget, {
} }
}, },
_calculateSizeAndPositionData: function () {
var o = this.options;
this.columnCount = 0;
this.rowCount = 0;
if (BI.isNumber(o.columnCount)) {
this.columnCount = o.columnCount;
} else if (o.items.length > 0) {
this.columnCount = o.items[0].length;
}
if (BI.isNumber(o.rowCount)) {
this.rowCount = o.rowCount;
} else {
this.rowCount = o.items.length;
}
this._columnSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.columnCount, o.columnWidthGetter, o.estimatedColumnSize);
this._rowSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.rowCount, o.rowHeightGetter, o.estimatedRowSize);
},
_getOverscanIndices: function (cellCount, overscanCellsCount, startIndex, stopIndex) { _getOverscanIndices: function (cellCount, overscanCellsCount, startIndex, stopIndex) {
return { return {
overscanStartIndex: Math.max(0, startIndex - overscanCellsCount), overscanStartIndex: Math.max(0, startIndex - overscanCellsCount),
@ -170,7 +189,7 @@ BI.GridView = BI.inherit(BI.Widget, {
left: columnDatum.offset + horizontalOffsetAdjustment, left: columnDatum.offset + horizontalOffsetAdjustment,
top: rowDatum.offset + verticalOffsetAdjustment, top: rowDatum.offset + verticalOffsetAdjustment,
_left: columnDatum.offset + horizontalOffsetAdjustment, _left: columnDatum.offset + horizontalOffsetAdjustment,
_top: rowDatum.offset + verticalOffsetAdjustment, _top: rowDatum.offset + verticalOffsetAdjustment
// _width: columnDatum.size, // _width: columnDatum.size,
// _height: rowDatum.size // _height: rowDatum.size
}); });
@ -226,14 +245,14 @@ BI.GridView = BI.inherit(BI.Widget, {
* 对于grid_view如果没有全部渲染过this._columnSizeAndPositionManager.getTotalSize获取的宽度是不准确的 * 对于grid_view如果没有全部渲染过this._columnSizeAndPositionManager.getTotalSize获取的宽度是不准确的
* 因此在调用setScrollLeft等函数时会造成没法移动到最右端(预估可移动距离太短) * 因此在调用setScrollLeft等函数时会造成没法移动到最右端(预估可移动距离太短)
*/ */
_getRealMaxScrollLeft: function () { // _getRealMaxScrollLeft: function () {
var o = this.options; // var o = this.options;
var totalWidth = 0; // var totalWidth = 0;
BI.count(0, this.columnCount, function (index) { // BI.count(0, this.columnCount, function (index) {
totalWidth += o.columnWidthGetter(index); // totalWidth += o.columnWidthGetter(index);
}); // });
return Math.max(0, totalWidth - this.options.width + (this.options.overflowX ? BI.DOM.getScrollWidth() : 0)); // return Math.max(0, totalWidth - this.options.width + (this.options.overflowX ? BI.DOM.getScrollWidth() : 0));
}, // },
_getMaxScrollLeft: function () { _getMaxScrollLeft: function () {
return Math.max(0, this._columnSizeAndPositionManager.getTotalSize() - this.options.width + (this.options.overflowX ? BI.DOM.getScrollWidth() : 0)); return Math.max(0, this._columnSizeAndPositionManager.getTotalSize() - this.options.width + (this.options.overflowX ? BI.DOM.getScrollWidth() : 0));
@ -246,27 +265,14 @@ BI.GridView = BI.inherit(BI.Widget, {
_populate: function (items) { _populate: function (items) {
var self = this, o = this.options; var self = this, o = this.options;
this._reRange(); this._reRange();
this.columnCount = 0;
this.rowCount = 0;
if (items && items !== this.options.items) { if (items && items !== this.options.items) {
this.options.items = items; this.options.items = items;
this._calculateSizeAndPositionData();
} }
if (BI.isNumber(o.columnCount)) { if (o.items.length > 0) {
this.columnCount = o.columnCount;
} else if (o.items.length > 0) {
this.columnCount = o.items[0].length;
}
if (BI.isNumber(o.rowCount)) {
this.rowCount = o.rowCount;
} else {
this.rowCount = o.items.length;
}
this.container.setWidth(this.columnCount * o.estimatedColumnSize); this.container.setWidth(this.columnCount * o.estimatedColumnSize);
this.container.setHeight(this.rowCount * o.estimatedRowSize); this.container.setHeight(this.rowCount * o.estimatedRowSize);
this._columnSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.columnCount, o.columnWidthGetter, o.estimatedColumnSize);
this._rowSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.rowCount, o.rowHeightGetter, o.estimatedRowSize);
this._debounceRelease(); this._debounceRelease();
this._calculateChildrenToRender(); this._calculateChildrenToRender();
// 元素未挂载时不能设置scrollTop // 元素未挂载时不能设置scrollTop
@ -275,6 +281,7 @@ BI.GridView = BI.inherit(BI.Widget, {
this.element.scrollLeft(o.scrollLeft); this.element.scrollLeft(o.scrollLeft);
} catch (e) { } catch (e) {
} }
}
}, },
setScrollLeft: function (scrollLeft) { setScrollLeft: function (scrollLeft) {
@ -282,7 +289,7 @@ BI.GridView = BI.inherit(BI.Widget, {
return; return;
} }
this._scrollLock = true; this._scrollLock = true;
this.options.scrollLeft = BI.clamp(scrollLeft || 0, 0, this._getRealMaxScrollLeft()); this.options.scrollLeft = scrollLeft || 0;
this._debounceRelease(); this._debounceRelease();
this._calculateChildrenToRender(); this._calculateChildrenToRender();
this.element.scrollLeft(this.options.scrollLeft); this.element.scrollLeft(this.options.scrollLeft);
@ -293,7 +300,7 @@ BI.GridView = BI.inherit(BI.Widget, {
return; return;
} }
this._scrollLock = true; this._scrollLock = true;
this.options.scrollTop = BI.clamp(scrollTop || 0, 0, this._getMaxScrollTop()); this.options.scrollTop = scrollTop || 0;
this._debounceRelease(); this._debounceRelease();
this._calculateChildrenToRender(); this._calculateChildrenToRender();
this.element.scrollTop(this.options.scrollTop); this.element.scrollTop(this.options.scrollTop);

Loading…
Cancel
Save