UNPKG

40 kBJavaScriptView Raw
1
2/*!
3 * EyzyTree v0.0.19
4 * (c) 2019 amsik
5 * Released under the MIT License.
6 */
7
8import React from 'react';
9
10/*! *****************************************************************************
11Copyright (c) Microsoft Corporation. All rights reserved.
12Licensed under the Apache License, Version 2.0 (the "License"); you may not use
13this file except in compliance with the License. You may obtain a copy of the
14License at http://www.apache.org/licenses/LICENSE-2.0
15
16THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19MERCHANTABLITY OR NON-INFRINGEMENT.
20
21See the Apache Version 2.0 License for specific language governing permissions
22and limitations under the License.
23***************************************************************************** */
24/* global Reflect, Promise */
25
26var extendStatics = function(d, b) {
27 extendStatics = Object.setPrototypeOf ||
28 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
29 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
30 return extendStatics(d, b);
31};
32
33function __extends(d, b) {
34 extendStatics(d, b);
35 function __() { this.constructor = d; }
36 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37}
38
39var __assign = function() {
40 __assign = Object.assign || function __assign(t) {
41 for (var s, i = 1, n = arguments.length; i < n; i++) {
42 s = arguments[i];
43 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
44 }
45 return t;
46 };
47 return __assign.apply(this, arguments);
48};
49
50var hasOwn = {}.hasOwnProperty;
51function cn(args) {
52 var result = [];
53 for (var key in args) {
54 if (hasOwn.call(args, key) && args[key]) {
55 result.push(key);
56 }
57 }
58 return result.join(' ');
59}
60//# sourceMappingURL=cn.js.map
61
62function shallowEqual(objA, objB, keys) {
63 if (objA === objB) {
64 return true;
65 }
66 for (var i = 0; i < keys.length; i++) {
67 var key = keys[i];
68 var valueA = objA[key];
69 var valueB = objB[key];
70 if (valueA !== valueB) {
71 return false;
72 }
73 }
74 return true;
75}
76//# sourceMappingURL=shallowEqual.js.map
77
78var hasChild = function (node) {
79 return node.isBatch || Array.isArray(node.child) && node.child.length > 0;
80};
81var comparingKeys = [
82 'id', 'checked', 'selected', 'child', 'checked', 'expanded', 'hash'
83];
84var TreeNode = /** @class */ (function (_super) {
85 __extends(TreeNode, _super);
86 function TreeNode() {
87 var _this = _super !== null && _super.apply(this, arguments) || this;
88 _this.handleSelect = function (event) {
89 if (_this.props.disabled) {
90 return;
91 }
92 if (_this.props.onSelect) {
93 _this.props.onSelect(_this.getNode(), event);
94 }
95 };
96 _this.handleCheck = function () {
97 if (_this.props.disabled || _this.props.disabledCheckbox) {
98 return;
99 }
100 if (_this.props.onCheck) {
101 _this.props.onCheck(_this.getNode());
102 }
103 };
104 _this.handleExpand = function () {
105 if (_this.props.onExpand) {
106 _this.props.onExpand(_this.getNode());
107 }
108 };
109 _this.handleDoubleClick = function (e) {
110 if (_this.props.onDoubleClick) {
111 _this.props.onDoubleClick(_this.getNode());
112 }
113 };
114 _this.renderCheckbox = function () {
115 if (!_this.props.checkable || _this.props.hidenCheckbox) {
116 return null;
117 }
118 var Checkbox = _this.props.checkboxRenderer;
119 if (!Checkbox) {
120 return React.createElement("span", { className: "node-checkbox", onMouseUp: _this.handleCheck });
121 }
122 return (React.createElement("span", { className: "node-checkbox-overrided", onMouseUp: _this.handleCheck },
123 React.createElement(Checkbox, { node: _this.getNode() })));
124 };
125 _this.renderArrow = function () {
126 var ArrowRenderer = _this.props.arrowRenderer;
127 if (!hasChild(_this.props)) {
128 return React.createElement("span", { className: "node-noop" });
129 }
130 if (!ArrowRenderer) {
131 return React.createElement("span", { className: "node-arrow", onMouseUp: _this.handleExpand });
132 }
133 return (React.createElement("span", { className: "node-arrow-extended", onMouseUp: _this.handleExpand },
134 React.createElement(ArrowRenderer, { node: _this.getNode() })));
135 };
136 return _this;
137 }
138 TreeNode.prototype.shouldComponentUpdate = function (nextProps) {
139 if (0 === nextProps.depth && this.props.hash !== nextProps.hash) {
140 return true;
141 }
142 return !shallowEqual(this.props, nextProps, comparingKeys);
143 };
144 TreeNode.prototype.getNode = function () {
145 var _a = this.props, id = _a.id, checked = _a.checked, selected = _a.selected, text = _a.text, child = _a.child, expanded = _a.expanded, disabled = _a.disabled, disabledCheckbox = _a.disabledCheckbox, parent = _a.parent, isBatch = _a.isBatch, depth = _a.depth;
146 var node = {
147 id: id,
148 checked: checked,
149 selected: selected,
150 text: text,
151 child: child,
152 parent: parent,
153 depth: depth
154 };
155 node.expanded = !!expanded;
156 node.disabled = !!disabled;
157 node.isBatch = !!isBatch;
158 node.disabledCheckbox = !!disabledCheckbox;
159 return node;
160 };
161 TreeNode.prototype.render = function () {
162 var _a = this.props, loading = _a.loading, checked = _a.checked, selected = _a.selected, children = _a.children, expanded = _a.expanded, disabled = _a.disabled, disabledCheckbox = _a.disabledCheckbox, indeterminate = _a.indeterminate, useIndeterminateState = _a.useIndeterminateState, TextRenderer = _a.textRenderer;
163 var text = this.props.text;
164 var nodeContentClass = cn({
165 'node-content': true,
166 'has-child': hasChild(this.props),
167 'selected': selected,
168 'checked': checked,
169 'expanded': expanded,
170 'disabled': disabled,
171 'loading': loading,
172 'disabled-checkbox': disabledCheckbox,
173 'indeterminate': !checked && indeterminate && false !== useIndeterminateState
174 });
175 return (React.createElement("li", { className: "tree-node" },
176 React.createElement("div", { className: nodeContentClass },
177 this.renderArrow(),
178 this.renderCheckbox(),
179 React.createElement("span", { className: "node-text", onMouseUp: this.handleSelect, onDoubleClick: this.handleDoubleClick }, TextRenderer ? React.createElement(TextRenderer, { node: this.getNode() }) : text)),
180 hasChild(this.props) && expanded &&
181 React.createElement("ul", { className: "node-child" }, children)));
182 };
183 return TreeNode;
184}(React.Component));
185//# sourceMappingURL=TreeNode.js.map
186
187var hasOwnProp = {}.hasOwnProperty;
188function grapObjProps(obj, props) {
189 return props.reduce(function (result, name) {
190 if (name in obj) {
191 result[name] = obj[name];
192 }
193 return result;
194 }, {});
195}
196function isArray(obj) {
197 return Array.isArray(obj);
198}
199function isExpandable(node) {
200 return !!(node.child && node.child.length) || !!node.isBatch;
201}
202function isNodeCheckable(node) {
203 return !(!!node.disabled || !!node.disabledCheckbox);
204}
205function isFunction(value) {
206 return 'function' === typeof value;
207}
208function has(targetArray, targetValue) {
209 return !!~targetArray.indexOf(targetValue);
210}
211function copyArray(arr) {
212 return arr.concat([]);
213}
214function copyObject(obj) {
215 var newObj = {};
216 for (var i in obj) {
217 if (hasOwnProp.call(obj, i)) {
218 newObj[i] = isArray(obj[i]) ? copyArray(obj[i]) : obj[i];
219 }
220 }
221 return newObj;
222}
223function isRoot(node) {
224 return node && !node.parent;
225}
226function isLeaf(node) {
227 return !node.child || (0 === node.child.length && !node.isBatch);
228}
229function isNodeIndeterminate(node, treeCheckedNodes, indeterminateNodes) {
230 if (!node.child.length) {
231 return false;
232 }
233 var hasIndeterminate = node.child.some(function (child) {
234 return !child.disabled && !child.disabledCheckbox && -1 !== indeterminateNodes.indexOf(child.id);
235 });
236 if (hasIndeterminate) {
237 return true;
238 }
239 var uncheckedNodes = node.child.reduce(function (count, item) {
240 if (true !== item.disabled && true !== item.disabledCheckbox && -1 === treeCheckedNodes.indexOf(item.id)) {
241 count++;
242 }
243 return count;
244 }, 0);
245 return uncheckedNodes > 0 && uncheckedNodes < node.child.length;
246}
247//# sourceMappingURL=index.js.map
248
249var TreeAPI = /** @class */ (function () {
250 function TreeAPI(tree, state) {
251 this.tree = tree;
252 this.state = state;
253 }
254 TreeAPI.prototype.selected = function () {
255 var state = this.state;
256 var selectedNodes = this.tree.selectedNodes
257 .map(function (id) { return state.getNodeById(id); });
258 return selectedNodes.filter(function (item) { return null !== item; });
259 };
260 TreeAPI.prototype.checked = function (valueConsistsOf, ignoreDisabled) {
261 var state = this.state;
262 var checkedNodes = [];
263 this.tree.checkedNodes.forEach(function (id) {
264 var node = state.getNodeById(id);
265 if (node) {
266 checkedNodes.push(node);
267 }
268 });
269 if ('WITH_INDETERMINATE' === valueConsistsOf) {
270 this.tree.indeterminateNodes.forEach(function (id) {
271 var node = state.getNodeById(id);
272 if (node) {
273 checkedNodes.push(node);
274 }
275 });
276 }
277 if (ignoreDisabled) {
278 checkedNodes = checkedNodes.filter(isNodeCheckable);
279 }
280 switch (valueConsistsOf) {
281 case 'LEAF': return checkedNodes.filter(function (node) { return isLeaf(node); });
282 case 'BRANCH':
283 return checkedNodes.filter(function (node) {
284 if (node.parent && node.parent.checked) {
285 return false;
286 }
287 return true;
288 });
289 }
290 return checkedNodes;
291 };
292 return TreeAPI;
293}());
294//# sourceMappingURL=TreeAPI.js.map
295
296function recurseDown(obj, fn, excludeSelf, depth) {
297 if (depth === void 0) { depth = 0; }
298 var res;
299 if (Array.isArray(obj)) {
300 return obj.map(function (node) { return recurseDown(node, fn, false, depth); });
301 }
302 // TODO: get rid of it: create a State interface
303 if (obj[0]) {
304 return Object.keys(obj)
305 .filter(function (key) { return isFinite(+key); })
306 .map(function (key) { return recurseDown(obj[key], fn, false, depth); });
307 }
308 if (!excludeSelf) {
309 res = fn(obj, depth);
310 }
311 if (res !== false && obj.child && obj.child.length) {
312 res = recurseDown(obj.child, fn, false, depth + 1);
313 }
314 return res;
315}
316function rootElement(obj) {
317 var node = obj.parent;
318 while (node) {
319 if (!node.parent) {
320 return node;
321 }
322 node = node.parent;
323 }
324 return null;
325}
326function traverseUp(obj, fn) {
327 var node = obj.parent;
328 while (node) {
329 if (false === fn(node) || !node.parent) {
330 return;
331 }
332 node = node.parent;
333 }
334}
335function getFirstChild(node, onlyEnabled) {
336 if (onlyEnabled) {
337 var enabledNode = node.child.filter(function (n) { return !n.disabled; });
338 return enabledNode.length ? enabledNode[0] : null;
339 }
340 return node.child[0] || null;
341}
342function getLastChild(node, onlyEnabled) {
343 var len = node.child ? node.child.length : 0;
344 if (!len) {
345 return null;
346 }
347 if (onlyEnabled) {
348 var enabledNode = node.child.filter(function (n) { return !n.disabled; });
349 var enabledNodeLen = enabledNode.length;
350 return enabledNodeLen ? enabledNode[enabledNodeLen - 1] : null;
351 }
352 return node.child[len - 1];
353}
354function flatMap(collection, ignoreCollapsed) {
355 var result = {
356 nodes: [],
357 ids: []
358 };
359 recurseDown(collection, function (node) {
360 if (node.disabled) {
361 return;
362 }
363 if (ignoreCollapsed && node.parent && !node.parent.expanded) {
364 return;
365 }
366 result.nodes.push(node);
367 result.ids.push(node.id);
368 });
369 return result;
370}
371//# sourceMappingURL=traveler.js.map
372
373function iterable(key, value) {
374 if ('string' === typeof key) {
375 return [[key, value]];
376 }
377 if (!value) {
378 var res = [];
379 for (var i in key) {
380 if (hasOwnProp.call(key, i)) {
381 res.push([i, key[i]]);
382 }
383 }
384 return res;
385 }
386 return [];
387}
388function replaceChild(node) {
389 if (!node || !node.child) {
390 return node;
391 }
392 node.child = copyArray(node.child);
393 node.child.forEach(function (child) {
394 var replaced = replaceChild(child);
395 replaced.parent = node;
396 return replaced;
397 });
398 return node;
399}
400function getItemById(id, targetState) {
401 return Object.keys(targetState)
402 .find(function (k) { return targetState[k].id === id; }) || null;
403}
404function getNodeIndex(nodeId, parent, nodesLength) {
405 if (parent.child) {
406 var childIndex_1 = null;
407 parent.child.some(function (node, i) {
408 if (nodeId === node.id) {
409 childIndex_1 = i;
410 return true;
411 }
412 return false;
413 });
414 return childIndex_1;
415 }
416 for (var i = 0; i < nodesLength; i++) {
417 if (parent[i].id === nodeId) {
418 return i;
419 }
420 }
421 return null;
422}
423function updateChildNodes(parentNode) {
424 parentNode.child.forEach(function (child) {
425 child.parent = parentNode;
426 });
427 return parentNode;
428}
429var State = /** @class */ (function () {
430 function State(state, stateLength) {
431 this.length = stateLength;
432 this.nodes = state;
433 }
434 State.prototype.updateRootNode = function (node, iterableValue) {
435 var i = getItemById(node.id, this.nodes);
436 var newObj = copyObject(node);
437 if (iterableValue) {
438 iterableValue.forEach(function (_a) {
439 var key = _a[0], value = _a[1];
440 node[key] = value;
441 newObj[key] = value;
442 });
443 }
444 if (null !== i) {
445 this.nodes[i] = updateChildNodes(newObj);
446 }
447 };
448 State.prototype.updateLeafNode = function (node, iterableValue) {
449 var root = rootElement(node);
450 var parentNode = node.parent;
451 if (!parentNode || !root || !parentNode.child) {
452 return;
453 }
454 var index = getNodeIndex(node.id, parentNode, this.length);
455 if (null === index) {
456 return;
457 }
458 if (iterableValue) {
459 iterableValue.forEach(function (_a) {
460 var key = _a[0], value = _a[1];
461 node[key] = value;
462 parentNode.child[index][key] = value;
463 });
464 }
465 this.updateRootNode(replaceChild(root));
466 };
467 State.prototype.set = function (id, key, value) {
468 var node = this.getNodeById(id);
469 if (!node) {
470 return this.nodes;
471 }
472 if (isRoot(node)) {
473 this.updateRootNode(node, iterable(key, value));
474 }
475 else {
476 this.updateLeafNode(node, iterable(key, value));
477 }
478 return this.nodes;
479 };
480 State.prototype.getNodeById = function (id) {
481 var node = null;
482 recurseDown(this.nodes, function (obj) {
483 if (obj.id === id) {
484 node = obj;
485 return false;
486 }
487 });
488 return node;
489 };
490 State.prototype.get = function () {
491 return this.nodes;
492 };
493 State.prototype.toArray = function () {
494 var result = [];
495 var state = this.nodes;
496 for (var i in state) {
497 if (hasOwnProp.call(state, i)) {
498 result.push(state[i]);
499 }
500 }
501 return result;
502 };
503 return State;
504}());
505//# sourceMappingURL=state.js.map
506
507function s4() {
508 return Math.floor((1 + Math.random()) * 0x10000)
509 .toString(16)
510 .substring(1);
511}
512function uuid() {
513 return s4() + '-' + s4();
514}
515//# sourceMappingURL=uuid.js.map
516
517function parseNode(data, parentNode) {
518 if (!data || !Array.isArray(data)) {
519 return [];
520 }
521 var parent = parentNode || null;
522 if ('string' === typeof data) {
523 return [
524 { text: data, id: uuid(), parent: parent, child: [] }
525 ];
526 }
527 return data.map(function (node) {
528 if ('string' === typeof node) {
529 return {
530 id: uuid(),
531 text: node,
532 parent: parent,
533 child: []
534 };
535 }
536 node.id = node.id || uuid();
537 node.child = Array.isArray(node.child)
538 ? parseNode(node.child, node)
539 : [];
540 node.parent = parent;
541 return node;
542 });
543}
544//# sourceMappingURL=parser.js.map
545
546function linkedNode(node, state, ignoreExpanded) {
547 var result = {
548 current: node
549 };
550 var currentNodeId = node.id;
551 var parent = node.parent;
552 var neighborIds = (parent ? parent.child : state.toArray())
553 .map(function (n) { return n.id; });
554 var currentPos = neighborIds.indexOf(currentNodeId);
555 var i = 0;
556 if (node.expanded && node.child.length && true !== ignoreExpanded) {
557 var firstChild = getFirstChild(node, true);
558 if (firstChild) {
559 result.next = state.getNodeById(firstChild.id);
560 }
561 }
562 while (i++ < neighborIds.length) {
563 if (!result.next) {
564 var node_1 = state.getNodeById(neighborIds[currentPos + i]);
565 if (node_1 && !node_1.disabled) {
566 result.next = node_1;
567 }
568 }
569 if (!result.prev) {
570 var node_2 = state.getNodeById(neighborIds[currentPos - i]);
571 if (node_2 && !node_2.disabled) {
572 if (node_2.expanded) {
573 var lastChild = getLastChild(node_2, true);
574 if (lastChild) {
575 result.prev = state.getNodeById(lastChild.id);
576 }
577 }
578 else {
579 result.prev = node_2;
580 }
581 }
582 }
583 }
584 if (parent) {
585 if (!result.prev) {
586 result.prev = state.getNodeById(parent.id);
587 }
588 if (!result.next) {
589 result.next = linkedNode(parent, state, true).next;
590 }
591 }
592 return result;
593}
594//# sourceMappingURL=linkedNode.js.map
595
596var mutatingFields = [
597 'checkable',
598 'useIndeterminateState',
599 'checkboxRenderer',
600 'arrowRenderer',
601 'textRenderer'
602];
603var EyzyTree = /** @class */ (function (_super) {
604 __extends(EyzyTree, _super);
605 function EyzyTree(props) {
606 var _this = _super.call(this, props) || this;
607 _this.selectedNodes = [];
608 _this.checkedNodes = [];
609 _this.indeterminateNodes = [];
610 _this.fireEvent = function (name, id) {
611 var args = [];
612 for (var _i = 2; _i < arguments.length; _i++) {
613 args[_i - 2] = arguments[_i];
614 }
615 var eventCb = _this.props[name];
616 if (!eventCb) {
617 return;
618 }
619 var node = _this.getState().getNodeById(id);
620 if (node) {
621 eventCb.call.apply(eventCb, [null, node].concat(args));
622 }
623 };
624 _this.refreshIndeterminateState = function (id, willBeChecked, shouldRender) {
625 var checkedNodes = copyArray(_this.checkedNodes);
626 var indeterminateNodes = copyArray(_this.indeterminateNodes);
627 var state = _this.getState();
628 var childIds = [];
629 var node = state.getNodeById(id);
630 var nodesForEvent = [];
631 if (!node) {
632 return;
633 }
634 recurseDown(node, function (child) {
635 if (!child.disabled && !child.disabledCheckbox) {
636 childIds.push(child.id);
637 if (child.checked !== willBeChecked) {
638 nodesForEvent.push(child.id);
639 }
640 }
641 }, true);
642 if (willBeChecked) {
643 checkedNodes.push.apply(checkedNodes, childIds.filter(function (id) { return !has(checkedNodes, id); }));
644 }
645 else {
646 checkedNodes = checkedNodes.filter(function (nodeId) { return !has(childIds, nodeId); });
647 }
648 traverseUp(node, function (parentNode) {
649 if (parentNode.disabledCheckbox || parentNode.disabled) {
650 return false;
651 }
652 var isIndeterminate = isNodeIndeterminate(parentNode, checkedNodes, indeterminateNodes);
653 var id = parentNode.id;
654 if (isIndeterminate) {
655 if (!has(indeterminateNodes, id)) {
656 indeterminateNodes.push(id);
657 }
658 checkedNodes = checkedNodes.filter(function (nodeId) { return nodeId !== id; });
659 }
660 else {
661 indeterminateNodes = indeterminateNodes.filter(function (nodeId) { return nodeId !== id; });
662 if (willBeChecked && !has(checkedNodes, id)) {
663 checkedNodes.push(id);
664 }
665 else {
666 checkedNodes = checkedNodes.filter(function (nodeId) { return nodeId !== id; });
667 }
668 }
669 });
670 indeterminateNodes = indeterminateNodes.filter(function (nodeId) { return !has(checkedNodes, nodeId); });
671 if (willBeChecked) {
672 checkedNodes.forEach(function (id) {
673 if (!has(_this.checkedNodes, id)) {
674 state.set(id, 'checked', true);
675 }
676 });
677 }
678 else {
679 _this.checkedNodes.forEach(function (id) {
680 if (!has(checkedNodes, id)) {
681 state.set(id, 'checked', false);
682 }
683 });
684 childIds.forEach(function (id) {
685 state.set(id, 'checked', false);
686 });
687 }
688 _this.indeterminateNodes.forEach(function (id) {
689 state.set(id, 'indeterminate', false);
690 });
691 var useIndeterminateState = false !== _this.props.useIndeterminateState;
692 if (useIndeterminateState) {
693 indeterminateNodes.forEach(function (id) { return state.set(id, 'indeterminate', true); });
694 }
695 _this.checkedNodes = checkedNodes;
696 _this.indeterminateNodes = indeterminateNodes;
697 if (false !== shouldRender) {
698 _this.updateState(state);
699 }
700 nodesForEvent.forEach(function (id) {
701 _this.fireEvent('onCheck', id, willBeChecked);
702 });
703 };
704 _this.getState = function () {
705 return _this._state;
706 };
707 _this.getSelectedNode = function () {
708 var lastSelectedNode = _this.selectedNodes[_this.selectedNodes.length - 1];
709 if (!lastSelectedNode) {
710 return null;
711 }
712 return _this.getState().getNodeById(lastSelectedNode);
713 };
714 _this.updateState = function (state) {
715 _this.setState({ nodes: state.get() });
716 };
717 _this.unselectAll = function () {
718 var state = _this.getState();
719 _this.selectedNodes = _this.selectedNodes.filter(function (id) {
720 state.set(id, 'selected', false);
721 return false;
722 });
723 _this.updateState(state);
724 };
725 _this.unselect = function (node) {
726 if (!node.selected) {
727 return;
728 }
729 var state = _this.getState();
730 state.set(node.id, 'selected', false);
731 _this.selectedNodes = _this.selectedNodes.filter(function (id) {
732 if (id !== node.id) {
733 return true;
734 }
735 if (state.getNodeById(id)) {
736 state.set(id, 'selected', false);
737 _this.fireEvent('onUnSelect', id);
738 }
739 return false;
740 });
741 _this.updateState(state);
742 };
743 _this.select = function (node, ignoreEvent, extendSelection) {
744 var state = _this.getState();
745 var id = node.id;
746 var events = [];
747 var multiple = _this.props.multiple;
748 if (extendSelection && node.selected) {
749 return _this.unselect(node);
750 }
751 if (!multiple && node.selected) {
752 return;
753 }
754 if (!multiple || (multiple && !extendSelection)) {
755 _this.selectedNodes = _this.selectedNodes.filter(function (nodeId) {
756 var node = state.getNodeById(nodeId);
757 if (node) {
758 state.set(node.id, 'selected', false);
759 events.push(['onUnSelect', node.id]);
760 }
761 return false;
762 });
763 }
764 state.set(id, 'selected', true);
765 if (!extendSelection) {
766 _this.focusedNode = id;
767 }
768 _this.selectedNodes.push(id);
769 _this.updateState(state);
770 if (true !== ignoreEvent) {
771 events.push(['onSelect', id]);
772 events.forEach(function (event) { return _this.fireEvent(event[0], event[1]); });
773 }
774 };
775 _this.selectRange = function (focusedNode, targetNode) {
776 var _a = flatMap(_this.state.nodes, true), ids = _a.ids, nodes = _a.nodes;
777 var focusedIndex = ids.indexOf(focusedNode);
778 var targetIndex = ids.indexOf(targetNode);
779 if (!~focusedIndex || !~targetIndex) {
780 return;
781 }
782 var start = Math.min(focusedIndex, targetIndex);
783 var end = Math.max(focusedIndex, targetIndex) + 1;
784 var state = _this.getState();
785 var willBeSelected = nodes.slice(start, end).map(function (node) { return node.id; });
786 var fireEvents = [];
787 _this.selectedNodes.forEach(function (id) {
788 if (!has(willBeSelected, id)) {
789 state.set(id, 'selected', false);
790 fireEvents.push(['onUnSelect', id]);
791 }
792 });
793 _this.selectedNodes = willBeSelected.map(function (id) {
794 if (!has(_this.selectedNodes, id)) {
795 state.set(id, 'selected', true);
796 fireEvents.push(['onSelect', id]);
797 }
798 return id;
799 });
800 _this.updateState(state);
801 fireEvents.forEach(function (_a) {
802 var name = _a[0], id = _a[1];
803 _this.fireEvent(name, id);
804 });
805 };
806 _this.check = function (node) {
807 if (!_this.props.checkable) {
808 return;
809 }
810 var state = _this.getState();
811 var willBeChecked = !node.checked;
812 var id = node.id;
813 state.set(id, 'checked', willBeChecked);
814 if (willBeChecked) {
815 _this.checkedNodes = _this.checkedNodes.concat([id]);
816 }
817 else {
818 _this.checkedNodes = _this.checkedNodes.filter(function (checkedId) { return id !== checkedId; });
819 }
820 if (_this.props.selectOnCheck) {
821 _this.select(node);
822 }
823 if (_this.props.noCascade !== true) {
824 _this.refreshIndeterminateState(node.id, willBeChecked);
825 _this.fireEvent('onCheck', id, willBeChecked);
826 }
827 else {
828 _this.updateState(state);
829 _this.fireEvent('onCheck', id, willBeChecked);
830 }
831 };
832 _this.expand = function (node) {
833 if (node.isBatch) {
834 return _this.loadChild(node);
835 }
836 if (!node.child.length) {
837 return;
838 }
839 var state = _this.getState();
840 var selectOnExpand = _this.props.selectOnExpand;
841 state.set(node.id, 'expanded', !node.expanded);
842 if (selectOnExpand && !node.selected) {
843 _this.select(node);
844 }
845 _this.updateState(state);
846 _this.fireEvent('onExpand', node.id, !node.expanded);
847 };
848 _this.handleDoubleClick = function (node) {
849 _this.fireEvent('onDoubleClick', node.id);
850 if (node.disabled || isLeaf(node) || _this.props.expandOnSelect) {
851 return;
852 }
853 _this.expand(node);
854 };
855 _this.handleSelect = function (node, event) {
856 if (node.disabled) {
857 return;
858 }
859 if (_this.props.preventSelectParent && isExpandable(node)) {
860 return _this.expand(node);
861 }
862 var _a = _this.props, multiple = _a.multiple, checkOnSelect = _a.checkOnSelect, expandOnSelect = _a.expandOnSelect, checkable = _a.checkable;
863 if (event.shiftKey && multiple && _this.focusedNode) {
864 return _this.selectRange(_this.focusedNode, node.id);
865 }
866 _this.select(node, false, event.ctrlKey);
867 if (event.ctrlKey) {
868 return;
869 }
870 if (checkable && checkOnSelect && !node.disabledCheckbox) {
871 _this.check(node);
872 }
873 else if (expandOnSelect) {
874 _this.expand(node);
875 }
876 };
877 _this.handleKeyUp = function (event) {
878 if (false === _this.props.keyboardNavigation) {
879 return;
880 }
881 var keyCode = event.keyCode;
882 var selectedNode = _this.getSelectedNode();
883 if (selectedNode) {
884 switch (keyCode) {
885 case 32: // space
886 case 13: // enter
887 if (_this.props.checkable && !selectedNode.disabled && !selectedNode.disabledCheckbox) {
888 _this.check(selectedNode);
889 }
890 else if (!isLeaf(selectedNode)) {
891 _this.expand(selectedNode);
892 }
893 break;
894 case 27: // esc
895 if (_this.props.multiple) {
896 _this.unselectAll();
897 }
898 else {
899 _this.unselect(selectedNode);
900 }
901 break;
902 case 39: // right arrow
903 if (!isLeaf(selectedNode)) {
904 if (!selectedNode.expanded) {
905 _this.expand(selectedNode);
906 }
907 else {
908 var firstChild = getFirstChild(selectedNode, true);
909 if (firstChild) {
910 _this.select(firstChild);
911 }
912 }
913 }
914 break;
915 case 37: // left arrow
916 if (isLeaf(selectedNode) || !selectedNode.expanded) {
917 var parentNode = selectedNode.parent;
918 if (parentNode) {
919 _this.select(parentNode);
920 }
921 }
922 else if (selectedNode.expanded) {
923 _this.expand(selectedNode);
924 }
925 break;
926 case 40: // bottom arrow
927 var next = linkedNode(selectedNode, _this.getState()).next;
928 if (next) {
929 _this.select(next);
930 }
931 break;
932 case 38: // up arrow
933 var prev = linkedNode(selectedNode, _this.getState()).prev;
934 if (prev) {
935 _this.select(prev);
936 }
937 break;
938 }
939 }
940 };
941 _this.appendChild = function (id, nodes) {
942 var _a;
943 var state = _this.getState();
944 var node = state.getNodeById(id);
945 if (!node) {
946 return null;
947 }
948 var parentDepth = node.depth || 0;
949 var child = parseNode(nodes).map(function (obj) {
950 obj.parent = node;
951 return obj;
952 });
953 var cascadeCheck = true !== _this.props.noCascade;
954 var checkedNodes = [];
955 recurseDown(child, function (obj, depth) {
956 obj.depth = parentDepth + depth + 1;
957 if (cascadeCheck && obj.parent && obj.parent.checked) {
958 obj.checked = true;
959 }
960 if (obj.checked && !~_this.checkedNodes.indexOf(obj.id)) {
961 checkedNodes.push(obj.id);
962 }
963 });
964 (_a = _this.checkedNodes).push.apply(_a, checkedNodes);
965 state.set(node.id, 'child', child);
966 if (cascadeCheck) {
967 checkedNodes.forEach(function (id) {
968 var node = state.getNodeById(id);
969 if (node && isLeaf(node)) {
970 _this.refreshIndeterminateState(id, true, false);
971 }
972 });
973 }
974 return node;
975 };
976 _this.loadChild = function (node) {
977 var _a = _this.props, fetchData = _a.fetchData, selectOnExpand = _a.selectOnExpand;
978 if (!fetchData || !isFunction(fetchData)) {
979 return;
980 }
981 var result = fetchData(node);
982 if (!result || !result.then) {
983 throw new Error('`fetchData` property must return a Promise');
984 }
985 var state = _this.getState();
986 var id = node.id;
987 state.set(id, 'loading', true);
988 result.then(function (nodes) {
989 _this.appendChild(id, nodes);
990 var selected = selectOnExpand ? true : !!node.selected;
991 state.set(id, {
992 loading: false,
993 expanded: true,
994 isBatch: false,
995 selected: selected
996 });
997 _this.fireEvent('onExpand', id, true);
998 if (selectOnExpand) {
999 _this.fireEvent('onSelect', id);
1000 }
1001 _this.updateState(state);
1002 });
1003 _this.updateState(state);
1004 };
1005 _this.renderNode = function (node) {
1006 var treePropsKeys = [
1007 'checkable', 'arrowRenderer', 'textRenderer', 'checkboxRenderer'
1008 ];
1009 var treeProps = treePropsKeys.reduce(function (props, key) {
1010 if (_this.props[key]) {
1011 props[key] = _this.props[key];
1012 }
1013 return props;
1014 }, {});
1015 treeProps.hash = _this.state.hash;
1016 treeProps.useIndeterminateState = _this.props.useIndeterminateState;
1017 return (React.createElement(TreeNode, __assign({ key: node.id, node: node, onSelect: _this.handleSelect, onDoubleClick: _this.handleDoubleClick, onCheck: _this.check, onExpand: _this.expand }, treeProps, node), node.expanded ? node.child.map(_this.renderNode) : null));
1018 };
1019 var data = parseNode(props.data || []);
1020 var stateObject = {};
1021 data.forEach(function (item, i) {
1022 stateObject[i] = item;
1023 });
1024 recurseDown(stateObject, function (obj, depth) {
1025 obj.depth = depth;
1026 if (obj.selected) {
1027 _this.selectedNodes.push(obj.id);
1028 }
1029 if (obj.checked) {
1030 _this.checkedNodes.push(obj.id);
1031 }
1032 });
1033 _this._state = new State(stateObject, data.length);
1034 _this.checkedNodes.forEach(function (id) {
1035 var node = _this._state.getNodeById(id);
1036 if (node && isLeaf(node) && _this.props.noCascade !== true) {
1037 _this.refreshIndeterminateState(id, true, false);
1038 }
1039 });
1040 _this.state = {
1041 nodes: _this._state.get(),
1042 hash: uuid(),
1043 mutatingFields: grapObjProps(props, mutatingFields)
1044 };
1045 return _this;
1046 }
1047 EyzyTree.getDerivedStateFromProps = function (nextProps, state) {
1048 if (!shallowEqual(nextProps, state.mutatingFields, mutatingFields)) {
1049 return {
1050 hash: uuid(),
1051 mutatingFields: grapObjProps(nextProps, mutatingFields)
1052 };
1053 }
1054 return null;
1055 };
1056 EyzyTree.prototype.componentDidMount = function () {
1057 if (this.props.onReady) {
1058 this.props.onReady(new TreeAPI(this, this._state));
1059 }
1060 };
1061 EyzyTree.prototype.render = function () {
1062 var nodes = [];
1063 var props = this.props;
1064 var treeClass = 'theme' in props
1065 ? 'eyzy-tree ' + props.theme
1066 : 'eyzy-tree eyzy-theme';
1067 var stateNodes = this.state.nodes;
1068 for (var i = 0; i < this._state.length; i++) {
1069 nodes.push(this.renderNode(stateNodes[i]));
1070 }
1071 return (React.createElement("ul", { className: treeClass, tabIndex: -1, onKeyDown: this.handleKeyUp }, nodes));
1072 };
1073 EyzyTree.TreeNode = TreeNode;
1074 return EyzyTree;
1075}(React.Component));
1076
1077//# sourceMappingURL=index.js.map
1078
1079export default EyzyTree;
1080//# sourceMappingURL=eyzy-tree.js.map
1081
\No newline at end of file