path_tree.min.js 4.2 KB

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