diff --git a/bbtreeview.js b/bbtreeview.js index cd4c78d..bdc8a3e 100644 --- a/bbtreeview.js +++ b/bbtreeview.js @@ -40,106 +40,8 @@ BBTreeview.prototype.AddItem = function(options) { const node = a.Find(_options.ID); - // Events - node.Node.addEventListener("click", function(e){ - e.stopPropagation(); - e.preventDefault(); - - if (!a.isTag(e.target, "li")) { - return; - } - - if ((e.offsetX < 0) || (e.offsetX > 16) || (e.offsetY < 0) || (e.offsetY > 16)) { - return; - } - - const myNode = a.getNode(e.target); - if (myNode == null) { - return; - } - - a.Toggle(myNode); - }); - - // Setup checkbox events - if (a.Options.ShowCheckbox) { - node.GetCheckbox().addEventListener("mousedown", function(e){ - e.stopPropagation(); - e.preventDefault(); - - // node.Checked = !node.Checked; - - // a.CheckNode(node, node.Checked); - node.Check(!node.IsChecked()); - }); - - node.GetCheckbox().addEventListener("click", function(e){ - e.stopPropagation(); - e.preventDefault(); - - // do nothing - }); - - node.GetCheckbox().addEventListener("dblclick", function(e){ - e.stopPropagation(); - e.preventDefault(); - - // do nothing - }); - } - - // node.Label.addEventListener("click", function(e){ - // e.stopPropagation(); - // e.preventDefault(); - - // }); - - // Highlighting - node.Container.addEventListener("mousedown", function(e){ - e.stopPropagation(); - e.preventDefault(); - - const myNode = a.getNode(e.target); - if (myNode == null) { - return; - } - - a.Container.querySelectorAll("div.highlighted").forEach(function(e) { - e.classList.remove("highlighted"); - }); - - // Don't show selection - if (!a.Options.ShowSelection) { - return; - } - - myNode.Container.classList.add("highlighted"); - }); - - // Toggle - node.Container.addEventListener("dblclick", function(e){ - e.stopPropagation(); - e.preventDefault(); - - const myNode = a.getNode(e.target); - if (myNode == null) { - return; - } - - a.Toggle(myNode); - }); - - // Invalidate state - if (node.GetParentNode() != null) { - if (node.GetParentNode().classList.contains("c")) { - node.Node.classList.add("hidden"); - } else if (node.GetParentNode().classList.contains("e")) { - node.Node.classList.remove("hidden"); - } else { - node.GetParentNode().classList.add("e"); - node.Node.classList.remove("hidden"); - } - } + // Setup events + node.SetupEvents(); return true; }; @@ -154,22 +56,9 @@ BBTreeview.prototype.Remove = function(id) { node.Remove(); - // node.Node.parentNode.removeChild(node.Node); - - // if (node.GetParentNode() != null) { - // if (node.GetParentNode().classList.contains("e")) { - // node.GetParentNode().classList.remove("e"); - // } - - // if (node.GetParentNode().classList.contains("c")) { - // node.GetParentNode().classList.remove("c"); - // } - // } - return true; }; - BBTreeview.prototype.Default = function() { return { TreeviewOptions: { @@ -182,75 +71,52 @@ BBTreeview.prototype.Default = function() { ParentID: null, Name: "", Hint: "", - Value: "hello", + Value: "", Icon: "folder", - Checked: false + Checked: false, + Tag: null } }; }; - -// BBTreeview.prototype.CheckNode = function(node, value) { -// const a = this; - -// a.setNodeCheckbox(node.Node, value); - -// // Update children -// node.Node.querySelectorAll("li").forEach(function(e) { -// a.setNodeCheckbox(e, value); -// }); - -// // Invalidate ParentNode -// // node.ParentNode = a.GetParentNode(node.Node); - -// // Update parent state -// if (node.GetParentNode() != null) { -// let uncheckedCount = 0; -// node.GetParentNode().querySelectorAll("li").forEach(function(e) { -// if (e.classList.contains("x")) { -// return; -// } - -// uncheckedCount++; -// }); - -// a.setNodeCheckbox(node.GetParentNode(), (uncheckedCount <= 0)); -// } -// }; - BBTreeview.prototype.Clear = function() { const a = this; a.initialise(a.Options); }; -// BBTreeview.prototype.CollapseNode = function(node) { -// const a = this; +BBTreeview.prototype.CollapseAll = function() { + const a = this; -// node.Node.classList.remove("e"); -// node.Node.classList.add("c"); + a.GetAllNodes().forEach(function(e) { + e.Collapse(); + }); +}; -// a.GetChildNodes(node.Node).forEach(function(e) { -// e.classList.add("hidden"); -// }); -// }; +BBTreeview.prototype.ExpandAll = function() { + const a = this; -// BBTreeview.prototype.ExpandNode = function(node) { -// const a = this; + a.GetAllNodes().forEach(function(e) { + e.Expand(); + }); +}; -// node.Node.classList.remove("c"); -// node.Node.classList.add("e"); +BBTreeview.prototype.CheckAll = function(value) { + const a = this; -// a.GetChildNodes(node.Node).forEach(function(e) { -// e.classList.remove("hidden"); -// }); -// }; + if (!a.Options.ShowCheckbox) { + return; + } + + a.GetAllNodes().forEach(function(e) { + e.Check(value); + }); +}; BBTreeview.prototype.Find = function(id) { const a = this; const node = a.Container.querySelectorAll("li[data-bbtv-id='" + id + "']"); - if (node.length <= 0) { return null; } @@ -258,76 +124,107 @@ BBTreeview.prototype.Find = function(id) { let treenode = new BBTreeviewNode(this); treenode.Load(node[0]); - console.log(treenode); + // console.log(treenode); return treenode; - - // let response = { - // ID: id, - // Node: node[0], - // ParentNode: a.GetParentNode(node[0]), - // ChildNode: a.getChildNode(node[0]), - // Container: node[0].querySelectorAll("div.li")[0], - // Label: node[0].querySelectorAll("span")[0], - // Checked: node[0].classList.contains("x"), - // Checkbox: (a.Options.ShowCheckbox ? node[0].querySelectorAll("div.icon.checkbox")[0] : null), - // Value: node[0].getAttribute("data-bbtv-value") - // }; - - // // console.log(response); - - // return response; }; -// BBTreeview.prototype.GetChildNodes = function(node) { -// const a = this; - -// const childNode = a.getChildNode(node); -// if (childNode == null) { -// return []; -// } - -// let nodes = childNode.querySelectorAll("li"); -// if (nodes.length <= 0) { -// return []; -// } - -// let result = []; -// nodes.forEach(function(e) { -// if (typeof(e.parentNode) == "undefined") { -// return; -// } - -// if (e.parentNode != childNode) { -// return; -// } - -// result.push(e); -// }); - -// return result; -// }; - -// BBTreeview.prototype.GetParentNode = function(node) { -// const a = this; - -// return a.parentsUntilTagName(node, "li"); -// }; - -BBTreeview.prototype.Toggle = function(node) { +BBTreeview.prototype.FindByName = function(value) { const a = this; - if (node.Node.classList.contains("c")) { - // a.ExpandNode(node); - node.Expand(); - } else if (node.Node.classList.contains("e")) { - // a.CollapseNode(node); - node.Collapse(); - } else { - // do nothing - } + let response = []; + a.GetAllNodes().forEach(function(e) { + if (e.Name != value) { + return; + } + + response.push(e); + }); + + return response; }; +BBTreeview.prototype.FindByValue = function(value) { + const a = this; + + let response = []; + a.GetAllNodes().forEach(function(e) { + if (e.Value != value) { + return; + } + + response.push(e); + }); + + return response; +}; + +BBTreeview.prototype.GetAllNodes = function() { + const a = this; + + const node = a.Container.querySelectorAll("li"); + if (node.length <= 0) { + return []; + } + + let response = []; + node.forEach(function(e) { + const id = e.getAttribute("data-bbtv-id"); + if (a.isNullOrWhitespace(id)) { + return; + } + + const myNode = a.Find(id); + if (myNode == null) { + return; + } + + response.push(myNode); + }); + + return response; +}; + +BBTreeview.prototype.GetCheckedNodes = function() { + const a = this; + + let response = []; + a.GetAllNodes().forEach(function(e) { + if (!e.Checked) { + return; + } + + response.push(e); + }); + + return response; +}; + +BBTreeview.prototype.GetCheckedValues = function() { + const a = this; + + let response = []; + a.GetCheckedNodes().map(function(e) { + response.push(e.Value); + }); + + return response; +}; + +BBTreeview.prototype.GetSelectedNode = function() { + const a = this; + + let response = null; + a.GetAllNodes().forEach(function(e) { + if (e.Highlighted) { + response = e; + + return false; + } + }); + + return response; +}; BBTreeview.prototype.initialise = function(options) { var a = this; @@ -368,7 +265,12 @@ BBTreeview.prototype.generateID = function() { BBTreeview.prototype.generateNodeHtml = function(options) { const a = this; - let html = '
Create demo treeview with checkboxes
+Create demo treeview with checkboxes and selection enabled
Find a treenode
++ + + +
+ +Get treenodes
++ + + +
++ +
+ +Various
++ + + + +
+ +