-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnodeManagement.js
More file actions
97 lines (82 loc) · 3.71 KB
/
Copy pathnodeManagement.js
File metadata and controls
97 lines (82 loc) · 3.71 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
let lastClickTime = 0; // To track the timing of clicks for double-click detection
// Function to change the value of a node
function editNodeValue(node) {
const currentValue = node.getAttribute("data-value");
const newValue = prompt("Enter new value for this node", currentValue);
if (newValue !== null && newValue.trim() !== "") {
node.setAttribute("data-value", newValue);
node.textContent = newValue; // Update the displayed value
updateLines(node); // Update lines to ensure correct positioning
}
}
function handleNodeRightClick(node, e) {
e.preventDefault(); // Prevent the default context menu from showing
// Display a custom menu (or use a prompt in this case)
const action = prompt("Choose an action: Type 'edit' to edit or 'delete' to delete", "edit");
if (action === "edit") {
editNodeValue(node);
} else if (action === "delete") {
handleNodeDoubleClick(node);
}
}
// Function to change the parameter of a line
// function editLineParameter(lineData) {
// const currentParameter = lineData.parameterLabel.innerText;
// const newParameter = prompt("Enter new parameter for this line", currentParameter);
// if (newParameter !== null && newParameter.trim() !== "") {
// lineData.parameterLabel.innerText = newParameter; // Update the displayed parameter
// }
// }
// Function to handle double-click for deleting a node
function handleNodeDoubleClick(node) {
const confirmed = confirm("Are you sure you want to delete this node?");
if (confirmed) {
// Remove the node
node.remove();
// Remove associated lines
lines = lines.filter(lineData => {
const isConnected = lineData.node1 === node || lineData.node2 === node;
if (isConnected) {
lineData.line.remove(); // Remove line from the DOM
lineData.parameterLabel.remove(); // Remove parameter label from the DOM
}
return !isConnected; // Keep lines not connected to the deleted node
});
connections = connections.filter(connection => connection.node1 !== node && connection.node2 !== node);
}
}
// Create a new node in the tree
function createNode(x, y, value) {
const newNode = document.createElement("div");
newNode.classList.add("node-in-tree");
newNode.style.left = `${x}px`;
newNode.style.top = `${y}px`;
newNode.textContent = value;
treeArea.appendChild(newNode);
// Add click event listener to select nodes for line connection
newNode.addEventListener("click", (e) => handleNodeClick(newNode, e));
// Add right-click event listener for editing or deleting the node
newNode.addEventListener("contextmenu", (e) => handleNodeRightClick(newNode, e));
// Make the node draggable within the tree area to reposition
newNode.addEventListener("mousedown", function (e) {
const node = this;
const offsetX = e.offsetX;
const offsetY = e.offsetY;
let isDragging = false;
function moveNode(e) {
isDragging = true;
node.style.left = `${e.clientX - treeArea.offsetLeft - offsetX}px`;
node.style.top = `${e.clientY - treeArea.offsetTop - offsetY}px`;
updateLines(node); // Update the lines when the node is moved
}
function stopMovingNode() {
document.removeEventListener("mousemove", moveNode);
document.removeEventListener("mouseup", stopMovingNode);
if (!isDragging) {
handleNodeClick(node); // Only trigger click if it wasn't a drag
}
}
document.addEventListener("mousemove", moveNode);
document.addEventListener("mouseup", stopMovingNode);
});
}