-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp2p-icp-graph-editor.js
126 lines (106 loc) · 4.19 KB
/
mp2p-icp-graph-editor.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
document.addEventListener("DOMContentLoaded", function () {
const yamlEditor = CodeMirror.fromTextArea(document.getElementById("yaml-editor"), {
mode: "yaml",
lineNumbers: true,
theme: "default"
});
const graphContainer = document.getElementById("graph-container");
const toolbox = document.getElementById("toolbox");
const uploadInput = document.getElementById("upload-yaml");
let blocks = [];
let connections = [];
function parseYAML(yamlText) {
try {
const doc = jsyaml.load(yamlText);
renderGraph(doc);
} catch (e) {
console.error("Invalid YAML:", e);
}
}
function renderGraph(yamlDoc) {
clearGraph();
// Example YAML parsing logic
["generators", "filters", "final_filters"].forEach((key) => {
if (yamlDoc[key]) {
yamlDoc[key].forEach((item, index) => {
createBlock(item.class_name, "algorithm", { x: 100, y: index * 100 });
});
}
});
// Example connections (based on YAML relationships)
// You’d add logic here to add arrows from params like 'input_pointcloud_layer' etc.
}
function createBlock(name, type, position) {
const block = document.createElement("div");
block.className = `block ${type}-block`;
block.textContent = name;
block.style.left = `${position.x}px`;
block.style.top = `${position.y}px`;
block.addEventListener("dblclick", () => jumpToYAML(name));
blocks.push({ name, type, element: block });
graphContainer.appendChild(block);
enableDrag(block);
}
function enableDrag(element) {
interact(element).draggable({
onmove(event) {
const { target, dx, dy } = event;
const x = (parseFloat(target.getAttribute('data-x')) || 0) + dx;
const y = (parseFloat(target.getAttribute('data-y')) || 0) + dy;
target.style.transform = `translate(${x}px, ${y}px)`;
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}
});
}
function clearGraph() {
blocks.forEach((block) => {
graphContainer.removeChild(block.element);
});
blocks = [];
connections.forEach((conn) => conn.remove());
connections = [];
}
function jumpToYAML(name) {
// Locate and jump to YAML text line
}
uploadInput.addEventListener("change", function (event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
yamlEditor.setValue(e.target.result);
parseYAML(e.target.result);
};
reader.readAsText(file);
}
});
});
document.addEventListener("DOMContentLoaded", function () {
const editorContainer = document.getElementById("editor-container");
const graphContainer = document.getElementById("graph-container");
const separator = document.getElementById("separator");
let isDragging = false;
// Mouse down on separator starts dragging
separator.addEventListener("mousedown", (e) => {
isDragging = true;
document.body.style.cursor = "col-resize"; // Change cursor during drag
});
// Mouse move adjusts the widths
document.addEventListener("mousemove", (e) => {
if (!isDragging) return;
// Calculate new width as percentage
const containerRect = document.getElementById("container").getBoundingClientRect();
const offsetX = e.clientX - containerRect.left;
const editorWidthPercent = (offsetX / containerRect.width) * 100;
const graphWidthPercent = 100 - editorWidthPercent;
// Set the widths of the editor and graph containers
editorContainer.style.width = `${editorWidthPercent}%`;
graphContainer.style.width = `${graphWidthPercent}%`;
});
// Mouse up stops dragging
document.addEventListener("mouseup", () => {
isDragging = false;
document.body.style.cursor = "default"; // Reset cursor after drag
});
});