-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (84 loc) · 3.31 KB
/
script.js
File metadata and controls
100 lines (84 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
document.addEventListener("DOMContentLoaded", () => {
const nodeTypeSelect = document.getElementById("nodeType");
const nodeValueInput = document.getElementById("nodeValue");
const createNodeButton = document.getElementById("createNode");
const treeContainer = document.getElementById("tree");
let nodeIdCounter = 1;
let firstNodeCreated = false; // To track if the first node is created
createNodeButton.addEventListener("click", () => {
const nodeType = nodeTypeSelect.value;
const nodeValue = nodeValueInput.value.trim();
if (nodeValue === "") {
alert("Please enter a node value.");
return;
}
if (nodeType === "single") {
createSingleNode(nodeValue);
} else if (nodeType === "parent") {
createParentNode(nodeValue);
} else if (nodeType === "child") {
createChildNode(nodeValue);
}
nodeValueInput.value = "";
});
function createSingleNode(value) {
const newNodeBox = createNodeBox();
const newNode = createNodeElement(value);
newNodeBox.appendChild(newNode);
treeContainer.insertBefore(newNodeBox, treeContainer.firstChild);
if (!firstNodeCreated) {
firstNodeCreated = true;
newNode.addEventListener("click", () => {
if (confirm("Do you want to delete this node?")) {
newNodeBox.remove();
}
});
}
}
function createParentNode(value) {
const newNodeBox = createNodeBox("parent-box");
const parent = createNodeElement(value, "parent");
newNodeBox.appendChild(parent);
if (!firstNodeCreated) {
firstNodeCreated = true;
parent.addEventListener("click", () => {
if (confirm("Do you want to delete this node and its children?")) {
newNodeBox.remove();
}
});
}
parent.addEventListener("click", () => {
if (confirm("Do you want to delete this node and its children?")) {
newNodeBox.remove();
}
});
treeContainer.appendChild(newNodeBox);
}
function createChildNode(value) {
const parentBoxes = document.querySelectorAll(".parent-box");
if (parentBoxes.length === 0) {
alert("There are no parent nodes to attach to.");
return;
}
const parentBox = parentBoxes[parentBoxes.length - 1];
const child = createNodeElement(value, "child");
parentBox.appendChild(child);
child.addEventListener("click", () => {
if (confirm("Do you want to delete this child node?")) {
child.remove();
}
});
}
function createNodeBox(type = "") {
const newNodeBox = document.createElement("div");
newNodeBox.className = `node-box ${type}`;
return newNodeBox;
}
function createNodeElement(value, type = "") {
const newNode = document.createElement("div");
newNode.className = `node ${type}`;
newNode.innerText = value;
newNode.dataset.nodeId = nodeIdCounter++;
return newNode;
}
});