UNPKG

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