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.

405 lines
16 KiB

8 years ago
/**
* GridView
8 years ago
*
* Created by GUY on 2016/1/11.
* @class BI.GridView
8 years ago
* @extends BI.Widget
*/
BI.GridView = BI.inherit(BI.Widget, {
8 years ago
_defaultConfig: function () {
return BI.extend(BI.GridView.superclass._defaultConfig.apply(this, arguments), {
8 years ago
baseCls: "bi-grid-view",
8 years ago
// width: 400, //必设
// height: 300, //必设
scrollable: true,
scrollx: false,
scrolly: false,
8 years ago
overflowX: true,
overflowY: true,
overscanColumnCount: 0,
overscanRowCount: 0,
7 years ago
rowHeightGetter: BI.emptyFn, // number类型或function类型
columnWidthGetter: BI.emptyFn, // number类型或function类型
8 years ago
// estimatedColumnSize: 100, //columnWidthGetter为function时必设
// estimatedRowSize: 30, //rowHeightGetter为function时必设
8 years ago
scrollLeft: 0,
scrollTop: 0,
3 years ago
items: [],
itemFormatter: function (item, row, col) {
return item;
}
8 years ago
});
},
render: function () {
8 years ago
var self = this, o = this.options;
this.renderedCells = [];
this.renderedKeys = [];
this.renderRange = {};
8 years ago
this._scrollLock = false;
this._debounceRelease = BI.debounce(function () {
self._scrollLock = false;
8 years ago
}, 1000 / 60);
4 years ago
this.container = BI._lazyCreateWidget({
8 years ago
type: "bi.absolute"
});
this.element.scroll(function () {
if (self._scrollLock === true) {
return;
}
o.scrollLeft = self.element.scrollLeft();
o.scrollTop = self.element.scrollTop();
self._calculateChildrenToRender();
self.fireEvent(BI.GridView.EVENT_SCROLL, {
8 years ago
scrollLeft: o.scrollLeft,
scrollTop: o.scrollTop
});
});
// 兼容一下
var scrollable = o.scrollable, scrollx = o.scrollx, scrolly = o.scrolly;
if (o.overflowX === false) {
if (o.overflowY === false) {
scrollable = false;
} else {
scrollable = "y"
}
} else {
if (o.overflowY === false) {
scrollable = "x";
}
}
4 years ago
BI._lazyCreateWidget({
8 years ago
type: "bi.vertical",
8 years ago
element: this,
scrollable: scrollable,
scrolly: scrolly,
scrollx: scrollx,
8 years ago
items: [this.container]
});
o.items = BI.isFunction(o.items) ? this.__watch(o.items, function (context, newValue) {
self.populate(newValue);
}) : o.items;
8 years ago
if (o.items.length > 0) {
3 years ago
this._calculateSizeAndPositionData();
8 years ago
this._populate();
}
6 years ago
},
4 years ago
// mounted之后绑定事件
6 years ago
mounted: function () {
var o = this.options;
8 years ago
if (o.scrollLeft !== 0 || o.scrollTop !== 0) {
6 years ago
this.element.scrollTop(o.scrollTop);
this.element.scrollLeft(o.scrollLeft);
8 years ago
}
},
3 years ago
_calculateSizeAndPositionData: function () {
var o = this.options;
this.rowCount = 0;
3 years ago
this.columnCount = 0;
3 years ago
if (BI.isNumber(o.columnCount)) {
3 years ago
this.columnCount = o.columnCount;
3 years ago
} else if (o.items.length > 0) {
3 years ago
this.columnCount = o.items[0].length;
3 years ago
}
if (BI.isNumber(o.rowCount)) {
3 years ago
this.rowCount = o.rowCount;
3 years ago
} else {
3 years ago
this.rowCount = o.items.length;
3 years ago
}
3 years ago
this._columnSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.columnCount, o.columnWidthGetter, o.estimatedColumnSize);
this._rowSizeAndPositionManager = new BI.ScalingCellSizeAndPositionManager(this.rowCount, o.rowHeightGetter, o.estimatedRowSize);
3 years ago
},
8 years ago
_getOverscanIndices: function (cellCount, overscanCellsCount, startIndex, stopIndex) {
return {
overscanStartIndex: Math.max(0, startIndex - overscanCellsCount),
overscanStopIndex: Math.min(cellCount - 1, stopIndex + overscanCellsCount)
7 years ago
};
8 years ago
},
_calculateChildrenToRender: function () {
var self = this, o = this.options;
var width = o.width, height = o.height, scrollLeft = BI.clamp(o.scrollLeft, 0, this._getMaxScrollLeft()),
scrollTop = BI.clamp(o.scrollTop, 0, this._getMaxScrollTop()),
8 years ago
overscanColumnCount = o.overscanColumnCount, overscanRowCount = o.overscanRowCount;
if (height > 0 && width > 0) {
var visibleColumnIndices = this._columnSizeAndPositionManager.getVisibleCellRange(width, scrollLeft);
var visibleRowIndices = this._rowSizeAndPositionManager.getVisibleCellRange(height, scrollTop);
if (BI.isEmpty(visibleColumnIndices) || BI.isEmpty(visibleRowIndices)) {
return;
}
8 years ago
var horizontalOffsetAdjustment = this._columnSizeAndPositionManager.getOffsetAdjustment(width, scrollLeft);
var verticalOffsetAdjustment = this._rowSizeAndPositionManager.getOffsetAdjustment(height, scrollTop);
this._renderedColumnStartIndex = visibleColumnIndices.start;
this._renderedColumnStopIndex = visibleColumnIndices.stop;
this._renderedRowStartIndex = visibleRowIndices.start;
this._renderedRowStopIndex = visibleRowIndices.stop;
3 years ago
var overscanColumnIndices = this._getOverscanIndices(this.columnCount, overscanColumnCount, this._renderedColumnStartIndex, this._renderedColumnStopIndex);
8 years ago
3 years ago
var overscanRowIndices = this._getOverscanIndices(this.rowCount, overscanRowCount, this._renderedRowStartIndex, this._renderedRowStopIndex);
8 years ago
var columnStartIndex = overscanColumnIndices.overscanStartIndex;
var columnStopIndex = overscanColumnIndices.overscanStopIndex;
var rowStartIndex = overscanRowIndices.overscanStartIndex;
var rowStopIndex = overscanRowIndices.overscanStopIndex;
7 years ago
// 算区间size
var minRowDatum = this._rowSizeAndPositionManager.getSizeAndPositionOfCell(rowStartIndex);
var minColumnDatum = this._columnSizeAndPositionManager.getSizeAndPositionOfCell(columnStartIndex);
var maxRowDatum = this._rowSizeAndPositionManager.getSizeAndPositionOfCell(rowStopIndex);
var maxColumnDatum = this._columnSizeAndPositionManager.getSizeAndPositionOfCell(columnStopIndex);
var top = minRowDatum.offset + verticalOffsetAdjustment;
var left = minColumnDatum.offset + horizontalOffsetAdjustment;
var bottom = maxRowDatum.offset + verticalOffsetAdjustment + maxRowDatum.size;
var right = maxColumnDatum.offset + horizontalOffsetAdjustment + maxColumnDatum.size;
7 years ago
// 如果滚动的区间并没有超出渲染的范围
if (top >= this.renderRange.minY && bottom <= this.renderRange.maxY && left >= this.renderRange.minX && right <= this.renderRange.maxX) {
return;
}
8 years ago
var renderedCells = [], renderedKeys = {}, renderedWidgets = {};
var minX = this._getMaxScrollLeft(), minY = this._getMaxScrollTop(), maxX = 0, maxY = 0;
8 years ago
var count = 0;
8 years ago
for (var rowIndex = rowStartIndex; rowIndex <= rowStopIndex; rowIndex++) {
var rowDatum = this._rowSizeAndPositionManager.getSizeAndPositionOfCell(rowIndex);
for (var columnIndex = columnStartIndex; columnIndex <= columnStopIndex; columnIndex++) {
var key = rowIndex + "-" + columnIndex;
8 years ago
var columnDatum = this._columnSizeAndPositionManager.getSizeAndPositionOfCell(columnIndex);
var index = this.renderedKeys[key] && this.renderedKeys[key][2];
8 years ago
var child;
if (index >= 0) {
3 years ago
// if (columnDatum.size !== this.renderedCells[index]._width) {
// this.renderedCells[index]._width = columnDatum.size;
this.renderedCells[index].el.setWidth(columnDatum.size);
// }
// if (rowDatum.size !== this.renderedCells[index]._height) {
// this.renderedCells[index]._height = rowDatum.size;
this.renderedCells[index].el.setHeight(rowDatum.size);
// }
// if (this.renderedCells[index]._left !== columnDatum.offset + horizontalOffsetAdjustment) {
this.renderedCells[index].el.element.css("left", (columnDatum.offset + horizontalOffsetAdjustment) / BI.pixRatio + BI.pixUnit);
// }
// if (this.renderedCells[index]._top !== rowDatum.offset + verticalOffsetAdjustment) {
this.renderedCells[index].el.element.css("top", (rowDatum.offset + verticalOffsetAdjustment) / BI.pixRatio + BI.pixUnit);
// }
child = this.renderedCells[index].el;
renderedCells.push(this.renderedCells[index]);
8 years ago
} else {
3 years ago
var item = o.itemFormatter(o.items[rowIndex][columnIndex], rowIndex, columnIndex);
4 years ago
child = BI._lazyCreateWidget(BI.extend({
8 years ago
type: "bi.label",
width: columnDatum.size,
height: rowDatum.size
3 years ago
}, item, {
cls: (item.cls || "") + " grid-cell" + (rowIndex === 0 ? " first-row" : "") + (columnIndex === 0 ? " first-col" : ""),
8 years ago
_rowIndex: rowIndex,
_columnIndex: columnIndex,
_left: columnDatum.offset + horizontalOffsetAdjustment,
_top: rowDatum.offset + verticalOffsetAdjustment
}), this);
8 years ago
renderedCells.push({
el: child,
left: columnDatum.offset + horizontalOffsetAdjustment,
top: rowDatum.offset + verticalOffsetAdjustment,
8 years ago
_left: columnDatum.offset + horizontalOffsetAdjustment,
3 years ago
_top: rowDatum.offset + verticalOffsetAdjustment
3 years ago
// _width: columnDatum.size,
// _height: rowDatum.size
8 years ago
});
}
minX = Math.min(minX, columnDatum.offset + horizontalOffsetAdjustment);
maxX = Math.max(maxX, columnDatum.offset + horizontalOffsetAdjustment + columnDatum.size);
minY = Math.min(minY, rowDatum.offset + verticalOffsetAdjustment);
maxY = Math.max(maxY, rowDatum.offset + verticalOffsetAdjustment + rowDatum.size);
renderedKeys[key] = [rowIndex, columnIndex, count];
renderedWidgets[count] = child;
count++;
8 years ago
}
}
7 years ago
// 已存在的, 需要添加的和需要删除的
8 years ago
var existSet = {}, addSet = {}, deleteArray = [];
BI.each(renderedKeys, function (i, key) {
if (self.renderedKeys[i]) {
8 years ago
existSet[i] = key;
} else {
addSet[i] = key;
}
});
BI.each(this.renderedKeys, function (i, key) {
if (existSet[i]) {
8 years ago
return;
}
if (addSet[i]) {
8 years ago
return;
}
deleteArray.push(key[2]);
8 years ago
});
BI.each(deleteArray, function (i, index) {
7 years ago
// 性能优化,不调用destroy方法防止触发destroy事件
self.renderedCells[index].el._destroy();
8 years ago
});
var addedItems = [];
BI.each(addSet, function (index, key) {
7 years ago
addedItems.push(renderedCells[key[2]]);
8 years ago
});
// 与listview一样, 给上下文
this.container.addItems(addedItems, this);
7 years ago
// 拦截父子级关系
8 years ago
this.container._children = renderedWidgets;
this.container.attr("items", renderedCells);
8 years ago
this.renderedCells = renderedCells;
this.renderedKeys = renderedKeys;
this.renderRange = {minX: minX, minY: minY, maxX: maxX, maxY: maxY};
8 years ago
}
},
_getMaxScrollLeft: function () {
3 years ago
return Math.max(0, this._getContainerWidth() - this.options.width + (this.options.overflowX ? BI.DOM.getScrollWidth() : 0));
8 years ago
},
_getMaxScrollTop: function () {
3 years ago
return Math.max(0, this._getContainerHeight() - this.options.height + (this.options.overflowY ? BI.DOM.getScrollWidth() : 0));
},
_getContainerWidth: function () {
3 years ago
return this.columnCount * this.options.estimatedColumnSize;
3 years ago
},
_getContainerHeight: function () {
3 years ago
return this.rowCount * this.options.estimatedRowSize;
8 years ago
},
8 years ago
_populate: function (items) {
8 years ago
var self = this, o = this.options;
8 years ago
this._reRange();
8 years ago
if (items && items !== this.options.items) {
this.options.items = items;
3 years ago
this._calculateSizeAndPositionData();
8 years ago
}
3 years ago
this.container.setWidth(this._getContainerWidth());
this.container.setHeight(this._getContainerHeight());
3 years ago
3 years ago
// 元素未挂载时不能设置scrollTop
this._debounceRelease();
try {
this.element.scrollTop(o.scrollTop);
this.element.scrollLeft(o.scrollLeft);
} catch (e) {
7 years ago
}
3 years ago
this._calculateChildrenToRender();
8 years ago
},
setScrollLeft: function (scrollLeft) {
if (this.options.scrollLeft === scrollLeft) {
return;
}
this._scrollLock = true;
3 years ago
this.options.scrollLeft = BI.clamp(scrollLeft || 0, 0, this._getMaxScrollLeft());
8 years ago
this._debounceRelease();
8 years ago
this.element.scrollLeft(this.options.scrollLeft);
3 years ago
this._calculateChildrenToRender();
8 years ago
},
setScrollTop: function (scrollTop) {
if (this.options.scrollTop === scrollTop) {
return;
}
this._scrollLock = true;
3 years ago
this.options.scrollTop = BI.clamp(scrollTop || 0, 0, this._getMaxScrollTop());
8 years ago
this._debounceRelease();
8 years ago
this.element.scrollTop(this.options.scrollTop);
3 years ago
this._calculateChildrenToRender();
8 years ago
},
7 years ago
setColumnCount: function (columnCount) {
7 years ago
this.options.columnCount = columnCount;
7 years ago
},
setRowCount: function (rowCount) {
7 years ago
this.options.rowCount = rowCount;
7 years ago
},
8 years ago
setOverflowX: function (b) {
var self = this;
if (this.options.overflowX !== !!b) {
this.options.overflowX = !!b;
BI.nextTick(function () {
7 years ago
self.element.css({overflowX: b ? "auto" : "hidden"});
8 years ago
});
}
},
setOverflowY: function (b) {
var self = this;
if (this.options.overflowY !== !!b) {
this.options.overflowY = !!b;
BI.nextTick(function () {
7 years ago
self.element.css({overflowY: b ? "auto" : "hidden"});
8 years ago
});
}
},
getScrollLeft: function () {
return this.options.scrollLeft;
},
getScrollTop: function () {
return this.options.scrollTop;
},
getMaxScrollLeft: function () {
return this._getMaxScrollLeft();
},
getMaxScrollTop: function () {
return this._getMaxScrollTop();
},
setEstimatedColumnSize: function (width) {
this.options.estimatedColumnSize = width;
},
setEstimatedRowSize: function (height) {
this.options.estimatedRowSize = height;
},
7 years ago
// 重新计算children
8 years ago
_reRange: function () {
8 years ago
this.renderRange = {};
},
8 years ago
_clearChildren: function () {
this.container._children = {};
this.container.attr("items", []);
},
8 years ago
restore: function () {
BI.each(this.renderedCells, function (i, cell) {
8 years ago
cell.el._destroy();
8 years ago
});
8 years ago
this._clearChildren();
8 years ago
this.renderedCells = [];
this.renderedKeys = [];
8 years ago
this.renderRange = {};
8 years ago
this._scrollLock = false;
},
populate: function (items) {
if (items && items !== this.options.items) {
8 years ago
this.restore();
8 years ago
}
8 years ago
this._populate(items);
8 years ago
}
});
BI.GridView.EVENT_SCROLL = "EVENT_SCROLL";
BI.shortcut("bi.grid_view", BI.GridView);