Files
KIN/js/tree.min.js
2025-09-29 17:55:08 +09:00

2 lines
8.0 KiB
JavaScript

function TreeView(a,b,c){function d(g){var h=document.createElement("li"),j=document.createElement("span");if(j.className="tj_description",j.tj_node=g,g.isEnabled()||(h.setAttribute("disabled",""),g.setExpanded(!1),g.setSelected(!1)),g.isSelected()&&j.classList.add("selected"),j.addEventListener("click",function(n){for(var o=n.target;"undefined"==typeof o.tj_node||o.classList.contains("tj_container");)o=o.parentElement;var p=o.tj_node;if("undefined"!=typeof p&&p.isEnabled())if(!1==n.ctrlKey&&(p.isLeaf()?p.open():(p.toggleExpanded(),f.reload()),p.on("click")(n,p)),!0==n.ctrlKey)p.toggleSelected(),f.reload();else{var q=p.getRoot();q instanceof TreeNode&&TreeUtil.getSelectedNodesForNode(q).forEach(function(r){r.setSelected(!1)}),p.setSelected(!0),f.reload()}}),j.addEventListener("contextmenu",function(n){for(var o=n.target;"undefined"==typeof o.tj_node||o.classList.contains("tj_container");)o=o.parentElement;var p=o.tj_node;"undefined"==typeof p||("undefined"==typeof p.getListener("contextmenu")?"function"==typeof TreeConfig.context_menu&&(TreeConfig.context_menu(n,p),n.preventDefault()):(p.on("contextmenu")(n,p),n.preventDefault()))}),g.isLeaf()){var k="",l=TreeUtil.getProperty(g.getOptions(),"icon","");k+=""==l?""==(l=TreeUtil.getProperty(c,"leaf_icon",""))?"<span class=\"tj_icon\">"+TreeConfig.leaf_icon+"</span>":"<span class=\"tj_icon\">"+l+"</span>":"<span class=\"tj_icon\">"+l+"</span>",j.innerHTML=k+g.toString()+"</span>",j.classList.add("tj_leaf"),h.appendChild(j)}else{var k="";k+=g.isExpanded()?"<span class=\"tj_mod_icon\">"+TreeConfig.open_icon+"</span>":"<span class=\"tj_mod_icon\">"+TreeConfig.close_icon+"</span>";var l=TreeUtil.getProperty(g.getOptions(),"icon","");if(k+=""==l?""==(l=TreeUtil.getProperty(c,"parent_icon",""))?"<span class=\"tj_icon\">"+TreeConfig.parent_icon+"</span>":"<span class=\"tj_icon\">"+l+"</span>":"<span class=\"tj_icon\">"+l+"</span>",j.innerHTML=k+g.toString()+"</span>",h.appendChild(j),g.isExpanded()){var m=document.createElement("ul");g.getChildren().forEach(function(n){m.appendChild(d(n))}),h.appendChild(m)}}return h}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(g){a instanceof TreeNode&&(a=g)},this.getRoot=function(){return a},this.expandAllNodes=function(){a.setExpanded(!0),a.getChildren().forEach(function(g){TreeUtil.expandNode(g)})},this.expandPath=function(g){if(!(g instanceof TreePath))throw new Error("Parameter 1 must be of type TreePath");g.getPath().forEach(function(h){h.setExpanded(!0)})},this.collapseAllNodes=function(){a.setExpanded(!1),a.getChildren().forEach(function(g){TreeUtil.collapseNode(g)})},this.setContainer=function(g){if(TreeUtil.isDOM(g))b=g;else if(g=document.querySelector(g),g instanceof Array&&(g=g[0]),!TreeUtil.isDOM(g))throw new Error("Parameter 1 must be either DOM-Object or CSS-QuerySelector (#, .)")},this.getContainer=function(){return b},this.setOptions=function(g){"object"==typeof g&&(c=g)},this.changeOption=function(g,h){c[g]=h},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 g=document.createElement("ul");g.appendChild(d(a)),b.innerHTML="",b.appendChild(g)},"undefined"!=typeof b&&this.reload()}function TreeNode(a,b){var c=[],d=this,f=[],g=!0,h=!0,j=!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?(g=TreeUtil.getProperty(b,"expanded",!0),h=TreeUtil.getProperty(b,"enabled",!0),j=TreeUtil.getProperty(b,"selected",!1)):b={},this.addChild=function(k){if(!TreeUtil.getProperty(b,"allowsChildren",!0))return void console.warn("Option allowsChildren is set to false, no child added");if(k instanceof TreeNode)c.push(k),Object.defineProperty(k,"parent",{value:this,writable:!1,enumerable:!0,configurable:!0});else throw new Error("Parameter 1 must be of type TreeNode")},this.removeChildPos=function(k){"undefined"!=typeof c[k]&&"undefined"!=typeof c[k]&&c.splice(k,1)},this.removeChild=function(k){if(!(k instanceof TreeNode))throw new Error("Parameter 1 must be of type TreeNode");this.removeChildPos(this.getIndexOfChild(k))},this.getChildren=function(){return c},this.getChildCount=function(){return c.length},this.getIndexOfChild=function(k){for(var l=0;l<c.length;l++)if(c[l].equals(k))return l;return-1},this.getRoot=function(){for(var k=this;"undefined"!=typeof k.parent;)k=k.parent;return k},this.setUserObject=function(k){if("string"!=typeof k||"function"!=typeof k.toString)throw new Error("Parameter 1 must be of type String or Object, where it must have the function toString()");else a=k},this.getUserObject=function(){return a},this.setOptions=function(k){"object"==typeof k&&(b=k)},this.changeOption=function(k,l){b[k]=l},this.getOptions=function(){return b},this.isLeaf=function(){return 0==c.length},this.setExpanded=function(k){if(!this.isLeaf()&&"boolean"==typeof k){if(g==k)return;g=k,k?this.on("expand")(this):this.on("collapse")(this),this.on("toggle_expanded")(this)}},this.toggleExpanded=function(){g?this.setExpanded(!1):this.setExpanded(!0)},this.isExpanded=function(){return!!this.isLeaf()||g},this.setEnabled=function(k){if("boolean"==typeof k){if(h==k)return;h=k,k?this.on("enable")(this):this.on("disable")(this),this.on("toggle_enabled")(this)}},this.toggleEnabled=function(){h?this.setEnabled(!1):this.setEnabled(!0)},this.isEnabled=function(){return h},this.setSelected=function(k){"boolean"!=typeof k||j==k||(j=k,k?this.on("select")(this):this.on("deselect")(this),this.on("toggle_selected")(this))},this.toggleSelected=function(){j?this.setSelected(!1):this.setSelected(!0)},this.isSelected=function(){return j},this.open=function(){this.isLeaf()||this.on("open")(this)},this.on=function(k,l){if("undefined"==typeof l)return"function"==typeof f[k]?f[k]:function(){};if("function"!=typeof l)throw new Error("Argument 2 must be of type function");f[k]=l},this.getListener=function(k){return f[k]},this.equals=function(k){return k instanceof TreeNode&&k.getUserObject()==a},this.toString=function(){return"string"==typeof a?a:a.toString()}}function TreePath(a,b){var c=[];this.setPath=function(d,f){for(c=[];"undefined"!=typeof f&&!f.equals(d);)c.push(f),f=f.parent;if(f.equals(d))c.push(d);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>&#128441;</span>",default_parent_icon:"<span>&#128449;</span>",default_open_icon:"<span>&#9698;</span>",default_close_icon:"<span>&#9654;</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(b){TreeUtil.expandNode(b)})},collapseNode:function(a){a.setExpanded(!1),a.isLeaf()||a.getChildren().forEach(function(b){TreeUtil.collapseNode(b)})},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(c){c.isSelected()&&-1==b.indexOf(c)&&b.push(c),c.isLeaf()||TreeUtil.getSelectedNodesForNode(c).forEach(function(d){-1==b.indexOf(d)&&b.push(d)})}),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};