-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
322 lines (283 loc) · 12.7 KB
/
index.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
position: relative;
width: 100vw;
height: 100vh;
}
.node {
position: absolute;
width: 80px;
height: 80px;
border-radius: 50%;
border: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
background: white;
cursor: move;
font-size: 14px;
text-align: center;
}
.node-input {
width: 40px;
text-align: center;
position: absolute;
bottom: -25px;
left: 50%;
transform: translateX(-50%);
border: 1px solid #ccc;
}
#controls {
border: 1px solid #ccc;
padding: 10px;
}
#arrowLayer {
width: 100%;
height: 100%;
pointer-events: none;
}
.numcontrol {
width: 28px;
text-align: center;
}
.textcontrol {
width: min-content;
}
</style>
<body>
<div id="controls">
<label>Number of Tasks:
<input type="number" class="numcontrol" id="taskcount" min="1" value="2">
</label>
<button onclick="GenerateTasks()">Generate Tasks</button>
<br><br>
<label>Connect Tasks (format: "A->B,C;B->D,C"):
<input onkeydown="if (event.keyCode == 13) AddConnections()" type="text" class="textcontrol" id="connection-input" placeholder="A->B,C;B->D,C">
</label>
<button onclick="AddConnections()">Add Connections</button>
<button onclick="ClearConnections()">Clear Connections</button>
</div>
<div id="container" class="container">
<svg id="arrowLayer"></svg>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/3.1.2/svg.min.js"></script>
<script>
let nodes = [];
let connections = [];
let isDragging = false;
let offset = { x: 0, y: 0 };
let draw;
function GenerateTasks() {
// Clearing existing tasks and connections
const container = document.getElementById('container');
container.innerHTML = '<svg id="arrowLayer"></svg>';
nodes = [];
connections = [];
// Initializing draw
draw = SVG().addTo('#arrowLayer').size('100%', '100%');
const count = document.getElementById('taskcount').value;
for (let i = 1; i <= count; i++) {
GenNode(i);
}
}
function GenNode(id) {
const node = document.createElement('div');
node.className = 'node';
node.id = String(id);
// Task letter as node text
const taskLetter = ConvertTaskNumberToLetter(id);
node.textContent = taskLetter;
// Duration input
const durInput = document.createElement('input');
durInput.type = 'text';
durInput.className = 'node-input';
durInput.id = `dur-${id}`;
durInput.placeholder = 'Dur';
node.appendChild(durInput);
// Setting the initial position of the nodes to the top-left corner
node.style.top = `${50 + (id-1) * 120}px`;
node.style.left = '10px';
setupDraggable(node);
container.appendChild(node);
nodes[id] = id;
}
function ConvertTaskNumberToLetter(taskNumber) {
return String.fromCharCode(64 + taskNumber);
}
function setupDraggable(element) {
element.addEventListener('mousedown', (e) => {
if (e.target.tagName.toLowerCase() !== 'input') {
isDragging = true;
currentNode = element;
offset.x = e.clientX - element.offsetLeft;
offset.y = e.clientY - element.offsetTop;
}
});
document.addEventListener('mousemove', (e) => {
if (isDragging && currentNode) {
currentNode.style.left = `${e.clientX - offset.x}px`;
currentNode.style.top = `${e.clientY - offset.y}px`;
UpdateArrows();
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
currentNode = null;
});
}
function AddConnections() {
connections = [];
const input = document.getElementById('connection-input').value;
const pairs = input.split(';');
pairs.forEach(pair => {
const [source, targets] = pair.split('->');
if (targets) {
targets.split(',').forEach(target => {
connections.push({
from: ConvertTaskLetterToNumber(source.trim()),
to: ConvertTaskLetterToNumber(target.trim())
});
});
}
});
UpdateArrows();
}
function ConvertTaskLetterToNumber(taskLetter) {
const uppercaseLetter = taskLetter.toUpperCase();
const charCode = uppercaseLetter.charCodeAt(0);
return charCode - 64;
}
function UpdateArrows() {
// Clearing existing content
draw.clear();
const PADDING = 5;
const CONNECTION_SPACING = 15;
// Counting connections from and to nodes
const fromConnectionCounts = {};
const toConnectionCounts = {};
const fromNodeConnections = {};
const toNodeConnections = {};
connections.forEach(conn => {
fromConnectionCounts[conn.from] = (fromConnectionCounts[conn.from] || 0) + 1;
toConnectionCounts[conn.to] = (toConnectionCounts[conn.to] || 0) + 1;
if (!fromNodeConnections[conn.from]) {
fromNodeConnections[conn.from] = 0;
}
if (!toNodeConnections[conn.to]) {
toNodeConnections[conn.to] = 0;
}
});
connections.forEach(conn => {
const fromNode = document.getElementById(String(conn.from));
const toNode = document.getElementById(String(conn.to));
if (fromNode && toNode) {
const fromIndex = fromNodeConnections[conn.from]++;
const toIndex = toNodeConnections[conn.to]++;
const totalFromConnections = fromConnectionCounts[conn.from];
const totalToConnections = toConnectionCounts[conn.to];
// Calculating centers
const fromCenter = {
x: fromNode.offsetLeft + (fromNode.offsetWidth / 2),
y: fromNode.offsetTop + (fromNode.offsetHeight / 2)
};
const toCenter = {
x: toNode.offsetLeft + (toNode.offsetWidth / 2),
y: toNode.offsetTop + (toNode.offsetHeight / 2)
};
// Determining directions
const horizontalDistance = Math.abs(fromCenter.x - toCenter.x);
const verticalDistance = Math.abs(fromCenter.y - toCenter.y);
const isHorizontal = horizontalDistance > verticalDistance;
// Calculating offsets
const fromOffset = CONNECTION_SPACING * (fromIndex - (totalFromConnections - 1) / 2);
const toOffset = CONNECTION_SPACING * (toIndex - (totalToConnections - 1) / 2);
const points = { from: { x: 0, y: 0 }, to: { x: 0, y: 0 } };
if (isHorizontal) {
if (fromCenter.x < toCenter.x) {
points.from.x = fromNode.offsetLeft + fromNode.offsetWidth;
points.to.x = toNode.offsetLeft;
} else {
points.from.x = fromNode.offsetLeft;
points.to.x = toNode.offsetLeft + toNode.offsetWidth;
}
points.from.y = fromCenter.y + fromOffset;
points.to.y = toCenter.y + toOffset;
} else {
if (fromCenter.y < toCenter.y) {
points.from.y = fromNode.offsetTop + fromNode.offsetHeight;
points.to.y = toNode.offsetTop;
} else {
points.from.y = fromNode.offsetTop;
points.to.y = toNode.offsetTop + toNode.offsetHeight;
}
points.from.x = fromCenter.x + fromOffset;
points.to.x = toCenter.x + toOffset;
}
const midX = points.from.x + (points.to.x - points.from.x) / 2;
// Drawing Arrows
const arrowSize = 10;
const offsetFromEdge = arrowSize;
let arrowPoints;
let pathEndPoint;
if (isHorizontal) {
if (fromCenter.x < toCenter.x) {
// Arrow pointing right
arrowPoints = [
[points.to.x - arrowSize - offsetFromEdge, points.to.y - arrowSize/2],
[points.to.x - offsetFromEdge, points.to.y],
[points.to.x - arrowSize - offsetFromEdge, points.to.y + arrowSize/2]
];
pathEndPoint = [points.to.x - arrowSize - offsetFromEdge, points.to.y];
} else {
// Arrow pointing left
arrowPoints = [
[points.to.x + arrowSize + offsetFromEdge, points.to.y - arrowSize/2],
[points.to.x + offsetFromEdge, points.to.y],
[points.to.x + arrowSize + offsetFromEdge, points.to.y + arrowSize/2]
];
pathEndPoint = [points.to.x + arrowSize + offsetFromEdge, points.to.y];
}
} else {
if (fromCenter.y < toCenter.y) {
// Arrow pointing down
arrowPoints = [
[points.to.x - arrowSize/2, points.to.y - arrowSize - offsetFromEdge],
[points.to.x, points.to.y - offsetFromEdge],
[points.to.x + arrowSize/2, points.to.y - arrowSize - offsetFromEdge]
];
pathEndPoint = [points.to.x, points.to.y - arrowSize - offsetFromEdge];
} else {
// Arrow pointing up
arrowPoints = [
[points.to.x - arrowSize/2, points.to.y + arrowSize + offsetFromEdge],
[points.to.x, points.to.y + offsetFromEdge],
[points.to.x + arrowSize/2, points.to.y + arrowSize + offsetFromEdge]
];
pathEndPoint = [points.to.x, points.to.y + arrowSize + offsetFromEdge];
}
}
// Drawing arrowheads
draw.polygon(arrowPoints).fill('#333');
// Drawing the path
draw.path(
`M ${points.from.x} ${points.from.y}
${isHorizontal
? `L ${midX} ${points.from.y}
L ${midX} ${pathEndPoint[1]}`
: `L ${points.from.x} ${(points.from.y + pathEndPoint[1]) / 2}
L ${pathEndPoint[0]} ${(points.from.y + pathEndPoint[1]) / 2}`
}
L ${pathEndPoint[0]} ${pathEndPoint[1]}`
).fill('none').stroke({ width: 1, color: '#333' });
}
});
}
function ClearConnections() {
connections = [];
UpdateArrows();
}
</script>
</body>
</html>