2024-01-08 21:41:43 +00:00
|
|
|
class BBTreeviewNode {
|
2024-01-09 21:26:40 +00:00
|
|
|
constructor(treeview, node) {
|
2024-01-08 23:45:52 +00:00
|
|
|
this.Treeview = treeview;
|
2024-01-09 21:26:40 +00:00
|
|
|
this.Node = node;
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
// this.initialiseComponents();
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
// initialiseComponents() {
|
|
|
|
// const a = this;
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
// }
|
2024-01-08 21:41:43 +00:00
|
|
|
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get Checkbox() {
|
2024-01-08 21:41:43 +00:00
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (!a.Treeview.Options.ShowCheckbox) {
|
2024-01-09 21:26:40 +00:00
|
|
|
return null;
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
const checkboxes = a.Node.querySelectorAll("div.icon.checkbox");
|
|
|
|
if (typeof(checkboxes) == "undefined") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkboxes[0];
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get ChildNode() {
|
2024-01-08 21:41:43 +00:00
|
|
|
const a = this;
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
let result = a.Node.querySelectorAll("ul");
|
|
|
|
if (result.length <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
return result[0];
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get ChildNodes() {
|
|
|
|
const a = this;
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
const childNode = a.ChildNode;
|
|
|
|
if (childNode == null) {
|
|
|
|
return [];
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
let nodes = childNode.querySelectorAll("li");
|
|
|
|
if (nodes.length <= 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
let result = [];
|
|
|
|
nodes.forEach(function(e) {
|
|
|
|
if (typeof(e.parentNode) == "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
if (e.parentNode != childNode) {
|
|
|
|
return;
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
result.push(e);
|
|
|
|
});
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
return result;
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get IsChecked() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (!a.Treeview.Options.ShowCheckbox) {
|
|
|
|
return false;
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
2024-01-09 21:26:40 +00:00
|
|
|
|
|
|
|
return a.Node.classList.contains("x");
|
|
|
|
}
|
|
|
|
|
|
|
|
get Container() {
|
|
|
|
return this.Node.querySelectorAll("div.li")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
get Hint() {
|
|
|
|
return this.Label.getAttribute("title");
|
|
|
|
}
|
|
|
|
|
|
|
|
get ID() {
|
|
|
|
return this.Node.getAttribute("data-bbtv-id");
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get IsExpanded() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (a.Node.classList.contains("e")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.Node.classList.contains("c")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get IsHighlighted() {
|
|
|
|
const a = this;
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
return a.Container.classList.contains("highlighted");
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get Label() {
|
|
|
|
return this.Node.querySelectorAll("span")[0];
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get Name() {
|
|
|
|
return this.Label.textContent;
|
|
|
|
}
|
2024-01-09 15:10:08 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get ParentID() {
|
2024-01-08 21:41:43 +00:00
|
|
|
const a = this;
|
|
|
|
|
|
|
|
const parentNode = a.ParentNode;
|
|
|
|
if (parentNode == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentNode.getAttribute("data-bbtv-id");
|
|
|
|
}
|
|
|
|
|
|
|
|
get ParentNode() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
return a.parentsUntilTagName(a.Node, "li");
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
get Tag() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
let tag = a.Node.getAttribute("data-bbtv-tag");
|
|
|
|
if (a.Treeview.isNullOrWhitespace(tag)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.parse(decodeURIComponent(tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
get Value() {
|
|
|
|
return this.Node.getAttribute("data-bbtv-value");
|
|
|
|
}
|
|
|
|
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
Check(value) {
|
2024-01-08 21:41:43 +00:00
|
|
|
const a = this;
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.setCheckbox(a.Node, value);
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
// Update children
|
|
|
|
a.Node.querySelectorAll("li").forEach(function(e) {
|
|
|
|
a.setCheckbox(e, value);
|
|
|
|
});
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
// Update parent state
|
|
|
|
let parentNode = a.ParentNode;
|
|
|
|
if (parentNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle pull-ups
|
|
|
|
if (a.Treeview.Options.EnablePullUp) {
|
|
|
|
while (true) {
|
|
|
|
const parentChecked = (parentNode.querySelectorAll("li.x").length > 0);
|
|
|
|
|
|
|
|
a.setCheckbox(parentNode, parentChecked);
|
|
|
|
|
|
|
|
parentNode = a.Treeview.getNode(parentNode);
|
|
|
|
parentNode = parentNode.ParentNode;
|
|
|
|
|
|
|
|
if (parentNode == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let uncheckedCount = 0;
|
|
|
|
|
|
|
|
parentNode.querySelectorAll("li").forEach(function(e) {
|
|
|
|
if (e.classList.contains("x")) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
uncheckedCount++;
|
|
|
|
});
|
2024-01-08 21:41:43 +00:00
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.setCheckbox(parentNode, (uncheckedCount <= 0));
|
|
|
|
}
|
2024-01-08 21:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Collapse() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (a.Node.classList.contains("e")) {
|
|
|
|
a.Node.classList.remove("e");
|
|
|
|
a.Node.classList.add("c");
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.ChildNodes.forEach(function(e) {
|
2024-01-08 21:41:43 +00:00
|
|
|
e.classList.add("hidden");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Expand() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (a.Node.classList.contains("c")) {
|
|
|
|
a.Node.classList.remove("c");
|
|
|
|
a.Node.classList.add("e");
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.ChildNodes.forEach(function(e) {
|
2024-01-08 21:41:43 +00:00
|
|
|
e.classList.remove("hidden");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Remove() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
if (a.ParentNode != null) {
|
|
|
|
if (a.ParentNode.classList.contains("e")) {
|
|
|
|
a.ParentNode.classList.remove("e");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.ParentNode.classList.contains("c")) {
|
|
|
|
a.ParentNode.classList.remove("c");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Node.parentNode.removeChild(a.Node);
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
InvalidateEvents() {
|
2024-01-08 21:41:43 +00:00
|
|
|
const a = this;
|
|
|
|
|
|
|
|
// collapsible icon behaviour
|
|
|
|
a.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.Treeview.getNode(e.target);
|
|
|
|
if (myNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
myNode.Toggle();
|
|
|
|
});
|
|
|
|
|
|
|
|
// collapsible label behaviour
|
|
|
|
a.Container.addEventListener("dblclick", function(e){
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const myNode = a.Treeview.getNode(e.target);
|
|
|
|
if (myNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
myNode.Toggle();
|
|
|
|
});
|
|
|
|
|
|
|
|
// checkbox behaviour
|
|
|
|
if (a.Treeview.Options.ShowCheckbox) {
|
2024-01-09 21:26:40 +00:00
|
|
|
a.Checkbox.addEventListener("mousedown", function(e){
|
2024-01-08 21:41:43 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.Check(!a.IsChecked);
|
2024-01-08 21:41:43 +00:00
|
|
|
});
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.Checkbox.addEventListener("click", function(e){
|
2024-01-08 21:41:43 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
});
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
a.Checkbox.addEventListener("dblclick", function(e){
|
2024-01-08 21:41:43 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// highlighting
|
|
|
|
a.Container.addEventListener("mousedown", function(e){
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const myNode = a.Treeview.getNode(e.target);
|
|
|
|
if (myNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Treeview.Container.querySelectorAll("div.highlighted").forEach(function(e) {
|
|
|
|
e.classList.remove("highlighted");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Don't show selection
|
|
|
|
if (!a.Treeview.Options.ShowSelection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
myNode.Container.classList.add("highlighted");
|
|
|
|
});
|
|
|
|
|
|
|
|
a.invalidateCollapsible();
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:26:40 +00:00
|
|
|
Toggle() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
switch (a.IsExpanded) {
|
|
|
|
case true:
|
|
|
|
a.Collapse();
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
a.Expand();
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-08 21:41:43 +00:00
|
|
|
invalidateCollapsible() {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
// Invalidate state
|
|
|
|
if (a.ParentNode == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.ParentNode.classList.contains("c")) {
|
|
|
|
a.Node.classList.add("hidden");
|
|
|
|
} else if (a.ParentNode.classList.contains("e")) {
|
|
|
|
a.Node.classList.remove("hidden");
|
|
|
|
} else {
|
|
|
|
a.ParentNode.classList.add("e");
|
|
|
|
a.Node.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 23:45:52 +00:00
|
|
|
isTag(el, tagName) {
|
2024-01-08 21:41:43 +00:00
|
|
|
return (el.tagName.toLowerCase() == tagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
parentsUntilTagName(el, tagName) {
|
|
|
|
const a = this;
|
|
|
|
|
|
|
|
let node = el;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (typeof(node.parentNode) == "undefined") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.parentNode;
|
|
|
|
|
|
|
|
if (typeof(node) == "undefined") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.isTag(node, "ul")) {
|
|
|
|
if (node.classList.contains("bbtreeview")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.isTag(node, tagName)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCheckbox(el, value) {
|
|
|
|
if (value) {
|
|
|
|
if (!el.classList.contains("x")) {
|
|
|
|
el.classList.add("x");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (el.classList.contains("x")) {
|
|
|
|
el.classList.remove("x");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|