UNPKG

41.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _react = require("react");
10
11var _react2 = _interopRequireDefault(_react);
12
13var _reactDom = require("react-dom");
14
15var _reactDom2 = _interopRequireDefault(_reactDom);
16
17var _propTypes = require("prop-types");
18
19var _propTypes2 = _interopRequireDefault(_propTypes);
20
21var _throttleDebounce = require("throttle-debounce");
22
23var _utils = require("./lib/utils");
24
25var _FilterType = require("./FilterType");
26
27var _FilterType2 = _interopRequireDefault(_FilterType);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
30
31function _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; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _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; }
36
37function _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); }
38
39var propTypes = {
40 clsPrefix: _propTypes2["default"].string,
41 rowStyle: _propTypes2["default"].object,
42 rows: _propTypes2["default"].array
43};
44
45function getDiv(id) {
46 var div = document.createElement("div");
47 div.className = "u-table-drag-hidden-cont";
48 div.id = id;
49 return div;
50}
51
52var TableHeader = function (_Component) {
53 _inherits(TableHeader, _Component);
54
55 function TableHeader(props) {
56 _classCallCheck(this, TableHeader);
57
58 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
59
60 _initialiseProps.call(_this);
61
62 _this.currentObj = null;
63 _this.theadKey = new Date().getTime();
64 _this.drag = {
65 option: ''
66 };
67 _this.minWidth = parseInt(props.minColumnWidth);
68 _this.table = null;
69 _this._thead = null; //当前对象
70 _this.event = false; //避免多次绑定问题
71 _this.lastColumWidth = null; //非固定列最后一列的初始化宽度
72 _this.fixedTable = {};
73 return _this;
74 }
75
76 TableHeader.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
77 var _this2 = this;
78
79 // 表格column改变时,要重新绑定拖拽事件,否则拖拽不生效
80 var oldCols = this.props.columnsChildrenList;
81 var newCols = nextProps.columnsChildrenList;
82
83 if (this._thead) {
84 if (newCols.length !== oldCols.length) {
85 this.event = false;
86 return;
87 }
88 oldCols.some(function (item, index) {
89 if (newCols[index] && newCols[index].dataIndex !== item.dataIndex) {
90 _this2.event = false;
91 return true;
92 }
93 });
94 }
95 };
96
97 TableHeader.prototype.componentDidUpdate = function componentDidUpdate() {
98 this.initTable();
99 this.initEvent();
100 };
101
102 TableHeader.prototype.componentDidMount = function componentDidMount() {
103 var uid = "_table_uid_" + new Date().getTime();
104 this._table_none_cont_id = uid;
105 var div = getDiv(uid);
106 document.querySelector("body").appendChild(div);
107 };
108
109 TableHeader.prototype.componentWillUnmount = function componentWillUnmount() {
110 var _this3 = this;
111
112 this.fixedTable = null;
113 if (!this.table) return;
114 if (this.props.draggable) {
115 this.removeDragAbleEvent();
116 }
117 if (this.props.dragborder) {
118 this.removeDragBorderEvent();
119 }
120 this.doEventList(this.table.tr, function (tr) {
121 _this3.eventListen([{ key: 'mousedown', fun: _this3.onTrMouseDown }], 'remove', tr);
122 });
123 // this.eventListen([{key:'mousedown',fun:this.onTrMouseDown}],'remove',this.table.tr[0]);
124 this.eventListen([{ key: 'mouseup', fun: this.bodyonLineMouseUp }], 'remove', document.body);
125 };
126
127 /**
128 * 获取table的属性存放在this.table 中。(公用方法)
129 * @returns
130 * @memberof TableHeader
131 */
132
133
134 TableHeader.prototype.initTable = function initTable() {
135 var contentTable = this.props.contentTable;
136
137 if (!this.props.dragborder && !this.props.draggable) return;
138 var tableDome = this._thead.parentNode;
139 var table = {};
140 if (tableDome && tableDome.nodeName && tableDome.nodeName.toUpperCase() == "TABLE") {
141 table.table = tableDome;
142 table.cols = tableDome.getElementsByTagName("col");
143 table.ths = tableDome.getElementsByTagName("th");
144 table.tr = tableDome.getElementsByTagName("tr");
145 table.tableBody = contentTable.querySelector('.u-table-scroll .u-table-body') && contentTable.querySelector('.u-table-scroll .u-table-body');
146 table.tableBodyCols = contentTable.querySelector('.u-table-scroll .u-table-body') && contentTable.querySelector('.u-table-scroll .u-table-body').getElementsByTagName("col");
147 table.bodyRows = table.tableBody && table.tableBody.querySelectorAll('tr') || [];
148
149 table.fixedLeftHeaderTable = contentTable.querySelector('.u-table-fixed-left .u-table-header');
150 table.fixedRighHeadertTable = contentTable.querySelector('.u-table-fixed-right .u-table-header');
151 table.contentTableHeader = contentTable.querySelector('.u-table-scroll .u-table-header');
152 table.fixedLeftBodyTable = contentTable.querySelectorAll('.u-table-fixed-left .u-table-body-outer');
153 if (table.fixedLeftBodyTable) {
154 var leftBodyTableIndex = table.fixedLeftBodyTable.length - 1 < 0 ? 0 : table.fixedLeftBodyTable.length - 1;
155 table.fixedLeftBodyTable = table.fixedLeftBodyTable[leftBodyTableIndex];
156 }
157
158 table.fixedRightBodyTable = contentTable.querySelectorAll('.u-table-fixed-right .u-table-body-outer');
159 if (table.fixedRightBodyTable) {
160 var rightBodyTableIndex = table.fixedRightBodyTable.length - 1 < 0 ? 0 : table.fixedRightBodyTable.length - 1;
161 table.fixedRightBodyTable = table.fixedRightBodyTable[rightBodyTableIndex];
162 }
163
164 table.innerTableBody = contentTable.querySelector('.u-table-scroll .u-table-body table');
165 table.fixedLeftBodyRows = table.fixedLeftBodyTable && table.fixedLeftBodyTable.querySelectorAll('tr') || [];
166 table.fixedRightBodyRows = table.fixedRightBodyTable && table.fixedRightBodyTable.querySelectorAll('tr') || [];
167 }
168
169 this.table = table;
170
171 if (!this.props.dragborder) return;
172 if (document.getElementById("u-table-drag-thead-" + this.theadKey)) {
173 this.fixedTable = {};
174 var _fixedParentContext = document.getElementById("u-table-drag-thead-" + this.theadKey).parentNode;
175 var siblingDom = _fixedParentContext.parentNode.nextElementSibling;
176 if (siblingDom) {
177 var fixedTable = siblingDom.querySelector("table");
178 this.fixedTable.table = fixedTable;
179 this.fixedTable.cols = fixedTable.getElementsByTagName("col");
180 // this.fixedTable.ths = fixedTable.tableDome.getElementsByTagName("th");
181 }
182 }
183 };
184
185 /**
186 * 事件初始化
187 */
188
189
190 TableHeader.prototype.initEvent = function initEvent() {
191 var _this4 = this;
192
193 var _props = this.props,
194 dragborder = _props.dragborder,
195 draggable = _props.draggable,
196 rows = _props.rows;
197 // 当传入的 columns 为空时,不绑定拖拽事件
198
199 if (Object.prototype.toString.call(rows) === '[object Array]' && rows.length === 0) {
200 return;
201 }
202 if (!this.event) {
203 //避免多次绑定问题。
204 this.event = true;
205 if (dragborder) {
206 this.dragBorderEventInit(); //列宽
207 }
208 if (draggable) {
209 this.dragAbleEventInit(); //交换列
210 }
211 if (this.table && this.table.tr) {
212 // this.eventListen([{key:'mousedown',fun:this.onTrMouseDown}],'',this.table.tr[0]);//body mouseup
213 this.doEventList(this.table.tr, function (tr) {
214 _this4.eventListen([{ key: 'mousedown', fun: _this4.onTrMouseDown }], '', tr); //body mouseup
215 });
216 }
217 this.eventListen([{ key: 'mouseup', fun: this.bodyonLineMouseUp }], '', document.body); //body mouseup
218 }
219 };
220
221 TableHeader.prototype.doEventList = function doEventList(trs, action) {
222 for (var index = 0; index < trs.length; index++) {
223 action(trs[index]);
224 }
225 };
226
227 /**
228 * 拖拽列宽事件的监听
229 */
230
231
232 TableHeader.prototype.dragBorderEventInit = function dragBorderEventInit() {
233 var _this5 = this;
234
235 if (!this.props.dragborder) return;
236 var events = [{ key: 'mouseup', fun: this.onTrMouseUp }, { key: 'mousemove', fun: this.onTrMouseMove }];
237 this.doEventList(this.table.tr, function (tr) {
238 _this5.eventListen(events, '', tr); //表示把事件添加到th元素上
239 });
240 // this.eventListen(events,'',this.table.tr[0]);//表示把事件添加到th元素上
241 };
242
243 /**
244 * 删除拖动改变列宽的事件监听
245 */
246
247
248 TableHeader.prototype.removeDragBorderEvent = function removeDragBorderEvent() {
249 var _this6 = this;
250
251 var events = [{ key: 'mouseup', fun: this.onTrMouseUp }, { key: 'mousemove', fun: this.onTrMouseMove }];
252 // this.eventListen(events,'remove',this.table.tr[0]);
253 this.doEventList(this.table.tr, function (tr) {
254 _this6.eventListen(events, 'remove', _this6.table.tr);
255 });
256 };
257
258 TableHeader.prototype.eventListen = function eventListen(events, type, eventSource) {
259 if (!this.table) return;
260 if (!eventSource) {
261 console.log("Please set the attributes of column !");
262 return;
263 }
264 var tr = this.table.tr;
265
266 for (var i = 0; i < events.length; i++) {
267 var _event = events[i];
268 if (type === "remove") {
269 _utils.EventUtil.removeHandler(eventSource, _event.key, _event.fun);
270 } else {
271 _utils.EventUtil.addHandler(eventSource, _event.key, _event.fun);
272 }
273 }
274 };
275
276 /**
277 *
278 *根据 data-type 来获取当前拖拽的对象的Object,如果为null表示拖动的对象并非是online
279 * @memberof TableHeader
280 */
281
282
283 /**
284 * 调整列宽的down事件
285 * @memberof TableHeader
286 */
287
288
289 /**
290 * 根据当前节点查找到有data-type类型的容器返回。
291 * @memberof TableHeader
292 */
293
294
295 /**
296 * 判断当前的target 是否是 th,如果不是,直接递归查找。
297 * @memberof TableHeader
298 */
299
300 /**
301 * 调整列宽的move事件
302 * @memberof TableHeader
303 */
304
305
306 /**
307 * 调整列宽的up事件
308 * @memberof TableHeader
309 */
310
311
312 TableHeader.prototype.mouseClear = function mouseClear() {
313 if (!this.drag || !this.drag.option) return;
314 var rows = this.props.rows;
315
316 var data = { rows: rows[0], cols: this.table.cols, currIndex: this.drag.currIndex };
317 this.props.afterDragColWidth && this.props.afterDragColWidth(data);
318 this.drag = {
319 option: ""
320 };
321 this.clearThsDr();
322 };
323
324 /**
325 * 当前对象上绑定全局事件,用于拖拽区域以外时的事件处理
326 * @param {*} events
327 * @param {*} type
328 * @memberof TableHeader
329 */
330
331
332 /**
333 *相关滚动条联动操作
334 *
335 * @memberof TableHeader
336 */
337
338
339 //---拖拽交换列代码----start-----
340 /**
341 * 添加换列的事件监听
342 */
343 TableHeader.prototype.dragAbleEventInit = function dragAbleEventInit() {
344 if (!this.props.draggable) return;
345 var events = [{ key: 'dragstart', fun: this.onDragStart }, //用户开始拖动元素时触发
346 { key: 'dragover', fun: this.onDragOver }, //当某被拖动的对象在另一对象容器范围内拖动时触发此事件
347 { key: 'drop', fun: this.onDrop }, //在一个拖动过程中,释放鼠标键时触发此事件
348
349 { key: 'dragenter', fun: this.onDragEnter }, { key: 'dragend', fun: this.onDragEnd }, { key: 'dragleave', fun: this.onDragLeave }];
350 this.eventListen(events, '', this.table.tr[0]); //表示把事件添加到th元素上
351 };
352
353 /**
354 * 删除换列的事件监听
355 */
356
357
358 TableHeader.prototype.removeDragAbleEvent = function removeDragAbleEvent() {
359 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 }];
360 this.eventListen(events, 'remove', this.table.tr[0]);
361 };
362
363 /**
364 * 开始调整交换列的事件
365 */
366
367
368 /**
369 * 在一个拖动过程中,释放鼠标键时触发此事件。【目标事件】
370 * @memberof TableHeader
371 */
372
373
374 /**
375 * 获取当前th上的对象数据
376 * @param {*} e
377 * @returns
378 * @memberof TableHeader
379 */
380 TableHeader.prototype.getCurrentEventData = function getCurrentEventData(th) {
381 if (!th) {
382 console.log(" event target is not th ! ");
383 return null;
384 }
385 var key = th.getAttribute('data-line-key');
386 var data = this.props.rows[0].find(function (da) {
387 return da.key == key;
388 });
389 if (data) {
390 return data;
391 } else {
392 console.log(" getCurrentEventData data is null ");
393 return null;
394 }
395 };
396
397 /**
398 * 根据当前鼠标点击的节点,进行递归遍历,最终找到th
399 * @param {*} element
400 * @returns <th />对象
401 * @memberof TableHeader
402 */
403
404
405 TableHeader.prototype.getThDome = function getThDome(element) {
406 var _tagName = element.tagName.toLowerCase();
407 if (element.getAttribute('data-filter-type') === 'filterContext') return null;
408 if (_tagName === 'i') return null;
409 if (_tagName != 'th') {
410 return this.getThDome(element.parentElement);
411 } else {
412 return element;
413 }
414 };
415
416 //---拖拽列交换----end-----
417
418 /**
419 * 过滤输入后或下拉条件的回调函数
420 */
421
422
423 /**
424 * 过滤行清除回调
425 */
426
427
428 /**
429 * 过滤渲染的组件类型
430 */
431
432
433 TableHeader.prototype.render = function render() {
434 var _this7 = this;
435
436 var _props2 = this.props,
437 clsPrefix = _props2.clsPrefix,
438 rowStyle = _props2.rowStyle,
439 draggable = _props2.draggable,
440 dragborder = _props2.dragborder,
441 rows = _props2.rows,
442 filterable = _props2.filterable,
443 fixed = _props2.fixed,
444 lastShowIndex = _props2.lastShowIndex,
445 columnsChildrenList = _props2.columnsChildrenList;
446
447 var attr = dragborder ? { id: "u-table-drag-thead-" + this.theadKey } : {};
448 var lastObj = columnsChildrenList[columnsChildrenList.length - 1];
449 return _react2["default"].createElement(
450 "thead",
451 _extends({ className: clsPrefix + "-thead" }, attr, { "data-theader-fixed": "scroll", ref: function ref(_thead) {
452 return _this7._thead = _thead;
453 } }),
454 rows.map(function (row, index) {
455 var _rowLeng = row.length - 1;
456 return _react2["default"].createElement(
457 "tr",
458 { key: index, style: rowStyle, className: filterable && index == rows.length - 1 ? 'filterable' : '' },
459 row.map(function (da, columIndex, arr) {
460 da.children = da.required ? _react2["default"].createElement(
461 "span",
462 null,
463 _react2["default"].createElement(
464 "span",
465 { className: "required" },
466 "*"
467 ),
468 da.children
469 ) : da.children;
470 var thHover = da.drgHover ? " " + clsPrefix + "-thead th-drag-hover" : "";
471 delete da.drgHover;
472 var fixedStyle = "";
473 var canDotDrag = "";
474 //主表格下、固定列或者是过滤行中含有固定列时添加该属性
475 if (!fixed && (da.fixed || filterable && index == rows.length - 1 && rows[0][columIndex].fixed)) {
476 fixedStyle = " " + clsPrefix + "-row-fixed-columns-in-body";
477 }
478
479 if (lastShowIndex == columIndex) {
480 canDotDrag = "th-can-not-drag";
481 }
482 var thClassName = "" + da.className ? "" + da.className : '';
483 if (da.titleAlign) {
484 thClassName += " text-" + da.titleAlign + " ";
485 } else if (da.textAlign) {
486 thClassName += " text-" + da.textAlign + " ";
487 }
488
489 delete da.textAlign;
490 delete da.titleAlign;
491 var keyTemp = {};
492 //避免key为undefined
493 // if(da.dataindex && da.key ===undefined ){
494 keyTemp.key = da.key || da.dataindex || index + '-' + columIndex;
495
496 // }
497 if (filterable && index == rows.length - 1) {
498 da.children = _this7.filterRenderType(da["filtertype"], da.dataindex, columIndex);
499 if (da.key === undefined) {
500 keyTemp.key = keyTemp.key + '-filterable';
501 }
502 delete da.filterdropdownfocus;
503 }
504
505 var thDefaultObj = {};
506
507 if (draggable) {
508 thClassName += " " + clsPrefix + "-thead th-drag " + thHover + " ";
509 }
510 if (dragborder) {
511 thClassName += " " + clsPrefix + "-thead-th " + canDotDrag;
512 }
513 thClassName += " " + fixedStyle;
514 if (!da.fixed) {
515 return _react2["default"].createElement(
516 "th",
517 _extends({}, da, keyTemp, { className: thClassName, "data-th-fixed": da.fixed, "data-line-key": da.key,
518 "data-line-index": columIndex, "data-th-width": da.width, "data-type": "draggable", onCopy: function onCopy(event) {
519 _this7.onCopy(da, columIndex, event);
520 } }),
521 da.children,
522
523 // && columIndex != _rowLeng
524 dragborder && lastObj && da.key != lastObj.key ? _react2["default"].createElement(
525 "div",
526 { ref: function ref(el) {
527 return _this7.gap = el;
528 }, "data-line-key": da.key,
529 "data-line-index": columIndex, "data-th-width": da.width,
530 "data-type": "online", className: clsPrefix + "-thead-th-drag-gap" },
531 _react2["default"].createElement("div", { className: "online" })
532 ) : ""
533 );
534 } else {
535 thDefaultObj = _extends({}, da, {
536 className: thClassName + " " + fixedStyle
537 });
538 da.onClick ? thDefaultObj.onClick = function (e) {
539 da.onClick(da, e);
540 } : "";
541 return _react2["default"].createElement("th", _extends({}, thDefaultObj, keyTemp, { "data-th-fixed": da.fixed, style: { maxWidth: da.width }, onCopy: _this7.onCopy }));
542 }
543 })
544 );
545 })
546 );
547 };
548
549 return TableHeader;
550}(_react.Component);
551
552TableHeader.defaultProps = {
553 contentWidthDiff: 0
554};
555
556var _initialiseProps = function _initialiseProps() {
557 var _this8 = this;
558
559 this.getOnLineObject = function (_element) {
560 var type = _element.getAttribute('data-type'),
561 elementObj = null;
562 if (!type) {
563 var element = _element.parentElement || parentNode; //兼容写法。
564 if (element.getAttribute('data-type')) {
565 elementObj = element;
566 }
567 } else {
568 elementObj = _element;
569 }
570 return elementObj;
571 };
572
573 this.onTrMouseDown = function (e) {
574 var eventNoStop = _this8.props.eventNoStop;
575
576 !eventNoStop && _utils.Event.stopPropagation(e);
577 var event = _utils.Event.getEvent(e),
578 targetEvent = _utils.Event.getTarget(event);
579 var _props3 = _this8.props,
580 clsPrefix = _props3.clsPrefix,
581 contentTable = _props3.contentTable,
582 lastShowIndex = _props3.lastShowIndex,
583 columnsChildrenList = _props3.columnsChildrenList;
584 // let currentElement = this.getOnLineObject(targetEvent);
585
586 var currentElement = _this8.getTargetToType(targetEvent);
587 if (!currentElement) return;
588 var type = currentElement.getAttribute('data-type');
589 if (!_this8.props.dragborder && !_this8.props.draggable) return;
590 if (type == 'online' && _this8.props.dragborder) {
591 // if(!this.props.dragborder)return;
592 targetEvent.setAttribute('draggable', false); //添加交换列效果
593 var currentIndex = -1;
594 var defaultWidth = currentElement.getAttribute("data-th-width");
595 _this8.drag.option = "border"; //拖拽操作
596 if (columnsChildrenList) {
597 var columnKey = currentElement.getAttribute("data-line-key");
598 if (columnKey) {
599 currentIndex = columnsChildrenList.findIndex(function (da) {
600 return (da.key && da.key.toLowerCase()) === columnKey.toLowerCase();
601 });
602 }
603 }
604 if (currentIndex < 0) {
605 console.log('Key must be set for column!');
606 return;
607 }
608 var currentObj = _this8.table.cols[currentIndex];
609 _this8.drag.currIndex = currentIndex;
610 _this8.drag.oldLeft = event.x;
611 _this8.drag.oldWidth = parseInt(currentObj.style.width);
612 _this8.drag.minWidth = currentObj.style.minWidth != "" ? parseInt(currentObj.style.minWidth) : defaultWidth;
613 _this8.drag.tableWidth = parseInt(_this8.table.table.style.width ? _this8.table.table.style.width : _this8.table.table.scrollWidth);
614 if (!_this8.tableOldWidth) {
615 _this8.tableOldWidth = _this8.drag.tableWidth; //this.getTableWidth();
616 }
617 if (!_this8.lastColumWidth) {
618 _this8.lastColumWidth = parseInt(_this8.table.cols[lastShowIndex].style.width);
619 }
620 } else if (type != 'online' && _this8.props.draggable) {
621 // if (!this.props.draggable || targetEvent.nodeName.toUpperCase() != "TH") return;
622 if (!_this8.props.draggable) return;
623 var th = _this8.getTargetToType(targetEvent);
624 th.setAttribute('draggable', true); //添加交换列效果
625 _this8.drag.option = 'dragAble';
626 _this8.currentDome = th;
627 var _currentIndex = parseInt(th.getAttribute("data-line-index"));
628 _this8.drag.currIndex = _currentIndex;
629 } else {
630 // console.log("onTrMouseDown dragborder or draggable is all false !");
631 return;
632 }
633 };
634
635 this.getTableWidth = function () {
636 var tableWidth = 0,
637 offWidth = 0; //this.table.cols.length;
638 for (var index = 0; index < _this8.table.cols.length; index++) {
639 var da = _this8.table.cols[index];
640 tableWidth += parseInt(da.style.width);
641 }
642 return tableWidth - offWidth;
643 };
644
645 this.getTargetToType = function (targetEvent) {
646 var tag = targetEvent;
647 if (targetEvent && !targetEvent.getAttribute("data-type")) {
648 tag = _this8.getTargetToType(targetEvent.parentElement);
649 }
650 return tag;
651 };
652
653 this.getTargetToTh = function (targetEvent) {
654 var th = targetEvent;
655 if (targetEvent.nodeName.toUpperCase() != "TH") {
656 th = _this8.getThDome(targetEvent);
657 }
658 // console.log(" getTargetToTh: ", th);
659 return th;
660 };
661
662 this.onTrMouseMove = function (e) {
663 if (!_this8.props.dragborder && !_this8.props.draggable) return;
664 var _props4 = _this8.props,
665 clsPrefix = _props4.clsPrefix,
666 dragborder = _props4.dragborder,
667 contentDomWidth = _props4.contentDomWidth,
668 scrollbarWidth = _props4.scrollbarWidth,
669 contentTable = _props4.contentTable,
670 headerScroll = _props4.headerScroll,
671 lastShowIndex = _props4.lastShowIndex,
672 onDraggingBorder = _props4.onDraggingBorder,
673 leftFixedWidth = _props4.leftFixedWidth,
674 rightFixedWidth = _props4.rightFixedWidth,
675 bodyDisplayInRow = _props4.bodyDisplayInRow,
676 eventNoStop = _props4.eventNoStop;
677
678 !eventNoStop && _utils.Event.stopPropagation(e);
679 var event = _utils.Event.getEvent(e);
680 if (_this8.props.dragborder && _this8.drag.option == "border") {
681 //移动改变宽度
682 var currentCols = _this8.table.cols[_this8.drag.currIndex];
683 var diff = event.x - _this8.drag.oldLeft;
684 var newWidth = _this8.drag.oldWidth + diff;
685 _this8.drag.newWidth = newWidth > 0 ? newWidth : _this8.minWidth;
686 if (newWidth > _this8.minWidth) {
687 currentCols.style.width = newWidth + 'px';
688
689 // displayinrow 判断、 固定行高判断
690 if (!bodyDisplayInRow) {
691 _this8.table.bodyRows.forEach(function (row, index) {
692 var leftRow = _this8.table.fixedLeftBodyRows[index];
693 var rightRow = _this8.table.fixedRightBodyRows[index];
694 if (leftRow || rightRow) {
695 var height = row.getBoundingClientRect().height;
696 leftRow && (leftRow.style.height = height + "px");
697 rightRow && (rightRow.style.height = height + "px");
698 }
699 });
700 }
701
702 //hao 支持固定表头拖拽 修改表体的width
703 if (_this8.fixedTable.cols) {
704 _this8.fixedTable.cols[_this8.drag.currIndex].style.width = newWidth + "px";
705 }
706
707 var newDiff = parseInt(currentCols.style.minWidth) - parseInt(currentCols.style.width);
708 if (newDiff > 0) {
709 //缩小
710 var lastWidth = _this8.lastColumWidth + newDiff;
711 _this8.table.cols[lastShowIndex].style.width = lastWidth + "px"; //同步表头
712 _this8.table.tableBodyCols[lastShowIndex].style.width = lastWidth + "px"; //同步表体
713 }
714 var showScroll = contentDomWidth - (leftFixedWidth + rightFixedWidth) - (_this8.drag.tableWidth + diff) - scrollbarWidth;
715 //表头滚动条处理
716 if (headerScroll) {
717 if (showScroll < 0) {
718 //小于 0 出现滚动条
719 //找到固定列表格,设置表头的marginBottom值为scrollbarWidth;
720 _this8.table.contentTableHeader.style.overflowX = 'scroll';
721 _this8.optTableMargin(_this8.table.fixedLeftHeaderTable, scrollbarWidth);
722 _this8.optTableMargin(_this8.table.fixedRighHeadertTable, scrollbarWidth);
723 } else {
724 //大于 0 不显示滚动条
725 _this8.table.contentTableHeader.style.overflowX = 'hidden';
726 _this8.optTableMargin(_this8.table.fixedLeftHeaderTable, 0);
727 _this8.optTableMargin(_this8.table.fixedRighHeadertTable, 0);
728 }
729 } else {
730 if (showScroll < 0) {
731 _this8.table.tableBody.style.overflowX = 'auto';
732 _this8.optTableMargin(_this8.table.fixedLeftBodyTable, '-' + scrollbarWidth);
733 _this8.optTableMargin(_this8.table.fixedRightBodyTable, '-' + scrollbarWidth);
734 _this8.optTableScroll(_this8.table.fixedLeftBodyTable, { x: 'scroll' });
735 _this8.optTableScroll(_this8.table.fixedRightBodyTable, { x: 'scroll' });
736 } else {
737 _this8.table.tableBody.style.overflowX = 'hidden';
738 _this8.optTableMargin(_this8.table.fixedLeftBodyTable, 0);
739 _this8.optTableMargin(_this8.table.fixedRightBodyTable, 0);
740 _this8.optTableScroll(_this8.table.fixedLeftBodyTable, { x: 'auto' });
741 _this8.optTableScroll(_this8.table.fixedRightBodyTable, { x: 'auto' });
742 }
743 }
744 } else {
745 _this8.drag.newWidth = _this8.minWidth;
746 }
747 }
748 // 增加拖拽列宽动作的回调函数
749 _this8.drag.newWidth && onDraggingBorder && onDraggingBorder(event, _this8.drag.newWidth);
750 };
751
752 this.onTrMouseUp = function (e) {
753 var event = _utils.Event.getEvent(e);
754 var width = _this8.drag.newWidth;
755 var opt = _this8.drag.option;
756 _this8.mouseClear();
757 if (opt !== "border") return; // fix:点击表头会触发onDropBorder事件的问题
758 _this8.props.onDropBorder && _this8.props.onDropBorder(event, width);
759 };
760
761 this.clearThsDr = function () {
762 var ths = _this8.table.ths;
763 for (var index = 0; index < ths.length; index++) {
764 ths[index].setAttribute('draggable', false); //去掉交换列效果
765 }
766 };
767
768 this.bodyonLineMouseUp = function (events, type) {
769 if (!_this8.drag || !_this8.drag.option) return;
770 _this8.mouseClear();
771 };
772
773 this.optTableMargin = function (table, scrollbarWidth) {
774 if (table) {
775 table.style.marginBottom = scrollbarWidth + "px";
776 }
777 };
778
779 this.optTableScroll = function (table) {
780 var overflow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
781
782 if (table) {
783 var innerTable = table.querySelector('.u-table-body-inner');
784 if (innerTable) {
785 //fixbug: 拖拽列宽后,滚动条滚到表格底部,会导致固定列和非固定列错行
786 overflow.x && (innerTable.style.overflowX = overflow.x);
787 overflow.y && (innerTable.style.overflowY = overflow.y);
788 }
789 }
790 };
791
792 this.onDragStart = function (e) {
793 if (!_this8.props.draggable) return;
794 if (_this8.drag && _this8.drag.option != 'dragAble') {
795 return;
796 }
797 var event = _utils.Event.getEvent(e),
798
799 // target = Event.getTarget(event);
800 target = _this8.getTargetToTh(_utils.Event.getTarget(event));
801 var currentIndex = parseInt(target.getAttribute("data-line-index"));
802 var currentKey = target.getAttribute('data-line-key');
803
804 if (event.dataTransfer.setDragImage) {
805 var crt = target.cloneNode(true);
806 crt.style.backgroundColor = "#ebecf0";
807 crt.style.width = _this8.table.cols[currentIndex].style.width; //拖动后再交换列的时候,阴影效果可同步
808 crt.style.height = "40px";
809 // crt.style['line-height'] = "40px";
810 // document.body.appendChild(crt);
811 document.getElementById(_this8._table_none_cont_id).appendChild(crt);
812 event.dataTransfer.setDragImage(crt, 0, 0);
813 }
814
815 event.dataTransfer.effectAllowed = "move";
816 event.dataTransfer.setData("Text", currentKey);
817 _this8.currentObj = _this8.props.rows[0][currentIndex];
818 };
819
820 this.onDragOver = function (e) {
821 var event = _utils.Event.getEvent(e);
822 event.preventDefault();
823 };
824
825 this.onDrop = function (e) {
826 if (!_this8.props.draggable) return;
827 var props = _this8.getCurrentEventData(_this8._dragCurrent);
828 e.column = { props: props };
829 if (_this8.drag && _this8.drag.option != 'dragAble') {
830 _this8.props.onDrop(e);
831 return;
832 }
833 var event = _utils.Event.getEvent(e),
834 target = _utils.Event.getTarget(event);
835 event.preventDefault();
836 event.stopPropagation();
837 _this8.currentDome.setAttribute('draggable', false); //添加交换列效果
838 // let data = this.getCurrentEventData(this._dragCurrent);
839 // if(!data){
840 // this.props.onDrop(e);
841 // return;
842 // }
843 if (!_this8.props.onDrop) return;
844 // this.props.onDrop(event,target);
845 _this8.props.onDrop(event, { dragSource: _this8.currentObj, dragTarg: e.column });
846 };
847
848 this.onDragEnter = function (e) {
849 var event = _utils.Event.getEvent(e),
850 target = _utils.Event.getTarget(event);
851 _this8._dragCurrent = target;
852 var currentIndex = target.getAttribute("data-line-index");
853 if (!currentIndex || parseInt(currentIndex) === _this8.drag.currIndex) return;
854 if (target.nodeName.toUpperCase() === "TH") {
855 // target.style.border = "2px dashed rgba(5,0,0,0.25)";
856 target.setAttribute("style", "border-right:2px dashed rgb(30, 136, 229)");
857 // target.style.backgroundColor = 'rgb(235, 236, 240)';
858 }
859 };
860
861 this.onDragEnd = function (e) {
862 var event = _utils.Event.getEvent(e),
863 target = _utils.Event.getTarget(event);
864 _this8._dragCurrent.setAttribute("style", "");
865 event.preventDefault();
866 event.stopPropagation();
867 // this._dragCurrent.style = "";
868 document.getElementById(_this8._table_none_cont_id).innerHTML = "";
869
870 var data = _this8.getCurrentEventData(_this8._dragCurrent);
871 if (!data) return;
872 if (!_this8.currentObj || _this8.currentObj.key == data.key) return;
873 if (!_this8.props.onDragEnd) return;
874 _this8.props.onDragEnd(event, { dragSource: _this8.currentObj, dragTarg: data });
875 };
876
877 this.onDragLeave = function (e) {
878 var event = _utils.Event.getEvent(e),
879 target = _utils.Event.getTarget(event);
880 var currentIndex = target.getAttribute("data-line-index");
881 if (!currentIndex || parseInt(currentIndex) === _this8.drag.currIndex) return;
882 if (target.nodeName.toUpperCase() === "TH") {
883 target.setAttribute("style", "");
884 // this._dragCurrent.style = "";
885 }
886 };
887
888 this.handlerFilterChange = function (key, value, condition) {
889 var onFilterChange = _this8.props.onFilterChange;
890
891 if (onFilterChange) {
892 onFilterChange(key, value, condition);
893 }
894 };
895
896 this.handlerFilterClear = function (field) {
897 var onFilterClear = _this8.props.onFilterClear;
898
899 if (onFilterClear) {
900 onFilterClear(field);
901 }
902 };
903
904 this.filterRenderType = function (type, dataIndex, index) {
905 var _props5 = _this8.props,
906 clsPrefix = _props5.clsPrefix,
907 rows = _props5.rows,
908 filterDelay = _props5.filterDelay,
909 locale = _props5.locale;
910
911 switch (type) {
912 //文本输入
913 case "text":
914 return _react2["default"].createElement(_FilterType2["default"], {
915 locale: locale //多语
916 , rendertype: type //渲染类型
917 , clsPrefix: clsPrefix //css前缀
918 , className: clsPrefix + " filter-text",
919 dataIndex: dataIndex //字段
920 , onFilterChange: (0, _throttleDebounce.debounce)(filterDelay || 300, _this8.handlerFilterChange) //输入框回调
921 , onFilterClear: _this8.handlerFilterClear //清除回调
922 , filterDropdown: rows[1][index]["filterdropdown"] //是否显示下拉条件
923 , filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
924 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
925 });
926 //数值输入
927 case "number":
928 return _react2["default"].createElement(_FilterType2["default"], {
929 locale: locale,
930 rendertype: type,
931 clsPrefix: clsPrefix,
932 className: clsPrefix + " filter-text",
933 dataIndex: dataIndex //字段
934 , onFilterChange: (0, _throttleDebounce.debounce)(filterDelay || 300, _this8.handlerFilterChange) //输入框回调并且函数防抖动
935 , onFilterClear: _this8.handlerFilterClear //清除回调
936 , filterDropdown: rows[1][index]["filterdropdown"],
937 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
938 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
939 , filterInputNumberOptions: rows[1][index]["filterinputnumberoptions"] //设置数值框内的详细属性
940 });
941 //下拉框选择
942 case "dropdown":
943 var selectDataSource = [];
944 //处理没有输入数据源的时候,系统自动查找自带的数据筛选后注入
945 if (rows.length > 0 && (rows[1][index]["filterdropdownauto"] || "auto") == "auto") {
946 var hash = {};
947 //处理下拉重复对象组装dropdown
948 selectDataSource = Array.from(rows[1][0].datasource, function (x) {
949 return {
950 key: x[dataIndex],
951 value: x[dataIndex]
952 };
953 });
954 selectDataSource = selectDataSource.reduceRight(function (item, next) {
955 hash[next.key] ? "" : hash[next.key] = true && item.push(next);
956 return item;
957 }, []);
958 } else {
959 //从外部数据源加载系统数据
960 selectDataSource = rows[1][index]["filterdropdowndata"];
961 }
962 return _react2["default"].createElement(_FilterType2["default"], {
963 locale: locale,
964 rendertype: type,
965 className: clsPrefix + " filter-dropdown",
966 data: selectDataSource,
967 notFoundContent: "Loading" //没有数据显示的默认字
968 , dataIndex: dataIndex //字段
969 , onFilterChange: _this8.handlerFilterChange //输入框回调
970 , onFilterClear: _this8.handlerFilterClear //清除回调
971 , filterDropdown: rows[1][index]["filterdropdown"],
972 onFocus: rows[1][index]["filterdropdownfocus"],
973 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
974 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
975 });
976 //日期
977 case "date":
978 return _react2["default"].createElement(_FilterType2["default"], {
979 locale: locale,
980 rendertype: type,
981 className: "filter-date",
982 onClick: function onClick() {},
983 format: rows[1][index]["format"] || "YYYY-MM-DD",
984 dataIndex: dataIndex //字段
985 , onFilterChange: _this8.handlerFilterChange //输入框回调
986 , onFilterClear: _this8.handlerFilterClear //清除回调
987 , filterDropdown: rows[1][index]["filterdropdown"],
988 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
989 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
990 });
991 //日期 年
992 case "dateyear":
993 return _react2["default"].createElement(_FilterType2["default"], {
994 locale: locale,
995 rendertype: type,
996 className: "filter-date",
997 onClick: function onClick() {},
998 format: rows[1][index]["format"] || "YYYY",
999 dataIndex: dataIndex //字段
1000 , onFilterChange: _this8.handlerFilterChange //输入框回调
1001 , onFilterClear: _this8.handlerFilterClear //清除回调
1002 , filterDropdown: rows[1][index]["filterdropdown"],
1003 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
1004 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
1005 });
1006 //日期 月
1007 case "datemonth":
1008 return _react2["default"].createElement(_FilterType2["default"], {
1009 locale: locale,
1010 rendertype: type,
1011 className: "filter-date",
1012 onClick: function onClick() {},
1013 format: rows[1][index]["format"] || "YYYY-MM",
1014 dataIndex: dataIndex //字段
1015 , onFilterChange: _this8.handlerFilterChange //输入框回调
1016 , onFilterClear: _this8.handlerFilterClear //清除回调
1017 , filterDropdown: rows[1][index]["filterdropdown"],
1018 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
1019 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
1020 });
1021 //日期 周
1022 case "dateweek":
1023 return _react2["default"].createElement(_FilterType2["default"], {
1024 locale: locale,
1025 rendertype: type,
1026 className: "filter-date",
1027 onClick: function onClick() {},
1028 format: rows[1][index]["format"] || "YYYY-Wo",
1029 dataIndex: dataIndex //字段
1030 , onFilterChange: _this8.handlerFilterChange //输入框回调
1031 , onFilterClear: _this8.handlerFilterClear //清除回调
1032 , filterDropdown: rows[1][index]["filterdropdown"],
1033 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
1034 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
1035 });
1036 //日期范围
1037 case "daterange":
1038 return _react2["default"].createElement(_FilterType2["default"], {
1039 locale: locale,
1040 rendertype: type,
1041 className: "filter-date",
1042 onClick: function onClick() {},
1043 format: rows[1][index]["format"] || "YYYY-MM-DD",
1044 dataIndex: dataIndex //字段
1045 , onFilterChange: _this8.handlerFilterChange //输入框回调
1046 , onFilterClear: _this8.handlerFilterClear //清除回调
1047 , filterDropdown: rows[1][index]["filterdropdown"],
1048 filterDropdownType: rows[1][index]["filterdropdowntype"] //下拉的条件类型为string,number
1049 , filterDropdownIncludeKeys: rows[1][index]["filterdropdownincludekeys"] //下拉条件按照指定的keys去显示
1050 });
1051 default:
1052 //不匹配类型默认文本输入
1053 return _react2["default"].createElement("div", null);
1054 }
1055 };
1056
1057 this.onCopy = function (data, index, event) {
1058 if (_this8.props.onCopy) {
1059 _this8.props.onCopy(_extends(data, { col: index }), event);
1060 }
1061 };
1062};
1063
1064TableHeader.propTypes = propTypes;
1065exports["default"] = TableHeader;
1066module.exports = exports["default"];
\No newline at end of file