-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionLineGenerator.js
253 lines (224 loc) · 8.62 KB
/
connectionLineGenerator.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
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
const bubbleHeight = 120;
const bubbleWidth = 160;
/**
* @class Line generates svg <path> tag that connects two bubbles and computes its movement based on bubble dragging and dropping.
*/
class Line {
_bubble1;
_bubble2;
_line_html;
_line_id;
constructor(bubble1, bubble2) {
this._bubble1 = bubble1;
this._bubble2 = bubble2;
this._line_id = (this._bubble1.getAttribute('id') + "-" + this._bubble2.getAttribute('id') + "-line").toString();
this._renderLine();
}
_renderLine() {
let x1, x2, y1, y2;
const b1x = parseInt(this._bubble1.getAttribute('cx'));
const b1y = parseInt(this._bubble1.getAttribute('cy'));
const b1rx = parseInt(this._bubble1.getAttribute('rx'));
const b1ry = parseInt(this._bubble1.getAttribute('ry'));
const b2x = parseInt(this._bubble2.getAttribute('cx'));
const b2y = parseInt(this._bubble2.getAttribute('cy'));
const b2rx = parseInt(this._bubble2.getAttribute('rx'));
const b2ry = parseInt(this._bubble2.getAttribute('ry'));
const line = document.getElementById(this._line_id);
if (line)
line.remove();
if (Math.abs(b1x - b2x) <= bubbleWidth && Math.abs(b1y - b2y) <= bubbleHeight) {
console.error(`Invalid bubble possitions to draw line: ${this._bubble1.getAttribute('id')} {x:${b1x}, y:${b1y}},
${this._bubble2.getAttribute('id')} { x:${b2x}, y:${b2y}}!`);
return;
}
//bubble1 is on left
if (b1x <= b2x) {
//bubble1 is on left up
if (b1y < b2y) {
if (Math.abs(b1x - b2x) > 200) {
if (Math.abs(b1y - b2y) > bubbleHeight*2) {
x1 = b1x;
y1 = b1y + b1ry;
x2 = b2x;
y2 = b2y - b2ry;
} else {
x1 = b1x + b1rx;
y1 = b1y;
x2 = b2x - b2rx;
y2 = b2y;
}
} else {
if (Math.abs(b1y - b2y) <= bubbleHeight) {
x1 = b1x + b1rx;
y1 = b1y;
x2 = b2x - b2rx;
y2 = b2y;
} else {
x1 = b1x;
y1 = b1y + b1ry;
x2 = b2x;
y2 = b2y - b2ry;
}
}
} else { //bubble1 is left down
if (Math.abs(b1x - b2x) > 200) {
if (Math.abs(b1y - b2y) > bubbleHeight*2) {
x1 = b1x;
y1 = b1y - b1ry;
x2 = b2x;
y2 = b2y + b2ry;
} else {
x1 = b1x + b1rx;
y1 = b1y;
x2 = b2x - b2rx;
y2 = b2y;
}
} else {
if (Math.abs(b1y - b2y) <= bubbleHeight) {
x1 = b1x + b1rx;
y1 = b1y;
x2 = b2x - b2rx;
y2 = b2y;
} else {
x1 = b1x;
y1 = b1y - b1ry;
x2 = b2x;
y2 = b2y + b2ry;
}
}
}
} else {//bubble is on right
//bubble1 is on right up
if (b1y < b2y) {
if (Math.abs(b1x - b2x) > 200) {
if (Math.abs(b1y - b2y) > bubbleHeight*2) {
x1 = b1x;
y1 = b1y + b1ry;
x2 = b2x;
y2 = b2y - b2ry;
} else {
x1 = b1x - b1rx;
y1 = b1y;
x2 = b2x + b2rx;
y2 = b2y;
}
} else {
if (Math.abs(b1y - b2y) <= bubbleHeight) {
x1 = b1x - b1rx;
y1 = b1y;
x2 = b2x + b2rx;
y2 = b2y;
} else {
x1 = b1x;
y1 = b1y + b1ry;
x2 = b2x;
y2 = b2y - b2ry;
}
}
} else { //bubble1 is on right down
if (Math.abs(b1x - b2x) >= 200) {
if (Math.abs(b1y - b2y) > bubbleHeight*2) {
x1 = b1x;
y1 = b1y - b1ry;
x2 = b2x;
y2 = b2y + b2ry;
} else {
x1 = b1x - b1rx;
y1 = b1y;
x2 = b2x + b2rx;
y2 = b2y;
}
} else {
if (Math.abs(b1y - b2y) < bubbleHeight) {
x1 = b1x - b1rx;
y1 = b1y;
x2 = b2x + b2rx;
y2 = b2y;
} else {
x1 = b1x;
y1 = b1y - b1ry;
x2 = b2x;
y2 = b2y + b2ry;
}
}
}
}
this._line_html = `<line id="${this._line_id}" x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}"
stroke="black" stroke-width="3"/>`;
this._bubble2.element.insertAdjacentHTML('afterend', this._line_html);
}
getHTML() {
return this._line_html;
}
getId() {return this._line_id}
get id() {return this._line_id}
get bubble1() {return this._bubble1}
get bubble2() {return this._bubble2}
}
import { setCounter } from './buttons.js';
/**
* @class LineGenerator handles all logic regarding line management.
* @property _bubbleManager for its information about all bubbles.
*/
class LineGenerator {
_lines;
_bubbleManager;
constructor(bubbleManager) {
this._lines = new Array();
this._bubbleManager = bubbleManager;
}
addLineBetween(bubble1Id, bubble2Id) {
const b1 = this._bubbleManager.find(bubble1Id);
const b2 = this._bubbleManager.find(bubble2Id);
if (b1 == null || b2 == null || b1 == b2) {
console.error(`Invalid bubbles to connect with line. Bubble's IDs: ${bubble1Id}, ${bubble2Id}`);
return;
}
if (!this._lines.find(ln => {
//if bubbles already connected with line
if ((ln.bubble1.id == bubble1Id && ln.bubble2.id == bubble2Id)
|| (ln.bubble1.id == bubble2Id && ln.bubble2.id == bubble1Id)) {
return true;
}
return false;
})) {
const line = new Line(b1, b2);
this._lines.push(line);
}
}
loadSave() {
const bubbleGroups = document.querySelectorAll('g');
bubbleGroups.forEach(group => {
this._bubbleManager.add(group);
});
setCounter(this._bubbleManager.getFirstAvailableId());
bubbleGroups.forEach(group => {
if (group.querySelector('line')) {
const line = group.querySelector('line');
const id1 = line.getAttribute('id').split('-')[0];
const id2 = line.getAttribute('id').split('-')[1];
this.addLineBetween(id1, id2);
}
});
}
render() {
this._lines.forEach(line => line._renderLine());
}
findAllLinesForBubble(bubbleId) {
return this._lines.filter(ln => ln.bubble1.id == bubbleId || ln.bubble2.id == bubbleId);
}
removeLineBetween(bubble1Id, bubble2Id) {
let line = this._lines.find(ln => ((ln.bubble1.id == bubble1Id && ln.bubble2.id == bubble2Id) || (ln.bubble2.id == bubble1Id && ln.bubble1.id == bubble2Id)));
document.getElementById(line.id).remove();
this._lines = this._lines.filter(ln => ln.id != line.id);
}
removeLinesForBubble(bubbleId) {
this.findAllLinesForBubble(bubbleId).forEach(line => document.getElementById(line.id).remove());
this._lines = this._lines.filter(ln => ln.bubble1.id != bubbleId && ln.bubble2.id != bubbleId);
}
get lines() {
return this._lines;
}
}
export function initLineGenerator(bubbleManager) {return new LineGenerator(bubbleManager)};