UNPKG

20.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 _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _reactDom = require('react-dom');
16
17var _reactDom2 = _interopRequireDefault(_reactDom);
18
19var _classnames = require('classnames');
20
21var _classnames2 = _interopRequireDefault(_classnames);
22
23var _beeAnimate = require('bee-animate');
24
25var _beeAnimate2 = _interopRequireDefault(_beeAnimate);
26
27var _util = require('./util');
28
29var _propTypes = require('prop-types');
30
31var _propTypes2 = _interopRequireDefault(_propTypes);
32
33var _tinperBeeCore = require('tinper-bee-core');
34
35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
36
37function _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; }
38
39function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
40
41function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
42
43function _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; }
44
45function _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); }
46
47var browserUa = typeof window !== 'undefined' ? (0, _util.browser)(window.navigator) : '';
48var ieOrEdge = /.*(IE|Edge).+/.test(browserUa);
49// const uaArray = browserUa.split(' ');
50// const gtIE8 = uaArray.length !== 2 || uaArray[0].indexOf('IE') === -1 || Number(uaArray[1]) > 8;
51
52var defaultTitle = '---';
53
54var TreeNode = function (_React$Component) {
55 _inherits(TreeNode, _React$Component);
56
57 function TreeNode(props) {
58 _classCallCheck(this, TreeNode);
59
60 var _this2 = _possibleConstructorReturn(this, _React$Component.call(this, props));
61
62 _this2.getNodeChildren = function () {
63 var children = _this2.props.children;
64
65 var originList = (0, _util.toArray)(children).filter(function (node) {
66 return node;
67 });
68 var targetList = (0, _util.getNodeChildren)(originList);
69
70 if (originList.length !== targetList.length) {
71 (0, _util.warnOnlyTreeNode)();
72 }
73
74 return targetList;
75 };
76
77 ['onExpand', 'onCheck', 'onContextMenu', 'onMouseEnter', 'onMouseLeave', 'onDragStart', 'onDragEnter', 'onDragOver', 'onDragLeave', 'onDrop', 'onDragEnd', 'onDoubleClick', 'onKeyDown'].forEach(function (m) {
78 _this2[m] = _this2[m].bind(_this2);
79 });
80 _this2.state = {
81 dataLoading: false,
82 dragNodeHighlight: false
83 };
84 return _this2;
85 }
86
87 TreeNode.prototype.componentDidMount = function componentDidMount() {
88 if (!this.props.root._treeNodeInstances) {
89 this.props.root._treeNodeInstances = [];
90 }
91 this.props.root._treeNodeInstances.push(this);
92 };
93 // shouldComponentUpdate(nextProps) {
94 // if (!nextProps.expanded) {
95 // return false;
96 // }
97 // return true;
98 // }
99
100 TreeNode.prototype.onCheck = function onCheck() {
101
102 this.props.root.onCheck(this);
103 };
104
105 TreeNode.prototype.onSelect = function onSelect() {
106 clearTimeout(this.doubleClickFlag);
107 var _this = this;
108 if (this.props.onDoubleClick) {
109 //执行延时
110 this.doubleClickFlag = setTimeout(function () {
111 //do function在此处写单击事件要执行的代码
112 _this.props.root.onSelect(_this);
113 }, 300);
114 } else {
115 _this.props.root.onSelect(_this);
116 }
117 };
118
119 TreeNode.prototype.onDoubleClick = function onDoubleClick() {
120 clearTimeout(this.doubleClickFlag);
121 this.props.root.onDoubleClick(this);
122 };
123
124 TreeNode.prototype.onMouseEnter = function onMouseEnter(e) {
125 e.preventDefault();
126 this.props.root.onMouseEnter(e, this);
127 };
128
129 TreeNode.prototype.onMouseLeave = function onMouseLeave(e) {
130 e.preventDefault();
131 this.props.root.onMouseLeave(e, this);
132 };
133
134 TreeNode.prototype.onContextMenu = function onContextMenu(e) {
135 e.preventDefault();
136 this.props.root.onContextMenu(e, this);
137 };
138
139 TreeNode.prototype.onDragStart = function onDragStart(e) {
140 // console.log('dragstart', this.props.eventKey, e);
141 // e.preventDefault();
142 e.stopPropagation();
143 this.setState({
144 dragNodeHighlight: true
145 });
146 this.props.root.onDragStart(e, this);
147 try {
148 // ie throw error
149 // firefox-need-it
150 e.dataTransfer.setData('text/plain', '');
151 } finally {
152 // empty
153 }
154 };
155
156 TreeNode.prototype.onDragEnter = function onDragEnter(e) {
157 e.preventDefault();
158 e.stopPropagation();
159 this.props.root.onDragEnter(e, this);
160 };
161
162 TreeNode.prototype.onDragOver = function onDragOver(e) {
163 // todo disabled
164 e.preventDefault();
165 e.stopPropagation();
166 this.props.root.onDragOver(e, this);
167 return false;
168 };
169
170 TreeNode.prototype.onDragLeave = function onDragLeave(e) {
171 e.stopPropagation();
172 this.props.root.onDragLeave(e, this);
173 };
174
175 TreeNode.prototype.onDrop = function onDrop(e) {
176 e.preventDefault();
177 e.stopPropagation();
178 this.setState({
179 dragNodeHighlight: false
180 });
181 this.props.root.onDrop(e, this);
182 };
183
184 TreeNode.prototype.onDragEnd = function onDragEnd(e) {
185 e.stopPropagation();
186 this.setState({
187 dragNodeHighlight: false
188 });
189 this.props.root.onDragEnd(e, this);
190 };
191
192 TreeNode.prototype.onExpand = function onExpand() {
193 var _this3 = this;
194
195 var callbackPromise = this.props.root.onExpand(this);
196 if (callbackPromise && (typeof callbackPromise === 'undefined' ? 'undefined' : _typeof(callbackPromise)) === 'object') {
197 var setLoading = function setLoading(dataLoading) {
198 _this3.setState({
199 dataLoading: dataLoading
200 });
201 };
202 setLoading(true);
203 callbackPromise.then(function () {
204 setLoading(false);
205 }, function () {
206 setLoading(false);
207 });
208 }
209 };
210
211 // keyboard event support
212
213
214 TreeNode.prototype.onKeyDown = function onKeyDown(e) {
215 this.props.root.onKeyDown(e, this);
216 if (e.keyCode == _tinperBeeCore.KeyCode.SPACE || e.keyCode == _tinperBeeCore.KeyCode.DOWN || e.keyCode == _tinperBeeCore.KeyCode.LEFT || e.keyCode == _tinperBeeCore.KeyCode.RIGHT || e.keyCode == _tinperBeeCore.KeyCode.UP) {
217 e.preventDefault();
218 }
219 };
220
221 TreeNode.prototype.renderSwitcher = function renderSwitcher(props, expandedState) {
222 var stateIcon = void 0;
223 var prefixCls = props.prefixCls;
224 var switcherCls = _defineProperty({}, prefixCls + '-switcher', true);
225 if (!props.showLine) {
226 switcherCls[prefixCls + '-noline_' + expandedState] = true;
227 } else if (props.pos === '0-0') {
228 switcherCls[prefixCls + '-roots_' + expandedState] = true;
229 } else {
230 switcherCls[prefixCls + '-center_' + expandedState] = !props.last;
231 switcherCls[prefixCls + '-bottom_' + expandedState] = props.last;
232 }
233
234 if (expandedState === 'open' && props.openIcon) {
235 stateIcon = props.openIcon;
236 switcherCls['icon-none'] = true;
237 }
238 if (expandedState === 'close' && props.closeIcon) {
239 stateIcon = props.closeIcon;
240 switcherCls['icon-none'] = true;
241 }
242 //switcherCls[stateIcon] = stateIcon;
243 props.switcherClass ? switcherCls['' + props.switcherClass] = true : '';
244 if (props.disabled && !props.mustExpandable) {
245 switcherCls[prefixCls + '-switcher-disabled'] = true;
246 return _react2["default"].createElement(
247 'span',
248 { className: (0, _classnames2["default"])(switcherCls), style: props.switcherStyle },
249 stateIcon
250 );
251 }
252 return _react2["default"].createElement(
253 'span',
254 { className: (0, _classnames2["default"])(switcherCls), style: props.switcherStyle, onClick: this.onExpand },
255 stateIcon
256 );
257 };
258
259 TreeNode.prototype.renderCheckbox = function renderCheckbox(props) {
260 var prefixCls = props.prefixCls;
261 var checkboxCls = _defineProperty({}, prefixCls + '-checkbox', true);
262 if (props.checked) {
263 checkboxCls[prefixCls + '-checkbox-checked'] = true;
264 } else if (props.halfChecked) {
265 checkboxCls[prefixCls + '-checkbox-indeterminate'] = true;
266 }
267 var customEle = null;
268 if (typeof props.checkable !== 'boolean') {
269 customEle = props.checkable;
270 }
271 if (props.disabled || props.disableCheckbox) {
272 checkboxCls[prefixCls + '-checkbox-disabled'] = true;
273 return _react2["default"].createElement(
274 'span',
275 { className: (0, _classnames2["default"])(checkboxCls) },
276 customEle
277 );
278 }
279 return _react2["default"].createElement(
280 'span',
281 {
282 className: (0, _classnames2["default"])(checkboxCls),
283 onClick: this.onCheck
284 },
285 customEle
286 );
287 };
288
289 TreeNode.prototype.renderChildren = function renderChildren(props) {
290 var renderFirst = this.renderFirst;
291 this.renderFirst = 1;
292 var transitionAppear = true;
293 if (!renderFirst && props.expanded) {
294 transitionAppear = false;
295 }
296 var children = props.children;
297 var newChildren = children;
298 // 确定所有子节点是否是TreeNode
299 var allTreeNode = false;
300 if (Array.isArray(children)) {
301 for (var index = 0; index < children.length; index++) {
302 var item = children[index];
303 allTreeNode = item.type.isTreeNode == 1;
304 if (!allTreeNode) {
305 //当检查到子节点中有不是 TreeNode 的,则直接结束检查。同时不会渲染所有子节点
306 break;
307 }
308 }
309 } else if (children && children.type && children.type.isTreeNode == 1) {
310 allTreeNode = true;
311 }
312 // 如果props.children的长度大于0才可以生成子对象
313 if (allTreeNode && _react2["default"].Children.count(children)) {
314 var _cls;
315
316 var cls = (_cls = {}, _defineProperty(_cls, props.prefixCls + '-child-tree', true), _defineProperty(_cls, props.prefixCls + '-child-tree-open', props.expanded), _cls);
317 if (props.showLine) {
318 cls[props.prefixCls + '-line'] = !props.last;
319 }
320 var animProps = {};
321 if (props.openTransitionName) {
322 animProps.transitionName = props.openTransitionName;
323 } else if (_typeof(props.openAnimation) === 'object') {
324 animProps.animation = _extends({}, props.openAnimation);
325 if (!transitionAppear) {
326 delete animProps.animation.appear;
327 }
328 }
329 newChildren = _react2["default"].createElement(
330 _beeAnimate2["default"],
331 _extends({}, animProps, {
332 showProp: 'data-expanded',
333 transitionAppear: transitionAppear,
334 component: ''
335 }),
336 !props.expanded ? null : _react2["default"].createElement(
337 'ul',
338 { className: (0, _classnames2["default"])(cls), 'data-expanded': props.expanded },
339 _react2["default"].Children.map(children, function (item, index) {
340 return props.root.renderTreeNode(item, index, props.pos);
341 }, props.root)
342 )
343 );
344 }
345 return newChildren;
346 };
347
348 /**
349 *判断是否为叶子节点,isLeaf的优先级>props.children。如果是异步加载是根据isLeaf的值进行判断的
350 *
351 * @returns
352 * @memberof TreeNode
353 */
354 TreeNode.prototype.checkIsLeaf = function checkIsLeaf() {
355 var _props = this.props,
356 isLeaf = _props.isLeaf,
357 loadData = _props.loadData;
358
359 var rs = isLeaf;
360 if (rs === false || rs === true) {
361 return rs;
362 } else {
363 var hasChildren = this.getNodeChildren().length !== 0;
364 return !loadData && !hasChildren;
365 }
366 };
367
368 TreeNode.prototype.render = function render() {
369 var _iconEleCls,
370 _this4 = this;
371
372 var props = this.props;
373 var prefixCls = props.prefixCls;
374 var expandedState = props.expanded ? 'open' : 'close';
375 var iconState = expandedState;
376
377 var canRenderSwitcher = true;
378 var content = props.title;
379 var newChildren = this.renderChildren(props);
380 var openIconCls = false,
381 closeIconCls = false;
382
383 //以下变量控制是否鼠标单机双击方法中的变量
384 var timer = 0;
385 var delay = 500;
386 var prevent = false;
387
388 // if (!newChildren || newChildren === props.children) {
389 // // content = newChildren;
390 // newChildren = null;
391 // if (!props.loadData || props.isLeaf) {
392 // canRenderSwitcher = false;
393 // iconState = 'docu';
394 // }
395 // }
396 if (this.checkIsLeaf()) {
397 canRenderSwitcher = false;
398 iconState = 'docu';
399 }
400 // For performance, does't render children into dom when `!props.expanded` (move to Animate)
401 // if (!props.expanded) {
402 // newChildren = null;
403 // }
404
405 var iconEleCls = (_iconEleCls = {}, _defineProperty(_iconEleCls, prefixCls + '-iconEle', true), _defineProperty(_iconEleCls, prefixCls + '-icon_loading', this.state.dataLoading), _defineProperty(_iconEleCls, prefixCls + '-icon__' + iconState, true), _iconEleCls);
406 var selectHandle = function selectHandle() {
407 var titleClass = props.titleClass ? prefixCls + '-title' + ' ' + props.className : prefixCls + '-title';
408 // const icon = (props.showIcon || props.loadData && this.state.dataLoading) ?
409 // <span className={classNames(iconEleCls)}></span> : null;
410 var icon = void 0;
411 if (props.showIcon && props.icon) {
412 icon = _react2["default"].createElement(
413 'span',
414 {
415 className: (0, _classnames2["default"])(prefixCls + '-iconEle', prefixCls + '-icon__customize')
416 },
417 typeof currentIcon === 'function' ? _react2["default"].createElement(props.icon, _extends({}, _this4.props)) : props.icon
418 );
419 } else if (props.showIcon || props.loadData && _this4.state.dataLoading) {
420 icon = _react2["default"].createElement('span', { className: (0, _classnames2["default"])(iconEleCls) });
421 }
422 var title = _react2["default"].createElement(
423 'span',
424 { className: titleClass, style: props.titleStyle },
425 content
426 );
427 var wrap = prefixCls + '-node-content-wrapper';
428 var domProps = {
429 className: wrap + ' ' + wrap + '-' + (iconState === expandedState ? iconState : 'normal')
430 };
431 if (!props.disabled) {
432 if (props.selected || !props._dropTrigger && _this4.state.dragNodeHighlight) {
433 domProps.className += ' ' + prefixCls + '-node-selected';
434 }
435 domProps.onClick = function (e) {
436 var _this = _this4;
437 e.preventDefault();
438 if (props.selectable) {
439 _this.onSelect();
440 }
441
442 // not fire check event
443 // if (props.checkable) {
444 // this.onCheck();
445 // }
446 };
447
448 if (props.onDoubleClick) {
449 domProps.onDoubleClick = _this4.onDoubleClick;
450 }
451
452 if (props.onRightClick) {
453 domProps.onContextMenu = _this4.onContextMenu;
454 }
455 if (props.onMouseEnter) {
456 domProps.onMouseEnter = _this4.onMouseEnter;
457 }
458 if (props.onMouseLeave) {
459 domProps.onMouseLeave = _this4.onMouseLeave;
460 }
461
462 if (props.draggable) {
463 domProps.className += ' draggable';
464 if (ieOrEdge) {
465 // ie bug!
466 domProps.href = '#';
467 }
468 domProps.draggable = true;
469 domProps['aria-grabbed'] = true;
470 domProps.onDragStart = _this4.onDragStart;
471 }
472 }
473 //设置tabIndex
474 if (props.focusable) {
475 domProps.onKeyDown = _this4.onKeyDown;
476 domProps.tabIndex = -1;
477 if (props.tabIndexKey) {
478 if (props.eventKey == props.tabIndexKey) {
479 domProps.tabIndex = props.tabIndexValue;
480 }
481 } else if (props.pos == '0-0') {
482 domProps.tabIndex = props.tabIndexValue;
483 }
484 }
485
486 return _react2["default"].createElement(
487 'a',
488 _extends({ ref: function ref(el) {
489 _this4.selectHandle = el;
490 }, pos: props.pos, title: typeof content === 'string' ? content : '' }, domProps),
491 icon,
492 title
493 );
494 };
495
496 var liProps = {};
497 if (props.liAttr) {
498 liProps = _extends({}, props.liAttr);
499 }
500 if (props.draggable) {
501 liProps.onDragEnter = this.onDragEnter;
502 liProps.onDragOver = this.onDragOver;
503 liProps.onDragLeave = this.onDragLeave;
504 liProps.onDrop = this.onDrop;
505 liProps.onDragEnd = this.onDragEnd;
506 }
507 var disabledCls = '';
508 var dragOverCls = '';
509 if (props.disabled) {
510 disabledCls = prefixCls + '-treenode-disabled';
511 } else if (props.dragOver) {
512 dragOverCls = 'drag-over';
513 } else if (props.dragOverGapTop) {
514 dragOverCls = 'drag-over-gap-top';
515 } else if (props.dragOverGapBottom) {
516 dragOverCls = 'drag-over-gap-bottom';
517 }
518
519 var filterCls = props.filterTreeNode ? props.filterTreeNode(this) ? 'filter-node' : '' : '';
520
521 var noopSwitcher = function noopSwitcher() {
522 var _cls2;
523
524 var cls = (_cls2 = {}, _defineProperty(_cls2, prefixCls + '-switcher', true), _defineProperty(_cls2, prefixCls + '-switcher-noop', true), _cls2);
525 if (props.showLine) {
526 // console.log('line---------');
527 cls[prefixCls + '-center_docu'] = !props.last;
528 cls[prefixCls + '-bottom_docu'] = props.last;
529 } else {
530 cls[prefixCls + '-noline_docu'] = true;
531 }
532 return _react2["default"].createElement('span', { className: (0, _classnames2["default"])(cls) });
533 };
534 var selectedCls = props.selected ? prefixCls + '-treenode-selected' : '';
535 var focusedCls = props.focused ? prefixCls + '-treenode-focused' : '';
536 var expandedCls = prefixCls + '-treenode-' + expandedState;
537 return _react2["default"].createElement(
538 'li',
539 _extends({}, liProps, { style: props.style,
540 className: (0, _classnames2["default"])(props.className, disabledCls, dragOverCls, filterCls, selectedCls, focusedCls, expandedCls)
541 }),
542 canRenderSwitcher ? this.renderSwitcher(props, expandedState) : noopSwitcher(),
543 props.checkable ? this.renderCheckbox(props) : null,
544 selectHandle(),
545 newChildren
546 );
547 };
548
549 return TreeNode;
550}(_react2["default"].Component);
551
552TreeNode.isTreeNode = 1;
553
554TreeNode.propTypes = {
555 prefixCls: _propTypes2["default"].string,
556 disabled: _propTypes2["default"].bool,
557 disableCheckbox: _propTypes2["default"].bool,
558 expanded: _propTypes2["default"].bool,
559 isLeaf: _propTypes2["default"].bool,
560 root: _propTypes2["default"].object,
561 onSelect: _propTypes2["default"].func,
562 openIcon: _propTypes2["default"].element,
563 closeIcon: _propTypes2["default"].element,
564 style: _propTypes2["default"].object,
565 className: _propTypes2["default"].string,
566 titleClass: _propTypes2["default"].string,
567 titleStyle: _propTypes2["default"].object,
568 switcherClass: _propTypes2["default"].string,
569 switcherStyle: _propTypes2["default"].object
570};
571
572TreeNode.defaultProps = {
573 title: defaultTitle,
574 tabIndexValue: 0,
575 mustExpandable: false
576};
577
578exports["default"] = TreeNode;
579module.exports = exports['default'];
\No newline at end of file