bee-table/build/lib/bigData.js

515 lines
20 KiB
JavaScript
Raw Normal View History

2019-01-07 14:57:58 +08:00
"use strict";
2018-12-17 11:12:10 +08:00
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports["default"] = bigData;
2019-01-07 14:57:58 +08:00
var _react = require("react");
2018-12-17 11:12:10 +08:00
var _react2 = _interopRequireDefault(_react);
2019-01-07 14:57:58 +08:00
var _propTypes = require("prop-types");
2018-12-17 11:12:10 +08:00
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
var defaultHeight = 30;
var rowDiff = 3; //行差值
2019-01-07 14:57:58 +08:00
var treeTypeIndex = 0;
2018-12-17 11:12:10 +08:00
function bigData(Table) {
var _class, _temp, _initialiseProps;
return _temp = _class = function (_Component) {
2018-12-17 11:12:10 +08:00
_inherits(BigData, _Component);
function BigData(props) {
_classCallCheck(this, BigData);
2018-12-20 18:07:30 +08:00
var _this2 = _possibleConstructorReturn(this, _Component.call(this, props));
2018-12-17 11:12:10 +08:00
2018-12-20 18:07:30 +08:00
_initialiseProps.call(_this2);
_this2.state = {
2018-12-17 11:12:10 +08:00
scrollLeft: 0,
scrollTop: 0
};
2018-12-20 18:07:30 +08:00
var rowHeight = _this2.props.height ? _this2.props.height : defaultHeight;
//默认显示25条rowsInView根据定高算的。在非固定高下这个只是一个大概的值。
2018-12-26 10:23:51 +08:00
var scrollY = _this2.props.scroll.y ? parseInt(_this2.props.scroll.y) : 0;
_this2.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
2018-12-20 18:07:30 +08:00
_this2.currentIndex = 0;
_this2.loadCount = props.loadBuffer ? _this2.rowsInView + props.loadBuffer * 2 : 26; //一次加载多少数据
_this2.cachedRowHeight = []; //缓存每行的高度
2019-01-07 14:57:58 +08:00
_this2.cachedRowParentIndex = [];
_this2.expandChildRowKeys = [];
_this2.firstLevelKey = [];
_this2.keys = [];
2018-12-20 18:07:30 +08:00
_this2.lastScrollTop = 0;
_this2.currentScrollTop = 0;
_this2.startIndex = _this2.currentIndex; //数据开始位置
2019-01-07 14:57:58 +08:00
_this2.endIndex = _this2.currentIndex + _this2.loadCount; //数据结束位置
2018-12-20 18:07:30 +08:00
_this2.setRowHeight = _this2.setRowHeight.bind(_this2);
2019-01-07 14:57:58 +08:00
_this2.setRowParentIndex = _this2.setRowParentIndex.bind(_this2);
2018-12-20 18:07:30 +08:00
return _this2;
2018-12-17 11:12:10 +08:00
}
2019-01-07 14:57:58 +08:00
BigData.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
2019-01-10 19:42:30 +08:00
var props = this.props;
var currentIndex = nextProps.currentIndex,
data = nextProps.data;
var _this = this,
dataLen = data.length;
2019-01-10 19:42:30 +08:00
if (nextProps.scroll.y !== props.scroll.y) {
var rowHeight = nextProps.height ? nextProps.height : defaultHeight;
2019-01-07 14:57:58 +08:00
var scrollY = nextProps.scroll.y ? parseInt(nextProps.scroll.y) : 0;
_this.rowsInView = scrollY ? Math.ceil(scrollY / rowHeight) : 20;
_this.loadCount = props.loadBuffer ? _this.rowsInView + props.loadBuffer * 2 : 26; //一次加载多少数据
_this.currentIndex = 0;
_this.startIndex = _this.currentIndex; //数据开始位置
_this.endIndex = _this.currentIndex + _this.loadCount; //数据结束位置
if (_this.endIndex > dataLen) {
_this.endIndex = dataLen;
}
2019-01-07 14:57:58 +08:00
}
2019-01-16 13:57:31 +08:00
if (nextProps.data !== props.data) {
_this.computeCachedRowParentIndex(nextProps.data);
}
//如果传currentIndex会判断该条数据是否在可视区域如果没有的话则重新计算startIndex和endIndex
if (currentIndex !== -1 && currentIndex !== this.currentIndex) {
_this.setStartAndEndIndex(currentIndex, dataLen);
2019-01-16 13:57:31 +08:00
}
2019-01-07 14:57:58 +08:00
};
BigData.prototype.componentDidMount = function componentDidMount() {
2019-01-16 13:57:31 +08:00
var data = this.props.data;
2019-01-07 14:57:58 +08:00
2019-01-16 13:57:31 +08:00
this.computeCachedRowParentIndex(data);
2019-01-07 14:57:58 +08:00
};
2019-01-16 13:57:31 +08:00
/**
*设置data中每个元素的parentIndex
*
*/
BigData.prototype.setStartAndEndIndex = function setStartAndEndIndex(currentIndex, dataLen) {
var _this = this;
2019-02-13 17:11:07 +08:00
if (currentIndex > _this.currentIndex + _this.rowsInView) {
_this.currentIndex = currentIndex;
_this.endIndex = _this.currentIndex; //数据开始位置
_this.startIndex = _this.currentIndex - _this.loadCount; //数据结束位置
if (_this.endIndex > dataLen) {
_this.endIndex = dataLen;
}
if (_this.startIndex < 0) {
_this.startIndex = 0;
}
//重新设定scrollTop值
_this.scrollTop = _this.getSumHeight(0, _this.endIndex - _this.rowsInView + 2);
} else if (currentIndex < _this.currentIndex) {
_this.currentIndex = currentIndex;
_this.startIndex = currentIndex;
_this.endIndex = currentIndex + _this.loadCount;
if (_this.endIndex > dataLen) {
_this.endIndex = dataLen;
}
if (_this.startIndex < 0) {
_this.startIndex = 0;
}
//重新设定scrollTop值
_this.scrollTop = _this.getSumHeight(0, _this.startIndex);
}
};
2019-01-07 14:57:58 +08:00
BigData.prototype.getRowKey = function getRowKey(record, index) {
var rowKey = this.props.rowKey;
2019-01-16 13:57:31 +08:00
var key = typeof rowKey === "function" ? rowKey(record, index) : record[rowKey];
2019-01-07 14:57:58 +08:00
return key;
};
/**
*判断是否是树形结构
*
*/
BigData.prototype.checkIsTreeType = function checkIsTreeType() {
var data = this.props.data;
var rs = false;
var len = data.length > 30 ? 30 : data.length;
//取前三十个看看是否有children属性有则为树形结构
for (var i = 0; i < len; i++) {
if (data[i].children) {
rs = true;
break;
}
}
return rs;
};
BigData.prototype.getData = function getData(data, parentIndex) {
2019-01-16 13:57:31 +08:00
var _this3 = this;
2019-01-07 14:57:58 +08:00
data.forEach(function (subItem, subIndex) {
2019-01-16 13:57:31 +08:00
_this3.cachedRowParentIndex[treeTypeIndex] = parentIndex;
_this3.keys[treeTypeIndex] = _this3.getRowKey(subItem, subIndex);
2019-01-07 14:57:58 +08:00
treeTypeIndex++;
if (subItem.children) {
2019-01-16 13:57:31 +08:00
_this3.getData(subItem.children, parentIndex);
2019-01-07 14:57:58 +08:00
}
});
};
BigData.prototype.componentWillUnmount = function componentWillUnmount() {
this.cachedRowHeight = [];
this.cachedRowParentIndex = [];
};
2018-12-17 11:12:10 +08:00
/**
*获取数据区高度
*
*
**/
2018-12-17 11:12:10 +08:00
BigData.prototype.getContentHeight = function getContentHeight() {
if (!this.props.data) return 0;
return this.getSumHeight(0, this.props.data.length);
};
BigData.prototype.getSumHeight = function getSumHeight(start, end) {
var height = this.props.height;
2018-12-17 11:12:10 +08:00
var rowHeight = height ? height : defaultHeight;
2019-01-07 14:57:58 +08:00
var sumHeight = 0,
currentKey = void 0,
currentRowHeight = rowHeight;
2018-12-17 11:12:10 +08:00
for (var i = start; i < end; i++) {
2019-01-07 14:57:58 +08:00
if (this.cachedRowHeight[i] == undefined) {
if (this.treeType) {
currentKey = this.keys[i];
currentRowHeight = 0;
if (this.firstLevelKey.indexOf(currentKey) >= 0 || this.expandChildRowKeys.indexOf(currentKey) >= 0) {
currentRowHeight = rowHeight;
}
}
sumHeight += currentRowHeight;
} else {
sumHeight += this.cachedRowHeight[i];
}
2018-12-17 11:12:10 +08:00
}
return sumHeight;
2018-12-17 11:12:10 +08:00
};
2019-01-07 14:57:58 +08:00
/**
*@description 根据返回的scrollTop计算当前的索引此处做了两次缓存一个是根据上一次的currentIndex计算当前currentIndex另一个是根据当前内容区的数据是否在缓存中如果在则不重新render页面
*@param 最新一次滚动的scrollTop
2019-01-07 14:57:58 +08:00
*@param treeType是否是树状表
*/
BigData.prototype.setRowHeight = function setRowHeight(height, index) {
this.cachedRowHeight[index] = height;
};
2018-12-17 11:12:10 +08:00
2019-01-07 14:57:58 +08:00
BigData.prototype.setRowParentIndex = function setRowParentIndex(parentIndex, index) {}
2019-01-16 13:57:31 +08:00
// this.cachedRowParentIndex[index] = parentIndex;
2019-01-07 14:57:58 +08:00
/**
*
*根据当前行号获取该行的父节点行号
* @param {*} currentIndex 当前行号
*/
;
BigData.prototype.getParentIndex = function getParentIndex(targetIndex) {
var data = this.props.data;
var parentIndex = -1;
parentIndex = this.getIndex(data, -1, targetIndex);
if (parentIndex < 0) {
//小于0说明没有展开的子节点
parentIndex = targetIndex;
}
return parentIndex;
};
BigData.prototype.getIndex = function getIndex(data, index, targetIndex) {
var parentIndex = index;
for (var i = 0; i < data.length; i++) {
index++;
if (targetIndex <= index) {
break;
}
if (data[i].children) {
this.getIndex(data[i].children, index, targetIndex);
}
}
return parentIndex;
};
2018-12-17 11:12:10 +08:00
BigData.prototype.render = function render() {
var data = this.props.data;
var scrollTop = this.scrollTop;
var endIndex = this.endIndex,
2018-12-20 18:07:30 +08:00
startIndex = this.startIndex;
var lazyLoad = {
2019-01-07 14:57:58 +08:00
startIndex: startIndex,
startParentIndex: startIndex //为树状节点做准备
};
2019-01-07 14:57:58 +08:00
if (this.treeType) {
var preSubCounts = this.cachedRowParentIndex.findIndex(function (item) {
return item == startIndex;
});
var sufSubCounts = this.cachedRowParentIndex.findIndex(function (item) {
return item == endIndex;
});
lazyLoad.preHeight = this.getSumHeight(0, preSubCounts > -1 ? preSubCounts : 0);
lazyLoad.sufHeight = this.getSumHeight(sufSubCounts + 1 > 0 ? sufSubCounts + 1 : this.cachedRowParentIndex.length, this.cachedRowParentIndex.length);
if (preSubCounts > 0) {
lazyLoad.startIndex = preSubCounts;
}
} else {
lazyLoad.preHeight = this.getSumHeight(0, startIndex);
lazyLoad.sufHeight = this.getSumHeight(endIndex, data.length);
}
// console.log('*******ScrollTop*****'+scrollTop);
return _react2["default"].createElement(Table, _extends({}, this.props, {
2018-12-20 18:07:30 +08:00
data: data.slice(startIndex, endIndex),
lazyLoad: lazyLoad,
2019-01-10 19:42:30 +08:00
handleScrollY: this.handleScrollY,
scrollTop: scrollTop,
2019-01-07 14:57:58 +08:00
setRowHeight: this.setRowHeight,
setRowParentIndex: this.setRowParentIndex,
onExpand: this.onExpand,
onExpandedRowsChange: this.onExpandedRowsChange
// className={'lazy-table'}
}));
2018-12-17 11:12:10 +08:00
};
return BigData;
}(_react.Component), _class.defaultProps = {
data: [],
2019-01-07 14:57:58 +08:00
loadBuffer: 5,
2019-01-16 13:57:31 +08:00
rowKey: "key",
2019-01-10 19:42:30 +08:00
onExpand: function onExpand() {},
scroll: {},
2019-01-25 11:17:52 +08:00
currentIndex: -1,
isTree: false
}, _class.propTypes = {
loadBuffer: _propTypes2["default"].number
}, _initialiseProps = function _initialiseProps() {
2019-01-16 13:57:31 +08:00
var _this4 = this;
this.computeCachedRowParentIndex = function (data) {
2019-01-25 11:17:52 +08:00
var isTree = _this4.props.isTree;
var isTreeType = isTree ? true : _this4.checkIsTreeType();
2019-01-16 13:57:31 +08:00
if (isTreeType) {
data.forEach(function (item, index) {
_this4.firstLevelKey[index] = _this4.getRowKey(item, index);
_this4.cachedRowParentIndex[treeTypeIndex] = index;
//保存所有的keys跟小标对应起来
_this4.keys[treeTypeIndex] = _this4.getRowKey(item, index);
treeTypeIndex++;
if (item.children) {
_this4.getData(item.children, index);
}
});
}
};
2019-01-07 14:57:58 +08:00
2019-01-10 19:42:30 +08:00
this.handleScrollY = function (nextScrollTop, treeType) {
2019-01-07 14:57:58 +08:00
//树表逻辑
// 关键点是动态的获取startIndex和endIndex
// 法子一子节点也看成普通tr最开始需要设置一共有多少行哪行显示哪行不显示如何确定
// 动态取start = current+buffer对应的父节点、end = start+loadCount+row的height为0的行数 展开节点的下一个节点作为end值
2019-01-16 13:57:31 +08:00
var _this = _this4;
var _this$props = _this.props,
data = _this$props.data,
height = _this$props.height,
_this$props$scroll = _this$props.scroll,
2018-12-26 14:26:42 +08:00
scroll = _this$props$scroll === undefined ? {} : _this$props$scroll,
loadBuffer = _this$props.loadBuffer;
2018-12-20 18:07:30 +08:00
var rowHeight = height ? height : defaultHeight;
2018-12-20 18:07:30 +08:00
var _this$currentIndex = _this.currentIndex,
currentIndex = _this$currentIndex === undefined ? 0 : _this$currentIndex,
loadCount = _this.loadCount,
scrollTop = _this.scrollTop,
currentScrollTop = _this.currentScrollTop;
2018-12-20 18:07:30 +08:00
var endIndex = _this.endIndex,
startIndex = _this.startIndex;
var needRender = _this.state.needRender;
_this.scrollTop = nextScrollTop;
2018-12-20 18:07:30 +08:00
var viewHeight = parseInt(scroll.y);
2019-01-07 14:57:58 +08:00
_this.treeType = treeType;
2018-12-26 10:23:51 +08:00
// let index = currentIndex;//记录下次当前位置
// let temp = currentIndex ?nextScrollTop - currentScrollTop:nextScrollTop;
// const isOrder = temp > 0 ?true:false;//true为向下滚动、false为向上滚动
// //根据scrollTop计算下次当前索引的位置
// if(isOrder){
// while (temp > 0) {
// temp -= this.cachedRowHeight[index] || rowHeight
// if(temp > 0){
// index += 1
// //保存当前index对应的scrollTop
// this.currentScrollTop += this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }else{
// while(temp < 0){
// temp += this.cachedRowHeight[index] || rowHeight
// if(temp < 0){
// index -= 1
// this.currentScrollTop -= this.cachedRowHeight[index]|| rowHeight;
// }
// }
// }
var index = 0;
var temp = nextScrollTop;
2019-01-07 14:57:58 +08:00
var currentKey = void 0;
2018-12-26 10:23:51 +08:00
while (temp > 0) {
2019-01-16 13:57:31 +08:00
var currentRowHeight = _this4.cachedRowHeight[index];
2019-01-07 14:57:58 +08:00
if (currentRowHeight === undefined) {
2019-01-16 13:57:31 +08:00
if (_this4.treeType) {
currentKey = _this4.keys[index];
2019-01-07 14:57:58 +08:00
currentRowHeight = 0;
2019-01-16 13:57:31 +08:00
if (_this4.firstLevelKey.indexOf(currentKey) >= 0 || _this4.expandChildRowKeys.indexOf(currentKey) >= 0) {
2019-01-07 14:57:58 +08:00
currentRowHeight = rowHeight;
}
} else {
currentRowHeight = rowHeight;
}
}
temp -= currentRowHeight;
2018-12-26 10:23:51 +08:00
if (temp > 0) {
index += 1;
}
}
// console.log('currentIndex****',index);
2018-12-26 10:23:51 +08:00
var isOrder = index - currentIndex > 0 ? true : false;
if (index < 0) index = 0;
//如果之前的索引和下一次的不一样则重置索引和滚动的位置
if (currentIndex !== index) {
2018-12-20 18:07:30 +08:00
_this.currentIndex = index;
var rowsInView = 0; //可视区域显示多少行
var rowsHeight = 0; //可视区域内容高度
2019-01-07 14:57:58 +08:00
var tempIndex = index;
//如果可视区域中需要展示的数据已经在缓存中则不重现render。
2018-12-20 18:07:30 +08:00
if (viewHeight) {
//有时滚动过快时this.cachedRowHeight[rowsInView + index]为undifined
2019-01-07 14:57:58 +08:00
2019-01-16 13:57:31 +08:00
while (rowsHeight < viewHeight && tempIndex < _this4.cachedRowHeight.length) {
if (_this4.cachedRowHeight[tempIndex]) {
rowsHeight += _this4.cachedRowHeight[tempIndex];
2019-01-07 14:57:58 +08:00
if (treeType && _this.cachedRowParentIndex[tempIndex] !== tempIndex || !treeType) {
rowsInView++;
}
}
tempIndex++;
2018-12-20 18:07:30 +08:00
}
2019-01-07 14:57:58 +08:00
if (treeType) {
var treeIndex = index;
index = _this.cachedRowParentIndex[treeIndex];
if (index === undefined) {
// console.log('index is undefined********'+treeIndex);
2019-01-16 13:57:31 +08:00
index = _this4.getParentIndex(treeIndex);
// console.log("getParentIndex****"+index);
2019-01-07 14:57:58 +08:00
}
}
// console.log('parentIndex*********',index);
2019-01-07 14:57:58 +08:00
// 如果rowsInView 小于 缓存的数据则重新render
// 向下滚动 下临界值超出缓存的endIndex则重新渲染
if (rowsInView + index > endIndex - rowDiff && isOrder) {
2019-01-07 14:57:58 +08:00
startIndex = index - loadBuffer > 0 ? index - loadBuffer : 0;
endIndex = startIndex + loadCount;
//树状结构则根据当前的节点重新计算startIndex和endIndex
// if(treeType){
// const currentParentIndex = _this.cachedRowParentIndex[index];
// startIndex = currentParentIndex - loadBuffer>0?currentParentIndex - loadBuffer:0;
// endIndex = startIndex + loadCount;
// // console.log(endIndex,"endIndex的parentIndex",parentEndIndex);
// // endIndex = parentEndIndex +1
// }else{
// startIndex = index - loadBuffer>0?index - loadBuffer:0;
// endIndex = startIndex + loadCount;
// }
if (endIndex > data.length) {
endIndex = data.length;
2018-12-20 18:07:30 +08:00
}
2019-01-16 13:57:31 +08:00
if (startIndex !== _this4.startIndex || endIndex !== _this4.endIndex) {
_this4.startIndex = startIndex;
_this4.endIndex = endIndex;
_this4.setState({ needRender: !needRender });
2019-01-07 14:57:58 +08:00
}
// console.log(
// "===================",
// "**index**" + index,
// " **startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
2018-12-20 18:07:30 +08:00
}
// 向上滚动当前的index是否已经加载currentIndex若干上临界值小于startIndex则重新渲染
if (!isOrder && index < startIndex + rowDiff) {
2018-12-26 14:26:42 +08:00
startIndex = index - loadBuffer;
2018-12-20 18:07:30 +08:00
if (startIndex < 0) {
startIndex = 0;
}
2019-01-16 13:57:31 +08:00
if (startIndex !== _this4.startIndex || endIndex !== _this4.endIndex) {
_this4.startIndex = startIndex;
_this4.endIndex = _this4.startIndex + loadCount;
_this4.setState({ needRender: !needRender });
2019-01-07 14:57:58 +08:00
}
// console.log(
// "**index**" + index,
// "**startIndex**" + this.startIndex,
// "**endIndex**" + this.endIndex
// );
2018-12-20 18:07:30 +08:00
}
}
2019-01-07 14:57:58 +08:00
}
};
this.onExpand = function (expandState, record) {
2019-01-16 13:57:31 +08:00
var _this = _this4;
2019-01-07 14:57:58 +08:00
// 展开
if (expandState) {
record.children && record.children.forEach(function (item, index) {
_this.expandChildRowKeys.push(_this.getRowKey(item, index));
});
} else {
// 收起
record.children && record.children.forEach(function (item, index) {
_this.expandChildRowKeys.splice(_this.expandChildRowKeys.findIndex(function (fitem) {
return fitem.key === item.key;
}), 1);
});
}
2019-01-10 19:42:30 +08:00
_this.props.onExpand(expandState, record);
};
}, _temp;
2018-12-17 11:12:10 +08:00
}
2019-01-07 14:57:58 +08:00
module.exports = exports["default"];