-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
212 lines (183 loc) · 5.71 KB
/
script.js
File metadata and controls
212 lines (183 loc) · 5.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let itemsCount = 1000;
document.addEventListener("DOMContentLoaded", function () {
const formContainer = document.getElementById("form-container");
for (let i = 1; i <= 1000; i++) {
addFormItem(i, formContainer);
}
makeConnections();
});
const offset = 50; // Define your offset value
function connectElements(elementId, targetElementId, secondConnection = false) {
const svg = document.getElementById("container");
const targetElement = document.getElementById(targetElementId);
const element = document.getElementById(elementId);
if (targetElement && element) {
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
const current = element.getBoundingClientRect();
const target = targetElement.getBoundingClientRect();
const currentX = secondConnection
? current.left + current.width / 2 + offset
: current.left + current.width / 2 - offset;
const currentY = current.top + current.height / 2;
const targetX = target.left + target.width / 2;
const targetY = target.top + target.height / 2;
const isVerticalAlignment =
target.left >= current.left && target.right <= current.right;
if (isVerticalAlignment) {
const x1 = targetX;
const y1 = currentY;
const x2 = targetX;
const y2 = targetY > currentY ? target.top : target.bottom;
drawLine(
svg,
line,
x1,
y1 + current.height / 2.5,
x2,
y2 - current.height / 4,
true
);
} else {
const verticalX = currentX;
const verticalY1 = Math.min(currentY, targetY) + current.height / 2.5;
const verticalY2 =
Math.max(currentY, targetY) - target.height / 2 - offset;
const horizontalX1 = currentX;
const horizontalX2 =
targetX > currentX
? target.left + target.width / 2
: target.right - target.width / 2;
const horizontalY = targetY - target.height / 2 - offset;
const verticalLine = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
const horizontalLine = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
const verticalLine2 = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
drawLine(
svg,
verticalLine,
verticalX,
verticalY1,
verticalX,
verticalY2,
false
);
drawLine(
svg,
horizontalLine,
horizontalX1,
horizontalY,
horizontalX2,
horizontalY,
false
);
const button = document.createElement("button");
button.textContent = "And/Or";
button.classList.add("joint-button");
document.body.appendChild(button);
button.style.borderRadius = "15%";
button.style.width = "55px";
button.style.height = "30px";
const svgRect = svg.getBoundingClientRect();
button.style.position = "absolute";
button.style.left = `${svgRect.left - 25 + horizontalX2}px`;
button.style.top = `${svgRect.top - 10 + horizontalY}px`;
drawLine(
svg,
verticalLine2,
horizontalX2,
horizontalY,
horizontalX2,
horizontalY + target.height / 4,
true
);
targetElement.classList.add("connected");
}
}
}
function drawLine(svg, line, x1, y1, x2, y2, hasArrowhead) {
line.setAttribute("x1", x1);
line.setAttribute("y1", y1);
line.setAttribute("x2", x2);
line.setAttribute("y2", y2);
line.setAttribute("stroke", "blue");
line.setAttribute("stroke-width", "2");
line.setAttribute("stroke-opacity", "0.3");
if (hasArrowhead) {
line.setAttribute("marker-end", "url(#arrowhead)");
}
svg.appendChild(line);
}
function makeConnections() {
connectElements("form1", "form5");
connectElements("form7", "form11");
const forms = document.querySelectorAll(".form");
let row = 0;
for (let i = 0; i < forms.length; i++) {
const elementId = `form${i}`;
const targetId = `form${i + 5}`;
if (i !== forms.length - 1) {
connectElements(elementId, targetId);
}
if ((i + 1) % 5 === 0) {
row++;
}
}
}
function clearSVG() {
const svg = document.getElementById("container");
while (svg.firstChild) {
svg.removeChild(svg.firstChild);
}
}
function removeJointAllButtons() {
const buttons = document.querySelectorAll(".joint-button");
buttons.forEach((button) => {
button.remove();
});
}
function addFormItem(i, formContainer, munuallyAdded = false) {
const form = document.createElement("form");
form.setAttribute("id", `form${i}`);
form.setAttribute("class", "form");
const span = document.createElement("span");
span.style.width = "calc(100% - 10px)";
span.textContent = `Form - ${i}`;
form.appendChild(span);
const input = document.createElement("input");
input.style.width = "calc(100% - 10px)";
input.setAttribute("type", "text");
form.appendChild(input);
const button = document.createElement("button");
button.type = "button";
button.style.width = "calc(100% - 10px)";
button.textContent = `Add Form and Redraw`;
button.addEventListener("click", onAddFormItem);
form.appendChild(button);
if (munuallyAdded) {
const forms = formContainer.querySelectorAll(".form");
const referenceNode = forms[7].nextSibling;
formContainer.insertBefore(form, referenceNode);
} else {
formContainer.appendChild(form);
}
}
function redrawAll() {
clearSVG();
removeJointAllButtons();
makeConnections();
}
function onAddFormItem() {
itemsCount++;
const formContainer = document.getElementById("form-container");
addFormItem(itemsCount, formContainer, true);
redrawAll();
}
window.addEventListener("resize", redrawAll);