bee-table6/build/TableHeader.js

953 lines
35 KiB
JavaScript
Raw Normal View History

"use strict";
2017-01-12 08:53:51 +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; };
var _react = require("react");
2017-01-12 08:53:51 +08:00
var _react2 = _interopRequireDefault(_react);
2018-12-02 16:53:06 +08:00
var _reactDom = require("react-dom");
var _reactDom2 = _interopRequireDefault(_reactDom);
var _propTypes = require("prop-types");
var _propTypes2 = _interopRequireDefault(_propTypes);
var _throttleDebounce = require("throttle-debounce");
2019-07-26 09:47:18 +08:00
var _utils = require("./lib/utils");
2018-05-11 11:30:56 +08:00
var _FilterType = require("./FilterType");
var _FilterType2 = _interopRequireDefault(_FilterType);
2017-01-12 08:53:51 +08:00
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 propTypes = {
clsPrefix: _propTypes2["default"].string,
rowStyle: _propTypes2["default"].object,
rows: _propTypes2["default"].array
2017-01-12 08:53:51 +08:00
};
2019-04-22 10:24:52 +08:00
function getDiv(id) {
var div = document.createElement("div");
2019-04-23 18:47:27 +08:00
div.className = "u-table-drag-hidden-cont";
2019-04-22 10:24:52 +08:00
div.id = id;
return div;
}
2017-01-12 08:53:51 +08:00
var TableHeader = function (_Component) {
_inherits(TableHeader, _Component);
function TableHeader(props) {
_classCallCheck(this, TableHeader);
2018-05-11 09:29:43 +08:00
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
2019-08-30 13:43:25 +08:00
_initialiseProps.call(_this);
2018-05-11 11:30:56 +08:00
2018-05-11 09:29:43 +08:00
_this.currentObj = null;
2018-05-14 10:01:06 +08:00
_this.theadKey = new Date().getTime();
2018-05-11 11:30:56 +08:00
_this.drag = {
2018-12-02 16:53:06 +08:00
option: ''
};
2018-12-03 11:17:39 +08:00
_this.minWidth = 80; //确定最小宽度就是80
2018-12-02 16:53:06 +08:00
_this.table = null;
2018-12-18 19:32:36 +08:00
_this._thead = null; //当前对象
2019-04-25 14:19:48 +08:00
_this.event = false; //避免多次绑定问题
2019-06-26 16:37:41 +08:00
_this.lastColumWidth = null; //非固定列最后一列的初始化宽度
2019-07-18 11:25:29 +08:00
_this.fixedTable = {};
2018-05-11 09:29:43 +08:00
return _this;
2017-01-12 08:53:51 +08:00
}
2018-12-02 16:53:06 +08:00
TableHeader.prototype.componentDidUpdate = function componentDidUpdate() {
this.initTable();
this.initEvent();
};
2019-04-22 10:24:52 +08:00
TableHeader.prototype.componentDidMount = function componentDidMount() {
var uid = "_table_uid_" + new Date().getTime();
this._table_none_cont_id = uid;
var div = getDiv(uid);
document.querySelector("body").appendChild(div);
2019-04-19 11:02:20 +08:00
};
2019-03-20 12:38:43 +08:00
TableHeader.prototype.componentWillUnmount = function componentWillUnmount() {
2019-11-14 09:49:40 +08:00
var _this2 = this;
2019-07-18 11:25:29 +08:00
this.fixedTable = null;
2019-03-25 10:45:05 +08:00
if (!this.table) return;
2019-03-20 17:41:06 +08:00
if (this.props.draggable) {
this.removeDragAbleEvent();
}
if (this.props.dragborder) {
this.removeDragBorderEvent();
}
2019-11-14 09:49:40 +08:00
this.doEventList(this.table.tr, function (tr) {
_this2.eventListen([{ key: 'mousedown', fun: _this2.onTrMouseDown }], 'remove', tr);
});
// this.eventListen([{key:'mousedown',fun:this.onTrMouseDown}],'remove',this.table.tr[0]);
2019-07-18 11:25:29 +08:00
this.eventListen([{ key: 'mouseup', fun: this.bodyonLineMouseUp }], 'remove', document.body);
2018-12-02 16:53:06 +08:00
};
2018-12-02 16:53:06 +08:00
/**
* 获取table的属性存放在this.table (公用方法)
2018-12-18 19:32:36 +08:00
* @returns
* @memberof TableHeader
2018-12-02 16:53:06 +08:00
*/
TableHeader.prototype.initTable = function initTable() {
2019-03-20 12:38:43 +08:00
var contentTable = this.props.contentTable;
2018-12-02 16:53:06 +08:00
if (!this.props.dragborder && !this.props.draggable) return;
2018-12-18 19:32:36 +08:00
var tableDome = this._thead.parentNode;
2018-12-02 16:53:06 +08:00
var table = {};
if (tableDome && tableDome.nodeName && tableDome.nodeName.toUpperCase() == "TABLE") {
table.table = tableDome;
table.cols = tableDome.getElementsByTagName("col");
table.ths = tableDome.getElementsByTagName("th");
2019-04-19 11:02:20 +08:00
table.tr = tableDome.getElementsByTagName("tr");
table.tableBody = contentTable.querySelector('.u-table-scroll .u-table-body') && contentTable.querySelector('.u-table-scroll .u-table-body');
2019-07-03 21:12:17 +08:00
table.tableBodyCols = contentTable.querySelector('.u-table-scroll .u-table-body') && contentTable.querySelector('.u-table-scroll .u-table-body').getElementsByTagName("col");
2018-12-02 16:53:06 +08:00
}
2019-03-20 12:38:43 +08:00
table.fixedLeftHeaderTable = contentTable.querySelector('.u-table-fixed-left .u-table-header');
table.fixedRighHeadertTable = contentTable.querySelector('.u-table-fixed-right .u-table-header');
table.contentTableHeader = contentTable.querySelector('.u-table-scroll .u-table-header');
table.fixedLeftBodyTable = contentTable.querySelector('.u-table-fixed-left .u-table-body-outer');
table.fixedRightBodyTable = contentTable.querySelector('.u-table-fixed-right .u-table-body-outer');
table.innerTableBody = contentTable.querySelector('.u-table-scroll .u-table-body table');
2018-12-02 16:53:06 +08:00
this.table = table;
if (!this.props.dragborder) return;
if (document.getElementById("u-table-drag-thead-" + this.theadKey)) {
this.fixedTable = {};
var _fixedParentContext = document.getElementById("u-table-drag-thead-" + this.theadKey).parentNode;
var siblingDom = _fixedParentContext.parentNode.nextElementSibling;
if (siblingDom) {
var fixedTable = siblingDom.querySelector("table");
this.fixedTable.table = fixedTable;
this.fixedTable.cols = fixedTable.getElementsByTagName("col");
// this.fixedTable.ths = fixedTable.tableDome.getElementsByTagName("th");
}
}
};
2019-04-19 11:02:20 +08:00
/**
2019-04-22 10:24:52 +08:00
* 事件初始化
*/
2018-12-02 16:53:06 +08:00
2019-04-22 10:24:52 +08:00
TableHeader.prototype.initEvent = function initEvent() {
2019-11-14 09:49:40 +08:00
var _this3 = this;
2019-04-25 14:19:48 +08:00
var _props = this.props,
dragborder = _props.dragborder,
draggable = _props.draggable,
rows = _props.rows;
// 当传入的 columns 为空时,不绑定拖拽事件
2019-04-25 14:19:48 +08:00
if (Object.prototype.toString.call(rows) === '[object Array]' && rows.length === 0) {
return;
}
2019-04-25 14:19:48 +08:00
if (!this.event) {
//避免多次绑定问题。
this.event = true;
if (dragborder) {
this.dragBorderEventInit(); //列宽
}
if (draggable) {
2019-04-25 14:19:48 +08:00
this.dragAbleEventInit(); //交换列
}
if (this.table && this.table.tr) {
2019-11-14 09:49:40 +08:00
// this.eventListen([{key:'mousedown',fun:this.onTrMouseDown}],'',this.table.tr[0]);//body mouseup
this.doEventList(this.table.tr, function (tr) {
_this3.eventListen([{ key: 'mousedown', fun: _this3.onTrMouseDown }], '', tr); //body mouseup
});
2019-04-25 14:19:48 +08:00
}
this.eventListen([{ key: 'mouseup', fun: this.bodyonLineMouseUp }], '', document.body); //body mouseup
2019-04-23 16:43:34 +08:00
}
2019-04-19 11:02:20 +08:00
};
2019-11-14 09:49:40 +08:00
TableHeader.prototype.doEventList = function doEventList(trs, action) {
for (var index = 0; index < trs.length; index++) {
action(trs[index]);
}
};
2018-12-18 19:32:36 +08:00
/**
2019-04-22 10:24:52 +08:00
* 拖拽列宽事件的监听
2018-12-18 19:32:36 +08:00
*/
2019-04-22 10:24:52 +08:00
TableHeader.prototype.dragBorderEventInit = function dragBorderEventInit() {
2019-11-14 09:49:40 +08:00
var _this4 = this;
2019-04-23 16:43:34 +08:00
if (!this.props.dragborder) return;
var events = [{ key: 'mouseup', fun: this.onTrMouseUp }, { key: 'mousemove', fun: this.onTrMouseMove }];
2019-11-14 09:49:40 +08:00
this.doEventList(this.table.tr, function (tr) {
_this4.eventListen(events, '', tr); //表示把事件添加到th元素上
});
// this.eventListen(events,'',this.table.tr[0]);//表示把事件添加到th元素上
2018-12-02 16:53:06 +08:00
};
2018-12-18 19:32:36 +08:00
/**
* 删除拖动改变列宽的事件监听
2018-12-18 19:32:36 +08:00
*/
TableHeader.prototype.removeDragBorderEvent = function removeDragBorderEvent() {
2019-11-14 09:49:40 +08:00
var _this5 = this;
2019-04-23 16:43:34 +08:00
var events = [{ key: 'mouseup', fun: this.onTrMouseUp }, { key: 'mousemove', fun: this.onTrMouseMove }];
2019-11-14 09:49:40 +08:00
// this.eventListen(events,'remove',this.table.tr[0]);
this.doEventList(this.table.tr, function (tr) {
_this5.eventListen(events, 'remove', _this5.table.tr);
});
};
2019-04-22 10:24:52 +08:00
TableHeader.prototype.eventListen = function eventListen(events, type, eventSource) {
2019-04-23 16:43:34 +08:00
if (!this.table) return;
2019-07-02 17:11:40 +08:00
if (!eventSource) {
console.log("Please set the attributes of column !");
return;
}
2019-04-22 10:24:52 +08:00
var tr = this.table.tr;
2018-12-18 19:32:36 +08:00
2018-12-02 16:53:06 +08:00
for (var i = 0; i < events.length; i++) {
var _event = events[i];
2019-04-22 10:24:52 +08:00
if (type === "remove") {
_utils.EventUtil.removeHandler(eventSource, _event.key, _event.fun);
2018-12-02 16:53:06 +08:00
} else {
2019-04-22 10:24:52 +08:00
_utils.EventUtil.addHandler(eventSource, _event.key, _event.fun);
2018-12-02 16:53:06 +08:00
}
}
2018-12-02 16:53:06 +08:00
};
2018-12-18 19:32:36 +08:00
/**
*
2019-04-22 10:24:52 +08:00
*根据 data-type 来获取当前拖拽的对象的Object如果为null表示拖动的对象并非是online
2018-12-18 19:32:36 +08:00
* @memberof TableHeader
*/
/**
2019-04-22 10:24:52 +08:00
* 调整列宽的down事件
* @memberof TableHeader
*/
2018-12-18 19:32:36 +08:00
/**
* 根据当前节点查找到有data-type类型的容器返回
* @memberof TableHeader
*/
2019-06-06 16:15:47 +08:00
/**
* 判断当前的target 是否是 th如果不是直接递归查找
* @memberof TableHeader
*/
2018-12-18 19:32:36 +08:00
/**
2019-04-22 10:24:52 +08:00
* 调整列宽的move事件
2018-12-18 19:32:36 +08:00
* @memberof TableHeader
*/
2018-12-02 16:53:06 +08:00
2018-12-18 19:32:36 +08:00
/**
2019-04-22 10:24:52 +08:00
* 调整列宽的up事件
* @memberof TableHeader
*/
2018-12-02 16:53:06 +08:00
2019-04-22 10:24:52 +08:00
TableHeader.prototype.mouseClear = function mouseClear() {
2018-12-05 12:39:29 +08:00
if (!this.drag || !this.drag.option) return;
var rows = this.props.rows;
var data = { rows: rows[0], cols: this.table.cols, currIndex: this.drag.currIndex };
this.props.afterDragColWidth && this.props.afterDragColWidth(data);
2018-12-02 16:53:06 +08:00
this.drag = {
option: ""
};
2019-04-22 10:24:52 +08:00
this.clearThsDr();
2018-12-02 16:53:06 +08:00
};
/**
2019-04-22 10:24:52 +08:00
* 当前对象上绑定全局事件用于拖拽区域以外时的事件处理
* @param {*} events
* @param {*} type
* @memberof TableHeader
*/
2018-12-18 19:32:36 +08:00
/**
2019-04-22 10:24:52 +08:00
*相关滚动条联动操作
*
2018-12-18 19:32:36 +08:00
* @memberof TableHeader
*/
2019-04-22 10:24:52 +08:00
//---拖拽交换列代码----start-----
2018-12-18 19:32:36 +08:00
/**
* 添加换列的事件监听
2018-12-02 16:53:06 +08:00
*/
2019-04-22 10:24:52 +08:00
TableHeader.prototype.dragAbleEventInit = function dragAbleEventInit() {
if (!this.props.draggable) return;
2018-12-02 16:53:06 +08:00
var events = [{ key: 'dragstart', fun: this.onDragStart }, //用户开始拖动元素时触发
{ key: 'dragover', fun: this.onDragOver }, //当某被拖动的对象在另一对象容器范围内拖动时触发此事件
{ key: 'drop', fun: this.onDrop }, //在一个拖动过程中,释放鼠标键时触发此事件
2018-12-18 19:32:36 +08:00
2019-04-22 10:24:52 +08:00
{ key: 'dragenter', fun: this.onDragEnter }, { key: 'dragend', fun: this.onDragEnd }, { key: 'dragleave', fun: this.onDragLeave }];
this.eventListen(events, '', this.table.tr[0]); //表示把事件添加到th元素上
2018-12-02 16:53:06 +08:00
};
2018-12-18 19:32:36 +08:00
/**
* 删除换列的事件监听
*/
2018-12-02 16:53:06 +08:00
TableHeader.prototype.removeDragAbleEvent = function removeDragAbleEvent() {
var events = [{ key: 'dragstart', fun: this.onDragStart }, { key: 'dragover', fun: this.onDragOver }, { key: 'drop', fun: this.onDrop }, { key: 'dragenter', fun: this.onDragEnter }, { key: 'dragend', fun: this.onDragEnd }, { key: 'dragleave', fun: this.onDragLeave }];
this.eventListen(events, 'remove', this.table.tr[0]);
2018-12-02 16:53:06 +08:00
};
/**
2018-12-18 19:32:36 +08:00
* 开始调整交换列的事件
2018-12-02 16:53:06 +08:00
*/
2018-12-18 19:32:36 +08:00
2018-12-02 16:53:06 +08:00
/**
2019-04-22 10:24:52 +08:00
* 在一个拖动过程中释放鼠标键时触发此事件目标事件
* @memberof TableHeader
*/
2018-12-02 16:53:06 +08:00
2018-12-18 19:32:36 +08:00
/**
* 获取当前th上的对象数据
* @param {*} e
* @returns
* @memberof TableHeader
*/
2019-04-22 10:24:52 +08:00
TableHeader.prototype.getCurrentEventData = function getCurrentEventData(th) {
2018-12-03 16:03:32 +08:00
if (!th) {
console.log(" event target is not th ! ");
return null;
}
var key = th.getAttribute('data-line-key');
2018-12-02 16:53:06 +08:00
var data = this.props.rows[0].find(function (da) {
return da.key == key;
});
if (data) {
return data;
} else {
console.log(" getCurrentEventData data is null ");
return null;
}
};
2019-06-06 16:15:47 +08:00
/**
* 根据当前鼠标点击的节点进行递归遍历最终找到th
* @param {*} element
* @returns <th />对象
* @memberof TableHeader
*/
TableHeader.prototype.getThDome = function getThDome(element) {
var _tagName = element.tagName.toLowerCase();
if (element.getAttribute('data-filter-type') === 'filterContext') return null;
if (_tagName === 'i') return null;
if (_tagName != 'th') {
return this.getThDome(element.parentElement);
} else {
return element;
}
};
//---拖拽列交换----end-----
2018-12-03 16:03:32 +08:00
/**
* 过滤输入后或下拉条件的回调函数
*/
/**
* 过滤行清除回调
*/
/**
* 过滤渲染的组件类型
*/
2017-01-12 08:53:51 +08:00
TableHeader.prototype.render = function render() {
2019-11-14 09:49:40 +08:00
var _this6 = this;
2018-05-11 09:29:43 +08:00
2019-04-25 14:19:48 +08:00
var _props2 = this.props,
clsPrefix = _props2.clsPrefix,
rowStyle = _props2.rowStyle,
draggable = _props2.draggable,
dragborder = _props2.dragborder,
rows = _props2.rows,
filterable = _props2.filterable,
fixed = _props2.fixed,
2019-11-14 09:49:40 +08:00
lastShowIndex = _props2.lastShowIndex,
columnsChildrenList = _props2.columnsChildrenList;
2017-01-12 08:53:51 +08:00
var attr = dragborder ? { id: "u-table-drag-thead-" + this.theadKey } : {};
2019-11-14 09:49:40 +08:00
var lastObj = columnsChildrenList[columnsChildrenList.length - 1];
2017-01-12 08:53:51 +08:00
return _react2["default"].createElement(
"thead",
2018-12-18 19:32:36 +08:00
_extends({ className: clsPrefix + "-thead" }, attr, { "data-theader-fixed": "scroll", ref: function ref(_thead) {
2019-11-14 09:49:40 +08:00
return _this6._thead = _thead;
2018-12-18 19:32:36 +08:00
} }),
2017-01-12 08:53:51 +08:00
rows.map(function (row, index) {
2019-04-19 11:02:20 +08:00
var _rowLeng = row.length - 1;
2017-01-12 08:53:51 +08:00
return _react2["default"].createElement(
"tr",
2019-04-25 15:44:18 +08:00
{ key: index, style: rowStyle, className: filterable && index == rows.length - 1 ? 'filterable' : '' },
2018-12-02 16:53:06 +08:00
row.map(function (da, columIndex, arr) {
2019-11-25 17:58:02 +08:00
da.children = da.required ? _react2["default"].createElement(
"span",
null,
_react2["default"].createElement(
"span",
{ className: "required" },
"*"
),
da.children
) : da.children;
var thHover = da.drgHover ? " " + clsPrefix + "-thead th-drag-hover" : "";
delete da.drgHover;
var fixedStyle = "";
var canDotDrag = "";
2018-12-06 21:03:05 +08:00
//主表格下、固定列或者是过滤行中含有固定列时添加该属性
if (!fixed && (da.fixed || filterable && index == rows.length - 1 && rows[0][columIndex].fixed)) {
fixedStyle = " " + clsPrefix + "-row-fixed-columns-in-body";
}
2018-11-27 15:23:03 +08:00
2018-12-02 16:53:06 +08:00
if (lastShowIndex == columIndex) {
canDotDrag = "th-can-not-drag";
}
2019-01-17 18:42:57 +08:00
var thClassName = "" + da.className ? "" + da.className : '';
2019-08-26 13:58:29 +08:00
if (da.titleAlign) {
thClassName += " text-" + da.titleAlign + " ";
} else if (da.textAlign) {
2019-01-23 11:15:12 +08:00
thClassName += " text-" + da.textAlign + " ";
2019-01-17 18:42:57 +08:00
}
2019-08-26 13:58:29 +08:00
2019-01-17 18:42:57 +08:00
delete da.textAlign;
2019-08-26 13:58:29 +08:00
delete da.titleAlign;
var keyTemp = {};
//避免key为undefined
2019-01-16 13:57:31 +08:00
// if(da.dataindex && da.key ===undefined ){
keyTemp.key = da.key || da.dataindex || index + '-' + columIndex;
// }
2018-10-11 14:12:19 +08:00
if (filterable && index == rows.length - 1) {
2019-11-14 09:49:40 +08:00
da.children = _this6.filterRenderType(da["filtertype"], da.dataindex, columIndex);
if (da.key === undefined) {
2019-01-16 13:57:31 +08:00
keyTemp.key = keyTemp.key + '-filterable';
}
delete da.filterdropdownfocus;
2018-10-11 14:12:19 +08:00
}
2019-01-16 13:57:31 +08:00
2018-12-02 16:53:06 +08:00
var thDefaultObj = {};
2019-01-17 18:42:57 +08:00
2018-12-02 16:53:06 +08:00
if (draggable) {
2019-02-18 14:54:33 +08:00
thClassName += " " + clsPrefix + "-thead th-drag " + thHover + " ";
2018-12-02 16:53:06 +08:00
}
if (dragborder) {
2019-02-18 14:54:33 +08:00
thClassName += " " + clsPrefix + "-thead-th " + canDotDrag;
2018-12-02 16:53:06 +08:00
}
thClassName += " " + fixedStyle;
2018-12-02 16:53:06 +08:00
if (!da.fixed) {
return _react2["default"].createElement(
"th",
2019-04-22 10:24:52 +08:00
_extends({}, da, keyTemp, { className: thClassName, "data-th-fixed": da.fixed, "data-line-key": da.key,
"data-line-index": columIndex, "data-th-width": da.width, "data-type": "draggable" }),
da.children,
2019-11-14 09:49:40 +08:00
// && columIndex != _rowLeng
dragborder && da.key != lastObj.key ? _react2["default"].createElement(
2018-11-26 20:42:18 +08:00
"div",
2018-12-02 16:53:06 +08:00
{ ref: function ref(el) {
2019-11-14 09:49:40 +08:00
return _this6.gap = el;
2018-12-02 16:53:06 +08:00
}, "data-line-key": da.key,
"data-line-index": columIndex, "data-th-width": da.width,
"data-type": "online", className: clsPrefix + "-thead-th-drag-gap" },
2019-04-22 10:24:52 +08:00
_react2["default"].createElement("div", { className: "online" })
2018-12-02 16:53:06 +08:00
) : ""
2018-05-11 11:30:56 +08:00
);
2018-05-11 09:29:43 +08:00
} else {
thDefaultObj = _extends({}, da, {
2019-01-17 18:42:57 +08:00
className: thClassName + " " + fixedStyle
});
da.onClick ? thDefaultObj.onClick = function (e) {
da.onClick(da, e);
} : "";
2019-01-16 13:57:31 +08:00
return _react2["default"].createElement("th", _extends({}, thDefaultObj, keyTemp, { "data-th-fixed": da.fixed }));
2018-05-11 09:29:43 +08:00
}
2017-01-12 08:53:51 +08:00
})
);
})
);
};
return TableHeader;
}(_react.Component);
TableHeader.defaultProps = {
contentWidthDiff: 0
};
2019-08-30 13:43:25 +08:00
var _initialiseProps = function _initialiseProps() {
2019-11-14 09:49:40 +08:00
var _this7 = this;
2019-08-30 13:43:25 +08:00
this.getOnLineObject = function (_element) {
var type = _element.getAttribute('data-type'),
elementObj = null;
if (!type) {
var element = _element.parentElement || parentNode; //兼容写法。
if (element.getAttribute('data-type')) {
elementObj = element;
}
} else {
elementObj = _element;
}
return elementObj;
};
this.onTrMouseDown = function (e) {
_utils.Event.stopPropagation(e);
var event = _utils.Event.getEvent(e),
targetEvent = _utils.Event.getTarget(event);
2019-11-14 09:49:40 +08:00
var _props3 = _this7.props,
2019-08-30 13:43:25 +08:00
clsPrefix = _props3.clsPrefix,
contentTable = _props3.contentTable,
2019-11-14 09:49:40 +08:00
lastShowIndex = _props3.lastShowIndex,
columnsChildrenList = _props3.columnsChildrenList;
2019-08-30 13:43:25 +08:00
// let currentElement = this.getOnLineObject(targetEvent);
2019-11-14 09:49:40 +08:00
var currentElement = _this7.getTargetToType(targetEvent);
2019-08-30 13:43:25 +08:00
if (!currentElement) return;
var type = currentElement.getAttribute('data-type');
2019-11-14 09:49:40 +08:00
if (!_this7.props.dragborder && !_this7.props.draggable) return;
if (type == 'online' && _this7.props.dragborder) {
if (!_this7.props.dragborder) return;
2019-08-30 13:43:25 +08:00
targetEvent.setAttribute('draggable', false); //添加交换列效果
// let currentIndex = parseInt(currentElement.getAttribute("data-line-index"));
var currentIndex = -1;
2019-08-30 13:43:25 +08:00
var defaultWidth = currentElement.getAttribute("data-th-width");
2019-11-14 09:49:40 +08:00
_this7.drag.option = "border"; //拖拽操作
if (columnsChildrenList) {
var columnKey = currentElement.getAttribute("data-line-key");
if (columnKey) {
currentIndex = columnsChildrenList.findIndex(function (da) {
return da.key.toLowerCase() === columnKey.toLowerCase();
});
}
}
console.log("currentIndex :", currentIndex);
var currentObj = _this7.table.cols[currentIndex];
_this7.drag.currIndex = currentIndex;
_this7.drag.oldLeft = event.x;
_this7.drag.oldWidth = parseInt(currentObj.style.width);
_this7.drag.minWidth = currentObj.style.minWidth != "" ? parseInt(currentObj.style.minWidth) : defaultWidth;
_this7.drag.tableWidth = parseInt(_this7.table.table.style.width ? _this7.table.table.style.width : _this7.table.table.scrollWidth);
if (!_this7.tableOldWidth) {
_this7.tableOldWidth = _this7.drag.tableWidth; //this.getTableWidth();
2019-08-30 13:43:25 +08:00
}
2019-11-14 09:49:40 +08:00
if (!_this7.lastColumWidth) {
_this7.lastColumWidth = parseInt(_this7.table.cols[lastShowIndex].style.width);
2019-08-30 13:43:25 +08:00
}
2019-11-14 09:49:40 +08:00
} else if (type != 'online' && _this7.props.draggable) {
2019-08-30 13:43:25 +08:00
// if (!this.props.draggable || targetEvent.nodeName.toUpperCase() != "TH") return;
2019-11-14 09:49:40 +08:00
if (!_this7.props.draggable) return;
var th = _this7.getTargetToType(targetEvent);
2019-08-30 13:43:25 +08:00
th.setAttribute('draggable', true); //添加交换列效果
2019-11-14 09:49:40 +08:00
_this7.drag.option = 'dragAble';
_this7.currentDome = th;
2019-08-30 13:43:25 +08:00
var _currentIndex = parseInt(th.getAttribute("data-line-index"));
2019-11-14 09:49:40 +08:00
_this7.drag.currIndex = _currentIndex;
2019-08-30 13:43:25 +08:00
} else {
// console.log("onTrMouseDown dragborder or draggable is all false !");
return;
}
};
this.getTableWidth = function () {
var tableWidth = 0,
offWidth = 0; //this.table.cols.length;
2019-11-14 09:49:40 +08:00
for (var index = 0; index < _this7.table.cols.length; index++) {
var da = _this7.table.cols[index];
2019-08-30 13:43:25 +08:00
tableWidth += parseInt(da.style.width);
}
return tableWidth - offWidth;
};
this.getTargetToType = function (targetEvent) {
var tag = targetEvent;
if (targetEvent && !targetEvent.getAttribute("data-type")) {
2019-11-14 09:49:40 +08:00
tag = _this7.getTargetToType(targetEvent.parentElement);
2019-08-30 13:43:25 +08:00
}
return tag;
};
this.getTargetToTh = function (targetEvent) {
var th = targetEvent;
if (targetEvent.nodeName.toUpperCase() != "TH") {
2019-11-14 09:49:40 +08:00
th = _this7.getThDome(targetEvent);
2019-08-30 13:43:25 +08:00
}
console.log(" getTargetToTh: ", th);
return th;
};
this.onTrMouseMove = function (e) {
2019-11-14 09:49:40 +08:00
if (!_this7.props.dragborder && !_this7.props.draggable) return;
var _props4 = _this7.props,
2019-08-30 13:43:25 +08:00
clsPrefix = _props4.clsPrefix,
dragborder = _props4.dragborder,
contentDomWidth = _props4.contentDomWidth,
scrollbarWidth = _props4.scrollbarWidth,
contentTable = _props4.contentTable,
headerScroll = _props4.headerScroll,
lastShowIndex = _props4.lastShowIndex,
onDraggingBorder = _props4.onDraggingBorder,
leftFixedWidth = _props4.leftFixedWidth,
rightFixedWidth = _props4.rightFixedWidth;
_utils.Event.stopPropagation(e);
var event = _utils.Event.getEvent(e);
2019-11-14 09:49:40 +08:00
if (_this7.props.dragborder && _this7.drag.option == "border") {
2019-08-30 13:43:25 +08:00
//移动改变宽度
2019-11-14 09:49:40 +08:00
var currentCols = _this7.table.cols[_this7.drag.currIndex];
var diff = event.x - _this7.drag.oldLeft;
var newWidth = _this7.drag.oldWidth + diff;
_this7.drag.newWidth = newWidth > 0 ? newWidth : _this7.minWidth;
if (newWidth > _this7.minWidth) {
2019-08-30 13:43:25 +08:00
currentCols.style.width = newWidth + 'px';
//hao 支持固定表头拖拽 修改表体的width
2019-11-14 09:49:40 +08:00
if (_this7.fixedTable.cols) {
_this7.fixedTable.cols[_this7.drag.currIndex].style.width = newWidth + "px";
2019-08-30 13:43:25 +08:00
}
var newDiff = parseInt(currentCols.style.minWidth) - parseInt(currentCols.style.width);
if (newDiff > 0) {
//缩小
2019-11-14 09:49:40 +08:00
var lastWidth = _this7.lastColumWidth + newDiff;
_this7.table.cols[lastShowIndex].style.width = lastWidth + "px"; //同步表头
_this7.table.tableBodyCols[lastShowIndex].style.width = lastWidth + "px"; //同步表体
2019-08-30 13:43:25 +08:00
}
2019-11-14 09:49:40 +08:00
var showScroll = contentDomWidth - (leftFixedWidth + rightFixedWidth) - (_this7.drag.tableWidth + diff) - scrollbarWidth;
2019-08-30 13:43:25 +08:00
//表头滚动条处理
if (headerScroll) {
if (showScroll < 0) {
//小于 0 出现滚动条
//找到固定列表格设置表头的marginBottom值为scrollbarWidth;
2019-11-14 09:49:40 +08:00
_this7.table.contentTableHeader.style.overflowX = 'scroll';
_this7.optTableMargin(_this7.table.fixedLeftHeaderTable, scrollbarWidth);
_this7.optTableMargin(_this7.table.fixedRighHeadertTable, scrollbarWidth);
2019-08-30 13:43:25 +08:00
} else {
//大于 0 不显示滚动条
2019-11-14 09:49:40 +08:00
_this7.table.contentTableHeader.style.overflowX = 'hidden';
_this7.optTableMargin(_this7.table.fixedLeftHeaderTable, 0);
_this7.optTableMargin(_this7.table.fixedRighHeadertTable, 0);
2019-08-30 13:43:25 +08:00
}
} else {
if (showScroll < 0) {
2019-11-14 09:49:40 +08:00
_this7.table.tableBody.style.overflowX = 'auto';
_this7.optTableMargin(_this7.table.fixedLeftBodyTable, '-' + scrollbarWidth);
_this7.optTableMargin(_this7.table.fixedRightBodyTable, '-' + scrollbarWidth);
_this7.optTableScroll(_this7.table.fixedLeftBodyTable, { x: 'scroll' });
_this7.optTableScroll(_this7.table.fixedRightBodyTable, { x: 'scroll' });
2019-08-30 13:43:25 +08:00
} else {
2019-11-14 09:49:40 +08:00
_this7.table.tableBody.style.overflowX = 'hidden';
_this7.optTableMargin(_this7.table.fixedLeftBodyTable, 0);
_this7.optTableMargin(_this7.table.fixedRightBodyTable, 0);
_this7.optTableScroll(_this7.table.fixedLeftBodyTable, { x: 'auto' });
_this7.optTableScroll(_this7.table.fixedRightBodyTable, { x: 'auto' });
2019-08-30 13:43:25 +08:00
}
}
} else {
2019-11-14 09:49:40 +08:00
_this7.drag.newWidth = _this7.minWidth;
2019-08-30 13:43:25 +08:00
}
}
// 增加拖拽列宽动作的回调函数
2019-11-14 09:49:40 +08:00
_this7.drag.newWidth && onDraggingBorder && onDraggingBorder(event, _this7.drag.newWidth);
2019-08-30 13:43:25 +08:00
};
this.onTrMouseUp = function (e) {
var event = _utils.Event.getEvent(e);
2019-11-14 09:49:40 +08:00
var width = _this7.drag.newWidth;
_this7.mouseClear();
_this7.props.onDropBorder && _this7.props.onDropBorder(event, width);
2019-08-30 13:43:25 +08:00
};
this.clearThsDr = function () {
2019-11-14 09:49:40 +08:00
var ths = _this7.table.ths;
2019-08-30 13:43:25 +08:00
for (var index = 0; index < ths.length; index++) {
ths[index].setAttribute('draggable', false); //去掉交换列效果
}
};
this.bodyonLineMouseUp = function (events, type) {
2019-11-14 09:49:40 +08:00
if (!_this7.drag || !_this7.drag.option) return;
_this7.mouseClear();
2019-08-30 13:43:25 +08:00
};
this.optTableMargin = function (table, scrollbarWidth) {
if (table) {
table.style.marginBottom = scrollbarWidth + "px";
}
};
this.optTableScroll = function (table) {
var overflow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (table) {
var innerTable = table.querySelector('.u-table-body-inner');
if (innerTable) {
//fixbug: 拖拽列宽后,滚动条滚到表格底部,会导致固定列和非固定列错行
overflow.x && (innerTable.style.overflowX = overflow.x);
overflow.y && (innerTable.style.overflowY = overflow.y);
}
}
};
this.onDragStart = function (e) {
2019-11-14 09:49:40 +08:00
if (!_this7.props.draggable) return;
if (_this7.drag && _this7.drag.option != 'dragAble') {
2019-08-30 13:43:25 +08:00
return;
}
var event = _utils.Event.getEvent(e),
// target = Event.getTarget(event);
2019-11-14 09:49:40 +08:00
target = _this7.getTargetToTh(_utils.Event.getTarget(event));
2019-08-30 13:43:25 +08:00
var currentIndex = parseInt(target.getAttribute("data-line-index"));
var currentKey = target.getAttribute('data-line-key');
if (event.dataTransfer.setDragImage) {
var crt = target.cloneNode(true);
crt.style.backgroundColor = "#ebecf0";
2019-11-14 09:49:40 +08:00
crt.style.width = _this7.table.cols[currentIndex].style.width; //拖动后再交换列的时候,阴影效果可同步
2019-08-30 13:43:25 +08:00
crt.style.height = "40px";
// crt.style['line-height'] = "40px";
// document.body.appendChild(crt);
2019-11-14 09:49:40 +08:00
document.getElementById(_this7._table_none_cont_id).appendChild(crt);
2019-08-30 13:43:25 +08:00
event.dataTransfer.setDragImage(crt, 0, 0);
}
event.dataTransfer.effectAllowed = "move";
event.dataTransfer.setData("Text", currentKey);
2019-11-14 09:49:40 +08:00
_this7.currentObj = _this7.props.rows[0][currentIndex];
2019-08-30 13:43:25 +08:00
};
this.onDragOver = function (e) {
var event = _utils.Event.getEvent(e);
event.preventDefault();
};
this.onDrop = function (e) {
2019-11-14 09:49:40 +08:00
if (!_this7.props.draggable) return;
var props = _this7.getCurrentEventData(_this7._dragCurrent);
2019-08-30 13:43:25 +08:00
e.column = { props: props };
2019-11-14 09:49:40 +08:00
if (_this7.drag && _this7.drag.option != 'dragAble') {
_this7.props.onDrop(e);
2019-08-30 13:43:25 +08:00
return;
}
var event = _utils.Event.getEvent(e),
target = _utils.Event.getTarget(event);
2019-11-14 09:49:40 +08:00
_this7.currentDome.setAttribute('draggable', false); //添加交换列效果
2019-08-30 13:43:25 +08:00
// let data = this.getCurrentEventData(this._dragCurrent);
// if(!data){
// this.props.onDrop(e);
// return;
// }
2019-11-14 09:49:40 +08:00
if (!_this7.props.onDrop) return;
2019-08-30 13:43:25 +08:00
// this.props.onDrop(event,target);
2019-11-14 09:49:40 +08:00
_this7.props.onDrop(event, { dragSource: _this7.currentObj, dragTarg: e.column });
2019-08-30 13:43:25 +08:00
};
this.onDragEnter = function (e) {
var event = _utils.Event.getEvent(e),
target = _utils.Event.getTarget(event);
2019-11-14 09:49:40 +08:00
_this7._dragCurrent = target;
2019-08-30 13:43:25 +08:00
var currentIndex = target.getAttribute("data-line-index");
2019-11-14 09:49:40 +08:00
if (!currentIndex || parseInt(currentIndex) === _this7.drag.currIndex) return;
2019-08-30 13:43:25 +08:00
if (target.nodeName.toUpperCase() === "TH") {
// target.style.border = "2px dashed rgba(5,0,0,0.25)";
target.setAttribute("style", "border-right:2px dashed rgb(30, 136, 229)");
// target.style.backgroundColor = 'rgb(235, 236, 240)';
}
};
this.onDragEnd = function (e) {
var event = _utils.Event.getEvent(e),
target = _utils.Event.getTarget(event);
2019-11-14 09:49:40 +08:00
_this7._dragCurrent.setAttribute("style", "");
2019-08-30 13:43:25 +08:00
// this._dragCurrent.style = "";
2019-11-14 09:49:40 +08:00
document.getElementById(_this7._table_none_cont_id).innerHTML = "";
2019-08-30 13:43:25 +08:00
2019-11-14 09:49:40 +08:00
var data = _this7.getCurrentEventData(_this7._dragCurrent);
2019-08-30 13:43:25 +08:00
if (!data) return;
2019-11-14 09:49:40 +08:00
if (!_this7.currentObj || _this7.currentObj.key == data.key) return;
if (!_this7.props.onDragEnd) return;
_this7.props.onDragEnd(event, { dragSource: _this7.currentObj, dragTarg: data });
2019-08-30 13:43:25 +08:00
};
this.onDragLeave = function (e) {
var event = _utils.Event.getEvent(e),
target = _utils.Event.getTarget(event);
var currentIndex = target.getAttribute("data-line-index");
2019-11-14 09:49:40 +08:00
if (!currentIndex || parseInt(currentIndex) === _this7.drag.currIndex) return;
2019-08-30 13:43:25 +08:00
if (target.nodeName.toUpperCase() === "TH") {
target.setAttribute("style", "");
// this._dragCurrent.style = "";
}
};
this.handlerFilterChange = function (key, value, condition) {
2019-11-14 09:49:40 +08:00
var onFilterChange = _this7.props.onFilterChange;
2019-08-30 13:43:25 +08:00
if (onFilterChange) {
onFilterChange(key, value, condition);
}
};
this.handlerFilterClear = function (field) {
2019-11-14 09:49:40 +08:00
var onFilterClear = _this7.props.onFilterClear;
2019-08-30 13:43:25 +08:00
if (onFilterClear) {
onFilterClear(field);
}
};
this.filterRenderType = function (type, dataIndex, index) {
2019-11-14 09:49:40 +08:00
var _props5 = _this7.props,
2019-08-30 13:43:25 +08:00
clsPrefix = _props5.clsPrefix,
rows = _props5.rows,
filterDelay = _props5.filterDelay,
locale = _props5.locale;
switch (type) {
//文本输入
case "text":
return _react2["default"].createElement(_FilterType2["default"], {
locale: locale //多语
, rendertype: type //渲染类型
, clsPrefix: clsPrefix //css前缀
, className: clsPrefix + " filter-text",
dataIndex: dataIndex //字段
2019-11-14 09:49:40 +08:00
, onFilterChange: _this7.handlerFilterChange //输入框回调
, onFilterClear: _this7.handlerFilterClear //清除回调
2019-08-30 13:43:25 +08:00
, filterDropdown: rows[1][index]["filterdropdown"] //是否显示下拉条件
, filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
, filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
});
//数值输入
case "number":
return _react2["default"].createElement(_FilterType2["default"], {
locale: locale,
rendertype: type,
clsPrefix: clsPrefix,
className: clsPrefix + " filter-text",
dataIndex: dataIndex //字段
2019-11-14 09:49:40 +08:00
, onFilterChange: (0, _throttleDebounce.debounce)(filterDelay || 300, _this7.handlerFilterChange) //输入框回调并且函数防抖动
, onFilterClear: _this7.handlerFilterClear //清除回调
2019-08-30 13:43:25 +08:00
, filterDropdown: rows[1][index]["filterdropdown"],
filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
, filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
, filterInputNumberOptions: rows[1][index]["filterinputnumberoptions"] //设置数值框内的详细属性
});
//下拉框选择
case "dropdown":
var selectDataSource = [];
//处理没有输入数据源的时候,系统自动查找自带的数据筛选后注入
if (rows.length > 0 && (rows[1][index]["filterdropdownauto"] || "auto") == "auto") {
var hash = {};
//处理下拉重复对象组装dropdown
selectDataSource = Array.from(rows[1][0].datasource, function (x) {
return {
key: x[dataIndex],
value: x[dataIndex]
};
});
selectDataSource = selectDataSource.reduceRight(function (item, next) {
hash[next.key] ? "" : hash[next.key] = true && item.push(next);
return item;
}, []);
} else {
//从外部数据源加载系统数据
selectDataSource = rows[1][index]["filterdropdowndata"];
}
return _react2["default"].createElement(_FilterType2["default"], {
locale: locale,
rendertype: type,
className: clsPrefix + " filter-dropdown",
data: selectDataSource,
notFoundContent: "Loading" //没有数据显示的默认字
, dataIndex: dataIndex //字段
2019-11-14 09:49:40 +08:00
, onFilterChange: _this7.handlerFilterChange //输入框回调
, onFilterClear: _this7.handlerFilterClear //清除回调
2019-08-30 13:43:25 +08:00
, filterDropdown: rows[1][index]["filterdropdown"],
onFocus: rows[1][index]["filterdropdownfocus"],
filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
, filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
});
//日期
case "date":
return _react2["default"].createElement(_FilterType2["default"], {
locale: locale,
rendertype: type,
className: "filter-date",
onClick: function onClick() {},
format: rows[1][index]["format"] || "YYYY-MM-DD",
dataIndex: dataIndex //字段
2019-11-14 09:49:40 +08:00
, onFilterChange: _this7.handlerFilterChange //输入框回调
, onFilterClear: _this7.handlerFilterClear //清除回调
2019-08-30 13:43:25 +08:00
, filterDropdown: rows[1][index]["filterdropdown"],
filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
, filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
});
//日期范围
case "daterange":
return _react2["default"].createElement(_FilterType2["default"], {
locale: locale,
rendertype: type,
className: "filter-date",
onClick: function onClick() {},
format: rows[1][index]["format"] || "YYYY-MM-DD",
dataIndex: dataIndex //字段
2019-11-14 09:49:40 +08:00
, onFilterChange: _this7.handlerFilterChange //输入框回调
, onFilterClear: _this7.handlerFilterClear //清除回调
2019-08-30 13:43:25 +08:00
, filterDropdown: rows[1][index]["filterdropdown"],
filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
, filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
});
default:
//不匹配类型默认文本输入
return _react2["default"].createElement("div", null);
}
};
};
2017-01-12 08:53:51 +08:00
TableHeader.propTypes = propTypes;
exports["default"] = TableHeader;
module.exports = exports["default"];