UNPKG

react-hyper-tree

Version:
17 lines (14 loc) 30.5 kB
import*as e from"react";import{useState as t,useEffect as n,useMemo as r,useCallback as i}from"react"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var o=function(){return o=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},o.apply(this,arguments)};function a(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(n[r[i]]=e[r[i]])}return n}function s(e,t,n,r){return new(n||(n=Promise))((function(i,o){function a(e){try{l(r.next(e))}catch(e){o(e)}}function s(e){try{l(r.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}l((r=r.apply(e,t||[])).next())}))}function l(e,t){var n,r,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function d(e,t,n){if(n||2===arguments.length)for(var r,i=0,o=t.length;i<o;i++)!r&&i in t||(r||(r=Array.prototype.slice.call(t,0,i)),r[i]=t[i]);return e.concat(r||Array.prototype.slice.call(t))}var c=function(e,t){return"".concat(e*t,"px")},h=function(e){return e&&"[object Function]"==={}.toString.call(e)},p={childrenKey:"children",classes:{},depthGap:20,displayedName:function(e){return e.data.name},filter:function(){return!0},gapMode:"margin",horizontalLineStyles:{stroke:"black",strokeWidth:1,strokeDasharray:"1 1"},idKey:"id",opened:[],verticalLineOffset:5,verticalLineStyles:{stroke:"black",strokeWidth:1,strokeDasharray:"1 1"},verticalLineTopOffset:0},u=function(e){return Object.keys(e).filter((function(t){return!!e[t]})).reduce((function(e,t){return e?"".concat(e).concat(t?" ".concat(t):""):"".concat(t)}),"")},f=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e[0]&&"string"==typeof e[0]?e.join(" "):u(e[0])};function y(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],i=document.createElement("style");i.type="text/css","top"===n&&r.firstChild?r.insertBefore(i,r.firstChild):r.appendChild(i),i.styleSheet?i.styleSheet.cssText=e:i.appendChild(document.createTextNode(e))}}var g="ht_hyperNodeArrowIcon",v="ht_opened";y(".ht_hyperNodeArrowIcon {\n transition: transform 0.1s ease;\n cursor: pointer;\n margin-right: 4px; }\n .ht_hyperNodeArrowIcon.ht_opened {\n transform: rotate(90deg); }\n");var C=function(t){var n,r=t.onClick,i=t.opened;return e.createElement("svg",{className:f((n={},n[g]=!0,n[v]=!!i,n)),xmlns:"http://www.w3.org/2000/svg",width:"10",height:"10",viewBox:"0 0 24 24",onClick:r},e.createElement("path",{d:"M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z"}))},m="ht_loadingSpinner";y(".ht_loadingSpinner {\n animation: ht_ht-loading-spinner 1s linear infinite;\n margin-right: 4px; }\n\n@keyframes ht_ht-loading-spinner {\n from {\n transform: rotate(0deg); }\n to {\n transform: rotate(360deg); } }\n");var b=function(){return e.createElement("div",{className:m},e.createElement("svg",{xmlns:"http://www.w3.org/2000/svg",width:"10",height:"10",viewBox:"0 0 48 48"},e.createElement("g",{fill:"none"},e.createElement("path",{id:"track",fill:"#C6CCD2",d:"M24,48 C10.745166,48 0,37.254834 0,24 C0,10.745166 10.745166,0 24,0\n C37.254834,0 48,10.745166 48,24 C48,37.254834 37.254834,48 24,48 Z M24,44\n C35.045695,44 44,35.045695 44,24 C44,12.954305 35.045695,4 24,4\n C12.954305,4 4,12.954305 4,24 C4,35.045695 12.954305,44 24,44 Z"}),e.createElement("path",{id:"section",fill:"#3F4850",d:"M24,0 C37.254834,0 48,10.745166 48,24 L44,24 C44,12.954305 35.045695,4 24,4 L24,0 Z"}))))},N="ht_hyperNode";y(".ht_hyperNode {\n display: flex;\n align-items: center; }\n");var D=function(t){var n=t.displayedName,r=void 0===n?p.displayedName:n,i=t.node,o=t.onSelect,a=t.onToggle;return e.createElement("div",{className:N,onClick:o},(i.hasChildren()||i.options.async)&&!i.isLoading()&&e.createElement(C,{onClick:a,opened:i.isOpened()&&!!i.hasChildren()}),i.isLoading()&&e.createElement(b,null),e.createElement("div",null,h(r)?r(i):i.data[r]))},_="ht_verticalLine",L="ht_verticalSvgLine";y(".ht_verticalLine {\n position: absolute;\n top: 0;\n transition: all 0.2s ease; }\n .ht_verticalLine .ht_verticalSvgLine {\n transition: all 0.2s ease; }\n");var O=function(t){var n=t.depth,r=t.depthGap,i=t.count,o=t.nodeHeight,a=t.disableTransitions,s=t.verticalLineOffset,l=void 0===s?p.verticalLineOffset:s,d=t.verticalLineStyles,h=void 0===d?p.verticalLineStyles:d,u=t.verticalLineTopOffset,f=void 0===u?p.verticalLineTopOffset:u;return e.createElement("div",{className:_,style:{left:"".concat(l,"px"),marginLeft:c(n,r),top:"".concat(f,"px")}},e.createElement("svg",{style:{height:"".concat(Math.floor(i*o-(o?o/2:0))-f,"px"),width:"".concat(h.strokeWidth,"px"),position:"absolute",transition:a?"none":"all 0.2s ease"}},e.createElement("line",{x1:"0%",y1:"0%",x2:"0%",y2:"100%",className:L,style:h})))},E="ht_horizontalLine";y(".ht_horizontalLine {\n position: absolute;\n top: 0;\n display: flex;\n align-items: center;\n height: 100%; }\n");var w=function(t){var n=t.depth,r=t.depthGap,i=t.verticalLineOffset,o=void 0===i?p.verticalLineOffset:i,a=t.horizontalLineStyles,s=void 0===a?p.horizontalLineStyles:a,l=t.gapMode;return e.createElement("div",{className:E,style:{width:"".concat(r-o,"px"),left:"".concat("padding"===l?o-r+n*r:o-r,"px")}},e.createElement("svg",{style:{height:"".concat(s.strokeWidth,"px"),width:"100%"}},e.createElement("line",{x1:"0%",y1:"0%",x2:"100%",y2:"0%",style:s})))},K="ht_dragZone",S="ht_dragZoneVisible",x="ht_dragZoneItem",T="ht_dragZoneItemSelected";y(".ht_dragZone {\n position: absolute;\n top: 0;\n right: 0;\n bottom: 0;\n left: 0;\n display: flex;\n flex-direction: column;\n visibility: hidden;\n opacity: 0; }\n .ht_dragZone.ht_dragZoneVisible {\n visibility: visible;\n opacity: 1; }\n .ht_dragZone .ht_dragZoneItem {\n transition: background-color 0.2s ease;\n background-color: transparent;\n width: 100%;\n height: 100%; }\n .ht_dragZone .ht_dragZoneItem.ht_dragZoneItemSelected {\n background-color: rgba(179, 255, 192, 0.5); }\n");var k=function(t){var n,r,i,o,a=t.depth,s=t.depthGap,l=t.isDragging,d=t.node,h=t.onDragEnterAfter,p=t.onDragEnterBefore,u=t.onDragEnterChildren,y=t.onDragLeave;return e.createElement("div",{className:f((n={},n[K]=!0,n[S]=!!l,n)),style:{marginLeft:c(a,s)},onDragLeave:y},e.createElement("div",{className:f((r={},r[x]=!0,r[T]="before"===d.getNodeDropContainer(),r)),onDragEnter:p}),e.createElement("div",{className:f((i={},i[x]=!0,i[T]="children"===d.getNodeDropContainer(),i)),onDragEnter:u}),e.createElement("div",{className:f((o={},o[x]=!0,o[T]="after"===d.getNodeDropContainer(),o)),onDragEnter:h}))},H={hyperTreeView:"ht_hyperTreeView",hyperTreeNodeWrapper:"ht_hyperTreeNodeWrapper",hyperNodeWrapper:"ht_hyperNodeWrapper",child:"ht_child",collapsed:"ht_collapsed",selected:"ht_selected"};y(".ht_hyperTreeView .ht_hyperTreeNodeWrapper {\n position: relative; }\n .ht_hyperTreeView .ht_hyperTreeNodeWrapper .ht_hyperNodeWrapper {\n position: relative;\n opacity: 1; }\n .ht_hyperTreeView .ht_hyperTreeNodeWrapper .ht_child {\n position: relative;\n transition: all 0.2s ease;\n transform-origin: top;\n transform-style: flat;\n max-height: 99999px;\n transform: scaleY(1);\n opacity: 1;\n visibility: visible; }\n .ht_hyperTreeView .ht_hyperTreeNodeWrapper .ht_child.ht_collapsed {\n transition: all 0.2s cubic-bezier(0, 1, 0, 1);\n transform: scaleY(0);\n max-height: 0;\n opacity: 0;\n visibility: hidden;\n overflow: hidden; }\n\n.ht_selected {\n background-color: rgba(255, 0, 0, 0.2); }\n");var P=e.forwardRef((function(t,n){var r,i,a=t.classes,s=void 0===a?p.classes:a,l=t.depth,d=t.depthGap,h=void 0===d?p.depthGap:d,u=t.disableHorizontalLines,y=t.disableLines,g=t.displayedName,v=void 0===g?p.displayedName:g,C=t.draggable,m=t.draggableHandlers,b=t.gapMode,N=void 0===b?p.gapMode:b,_=t.horizontalLineStyles,L=t.isDragging,O=t.node,E=t.renderDragZone,K=t.renderNode,S=t.setOpen,x=t.setSelected,T=t.staticNodeHeight,P=t.verticalLineOffset,A=void 0===P?p.verticalLineOffset:P,B=e.useCallback((function(e){e.stopPropagation(),S&&S(O)}),[O,S]),I=e.useCallback((function(){x&&x(O,!O.isSelected())}),[O,x]),W={};T&&(W.height="".concat(T,"px"));var U="padding"===N?"paddingLeft":"marginLeft",G=e.useCallback((function(e){return m.handleDragEnter(O,e)}),[m,O]);return e.createElement("div",{draggable:C,onDragStart:C?m.handleDragStart:void 0,onDragEnd:C?m.handleDrop(O,O.getNodeDropContainer()):void 0,className:f((r={},r[H.hyperNodeWrapper]=!0,r[H.selected]=O.isSelected(),r[H.dropContainer]=!!O.getNodeDropContainer(),r[s.nodeWrapper||""]=!!s.nodeWrapper,r[s.selectedNodeWrapper||""]=!(!s.selectedNodeWrapper||!O.isSelected()),r)),style:o((i={},i[U]=c(l,h),i),W),ref:n},C&&E?E({depth:l,depthGap:h,isDragging:L,node:O,onDragEnterAfter:G("after"),onDragEnterBefore:G("before"),onDragEnterChildren:G("children"),onDragLeave:m.handleDragLeave(O)}):C&&e.createElement(k,{depth:l,depthGap:h,isDragging:L,node:O,onDragEnterAfter:G("after"),onDragEnterBefore:G("before"),onDragEnterChildren:G("children"),onDragLeave:m.handleDragLeave(O)}),K?K({depth:l,depthGap:h,displayedName:v,node:O,nodeRef:n,offsetProp:U,onSelect:I,onToggle:B}):e.createElement(D,{displayedName:v,node:O,onSelect:I,onToggle:B}),!O.options.root&&!(y||u)&&e.createElement(w,{depth:l,depthGap:h,gapMode:N,horizontalLineStyles:_,verticalLineOffset:A}))})),A=function(t){var n,r=t.classes,i=void 0===r?p.classes:r,s=t.depth,l=t.depthGap,d=void 0===l?p.depthGap:l,c=t.disableLines,h=t.disableTransitions,u=t.disableVerticalLines,y=t.node,g=t.nodeHeight,v=t.verticalLineOffset,C=t.verticalLineStyles,m=t.verticalLineTopOffset,b=a(t,["classes","depth","depthGap","disableLines","disableTransitions","disableVerticalLines","node","nodeHeight","verticalLineOffset","verticalLineStyles","verticalLineTopOffset"]);return y.hasChildren()?!y.isOpened()&&h?null:e.createElement("div",{className:f((n={},n[H.child]=!0,n[H.collapsed]=!y.isOpened(),n[i.children||""]=!!i.children,n[i.collapsedChildren||""]=!(!i.collapsedChildren||y.isOpened()),n))},e.createElement(B,o({classes:i,data:y.getChildren(),depth:s+1,depthGap:d,disableLines:c,disableVerticalLines:u,disableTransitions:h,nodeHeight:g,verticalLineOffset:v,verticalLineStyles:C,verticalLineTopOffset:m},b)),!(c||u)&&e.createElement(O,{count:y.options.currentChilrenCount,depth:s,depthGap:d,nodeHeight:g,disableTransitions:h,verticalLineOffset:v,verticalLineStyles:C,verticalLineTopOffset:m})):null},B=e.forwardRef((function(t,n){var r=t.classes,i=void 0===r?p.classes:r,s=t.data,l=t.depth,d=void 0===l?0:l,c=a(t,["classes","data","depth"]);return e.createElement("div",{className:f(H.hyperTreeView,i.level)},s.map((function(t){return e.createElement("div",{key:t.getReactKey(),className:f(H.hyperTreeNodeWrapper,i.parentChildren)},e.createElement(P,o({classes:i,depth:d,node:t,ref:n},c)),e.createElement(A,o({classes:i,depth:d,node:t},c)))})))})),I=function(t){var n=t.classes,r=void 0===n?p.classes:n,i=t.staticNodeHeight,s=a(t,["classes","staticNodeHeight"]),l=e.useRef(null),d=e.useState(i||0),c=d[0],h=d[1];return e.useLayoutEffect((function(){l.current&&!i&&setTimeout((function(){var e,t=null===(e=l.current)||void 0===e?void 0:e.getBoundingClientRect();t&&h(t.height)}),100)}),[c,i,l.current]),e.createElement("div",{className:r.treeWrapper},e.createElement(B,o({classes:r,depth:0,nodeHeight:c,ref:l,staticNodeHeight:i},s)))};function W(e,t){var n,r;if(0===t.length)return e;for(n=0,r=t.length;n<r;n++)e=(e<<5)-e+t.charCodeAt(n),e|=0;return e<0?-2*e:e}function U(e,t,n,r){var i,o=W(W(W(e,n),(i=t,Object.prototype.toString.call(i))),typeof t);if(null===t)return W(o,"null");if(void 0===t)return W(o,"undefined");if("object"==typeof t||"function"==typeof t){if(-1!==r.indexOf(t))return W(o,"[Circular]"+n);r.push(t);var a=function(e,t,n){return Object.keys(t).sort().reduce((function(e,r){return U(e,t[r],r,n)}),e)}(o,t,r);if(!("valueOf"in t)||"function"!=typeof t.valueOf)return a;try{return W(a,String(t.valueOf()))}catch(e){return W(a,"[valueOf exception]"+(e.stack||e.message))}}return W(o,t.toString())}var G=function(e){return function(e,t){for(;e.length<t;)e="0"+e;return e}(U(0,e,"",[]).toString(16),8)},Z=function(){function e(e,t,n){this.data=e,this.asyncNode=!!e.getChildren,this.asyncDataLoaded=!1,this.id=e[t.idKey],this.options=t,this.children=n}return e.prototype.getData=function(){return this.data},e.prototype.setData=function(e){this.data=e},e.prototype.getChildren=function(){return this.children||[]},e.prototype.setNodeChildren=function(e,t,n){return!this.hasChildren()||n?(this.setChildren(this.setNodesParent(e,this)),this.getChildren()):"first"===t?(this.setChildren(this.setNodesParent(d(d([],e,!0),this.children||[],!0),this)),this.getChildren()):(this.setChildren(this.setNodesParent(d(d([],this.children||[],!0),e,!0),this)),this.getChildren())},e.prototype.removeChild=function(e){this.setChildren(this.getChildren().filter((function(t){return t.id!==e.id})))},e.prototype.setChildren=function(e){this.children=this.setNodesParent(e,this)},e.prototype.hasChildren=function(){return this.children&&0!==this.children.length},e.prototype.getParent=function(){return this.options.parent},e.prototype.setParent=function(e){this.options.parent=e},e.prototype.setNodesParent=function(e,t){return e.map((function(e){return e.setParent(t),e}))},e.prototype.setChildrenCount=function(e){this.options.childrenCount=e},e.prototype.getFirstChild=function(){return this.getChildren()[0]||null},e.prototype.getLastChild=function(){return this.children&&0!==this.children.length?this.children[this.children.length-1]:null},e.prototype.setOpened=function(e){this.options.opened=e||!1},e.prototype.isOpened=function(){return this.options.opened},e.prototype.setLoading=function(e){this.options.loading=e||!1},e.prototype.isLoading=function(){return this.options.loading},e.prototype.setSelected=function(e){this.options.selected=e||!1},e.prototype.isSelected=function(){return this.options.selected},e.prototype.setPath=function(e){this.options.path="".concat(e,"/").concat(this.id)},e.prototype.getPath=function(e){return e?this.options.path.split("/").filter((function(e){return!!e})):this.options.path},e.prototype.setNodeDropContainer=function(e){void 0===e&&(e=!1),this.options.dropContainer=e},e.prototype.getNodeDropContainer=function(){return this.options.dropContainer},e.prototype.getReactKey=function(){return this.options.reactKey},e.prototype.setAsyncDataLoaded=function(e){this.asyncDataLoaded=e},e.prototype.isAsyncDataLoaded=function(){return this.asyncDataLoaded},e}(),j=function(){function e(e,t,n){this.id=e,this.data=t,this.options=n,this.enhancedData=[],this.ids=[],n.enhance&&this.enhance()}return e.prototype.on=function(e){e()},e.prototype.sort=function(e){return e.sort(this.options.sort),e},e.prototype.staticEnhance=function(e,t){var n;return t instanceof Z?n=t:t&&(n=this.getNodeById(t)),this._staticEnhance(e,n)},e.prototype.enhance=function(e){return this.enhancedData=this._enhance(e||this.data,this.options),this.enhancedData},e.prototype.enhanceNodes=function(e){var t=this;void 0===e&&(e=!1),e&&(this.enhancedData=this._staticShallowEnahance(this.enhancedData)),this.traverse((function(e){t._calculateChildrenCount(e)}),!1)},e.prototype.flat=function(e){var t=e?this.data:this.enhancedData;return this.flatData=this._flat(t),this.ids=this.flatData.map((function(e){return e.id})),this.flatData},e.prototype.selectAll=function(){this.traverse((function(e){e.setSelected(!0)}))},e.prototype.unselectAll=function(){this.traverse((function(e){e.setSelected(!1)}))},e.prototype.getOpenedNodes=function(){var e=[];return this.traverse((function(t){t.isOpened()&&e.push(t)})),e},e.prototype.addChildren=function(e,t,n){void 0===n&&(n="first"),this._addChildren(this.data,e,t,n)},e.prototype.addSibling=function(e,t,n){void 0===n&&(n="after"),this._addSibling(this.data,e,t,n)},e.prototype.remove=function(e,t){this._remove(this.data,null,e,t)},e.prototype.traverseRaw=function(e,t){void 0===t&&(t=!0),t?this._traverseDeep(this.data,e):this._traverseBreadth(this.data,e)},e.prototype.traverse=function(e,t){void 0===t&&(t=!0),t?this._traverseDeep(this.enhancedData,e):this._traverseBreadth(this.enhancedData,e)},e.prototype.getNodeById=function(e){return this._getNodeById(e,this.enhancedData)},e.prototype._addChildren=function(e,t,n,r){for(var i=0;i<e.length;i+=1){if(e[i][this.options.idKey]===t)switch(r){case"first":return void(e[i][this.options.childrenKey]=d(d([],n,!0),e[i][this.options.childrenKey]||[],!0));case"last":return void(e[i][this.options.childrenKey]=d(d([],e[i][this.options.childrenKey]||[],!0),n,!0));default:return}else if(e[i][this.options.childrenKey]&&0!==e[i][this.options.childrenKey].length)return void this._addChildren(e[i][this.options.childrenKey],t,n,r);return}},e.prototype._addSibling=function(e,t,n,r){for(var i=this,o=0;o<e.length;o+=1){if(e[o][this.options.childrenKey]&&0!==e[o][this.options.childrenKey].length){var a=e[o][this.options.childrenKey].findIndex((function(e){return e[i.options.idKey]===t}));if(a>-1)return void e[o][this.options.childrenKey].splice("before"===r?a:a+1,0,n);this._addSibling(e[o][this.options.childrenKey],t,n,r)}return}},e.prototype._flat=function(e){var t=this,n=[];return e.forEach((function(e){var r=e.children,i=a(e,["children"]);n.push(i),r&&0!==r.length&&n.push.apply(n,t._flat(r))})),n},e.prototype._enhance=function(e,t,n){var r=this,i=[];return this.sort(e).forEach((function(e,o){var a,s=e[r.options.childrenKey]&&0!==e[r.options.childrenKey].length?e[r.options.childrenKey]:[],l=!1;"boolean"==typeof t.defaultOpened&&(l=t.defaultOpened),Array.isArray(t.defaultOpened)&&(l=t.defaultOpened.includes(e[t.idKey]));var d=new Z(e,{opened:l,selected:!1,root:!n,leaf:!s,hasChildren:0!==s.length,async:!!e.getChildren,parent:n,childrenCount:0,currentChilrenCount:0,loading:!1,idKey:r.options.idKey,childrenKey:r.options.childrenKey,path:"",dropContainer:!1,reactKey:""});d.setPath(n?n.options.path:""),d.options.reactKey="".concat(d[r.options.idKey],"-").concat(d.getPath(),"-").concat(o);var c=[];if(0!==s.length&&(c=r._enhance(r.sort(s),t,d),t.filter&&0!==c.length&&0!==(c=c.filter((function(e){return!!e.getChildren().some((function(e){return e.isOpened()}))||t.filter&&t.filter(e)}))).length))for(var h=0;h<c.length;h+=1)c[h].setOpened(!0),c[h].options.parent&&(null===(a=c[h].options.parent)||void 0===a||a.setOpened(!0));d.options.leaf&&d.setOpened(!0),d.setChildren(c),r._calculateChildrenCount(d),(!t.filter||!d.options.leaf||t.filter&&t.filter(d))&&i.push(d)})),i},e.prototype._staticEnhance=function(e,t){var n=this,r=[];return e.forEach((function(e,i){var o=e[n.options.childrenKey]||[],a=new Z(e,{opened:!1,selected:!1,root:!t,leaf:0===o.length,hasChildren:0!==o.length,async:!!e.getChildren,parent:t,childrenCount:0,currentChilrenCount:0,loading:!1,idKey:n.options.idKey,childrenKey:n.options.childrenKey,path:"",dropContainer:!1,reactKey:""});a.setPath(t?t.options.path:""),a.options.reactKey="".concat(a[n.options.idKey],"-").concat(a.getPath(),"-").concat(i);var s=[];0!==o.length&&(s=n._staticEnhance(o,a)),a.setChildren(s),n._calculateChildrenCount(a),r.push(a)})),r},e.prototype._staticShallowEnahance=function(e,t){var n=this,r=[];return e.forEach((function(e){var i=e[n.options.childrenKey]||[];e.options.parent=t,e.setPath(t?t.options.path:"");var o=[];0!==i.length&&(o=n._staticShallowEnahance(i,e)),e.setChildren(o),n._calculateChildrenCount(e),r.push(e)})),r},e.prototype._calculateChildrenCount=function(e){var t=e.getChildren().reduce((function(e,t){return t.isOpened()?e+=t.options.childrenCount:e}),0);e.isOpened()?e.options.childrenCount=e.getChildren().length+t:e.options.childrenCount=e.getChildren().length;var n=e.getLastChild();if(n&&n.isOpened()){var r=e.options.childrenCount;e.options.currentChilrenCount=r-n.options.childrenCount}else e.options.currentChilrenCount=e.options.childrenCount},e.prototype._remove=function(e,t,n,r){for(var i=0;i<e.length;i+=1){if(e[i][this.options.idKey]===n){var o=e[i];return e.splice(i,1),void(t&&o[this.options.childrenKey]&&0!==o[this.options.childrenKey].length&&r&&(t[this.options.childrenKey]="start"===r?d(d([],o[this.options.childrenKey],!0),t[this.options.childrenKey]||[],!0):d(d([],t[this.options.childrenKey]||[],!0),o[this.options.childrenKey],!0)))}e[i][this.options.childrenKey]&&0!==e[i][this.options.childrenKey].length&&this._remove(e[i][this.options.childrenKey],e[i],n,r)}},e.prototype._getNodeById=function(e,t){for(var n=0;n<t.length;n+=1){if("".concat(t[n][this.options.idKey]).toLowerCase()==="".concat(e).toLowerCase())return t[n];if(t[n][this.options.childrenKey]&&0!==t[n][this.options.childrenKey].length){var r=this._getNodeById(e,t[n][this.options.childrenKey]);if(r)return r}}return null},e.prototype._traverseDeep=function(e,t,n){var r=this;void 0===n&&(n=0),e.forEach((function(e,i){t(e,i,n),e.children&&0!==e.children.length&&r._traverseDeep(e.children,t,n+1)}))},e.prototype._traverseBreadth=function(e,t,n){var r=this;void 0===n&&(n=0),e.forEach((function(e,i){e.children&&0!==e.children.length&&r._traverseBreadth(e.children,t,n+1),t(e,i,n)}))},e}(),V=new(function(){function e(){this.trees={}}return e.prototype.getIds=function(){return Object.keys(this.trees)},e.prototype.safeUpdate=function(e,t){return this.trees[e]?(this.trees[e].instance=t,this.trees[e].handlers={}):this.trees[e]={instance:t,handlers:{}},this},e.prototype.remove=function(e){var t=this.trees,n=e;t[n];var r=a(t,["symbol"==typeof n?n:n+""]);return this.trees=r,this},e.prototype.safeUpdateHandler=function(e,t,n){var r;return this.trees[e]&&(this.trees[e].handlers=o(o({},this.trees[e].handlers),((r={})[t]=n,r))),this},e.prototype.removeHandler=function(e,t){if(this.trees[e]){var n=this.trees[e].handlers,r=t;n[r];var i=a(n,["symbol"==typeof r?r:r+""]);this.trees[e].handlers=i}return this},e.prototype.updateLoadedData=function(e,t){this.trees[e]&&(this.trees[e].loadedData=t)},e}()),z={sort:function(){return 0}},M=function(e){var o,a=e.id,c=e.data,u=e.filter,f=e.sort,y=e.defaultOpened,g=e.multipleSelect,v=e.refreshAsyncNodes,C=e.idKey,m=void 0===C?p.idKey:C,b=e.childrenKey,N=void 0===b?p.childrenKey:b,D=(o=t(Object.create(null))[1],i((function(e){e&&e(),o(Object.create(null))}),[o])),_=t(null),L=_[0],O=_[1],E=t(!1),w=E[0],K=E[1],S=t(!1),x=S[0],T=S[1],k=t([]),H=k[0],P=k[1],A=t(!1),B=A[0],I=A[1];n((function(){var e=Array.isArray(c)?c:[c];G(e)!==G(H)&&P(e)}),[c,H]);var W=r((function(){return new j(a,H,{idKey:m,childrenKey:N,defaultOpened:!1,filter:u,enhance:!0,sort:f||z.sort})}),[a,H,m,N,u,f]),U=i((function(e){return e instanceof Z?e:W.getNodeById(e)||null}),[W]),M=i((function(e,t){if(e instanceof Z)e.setLoading(t),D();else{var n=W.getNodeById(e);n&&(n.setLoading(t),D())}}),[D,W]),R=i((function(e,t){!g&&t&&W.unselectAll();var n=U(e);n&&(n.setSelected(t),D())}),[D,W,g,U]),F=i((function(e,t){if(e instanceof Z)e.setNodeDropContainer(t),D();else{var n=W.getNodeById(e);n&&(n.setNodeDropContainer(t),D())}}),[D,W]),Y=i((function(e,t,n,r){if(e instanceof Z)e.setNodeChildren(t,n,r),W.enhanceNodes(),D();else{var i=W.getNodeById(e);i&&(i.setNodeChildren(t,n,r),W.enhanceNodes(),D())}}),[W,D]),q=i((function(e,t,n){var r,i=U(e);if(i&&i.options.parent){var o,a=d([],i.options.parent.getChildren(),!0);if(-1!==(o=a.findIndex((function(e){return i&&e.id===i.id})))){var s="before"===n?o:o+1;a.splice.apply(a,d([s,0],t,!1)),i.options.parent.setNodeChildren(a,"first",!0),W.enhanceNodes(!0),D()}}if(i&&i.options.root&&-1!==(o=W.enhancedData.findIndex((function(e){return i&&i.id===e.id})))){s="before"===n?o:o+1;var l=t.map((function(e){return e.options.parent=void 0,e.options.root=!0,e.options.leaf=!1,e}));(r=W.enhancedData).splice.apply(r,d([s,0],l,!1)),W.enhanceNodes(!0),D()}}),[W,D,U]),J=i((function(e,t,n,r){Y(e,W.staticEnhance(t,e),n,r)}),[Y,W]),Q=i((function(e,t){var n=U(e);n&&(n.setData(t),D((function(){W.enhanceNodes()})))}),[D,W,U]),X=i((function(e){var t=U(e);if(t)return t.getData()}),[U]),$=i((function(e,t){return void 0===t&&(t=!0),s(void 0,void 0,void 0,(function(){var n,r,i;return l(this,(function(o){switch(o.label){case 0:if(!(n=U(e)))return[2];if(!n.data.getChildren||!h(n.data.getChildren)||n.isOpened()||n.isAsyncDataLoaded())return[3,5];M(n,!0),o.label=1;case 1:return o.trys.push([1,3,,4]),[4,n.data.getChildren({node:e})];case 2:return r=o.sent(),M(e,!1),n.setOpened(!0),v||n.setAsyncDataLoaded(!0),J(e,r,"last",!0),[3,4];case 3:return i=o.sent(),console.error("react-hyper-tree: Error on getChildren",i),[3,4];case 4:return[3,6];case 5:n.setOpened(!t||!n.isOpened()),W.enhanceNodes(),o.label=6;case 6:return D((function(){W.enhanceNodes()})),[2]}}))}))}),[U,D,M,v,J,W]),ee=i((function(e){return s(void 0,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return[4,e.split("/").reduce((function(e,t){return s(void 0,void 0,void 0,(function(){return l(this,(function(n){switch(n.label){case 0:return[4,e];case 1:return n.sent(),[4,$(t)];case 2:return n.sent(),[2]}}))}))}),Promise.resolve())];case 1:return t.sent(),[2]}}))}))}),[$]),te=i((function(e,t,n){return void 0===t&&(t=!1),void 0===n&&(n=!1),s(void 0,void 0,void 0,(function(){var r,i,o,a;return l(this,(function(s){switch(s.label){case 0:r=e.split("/"),i=0,s.label=1;case 1:return i<r.length?(o=r[i])&&(a=U(o))?a.isOpened()?[3,3]:[4,$(a)]:[3,4]:[3,5];case 2:s.sent(),s.label=3;case 3:(t||i===r.length-1)&&R(a,!n||!a.isSelected()),s.label=4;case 4:return i+=1,[3,1];case 5:return W.enhanceNodes(),D(),[2]}}))}))}),[D,U,$,R,W]),ne=i((function(){g&&(W.selectAll(),W.enhanceNodes(),D())}),[W,D,g]),re=i((function(){W.unselectAll(),W.enhanceNodes(),D()}),[W,D]);n((function(){var e=function(){return s(void 0,void 0,void 0,(function(){var e,t,n,r;return l(this,(function(i){switch(i.label){case 0:if(I(!0),e=!1,0===(t=W.flat()).length)return[2];n=0,i.label=1;case 1:return n<t.length?(r=t[n],"boolean"!=typeof y?[3,3]:[4,$(r.id,!1)]):[3,7];case 2:return i.sent(),[3,5];case 3:return(null==y?void 0:y.includes(r.id))?[4,$(r.id,!1)]:[3,5];case 4:i.sent(),i.label=5;case 5:r.data.getChildren&&(e=!0),i.label=6;case 6:return n+=1,[3,1];case 7:if(!e)return[3,13];t=W.flat(),n=0,i.label=8;case 8:return n<t.length?(r=t[n],"boolean"!=typeof y?[3,10]:[4,$(r.id,!1)]):[3,13];case 9:return i.sent(),[3,12];case 10:return(null==y?void 0:y.includes(r.id))?[4,$(r.id,!1)]:[3,12];case 11:i.sent(),i.label=12;case 12:return n+=1,[3,8];case 13:return[2]}}))}))};if(!B&&void 0!==y&&0!==W.flat().length){var t="boolean"==typeof y;t&&y&&e(),t||0===y.length||e()}}),[y,B,$,W]);var ie=i((function(e){e.stopPropagation(),K(!0)}),[]),oe=i((function(e,t){return function(n){n.stopPropagation(),n.preventDefault(),F(e,t),O(e.id),T(t)}}),[F]),ae=i((function(e){return function(t){t.stopPropagation(),t.preventDefault(),e.id!==L&&F(e,!1)}}),[F,L]),se=i((function(e){return function(t){if(t.stopPropagation(),t.preventDefault(),K(!1),L&&F(L,!1),L&&L!==e.id){var n=W.getNodeById(L);e&&(e.options.parent?e.options.parent.removeChild(e):W.enhancedData=W.enhancedData.filter((function(t){return t.id!==e.id})),"children"===x?(e.options.parent=n,e.options.root=!1,e.options.leaf=!0,n.setNodeChildren([e],"last"),W.enhanceNodes(!0),D()):"after"!==x&&"before"!==x||q(n,[e],x),O(null))}}}),[L,D,W,F,q,x]);V.safeUpdate(a,W).safeUpdateHandler(a,"rerender",D).safeUpdateHandler(a,"setOpen",$).safeUpdateHandler(a,"setOpenByPath",ee).safeUpdateHandler(a,"setLoading",M).safeUpdateHandler(a,"setSelected",R).safeUpdateHandler(a,"setSelectedByPath",te).safeUpdateHandler(a,"setRawChildren",J).safeUpdateHandler(a,"setChildren",Y).safeUpdateHandler(a,"setSiblings",q).safeUpdateHandler(a,"getNode",U).safeUpdateHandler(a,"setNodeData",Q).safeUpdateHandler(a,"getNodeData",X).safeUpdateHandler(a,"selectAll",ne).safeUpdateHandler(a,"unselectAll",re);var le=r((function(){return{setChildren:Y,setLoading:M,setOpen:$,setRawChildren:J,setSelected:R,setSiblings:q,setNodeData:Q,getNode:U,getNodeData:X,selectAll:ne,unselectAll:re,draggableHandlers:{handleDragStart:ie,handleDragEnter:oe,handleDragLeave:ae,handleDrop:se}}}),[oe,ae,ie,se,Y,M,$,J,R,q,Q,U,X,ne,re]);return{instance:W,handlers:le,required:{isDragging:w,data:W.enhancedData}}};export{C as Arrow,D as DefaultNode,k as DragZone,w as HorizontalLine,b as Loader,I as Tree,O as VerticalLine,I as default,V as treeHandlers,M as useTreeState}; //# sourceMappingURL=index.es.js.map