path_tree.min.js 4.1 KB

1
  1. const createNewPathTree=function(t){const e=JSON.parse(JSON.stringify(t)),n=function(){this.datas=[],this.items={},this.nodes=[]},s=n.prototype;return s.sortTreeNode=function(){const t=this,e=function(n){for(let s=0;s<n.length;s++)t.nodes.push(n[s]),e(t.getChildren(n[s]))};t.nodes=[],e(this.getChildren(null))},s.loadDatas=function(t){this.items={},this.nodes=[];for(const n of t){const t="id_"+n[e.id];this.items[t]=JSON.parse(JSON.stringify(n)),this.datas.push(this.items[t])}this.sortTreeNode();for(const t of this.nodes){const e=this.getChildren(t);t.expanded=e.length>0,t.visible=!0}},s._updateData=function(t){const n=[];for(const s of t){let t=this.getItems(s[e.id]);if(t){for(const e in t)void 0!==s[e]&&(t[e]=s[e]);n.push(t)}}for(const t of n){const e=this.getChildren(t);t.expanded=e.length>0&&e[0].visible}return this.sortTreeNode(),n},s._loadData=function(t){const n=[];for(const s of t){let t=this.getItems(s[e.id]);if(t){for(const e in t)void 0!==s[e]&&(t[e]=s[e]);n.push(t)}else{const t="id_"+s[e.id],i=JSON.parse(JSON.stringify(s));this.items[t]=i,this.datas.push(i),i.expanded=!1,i.visible=!0,n.push(i)}}this.sortTreeNode();for(const t of n){const e=this.getChildren(t);!t.expanded&&e.length>0&&(t.expanded=!0,this._refreshChildrenVisible(t))}return n},s._freeData=function(t){const n=function(t,e){const n=t.indexOf(e);t.splice(n,1)};for(const s of t){const t=this.getItems(s[e.id]);t&&(delete this.items["id_"+t[e.id]],n(this.datas,t),n(this.nodes,t))}},s._loadExpandData=function(t){const n=[],s=[],i=[];for(const i of t){let t=this.getItems(i[e.id]);if(t)s.push(t);else{const t="id_"+i[e.id],s=JSON.parse(JSON.stringify(i));this.items[t]=s,this.datas.push(s),s.expanded=!1,s.visible=!0,n.push(s)}}this.sortTreeNode();for(const t of n){const e=this.getChildren(t);!t.expanded&&e.length>0&&(t.expaned=!0,this._refreshChildrenVisible(t))}for(const t of s){const e=this.getChildren(t);!t.expanded&&e.length>0&&(t.expanded=e.length>0,this._refreshChildrenVisible(t),i.push(t))}return[n,i]},s.getItems=function(t){return this.items["id_"+t]},s.getParent=function(t){return this.getItems(t[e.pid])},s.getChildren=function(t){const n=t?t[e.id]:e.rootId,s=this.datas.filter(function(t){return t[e.pid]===n});return s.sort(function(t,n){return t[e.order]-n[e.order]}),s},s.getPosterity=function(t){const n=new RegExp("^"+t[e.full_path]+".");return this.datas.filter(function(t){return n.test(t[e.full_path])})},s.isLastSibling=function(t){const n=this.getChildren(this.getParent(t));return t[e.order]===n[n.length-1][e.order]},s._refreshChildrenVisible=function(t){const e=this.getChildren(t);for(const n of e)n.visible=t.expanded&&t.visible,this._refreshChildrenVisible(n)},s.setExpanded=function(t,e){t.expanded=e,this._refreshChildrenVisible(t)},s.getNodeKeyData=function(t){const n={};for(const s of e.keys)n[s]=t[s];return n},s.getNodeKey=function(t){return t[e.id]},s.getNodeLevel=function(t){return t[e.level]},s.getNodeIsLeaf=function(t){return t[e.is_leaf]},s.getNodeOrder=function(t){return t[e.order]},s.loadChildren=function(t,n){const s=this,i=e.preUrl?e.preUrl+"/get-children":"get-children";postData(i,this.getNodeKeyData(t),function(t){s._loadData(t),n()})},s.baseOperation=function(t,n,s,i){const o=this,r={id:n[e.id],postType:s};postData(t,r,function(t){const e={};t.update&&(e.update=o._updateData(t.update)),t.create&&(e.create=o._loadData(t.create)),t.delete&&(e.delete=o._freeData(t.delete)),i(e)})},s.update=function(t,n,s){const i=this;postData(t,n,function(t){const e=i._updateData(t);s(e)},function(){if(n instanceof Array){const t=[];for(const s of n)t.push(i.getItems(s[e.id]));s(t)}else s([i.getItems(n[e.id])])})},s.pasteBlock=function(t,n,s,i){const o=this,r={id:n[e.id],block:s};postData(t,r,function(t){const e={};t.update&&(e.update=o._updateData(t.update)),t.create&&(e.create=o._loadData(t.create)),t.delete&&(e.delete=o._freeData(t.delete)),i(e)})},s.postData=function(t,n,s,i){const o=this;s.id=n[e.id],postData(t,s,function(t){const e={};if(t.update&&(e.update=o._updateData(t.update)),t.create&&(e.create=o._loadData(t.create)),t.delete&&(e.delete=o._freeData(t.delete)),t.expand){const[n,s]=o._loadExpandData(t.expand);e.create=e.create.concat(n),e.expand=s}i(e)})},new n};