-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.js
570 lines (464 loc) · 21.5 KB
/
buttons.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
let Canvg;
//check internet connection and imports
import('https://cdn.skypack.dev/canvg')
.then(module => Canvg = module.Canvg)
.catch(err => alert("You have no internet connection. Please connect at least once and refresh the page, to use the application fully (download your mind map)."));
let idCounter = 1;
let actionInProgress = false;
let infoSelected = false;
let exportSelected = false;
export function setCounter(count) { idCounter = count;}
/**
* @class Button has button property and _onClick base method.
* @property _btn_elm is the html button element.
*/
class Button {
_btn_elm;
constructor(buttonId) {
this._btn_elm = document.getElementById(buttonId);
}
_onClick(e) {
e.preventDefault();
}
}
/**
* @class ExportButton handles download and its setting.
* @extends Button for its base setting and preventingDefault.
*/
class ExportButton extends Button {
_export_hmtl_text = `
<section id="export_section">
<select id="export_type" name="export">
<option value="png">png</option>
<option value="jpg">jpg</option>
</select> <button id="download_btn">Download</button>
<section>`;
constructor(buttonId) {
super(buttonId);
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
super._onClick(e);
let popup = document.getElementById("placeholder");
if (popup.innerHTML === "") {
exportSelected = true;
document.querySelectorAll("nav button").forEach(b => b.classList.remove("activeText"));
document.querySelectorAll("button").forEach(b => b.classList.add("lower"));
document.querySelector("aside").classList.add("hiddenUnder");
this._btn_elm.classList.add("activeText");
popup.innerHTML = this._export_hmtl_text;
} else {
if (infoSelected) {
infoSelected = false;
exportSelected = true;
document.querySelectorAll("nav button").forEach(b => b.classList.remove("activeText"));
document.querySelectorAll("button").forEach(b => b.classList.add("lower"));
document.querySelector("aside").classList.add("hiddenUnder");
this._btn_elm.classList.add("activeText");
popup.innerHTML = this._export_hmtl_text;
} else if (exportSelected) {
popup.innerHTML = "";
exportSelected = false;
this._btn_elm.classList.remove("activeText");
document.querySelectorAll("button").forEach(b => b.classList.remove("lower"));
document.querySelector("aside").classList.remove("hiddenUnder");
}
}
const button = document.getElementById("download_btn");
if (button)
button.addEventListener('click', this._handleDownloadButtonClick);
}
_handleDownloadButtonClick = (e) => {
super._onClick(e);
if (!Canvg) {
if (navigator.onLine) {
import('https://cdn.skypack.dev/canvg').then(module => Canvg = module.Canvg).catch(err => console.log(err));
} else
return;
}
//using library canvg to create canvas from svg and then export that canvas to png/jpg.
const canvas = document.createElement('canvas');
canvas.style.visibility = "hidden";
canvas.width = window.screen.availWidth;
canvas.height = window.screen.availHeight;
const ctx = canvas.getContext('2d');
let v = Canvg.fromString(ctx, document.getElementById('svg').outerHTML);
v.start();
const now = new Date();
const offsetMs = now.getTimezoneOffset() * 60 * 1000;
const dateLocal = new Date(now.getTime() - offsetMs);
const str = dateLocal.toISOString().slice(0, 16).replace(/-/g, "/").replace("T", "_");
let img;
let link = document.createElement('a');
if (document.getElementById("export_type").value == "png") {
img = canvas.toDataURL('image/png');
link.download = 'export' + str + '.png';
} else {
img = canvas.toDataURL('image/jpg');
link.download = 'export' + str + '.jpg';
}
const i = document.createElement('img');
i.classList.add("hidden");
i.setAttribute('src', img );
document.querySelector("body").appendChild(i);
link.href = document.querySelector("img").src;
link.click();
document.querySelector('img').remove();
}
}
export function initExportButton(buttonId) {return new ExportButton(buttonId)}
/**
* @class InfoButton shows info paragraph when clicked and hides it when clicked again.
* @extends Button for its base setting and preventingDefault.
*/
class InfoButton extends Button {
_info_hmtl_text = `<p id="info">Mind map editor - KAJ is a client javascript application that is
used for mind map modelling by adding, naming and connecting 'bubbles'. This project was
created by Vojtěch Luňák for KAJ.<br><br>
<i><u>usage:</u> click button with given funcionality, then select base bubble and then final bubble (if possible), or click somewhere else in the editor to reset;
<br>
to add text to bubble, double click it to show input field in the left menu; there you write your text and submit it with the Set text button
<br>
click save button to save your current mind map; when you come back later or refresh the page, you will see your last local save
</i></p>`;
constructor(buttonId) {
super(buttonId);
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
e.preventDefault();
let popup = document.getElementById("placeholder");
if (popup.innerHTML === "") {
infoSelected = true;
popup.innerHTML = this._info_hmtl_text;
document.querySelectorAll("nav button").forEach(b => b.classList.remove("activeText"));
document.querySelectorAll("button").forEach(b => b.classList.add("lower"));
document.querySelector("aside").classList.add("hiddenUnder");
document.getElementById("svg_container").classList.add("hiddenUnder");
document.querySelectorAll("button").forEach(b => b.classList.remove("middle"));
this._btn_elm.classList.add("activeText");
} else {
if (exportSelected) {
exportSelected = false;
popup.innerHTML = this._info_hmtl_text;
infoSelected = true;
document.querySelectorAll("nav button").forEach(b => b.classList.remove("activeText"));
document.querySelectorAll("button").forEach(b => b.classList.add("lower"));
document.querySelectorAll("button").forEach(b => b.classList.remove("middle"));
document.querySelector("aside").classList.add("hiddenUnder");
document.getElementById("svg_container").classList.add("hiddenUnder");
this._btn_elm.classList.add("activeText");
}
else if (infoSelected) {
popup.innerHTML = "";
infoSelected = false;
this._btn_elm.classList.remove("activeText");
document.querySelectorAll("button").forEach(b => b.classList.remove("middle"));
document.querySelectorAll("button").forEach(b => b.classList.remove("lower"));
document.getElementById("svg_container").classList.remove("hiddenUnder");
document.querySelector("aside").classList.remove("hiddenUnder");
}
}
}
}
export function initInfoButton(buttonId) {return new InfoButton(buttonId)};
/**
* @class BubbleManagingButton has basic funcionalities and properties for working with bubbles.
* @extends Button for its base setting and preventingDefault.
*/
class BubbleManagingButton extends Button {
_svg_elm;
_bubbleManager;
constructor(buttonId, bubbleManager) {
super(buttonId);
this._bubbleManager = bubbleManager;
this._svg_elm = document.getElementById("svg");
}
_onClick = e => {
e.preventDefault();
this._bubbleManager.unselectAll();
}
}
/**
* @class AddBubbleButton handles bubble adding, generating html into svg tag.
* @extends BubbleManagingButton because it needs the access to bubble "database".
*/
class AddBubbleButton extends BubbleManagingButton {
constructor(buttonId, bubbleManager) {
super(buttonId, bubbleManager);
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
super._onClick(e);
const nextBubble = this._getNextBubbleHTML();
this._svg_elm.insertAdjacentHTML('beforeend', nextBubble);
this._bubbleManager.add(this._svg_elm.lastChild);
idCounter++;
}
_getNextBubbleHTML() {
return `
<g>
<ellipse id="bub${idCounter}" class="draggable" cx="${100 + Math.floor(Math.random() * 400)}" cy="${350 + Math.floor(Math.random() * 150)}" rx="80" ry="60" stroke="black" fill="transparent" stroke-width="4"></ellipse>
<text id="text_bub${idCounter}" x="240" y="160" font-family="Verdana" font-size="20" fill="black"></text>
</g>`;
}
}
export function initAddBubbleButton(buttonId, bubbleManager) {return new AddBubbleButton(buttonId, bubbleManager)};
/**
* @class RemoveBubbleButton represents all interactivity and logic for removing one bubble with additional checks for line removal when necessary.
* @extends BubbleManagingButton because it needs the access to bubble "database".
* @constructor Takes also LineGenerator as argument, in order to execute line logic.
*/
class RemoveBubbleButton extends BubbleManagingButton {
_lineGenerator;
constructor(buttonId, lineGenerator, bubbleManager) {
super(buttonId, bubbleManager);
this._lineGenerator = lineGenerator;
this._btn_elm.addEventListener('click', this._onClick);
this._svg_elm.addEventListener('dblclick', this._resetView);
this._svg_elm.addEventListener('click', this._resetView);
}
_onClick = e => {
super._onClick(e);
e.stopPropagation();
if (actionInProgress) {
return;
}
actionInProgress = true;
let bubbles = this._bubbleManager.get();
bubbles.forEach(b => {
b.setAttribute('opacity', 0.4);
b.setAttribute('fill', 'red');
b.element.addEventListener('click', this._clickOnBubble);
});
}
_resetView = e => {
e.preventDefault();
console.log("reset remove");
actionInProgress = false;
this._bubbleManager.removeEventListenerForAllBubbles('click', this._clickOnBubble);
}
_clickOnBubble = e => {
e.preventDefault();
e.stopPropagation();
const bubbleToRemove = this._bubbleManager.find(e.target.getAttribute('id'));
this._lineGenerator.removeLinesForBubble(bubbleToRemove.id);
this._bubbleManager.remove(bubbleToRemove.id);
//idCounter--;
this._bubbleManager.removeEventListenerForAllBubbles('click', this._clickOnBubble);
actionInProgress = false;
}
}
export function initRemoveBubbleButton(buttonId, lineGenerator, bubbleManager) {return new RemoveBubbleButton(buttonId, lineGenerator, bubbleManager)};
/**
* @class RemoveAllBubblesButton represents all interactivity and logic for removing all bubbles from the svg plane.
* @extends BubbleManagingButton because it needs the access to bubble "database".
* @constructor Takes also LineGenerator as argument, in order to execute line logic.
*/
class RemoveAllBubblesButton extends BubbleManagingButton {
_lineGenerator;
constructor(buttonId, lineGenerator, bubbleManager) {
super(buttonId, bubbleManager);
this._lineGenerator = lineGenerator;
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
super._onClick(e);
this._bubbleManager.get().forEach(bubble => {
this._lineGenerator.removeLinesForBubble(bubble.id);
this._bubbleManager.remove(bubble.id);
})
// idCounter = 1;
}
}
export function initRemoveAllBubblesButton(buttonId, lineGenerator, bubbleManager) {return new RemoveAllBubblesButton(buttonId, lineGenerator, bubbleManager)};
/**
* @class SetTextButton represents all interactivity and logic for adding/modifying text to bubble.
* @extends BubbleManagingButton because it needs the access to bubble "database".
*/
class SetTextButton extends BubbleManagingButton {
constructor(buttonId, bubbleManager) {
super(buttonId, bubbleManager);
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
super._onClick(e);
const selectedBubble = this._bubbleManager.getSelectedBubble();
console.log(selectedBubble);
if (!selectedBubble) {
alert("Select bubble by doubleclick before trying to add text.");
return;
}
const textInput = document.getElementById("text_input");
selectedBubble.setText(textInput.value);
selectedBubble.unselect(selectedBubble.id);
}
}
export function initSetTextButton(buttonId, bubbleManager) {return new SetTextButton(buttonId, bubbleManager);}
/**
* @class CreateLineButton represents all interactivity a logic for creating line between two bubbles.
* @extends BubbleManagingButton because it needs the access to bubble "database".
* @constructor Takes also LineGenerator as argument, in order to execute line logic.
*/
class CreateLineButton extends BubbleManagingButton {
_lineGenerator;
_firstSelectedBubble;
constructor(buttonId, lineGenerator, bubbleManager) {
super(buttonId, bubbleManager);
this._lineGenerator = lineGenerator;
this._btn_elm.addEventListener('click', this._onClick);
this._svg_elm.addEventListener('dblclick', this._resetView);
this._svg_elm.addEventListener('click', this._resetView);
}
_onClick = e => {
super._onClick(e);
if (actionInProgress) {
return;
}
actionInProgress = true;
let bubbles = this._bubbleManager.get();
bubbles.forEach(b => {
b.setAttribute('opacity', 0.4);
b.setAttribute('fill', 'green');
b.element.addEventListener('click', this._firstClickOnBubble);
});
}
_resetView = e => {
e.preventDefault();
console.log("reset create");
actionInProgress = false;
this._bubbleManager.removeEventListenerForAllBubbles('click', this._firstClickOnBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', this._secondClickOnAnotherBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', playError);
}
_firstClickOnBubble = e => {
e.preventDefault();
this._firstSelectedBubble = this._bubbleManager.find(e.target.getAttribute('id'));
let unavailableLines = this._lineGenerator.lines.filter(line => {
if (line.bubble1.id == this._firstSelectedBubble.id || line.bubble2.id == this._firstSelectedBubble.id) {
return true;
}
return false;
})
let unaviableBubblesToConnectTo = new Array();
unavailableLines.forEach(l => unaviableBubblesToConnectTo.push(l.bubble1, l.bubble2));
unaviableBubblesToConnectTo.forEach(b => b.element.addEventListener('click', playError));
let allViableBubbles = this._bubbleManager.get();
allViableBubbles = allViableBubbles.filter(b => {
if (b.id != this._firstSelectedBubble.id && !unaviableBubblesToConnectTo.find(bub => bub.id == b.id))
return true;
return false;
});
this._bubbleManager.removeEventListenerForAllBubbles('click', this._firstClickOnBubble);
if (allViableBubbles.length == 0) {
playError(e)
this._resetView(e);
return;
}
allViableBubbles.forEach(b => {
b.setAttribute('opacity', 0.55);
b.setAttribute('fill', 'cornflowerblue');
b.element.addEventListener('click', this._secondClickOnAnotherBubble);
});
}
_secondClickOnAnotherBubble = e => {
e.preventDefault();
actionInProgress = false;
this._lineGenerator.addLineBetween(this._firstSelectedBubble.id, e.target.getAttribute('id'));
this._bubbleManager.removeEventListenerForAllBubbles('click', this._secondClickOnAnotherBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', playError);
this._bubbleManager.recolorize();
}
}
export function initCreateLineButton(buttonId, lineGenerator, bubbleManager) {return new CreateLineButton(buttonId, lineGenerator, bubbleManager)}
/**
* @class DeleteLineButton represents all interactivity a logic for deleting line between two bubbles.
* @extends BubbleManagingButton because it needs the access to bubble "database".
* @constructor Takes also LineGenerator as argument, in order to execute line logic.
*/
class DeleteLineButton extends BubbleManagingButton {
_lineGenerator;
_firstSelectedBubble;
constructor(buttonId, lineGenerator, bubbleManager) {
super(buttonId, bubbleManager);
this._lineGenerator = lineGenerator;
this._btn_elm.addEventListener('click', this._onClick);
this._svg_elm.addEventListener('dblclick', this._resetView);
this._svg_elm.addEventListener('click', this._resetView);
}
_resetView = e => {
e.preventDefault();
console.log("reset delete line");
actionInProgress = false;
this._bubbleManager.removeEventListenerForAllBubbles('click', this._firstClickOnBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', this._secondClickOnAnotherBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', playError);
}
_onClick = e => {
super._onClick(e);
if (actionInProgress || this._lineGenerator.lines.length == 0) {
return;
}
actionInProgress = true;
this._lineGenerator.lines.forEach(ln => {
ln.bubble1.setAttribute('opacity', 0.45);
ln.bubble2.setAttribute('opacity', 0.45);
ln.bubble1.setAttribute('fill', 'grey');
ln.bubble2.setAttribute('fill', 'grey');
ln.bubble1.element.removeEventListener('click', this._firstClickOnBubble);
ln.bubble2.element.removeEventListener('click', this._firstClickOnBubble);
ln.bubble1.element.addEventListener('click', this._firstClickOnBubble);
ln.bubble2.element.addEventListener('click', this._firstClickOnBubble);
});
}
_firstClickOnBubble = e => {
e.preventDefault();
this._firstSelectedBubble = this._bubbleManager.find(e.target.getAttribute('id'));
this._firstSelectedBubble.element.addEventListener('click', playError);
const allLinesForSelectedBubble = this._lineGenerator.findAllLinesForBubble(this._firstSelectedBubble.id);
this._bubbleManager.removeEventListenerForAllBubbles('click', this._firstClickOnBubble);
const allViableBubblesToDelete = new Array();
allLinesForSelectedBubble.forEach(l => allViableBubblesToDelete.push(l.bubble1) && allViableBubblesToDelete.push(l.bubble2));
const unconnectedBubbles = this._bubbleManager.get().filter(b => allViableBubblesToDelete.filter(viableBub => b.id === viableBub.id).length === 0);
unconnectedBubbles.forEach(b => b.element.addEventListener('click', playError));
allLinesForSelectedBubble.forEach(line => {
if (line.bubble1.id == this._firstSelectedBubble.id) {
line.bubble2.setAttribute('opacity', 0.6);
line.bubble2.setAttribute('fill', 'black');
line.bubble2.element.addEventListener('click', this._secondClickOnAnotherBubble);
} else {
line.bubble1.setAttribute('opacity', 0.6);
line.bubble1.setAttribute('fill', 'black');
line.bubble1.element.addEventListener('click', this._secondClickOnAnotherBubble);
}
});
}
_secondClickOnAnotherBubble = e => {
e.preventDefault();
actionInProgress = false;
this._lineGenerator.removeLineBetween(this._firstSelectedBubble.id, e.target.getAttribute('id'));
this._bubbleManager.removeEventListenerForAllBubbles('click', this._secondClickOnAnotherBubble);
this._bubbleManager.removeEventListenerForAllBubbles('click', playError);
}
}
const playError = (e) => {
e.preventDefault();
new Audio("error.mp3").play();
}
export function initDeleteLineButton(buttonId, lineGenerator, bubbleManager) {return new DeleteLineButton(buttonId, lineGenerator, bubbleManager)};
/**
* @class SaveButton represents all interactivity a logic for saving svg innerHTML to local storage for later loading.
*/
class SaveButton extends Button {
constructor(buttonId) {
super(buttonId);
this._btn_elm.addEventListener('click', this._onClick);
}
_onClick = e => {
super._onClick(e);
localStorage.removeItem("save");
localStorage.setItem("save", document.getElementById("svg").innerHTML);
console.log("saved mind map");
}
}
export function initSaveButton(buttonId) {return new SaveButton(buttonId)};