diff --git a/build.bat b/build.bat index d19fa8a..e8ed8f6 100644 --- a/build.bat +++ b/build.bat @@ -6,7 +6,12 @@ cd src type version.txt >> ../bbtreeview.min.js -"C:\B\Portable Files (dev)\Google Closure Compiler\closure-compiler-v20231112.jar" --js *.js --js_output_file ../bbtreeview.min.tmp +@REM "C:\B\Portable Files (dev)\Google Closure Compiler\closure-compiler-v20231112.jar" --js *.js --js_output_file ../bbtreeview.min.tmp +@REM npx google-closure-compiler --compilation_level=ADVANCED --js=*.js --js_output_file=../bbtreeview.min.tmp +@REM npx google-closure-compiler --compilation_level=ADVANCED --warning_level=VERBOSE --js=*.js --js_output_file=bbtreeview.min.tmp +@REM npx google-closure-compiler --warning_level=VERBOSE --js=*.js --js_output_file=bbtreeview.min.tmp + +move bbtreeview.min.tmp ../bbtreeview.min.tmp cd.. @@ -14,4 +19,4 @@ type bbtreeview.min.tmp >> bbtreeview.min.js move bbtreeview.min.js build/bbtreeview.min.js -del bbtreeview.min.tmp \ No newline at end of file +del bbtreeview.min.tmp diff --git a/build/bbtreeview.min.js b/build/bbtreeview.min.js index 1afd2a7..edafacd 100644 --- a/build/bbtreeview.min.js +++ b/build/bbtreeview.min.js @@ -2,6 +2,18 @@ * BBTreeview * @version v0.1.2.011 (2024/01/07 0208) */ -class BBTreeview{constructor(e){this.initialiseComponents(e)}initialiseComponents(e){this.Options=Object.assign(this.DefaultOptions.Treeview,e)}get Container(){let e=document.getElementsByTagName("body");return e.length<=0?null:(e=e[0].querySelectorAll(this.Options.ID),e.length<=0||(e[0].innerHTML='',e[0].querySelectorAll("ul.bbtreeview"),e.length<=0)?void 0:e[0])}get DefaultOptions(){return{Treeview:{ID:null,ShowCheckbox:!1,ShowSelection:!0,EnablePullUp:!1,ShowIcon:!0},TreeNode:{ID:null,ParentID:null,Name:"",Hint:"",Value:"",Icon:"folder",Checked:!1,Tag:null}}}AddItem(e){const t=this,n=t.getOptions(e);if(null!=t.Find(n.ID))return!1;let l=null;if(null!=n.ParentID&&(l=t.Find(n.ParentID),null==l))return!1;if(l=null==l?t.Container:l.GetChildNode(),null==l)return!1;const i=t.generateNodeHtml(n);t.appendHtml(l,i);t.Find(n.ID);return!0}Remove(e){const t=this.Find(e);return null!=t&&(t.Remove(),!0)}Clear(){this.initialiseComponents(this.Options)}CollapseAll(){this.GetAllNodes().forEach((function(e){e.Collapse()}))}ExpandAll(){this.GetAllNodes().forEach((function(e){e.Expand()}))}CheckAll(e){this.Options.ShowCheckbox&&this.GetAllNodes().forEach((function(t){t.IsChecked=e}))}Find(e){if(null==this.Container)return void console.log("BBTreeview container not found");const t=this.Container.querySelectorAll("li[data-bbtv-id='"+e+"']");if(t.length<=0)return null;let n=new BBTreeviewNode(this);return n.Load(t[0]),n}FindByName(e){let t=[];return this.GetAllNodes().forEach((function(n){n.Name==e&&t.push(n)})),t}FindByValue(e){let t=[];return this.GetAllNodes().forEach((function(n){n.Value==e&&t.push(n)})),t}GetAllNodes(){const e=this,t=e.Container.querySelectorAll("li");if(t.length<=0)return[];let n=[];return t.forEach((function(t){const l=t.getAttribute("data-bbtv-id");if(e.isNullOrWhitespace(l))return;const i=e.Find(l);null!=i&&n.push(i)})),n}GetCheckedNodes(){let e=[];return this.GetAllNodes().forEach((function(t){t.Checked&&e.push(t)})),e}GetCheckedValues(){let e=[];return this.GetCheckedNodes().map((function(t){e.push(t.Value)})),e}GetCheckedTags(){let e=[];return this.GetCheckedNodes().map((function(t){e.push(t.Tag)})),e}GetSelectedNode(){let e=null;return this.GetAllNodes().forEach((function(t){if(t.Highlighted)return e=t,!1})),e}appendHtml(e,t){let n=document.createElement("template");n.innerHTML=t,n=n.content.firstChild,e.appendChild(n)}generateID(){return"treeviewItem"+(Math.floor(1000001*Math.random())+100).toString()}generateNodeHtml(e){const t=this;let n="";t.isNullOrWhitespace(e.Tag)||(n=encodeURIComponent(JSON.stringify(e.Tag)));let l='
  • ';return l+='
    ',t.Options.ShowCheckbox&&(l+='
    '),t.Options.ShowIcon?(l+='
    ',l+=''+e.Name+""):l+=''+e.Name+"",l+="
    ",l+="",l+="
  • ",l}getNode(e){const t=this;let n=null;n=t.isTag(e,"li")?e:t.parentsUntilTagName(e,"li");const l=n.getAttribute("data-bbtv-id");return t.isNullOrWhitespace(l)?null:t.Find(l)}getOptions(e){const t=this;let n=Object.assign(t.DefaultOptions.TreeNode,e);return null==n.ID&&(n.ID=t.generateID()),null==n.Tag&&(n.Tag=""),n}isNullOrWhitespace(e){return void 0===e||(null==e||e.toString().trim().length<=0)}isTag(e,t){return e.tagName.toLowerCase()==t}parentsUntilTagName(e,t){const n=this;let l=e;for(;void 0!==l.parentNode&&(l=l.parentNode,void 0!==l);){if(n.isTag(l,"ul")&&l.classList.contains("bbtreeview"))return null;if(n.isTag(l,t))break}return l}} - -class BBTreeviewNode{constructor(e){const t=this;t.Treeview=e,t.ID=null,t.Node=null,t.Container=null,t.Label=null,t.Value=null,t.Name="",t.Hint="",t.ParentID=null,t.IsChecked=!1,t.Highlighted=!1,t.Tag=null,t.initialiseComponents()}initialiseComponents(){}get IsChecked(){return!!this.Treeview.Options.ShowCheckbox&&this.Node.classList.contains("x")}set IsChecked(e){const t=this;t.setCheckbox(t.Node,e),t.Node.querySelectorAll("li").forEach((function(n){t.setCheckbox(n,e)}));let n=t.ParentNode;if(null!=n)if(t.Treeview.Options.EnablePullUp)for(;;){const e=n.querySelectorAll("li.x").length>0;if(t.setCheckbox(n,e),n=t.Treeview.getNode(n),n=n.GetParentNode(),null==n)break}else{let e=0;n.querySelectorAll("li").forEach((function(t){t.classList.contains("x")||e++})),t.setCheckbox(n,e<=0)}}get IsExpanded(){return!!this.Node.classList.contains("e")||!this.Node.classList.contains("c")&&null}get IsHighlighted(){return this.Container.classList.contains("highlighted")}get Tag(){let e=this.Node.getAttribute("data-bbtv-tag");return this.Treeview.isNullOrWhitespace(e)?null:JSON.parse(decodeURIComponent(e))}get ParentNodeID(){const e=this.ParentNode;return null==e?null:e.getAttribute("data-bbtv-id")}get ParentNode(){return this.parentsUntilTagName(this.Node,"li")}Load(e){const t=this;t.ID=e.getAttribute("data-bbtv-id"),t.Node=e,t.Container=e.querySelectorAll("div.li")[0],t.Label=e.querySelectorAll("span")[0],t.Value=e.getAttribute("data-bbtv-value"),t.Name=t.Label.textContent,t.Hint=t.Label.getAttribute("title"),t.ParentID=t.ParentNodeID,t.IsChecked=t.IsChecked,t.Highlighted=t.IsHighlighted,t.Tag=t.Tag,t.initialiseComponents_Events()}Collapse(){const e=this;e.Node.classList.contains("e")&&(e.Node.classList.remove("e"),e.Node.classList.add("c")),e.GetChildNodes().forEach((function(e){e.classList.add("hidden")}))}Expand(){const e=this;e.Node.classList.contains("c")&&(e.Node.classList.remove("c"),e.Node.classList.add("e")),e.GetChildNodes().forEach((function(e){e.classList.remove("hidden")}))}GetCheckbox(){if(!this.Treeview.Options.ShowCheckbox)return null;const e=this.Node.querySelectorAll("div.icon.checkbox");return void 0===e?null:e[0]}GetChildNode(){let e=this.Node.querySelectorAll("ul");return e.length<=0?null:e[0]}GetChildNodes(){const e=this.GetChildNode();if(null==e)return[];let t=e.querySelectorAll("li");if(t.length<=0)return[];let n=[];return t.forEach((function(t){void 0!==t.parentNode&&t.parentNode==e&&n.push(t)})),n}Remove(){const e=this;null!=e.ParentNode&&(e.ParentNode.classList.contains("e")&&e.ParentNode.classList.remove("e"),e.ParentNode.classList.contains("c")&&e.ParentNode.classList.remove("c")),e.Node.parentNode.removeChild(e.Node)}Toggle(){const e=this;switch(e.IsExpanded){case!0:e.Collapse();break;case!1:e.Expand()}}initialiseComponents_Events(){const e=this;e.Node.addEventListener("click",(function(t){if(t.stopPropagation(),t.preventDefault(),!e.isTag(t.target,"li"))return;if(t.offsetX<0||t.offsetX>16||t.offsetY<0||t.offsetY>16)return;const n=e.Treeview.getNode(t.target);null!=n&&n.Toggle()})),e.Container.addEventListener("dblclick",(function(t){t.stopPropagation(),t.preventDefault();const n=e.Treeview.getNode(t.target);null!=n&&n.Toggle()})),e.Treeview.Options.ShowCheckbox&&(e.GetCheckbox().addEventListener("mousedown",(function(t){t.stopPropagation(),t.preventDefault(),e.IsChecked=!e.IsChecked})),e.GetCheckbox().addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault()})),e.GetCheckbox().addEventListener("dblclick",(function(e){e.stopPropagation(),e.preventDefault()}))),e.Container.addEventListener("mousedown",(function(t){t.stopPropagation(),t.preventDefault();const n=e.Treeview.getNode(t.target);null!=n&&(e.Treeview.Container.querySelectorAll("div.highlighted").forEach((function(e){e.classList.remove("highlighted")})),e.Treeview.Options.ShowSelection&&n.Container.classList.add("highlighted"))})),e.invalidateCollapsible()}invalidateCollapsible(){const e=this;null!=e.ParentNode&&(e.ParentNode.classList.contains("c")?e.Node.classList.add("hidden"):(e.ParentNode.classList.contains("e")||e.ParentNode.classList.add("e"),e.Node.classList.remove("hidden")))}isTag=function(e,t){return e.tagName.toLowerCase()==t};parentsUntilTagName(e,t){const n=this;let s=e;for(;void 0!==s.parentNode&&(s=s.parentNode,void 0!==s);){if(n.isTag(s,"ul")&&s.classList.contains("bbtreeview"))return null;if(n.isTag(s,t))break}return s}setCheckbox(e,t){t?e.classList.contains("x")||e.classList.add("x"):e.classList.contains("x")&&e.classList.remove("x")}} \ No newline at end of file +class BBTreeview{constructor(a){this.Options=null;this.initialiseComponents(a)}initialiseComponents(a){this.Options=Object.assign(this.DefaultOptions.Treeview,a)}get Container(){let a=document.getElementsByTagName("body");if(0>=a.length)return null;a=a[0].querySelectorAll(this.Options.ID);if(!(0>=a.length||(a[0].innerHTML='',a[0].querySelectorAll("ul.bbtreeview"),0>=a.length)))return a[0]}get DefaultOptions(){return{Treeview:{ID:null,ShowCheckbox:!1,ShowSelection:!0,EnablePullUp:!1, +ShowIcon:!0},TreeNode:{ID:null,ParentID:null,Name:"",Hint:"",Value:"",Icon:"folder",Checked:!1,Tag:null}}}AddItem(a){a=this.getOptions(a);if(null!=this.Find(a.ID))return!1;let b=null;if(null!=a.ParentID&&(b=this.Find(a.ParentID),null==b))return!1;b=null==b?this.Container:b.GetChildNode();if(null==b)return!1;const c=this.generateNodeHtml(a);this.appendHtml(b,c);this.Find(a.ID);return!0}Remove(a){a=this.Find(a);if(null==a)return!1;a.Remove();return!0}Clear(){this.initialiseComponents(this.Options)}CollapseAll(){this.GetAllNodes().forEach(function(a){a.Collapse()})}ExpandAll(){this.GetAllNodes().forEach(function(a){a.Expand()})}CheckAll(a){this.Options.ShowCheckbox&& +this.GetAllNodes().forEach(function(b){b.IsChecked=a})}Find(a){if(null==this.Container)console.log("BBTreeview container not found");else{a=this.Container.querySelectorAll("li[data-bbtv-id='"+a+"']");if(0>=a.length)return null;var b=new BBTreeviewNode(this);b.Load(a[0]);return b}}FindByName(a){let b=[];this.GetAllNodes().forEach(function(c){c.Name==a&&b.push(c)});return b}FindByValue(a){let b=[];this.GetAllNodes().forEach(function(c){c.Value==a&&b.push(c)});return b}GetAllNodes(){const a=this,b=a.Container.querySelectorAll("li"); +if(0>=b.length)return[];let c=[];b.forEach(function(d){d=d.getAttribute("data-bbtv-id");a.isNullOrWhitespace(d)||(d=a.Find(d),null!=d&&c.push(d))});return c}GetCheckedNodes(){let a=[];this.GetAllNodes().forEach(function(b){b.Checked&&a.push(b)});return a}GetCheckedValues(){let a=[];this.GetCheckedNodes().map(function(b){a.push(b.Value)});return a}GetCheckedTags(){let a=[];this.GetCheckedNodes().map(function(b){a.push(b.Tag)});return a}GetSelectedNode(){let a=null;this.GetAllNodes().forEach(function(b){if(b.Highlighted)return a= +b,!1});return a}appendHtml(a,b){let c=document.createElement("template");c.innerHTML=b;c=c.content.firstChild;a.appendChild(c)}generateID(){return"treeviewItem"+(Math.floor(1000001*Math.random())+100).toString()}generateNodeHtml(a){var b="";this.isNullOrWhitespace(a.Tag)||(b=encodeURIComponent(JSON.stringify(a.Tag)));b='
  • ';b+='
    ';this.Options.ShowCheckbox&&(b+='
    ');this.Options.ShowIcon? +(b+='
    ',b+=''+a.Name+""):b+=''+a.Name+"";return b+"
  • "}getNode(a){a=(this.isTag(a,"li")?a:this.parentsUntilTagName(a,"li")).getAttribute("data-bbtv-id");return this.isNullOrWhitespace(a)?null:this.Find(a)}getOptions(a){a=Object.assign(this.DefaultOptions.TreeNode,a);null==a.ID&&(a.ID=this.generateID());null==a.Tag&&(a.Tag="");return a}isNullOrWhitespace(a){return"undefined"== +typeof a||null==a?!0:0>=a.toString().trim().length}isTag(a,b){return a.tagName.toLowerCase()==b}parentsUntilTagName(a,b){for(;"undefined"!=typeof a.parentNode;){a=a.parentNode;if("undefined"==typeof a)break;if(this.isTag(a,"ul")&&a.classList.contains("bbtreeview"))return null;if(this.isTag(a,b))break}return a}};class BBTreeviewNode{constructor(a){this.Treeview=a;this.Value=this.Label=this.Container=this.Node=this.ID=null;this.Hint=this.Name="";this.ParentID=null;this.Highlighted=this.IsChecked=!1;this.Tag=null;this.initialiseComponents()}initialiseComponents(){}get IsChecked(){return this.Treeview.Options.ShowCheckbox?this.Node.classList.contains("x"):!1}set IsChecked(a){const b=this;b.setCheckbox(b.Node,a);b.Node.querySelectorAll("li").forEach(function(d){b.setCheckbox(d,a)});let c=b.ParentNode;if(null!= +c)if(b.Treeview.Options.EnablePullUp)for(;;){const d=0=d)}}get IsExpanded(){return this.Node.classList.contains("e")?!0:this.Node.classList.contains("c")?!1:null}get IsHighlighted(){return this.Container.classList.contains("highlighted")}get Tag(){let a=this.Node.getAttribute("data-bbtv-tag"); +return this.Treeview.isNullOrWhitespace(a)?null:JSON.parse(decodeURIComponent(a))}get ParentNodeID(){const a=this.ParentNode;return null==a?null:a.getAttribute("data-bbtv-id")}get ParentNode(){return this.parentsUntilTagName(this.Node,"li")}Load(a){this.ID=a.getAttribute("data-bbtv-id");this.Node=a;this.Container=a.querySelectorAll("div.li")[0];this.Label=a.querySelectorAll("span")[0];this.Value=a.getAttribute("data-bbtv-value");this.Name=this.Label.textContent;this.Hint=this.Label.getAttribute("title"); +this.ParentID=this.ParentNodeID;this.IsChecked=this.IsChecked;this.Highlighted=this.IsHighlighted;this.Tag=this.Tag;this.initialiseComponents_Events()}Collapse(){this.Node.classList.contains("e")&&(this.Node.classList.remove("e"),this.Node.classList.add("c"));this.GetChildNodes().forEach(function(a){a.classList.add("hidden")})}Expand(){this.Node.classList.contains("c")&&(this.Node.classList.remove("c"),this.Node.classList.add("e"));this.GetChildNodes().forEach(function(a){a.classList.remove("hidden")})}GetCheckbox(){if(!this.Treeview.Options.ShowCheckbox)return null; +const a=this.Node.querySelectorAll("div.icon.checkbox");return"undefined"==typeof a?null:a[0]}GetChildNode(){let a=this.Node.querySelectorAll("ul");return 0>=a.length?null:a[0]}GetChildNodes(){const a=this.GetChildNode();if(null==a)return[];let b=a.querySelectorAll("li");if(0>=b.length)return[];let c=[];b.forEach(function(d){"undefined"!=typeof d.parentNode&&d.parentNode==a&&c.push(d)});return c}Remove(){null!=this.ParentNode&&(this.ParentNode.classList.contains("e")&&this.ParentNode.classList.remove("e"), +this.ParentNode.classList.contains("c")&&this.ParentNode.classList.remove("c"));this.Node.parentNode.removeChild(this.Node)}Toggle(){switch(this.IsExpanded){case !0:this.Collapse();break;case !1:this.Expand()}}initialiseComponents_Events(){const a=this;a.Node.addEventListener("click",function(b){b.stopPropagation();b.preventDefault();!a.isTag(b.target,"li")||0>b.offsetX||16b.offsetY||16