forked from m-thalmann/treejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.min.js
1 lines (1 loc) · 8.16 KB
/
tree.min.js
1
function TreeView(a,b,c){function d(a){var b=document.createElement("li"),e=document.createElement("span");if(e.className="tj_description",e.tj_node=a,a.isEnabled()||(b.setAttribute("disabled",""),a.setExpanded(!1),a.setSelected(!1)),a.isSelected()&&e.classList.add("selected"),e.addEventListener("click",function(a){for(var b=a.target;"undefined"==typeof b.tj_node||b.classList.contains("tj_container");)b=b.parentElement;var c=b.tj_node;if("undefined"!=typeof c&&c.isEnabled())if(!1==a.ctrlKey&&(c.isLeaf()?c.open():(c.toggleExpanded(),f.reload()),c.on("click")(a,c)),!0==a.ctrlKey)c.toggleSelected(),f.reload();else{var d=c.getRoot();d instanceof TreeNode&&TreeUtil.getSelectedNodesForNode(d).forEach(function(a){a.setSelected(!1)}),c.setSelected(!0),f.reload()}}),e.addEventListener("contextmenu",function(a){for(var b=a.target;"undefined"==typeof b.tj_node||b.classList.contains("tj_container");)b=b.parentElement;var c=b.tj_node;"undefined"==typeof c||("undefined"==typeof c.getListener("contextmenu")?"function"==typeof TreeConfig.context_menu&&(TreeConfig.context_menu(a,c),a.preventDefault()):(c.on("contextmenu")(a,c),a.preventDefault()))}),a.isLeaf()&&!TreeUtil.getProperty(a.getOptions(),"forceParent",!1)){var g="",h=TreeUtil.getProperty(a.getOptions(),"icon","");g+=""==h?""==(h=TreeUtil.getProperty(c,"leaf_icon",""))?"<span class=\"tj_icon\">"+TreeConfig.leaf_icon+"</span>":"<span class=\"tj_icon\">"+h+"</span>":"<span class=\"tj_icon\">"+h+"</span>",e.innerHTML=g+a.toString()+"</span>",e.classList.add("tj_leaf"),b.appendChild(e)}else{var g="";g+=a.isExpanded()?"<span class=\"tj_mod_icon\">"+TreeConfig.open_icon+"</span>":"<span class=\"tj_mod_icon\">"+TreeConfig.close_icon+"</span>";var h=TreeUtil.getProperty(a.getOptions(),"icon","");if(g+=""==h?""==(h=TreeUtil.getProperty(c,"parent_icon",""))?"<span class=\"tj_icon\">"+TreeConfig.parent_icon+"</span>":"<span class=\"tj_icon\">"+h+"</span>":"<span class=\"tj_icon\">"+h+"</span>",e.innerHTML=g+a.toString()+"</span>",b.appendChild(e),a.isExpanded()){var i=document.createElement("ul");a.getChildren().forEach(function(a){i.appendChild(d(a))}),b.appendChild(i)}}return b}var f=this;if("undefined"==typeof a)throw new Error("Parameter 1 must be set (root)");if(!(a instanceof TreeNode))throw new Error("Parameter 1 must be of type TreeNode");if(!b)b=null;else if(!TreeUtil.isDOM(b)&&(b=document.querySelector(b),b instanceof Array&&(b=b[0]),!TreeUtil.isDOM(b)))throw new Error("Parameter 2 must be either DOM-Object or CSS-QuerySelector (#, .)");c&&"object"==typeof c||(c={}),this.setRoot=function(b){a instanceof TreeNode&&(a=b)},this.getRoot=function(){return a},this.expandAllNodes=function(){a.setExpanded(!0),a.getChildren().forEach(function(a){TreeUtil.expandNode(a)})},this.expandPath=function(a){if(!(a instanceof TreePath))throw new Error("Parameter 1 must be of type TreePath");a.getPath().forEach(function(a){a.setExpanded(!0)})},this.collapseAllNodes=function(){a.setExpanded(!1),a.getChildren().forEach(function(a){TreeUtil.collapseNode(a)})},this.setContainer=function(a){if(TreeUtil.isDOM(a))b=a;else if(a=document.querySelector(a),a instanceof Array&&(a=a[0]),!TreeUtil.isDOM(a))throw new Error("Parameter 1 must be either DOM-Object or CSS-QuerySelector (#, .)")},this.getContainer=function(){return b},this.setOptions=function(a){"object"==typeof a&&(c=a)},this.changeOption=function(a,b){c[a]=b},this.getOptions=function(){return c},this.getSelectedNodes=function(){return TreeUtil.getSelectedNodesForNode(a)},this.reload=function(){if(null==b)return void console.warn("No container specified");b.classList.add("tj_container");var e=document.createElement("ul");TreeUtil.getProperty(c,"show_root",!0)?e.appendChild(d(a)):a.getChildren().forEach(function(a){e.appendChild(d(a))}),b.innerHTML="",b.appendChild(e)},"undefined"!=typeof b&&this.reload()}function TreeNode(a,b){var c=[],d=this,e=[],f=!0,g=!0,h=!1;if(!a)a="";else if("string"!=typeof a&&"function"!=typeof a.toString)throw new Error("Parameter 1 must be of type String or Object, where it must have the function toString()");b&&"object"==typeof b?(f=TreeUtil.getProperty(b,"expanded",!0),g=TreeUtil.getProperty(b,"enabled",!0),h=TreeUtil.getProperty(b,"selected",!1)):b={},this.addChild=function(a){if(!TreeUtil.getProperty(b,"allowsChildren",!0))return void console.warn("Option allowsChildren is set to false, no child added");if(a instanceof TreeNode)c.push(a),Object.defineProperty(a,"parent",{value:this,writable:!1,enumerable:!0,configurable:!0});else throw new Error("Parameter 1 must be of type TreeNode")},this.removeChildPos=function(a){"undefined"!=typeof c[a]&&"undefined"!=typeof c[a]&&c.splice(a,1)},this.removeChild=function(a){if(!(a instanceof TreeNode))throw new Error("Parameter 1 must be of type TreeNode");this.removeChildPos(this.getIndexOfChild(a))},this.getChildren=function(){return c},this.getChildCount=function(){return c.length},this.getIndexOfChild=function(a){for(var b=0;b<c.length;b++)if(c[b].equals(a))return b;return-1},this.getRoot=function(){for(var a=this;"undefined"!=typeof a.parent;)a=a.parent;return a},this.setUserObject=function(b){if("string"!=typeof b||"function"!=typeof b.toString)throw new Error("Parameter 1 must be of type String or Object, where it must have the function toString()");else a=b},this.getUserObject=function(){return a},this.setOptions=function(a){"object"==typeof a&&(b=a)},this.changeOption=function(a,c){b[a]=c},this.getOptions=function(){return b},this.isLeaf=function(){return 0==c.length},this.setExpanded=function(a){if(!this.isLeaf()&&"boolean"==typeof a){if(f==a)return;f=a,a?this.on("expand")(this):this.on("collapse")(this),this.on("toggle_expanded")(this)}},this.toggleExpanded=function(){f?this.setExpanded(!1):this.setExpanded(!0)},this.isExpanded=function(){return!!this.isLeaf()||f},this.setEnabled=function(a){if("boolean"==typeof a){if(g==a)return;g=a,a?this.on("enable")(this):this.on("disable")(this),this.on("toggle_enabled")(this)}},this.toggleEnabled=function(){g?this.setEnabled(!1):this.setEnabled(!0)},this.isEnabled=function(){return g},this.setSelected=function(a){"boolean"!=typeof a||h==a||(h=a,a?this.on("select")(this):this.on("deselect")(this),this.on("toggle_selected")(this))},this.toggleSelected=function(){h?this.setSelected(!1):this.setSelected(!0)},this.isSelected=function(){return h},this.open=function(){this.isLeaf()||this.on("open")(this)},this.on=function(a,b){if("undefined"==typeof b)return"function"==typeof e[a]?e[a]:function(){};if("function"!=typeof b)throw new Error("Argument 2 must be of type function");e[a]=b},this.getListener=function(a){return e[a]},this.equals=function(b){return!!(b instanceof TreeNode&&b.getUserObject()==a)},this.toString=function(){return"string"==typeof a?a:a.toString()}}function TreePath(a,b){var c=[];this.setPath=function(a,b){for(c=[];"undefined"!=typeof b&&!b.equals(a);)c.push(b),b=b.parent;if(b.equals(a))c.push(a);else throw c=[],new Error("Node is not contained in the tree of root");return c=c.reverse(),c},this.getPath=function(){return c},this.toString=function(){return c.join(" - ")},a instanceof TreeNode&&b instanceof TreeNode&&this.setPath(a,b)}const TreeUtil={default_leaf_icon:"<span>🖹</span>",default_parent_icon:"<span>🗁</span>",default_open_icon:"<span>◢</span>",default_close_icon:"<span>▶</span>",isDOM:function(a){try{return a instanceof HTMLElement}catch(b){return"object"==typeof a&&1===a.nodeType&&"object"==typeof a.style&&"object"==typeof a.ownerDocument}},getProperty:function(a,b,c){return"undefined"==typeof a[b]?c:a[b]},expandNode:function(a){a.setExpanded(!0),a.isLeaf()||a.getChildren().forEach(function(a){TreeUtil.expandNode(a)})},collapseNode:function(a){a.setExpanded(!1),a.isLeaf()||a.getChildren().forEach(function(a){TreeUtil.collapseNode(a)})},getSelectedNodesForNode:function(a){if(!(a instanceof TreeNode))throw new Error("Parameter 1 must be of type TreeNode");var b=[];return a.isSelected()&&b.push(a),a.getChildren().forEach(function(a){a.isSelected()&&-1==b.indexOf(a)&&b.push(a),a.isLeaf()||TreeUtil.getSelectedNodesForNode(a).forEach(function(a){-1==b.indexOf(a)&&b.push(a)})}),b}};var TreeConfig={leaf_icon:TreeUtil.default_leaf_icon,parent_icon:TreeUtil.default_parent_icon,open_icon:TreeUtil.default_open_icon,close_icon:TreeUtil.default_close_icon,context_menu:void 0};