libraries/dynamicDivModule.js (view raw)
1class DynamicDiv {
2 constructor (id) {
3 this.subElements = [];
4 this.visible = true;
5
6 this.div = document.createElement("div");
7 this.div.classList.add("dynamicDiv");
8 if (document.getElementById(id)) {
9 document.getElementById(id).appendChild(this.div);
10 }
11 }
12
13 addDiv () {
14 let localDiv = document.createElement("div");
15 this.subElements.push(localDiv);
16 this.div.appendChild(localDiv);
17 return localDiv;
18 }
19
20 addText (innerHTML="") {
21 let localP = document.createElement("p");
22 localP.innerHTML = innerHTML;
23 this.subElements.push(localP);
24 this.div.appendChild(localP);
25 return localP;
26 }
27
28 addButton (innerHTML="") {
29 let localButton = document.createElement("button");
30 localButton.innerHTML = innerHTML;
31 this.subElements.push(localButton);
32 this.div.appendChild(localButton);
33 localButton.classList.add('dynamicButton')
34 return localButton;
35 }
36
37 addTextbox (value="") {
38 let localBox = document.createElement("input");
39 localBox.type="text";
40 localBox.value= value;
41 localBox.classList.add('textbox');
42 this.subElements.push(localBox);
43 this.div.appendChild(localBox);
44 return localBox;
45 }
46
47 addSlider (value=0) {
48 let localSlider = document.createElement("input");
49 localSlider.type = "range";
50 localSlider.min = 0;
51 localSlider.max = 50;
52 localSlider.value = 0;
53 this.subElements.push(localSlider);
54 this.div.appendChild(localSlider);
55 return localSlider
56 }
57
58 hide () {
59 this.visible = false;
60 this.div.style.display = "none";
61 }
62
63 show () {
64 this.visible = true;
65 this.div.style.display = "block";
66 }
67
68 toggleVisible () {
69 if (this.visible) {
70 this.hide()
71 } else {
72 this.show()
73 }
74 }
75}
76
77class SymbolHandle extends DynamicDiv {
78 constructor (circle, id, notes) {
79 super("symbolMenu");
80 this.namedSubElements = {};
81 this.namedSubElements.circle = circle;
82 this.id = id;
83 this.tokenImage = null;
84 this.tokenHandle = null;
85
86 this.addTokenHandle(this.namedSubElements.circle.circleColor,this.namedSubElements.circle.circleType);
87
88
89 //Currently the width of the Symbol manager is 18em and the height is 2.7em
90 //1% of width corresponds to 6.67% of height
91 let labelbox = this.addTextbox();
92 labelbox.style.position = "absolute";
93 labelbox.style.left = "2.5em";
94 labelbox.style.width = "2.2em";
95 labelbox.style.top = ".1em"
96 labelbox.oninput = this.labelChangeListener;
97 labelbox.value = this.namedSubElements.circle.circleText;
98 this.namedSubElements.labelbox = labelbox;
99
100 let label = this.addText('Label');
101 label.style.position = "absolute";
102 label.style.left = "2.5em";
103 label.style.width = "2.2em";
104 label.style.top = "2em"
105 label.style.color = "rgb(130,130,130)";
106 label.style.textAlign = "center";
107 label.style.fontSize = "small";
108 this.namedSubElements.label = label;
109
110 let notesBox = this.addTextbox();
111 notesBox.style.position = "absolute";
112 notesBox.style.left = "5.5em";
113 notesBox.style.width = "12em";
114
115 notesBox.style.top = ".1em"
116 notesBox.value = notes;
117 this.namedSubElements.notesBox = notesBox;
118
119 let notesLabel = this.addText('Notes');
120 notesLabel.style.position = "absolute";
121 notesLabel.style.left = "5.5em";
122 notesLabel.style.width = "12em";
123 notesLabel.style.top = "2em";
124 notesLabel.style.color = "rgb(130,130,130)";
125 notesLabel.style.textAlign = "center";
126 notesLabel.style.fontSize = "small";
127 this.namedSubElements.notesLabel = notesLabel;
128
129 let button = this.addButton("");
130 button.classList.add('deleteButton');
131 button.id = this.id;
132 button.onclick = this.removeFromButton;
133 this.namedSubElements.button = button;
134 }
135
136 addTokenHandle(color='blue',type='circle') {
137 let circle = this.addDiv();
138 if (type === 'circle') {
139 circle.classList.add("circleHandle");
140 circle.style.backgroundColor = color;
141 } else if (type === 'square') {
142 circle.classList.add("squareHandle");
143 circle.style.backgroundColor = color;
144 } else if (type === 'cone') {
145 circle.classList.add("coneHandle");
146 circle.style.borderRight = `${1.25}em solid ${color}`;
147 } else if (type === 'custom') {
148 circle.classList.add("squareHandle")
149 this.tokenImage = new Image();
150 this.tokenImage.id = this.id;
151 circle.appendChild(this.tokenImage);
152 } else if (type === 'customCircle') {
153 circle.classList.add("circleHandle")
154 circle.classList.add("centering") //centers the token in the div, so that it gets cutoff as expected
155 this.tokenImage = new Image();
156 this.tokenImage.id = this.id;
157 circle.appendChild(this.tokenImage);
158 } else {
159 console.log('Circle type not recognized for dynamic div handle! '+this.namedSubElements.circle.circleType )
160 }
161 this.tokenHandle = circle;
162 }
163
164 redraw () {
165 //Redraws the circle, updating the text to match
166 let updatedText = this.namedSubElements.labelbox.value;
167 if (!updatedText) {
168 updatedText = "";
169 } else if (updatedText.length > 2) {
170 updatedText = updatedText.slice(0,2);
171 }
172 this.namedSubElements.circle.circleText = updatedText;
173 this.namedSubElements.circle.redraw();
174 }
175
176 labelChangeListener () {
177 redrawCircles();
178 postLabels();
179 }
180
181 removeFromButton () {
182 // Hacky solution to getting this object since .this refers to the button pressed
183 let thisSymbolHandle = globs.circleHandles[this.id]
184 thisSymbolHandle.removeCircle();
185 thisSymbolHandle.removeHandle();
186 thisSymbolHandle.removeFromMasterList();
187 broadcastChannel.postMessage(['removeCircle',this.id]);
188 }
189
190 removeCircle () {
191 this.namedSubElements.circle.remove();
192 }
193
194 removeHandle () {
195 this.div.remove();
196 }
197
198 removeFromMasterList () {
199 globs.circleHandles[this.id] = null;
200 }
201
202 updateLocation (x,y) {
203 this.namedSubElements.circle.updateLocation(x,y);
204 }
205
206 updateRotation (rot) {
207 this.namedSubElements.circle.updateRotation(rot);
208 }
209
210 processCustomImageLoad () {
211 if (this.namedSubElements.circle.needsRedraw) {
212 this.namedSubElements.circle.redraw();
213 }
214
215 function loadImageToken() {
216 circleHandle = globs.circleHandles[this.id];
217 let divHeight = circleHandle.tokenHandle.clientHeight;
218 let divWidth = circleHandle.tokenHandle.clientWidth;
219
220 this.height = divHeight;
221 this.width = divWidth;
222 }
223 this.tokenImage.src = URL.createObjectURL(this.namedSubElements.circle.imageFile);
224 this.tokenImage.onload = loadImageToken;
225 }
226
227 manuallyAppendImageFile (file) {
228 this.namedSubElements.circle.manuallyAppendImageFile(file);
229 }
230}
231
232class MusicHandle extends DynamicDiv {
233 constructor (musicObject, id) {
234 super("musicMenu");
235 this.namedSubElements = {};
236 this.file = musicObject.file;
237 this.name = musicObject.name;
238 this.playlist = musicObject.playlist;
239 this.audioObject = null;
240 this.id = id;
241 this.currentTime = 0.;
242 this.active = false;
243 this.duration = null;
244
245 if (this.name.length > 20) {
246 this.shortName = this.name.slice(0,17)+'...';
247 } else {
248 this.shortName = this.name;
249 }
250
251 this.div.classList.add("indented");
252 this.div.style.height = "3.5em";
253
254 let playButton = this.addButton("");
255 playButton.classList.add('playButton');
256 playButton.id = this.id;
257 playButton.onclick = this.startMusicFromButton;
258 this.namedSubElements.playButton = playButton;
259
260 let pauseButton = this.addButton("")
261 pauseButton.classList.add('pauseButton');
262 pauseButton.onclick = this.stopMusicFromButton;
263 pauseButton.id = this.id;
264 this.namedSubElements.pauseButton = pauseButton;
265
266 let songName = this.addText(this.shortName);
267 songName.classList.add("songName");
268 this.namedSubElements.songName = songName;
269
270 let songSlider = this.addSlider();
271 songSlider.classList.add('songProgressBar');
272 songSlider.id = this.id;
273 songSlider.onchange = this.updateSongTimeFromSlider;
274 songSlider.oninput = this.activelyMovingSlider;
275 songSlider.value = this.currentTime;
276 this.namedSubElements.songSlider = songSlider;
277
278 let songCurrentPosition = this.addText('0:00');
279 songCurrentPosition.classList.add('songCurrentPosition');
280 this.namedSubElements.songCurrentPosition = songCurrentPosition;
281
282 let songTotalLength = this.addText('0:00');
283 songTotalLength.classList.add('songTotalLength');
284 this.namedSubElements.songTotalLength = songTotalLength;
285
286 this.hide();
287 }
288
289 startMusic(inputFromSlider=false,cancelActivePlaylists=true) {
290 if (globs.musicManager.activeMusicHandle) {
291 globs.musicManager.activeMusicHandle.stopMusic()
292 }
293 if (cancelActivePlaylists && globs.musicManager.activePlaylistHandle) {
294 globs.musicManager.activePlaylistHandle.stop()
295 }
296 //If you play a song that's already at it's end, reset to it's beginning
297 if (this.duration && !inputFromSlider) {
298 if (2. > (this.duration-this.currentTime)) {
299 this.updateSongTime(0)
300 }
301 }
302 globs.musicManager.activeMusicHandle = this;
303
304 globs.musicManager.activeAudio.src = URL.createObjectURL(this.file);
305 globs.musicManager.activeAudio.load()
306 globs.musicManager.activeAudio.currentTime = this.currentTime;
307
308 this.active = true
309 this.div.style.backgroundColor = "rgba(50,100,205,.1)";
310 globs.musicManager.activeAudio.play();
311 }
312
313 startMusicFromButton(event) {
314 let musicHandle = globs.musicManager.musicHandles[this.id];
315 musicHandle.startMusic();
316 }
317
318 stopMusic() {
319 if (this.active) {
320 this.div.style.backgroundColor = "rgb(240,240,240)";
321 this.active = false
322 globs.musicManager.activeAudio.pause();
323 if (globs.musicManager.activePlaylistHandle) {
324 globs.musicManager.activePlaylistHandle.stop()
325 }
326 }
327 }
328 stopMusicFromButton(event) {
329 let musicHandle = globs.musicManager.musicHandles[this.id];
330 musicHandle.stopMusic();
331 }
332
333 updateSongTime(seconds) {
334 //Sets the slider to the given value. Value is seconds as a number
335 //Also sets the time object of this music id to match
336 this.namedSubElements.songSlider.value = seconds;
337 this.currentTime = seconds;
338 this.updateCurrentTimeLabel(seconds);
339 }
340
341 updateDuration(seconds) {
342 this.namedSubElements.songTotalLength.innerHTML = convertSecondsToMinutes(seconds);
343 this.namedSubElements.songSlider.max = seconds;
344 this.duration = seconds;
345 }
346
347 updateSongTimeFromSlider(event) {
348 //When the slider changes, set the current music time to match
349 let musicHandle = globs.musicManager.musicHandles[this.id];
350 let sliderValue = musicHandle.namedSubElements.songSlider.value;
351
352 musicHandle.currentTime = sliderValue;
353 musicHandle.updateCurrentTimeLabel(sliderValue);
354 //restart the music from the new time if it's the active song
355 if (musicHandle.active) {
356 musicHandle.startMusic(true)
357 }
358 musicHandle.unPauseAutoUpdates();
359 }
360
361 updateCurrentTimeLabel (seconds) {
362 this.namedSubElements.songCurrentPosition.innerHTML = convertSecondsToMinutes(seconds);
363 }
364
365 activelyMovingSlider (event) {
366 let musicHandle = globs.musicManager.musicHandles[this.id];
367 musicHandle.pauseAutoUpdates();
368 musicHandle.updateCurrentTimeLabel(musicHandle.namedSubElements.songSlider.value)
369 }
370
371 pauseAutoUpdates () {
372 //Pause automatic slider updates to prevent them happening while we are moving the slider
373 globs.musicManager.pauseAutoSliderUpdates = true;
374 }
375
376 unPauseAutoUpdates () {
377 globs.musicManager.pauseAutoSliderUpdates = false;
378 }
379}
380
381class PlaylistHandle extends DynamicDiv {
382 constructor (musicObject,id) {
383 super("musicMenu");
384 this.playlist = musicObject.playlist;
385 this.namedSubElements = {};
386 this.id = id;
387 this.active = false;
388
389 if (this.playlist.length > 20) {
390 this.shortName = this.playlist.slice(0,17)+'...'
391 } else {
392 this.shortName = this.playlist
393 }
394
395 let playlistExpanderButton = this.addButton("");
396 playlistExpanderButton.classList.add('songExpander');
397 playlistExpanderButton.onclick = this.toggleVisible;
398 playlistExpanderButton.id = this.id;
399 this.namedSubElements.playlistExpanderButton = playlistExpanderButton;
400
401 let shufflePlay = this.addButton("");
402 shufflePlay.classList.add('shufflePlay');
403 shufflePlay.onclick = this.shufflePlaylistFromButton;
404 shufflePlay.id = this.id;
405 this.namedSubElements.shufflePlay = shufflePlay;
406
407 let loopPlay = this.addButton("")
408 loopPlay.classList.add('loopPlay');
409 loopPlay.onclick = this.loopPlaylistFromButton;
410 loopPlay.id = this.id;
411 this.namedSubElements.loopPlay = loopPlay;
412
413 let playlistName = this.addText(this.shortName);
414 playlistName.classList.add("playlistName");
415 this.namedSubElements.playlistName = playlistName;
416 }
417
418 shufflePlaylist () {
419 if (globs.musicManager.activeMusicHandle) {
420 globs.musicManager.activeMusicHandle.stopMusic();
421 }
422 this.start();
423 let musicHandles = Array.from(globs.musicManager.categorizedPlaylists[this.playlist]);
424 musicHandles = shuffle(musicHandles);
425 musicHandles[0].startMusic(false,false)
426
427 musicHandles.shift()
428 globs.musicManager.songsToPlayNext = musicHandles;
429 }
430
431 shufflePlaylistFromButton (event) {
432 let playlistHandle = globs.musicManager.playlistHandles[this.id];
433 playlistHandle.shufflePlaylist()
434 }
435
436 loopPlaylist () {
437 if (globs.musicManager.activeMusicHandle) {
438 globs.musicManager.activeMusicHandle.stopMusic();
439 }
440 this.start();
441 let musicHandles = Array.from(globs.musicManager.categorizedPlaylists[this.playlist]);
442 musicHandles[0].startMusic(false,false)
443
444 musicHandles.shift()
445 globs.musicManager.songsToPlayNext = musicHandles;
446 }
447
448 loopPlaylistFromButton (event) {
449 let playlistHandle = globs.musicManager.playlistHandles[this.id];
450 playlistHandle.loopPlaylist();
451 }
452
453 start () {
454 this.div.style.backgroundColor = "rgba(50,100,205,.1)";
455 globs.musicManager.activePlaylistHandle = this;
456 this.active = true;
457 }
458
459 stop () {
460 if (this.active) {
461 this.div.style.backgroundColor = "rgb(240,240,240)";
462 globs.musicManager.activePlaylistHandle = null;
463 this.active = false;
464 }
465 }
466
467 toggleVisible (event) {
468 let playlist = globs.musicManager.playlistHandles[this.id];
469 let musicHandles = globs.musicManager.categorizedPlaylists[playlist.playlist];
470
471 for (let i=0;i<musicHandles.length;i+=1) {
472 musicHandles[i].toggleVisible()
473 }
474 }
475 }