-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjavascript.js
More file actions
1529 lines (1370 loc) · 54.7 KB
/
javascript.js
File metadata and controls
1529 lines (1370 loc) · 54.7 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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//East access document.getElementById()
const j$ = (id) => {
return document.getElementById(id);
};
//on page load trigger ResizeEvent();
addEventListener("load", (event) => {
ResizeEvent();
});
//on window resize, trigger ResizeEvent();
addEventListener("resize", (event) => {
ResizeEvent();
});
function ResizeEvent() {
//Triggered when window loads or when resized
//resize text area height
$("#lyricsTextArea").css("height", window.innerHeight - 55 + "px");
//resize topBarButtonsSpan width to window.InnterWidth - 170px
$("#topBar").css("width", window.innerWidth - 10 + "px");
//if window width is less than 580px
if (window.innerWidth <= 580) {
//the words cant fit anymore so change them to numbers
j$("topBarEntryLyricsButton").innerHTML = "1";
j$("topBarUploadFileButton").innerHTML = "2";
j$("topBarSongInfoButton").innerHTML = "3";
j$("topBarSyncLinesButton").innerHTML = "4";
} else {
j$("topBarEntryLyricsButton").innerHTML = "Enter Lyrics";
j$("topBarUploadFileButton").innerHTML = "Upload File";
j$("topBarSongInfoButton").innerHTML = "Song Info";
j$("topBarSyncLinesButton").innerHTML = "Sync Lines";
}
//Changes width of the topBarButtonsSpan when they no longer fit
if (window.innerWidth <= 768) {
//(window.innerWidth - 10) / 4 - (padding + margin)
$(".topBarButton").css(
"width",
$("#topBar").css("width").slice(0, -2) / 4 - 9 + "px"
);
} else {
$(".topBarButton").css("width", "auto");
}
if (window.innerWidth <= 659) {
$("#songInfoScreen").css("width", window.innerWidth - 20 + "px");
$("#controlsConainer").css("width", window.innerWidth + "px");
$("#syncButton").css("width", window.innerWidth);
//$("#GenerateFileButton").css("width", window.innerWidth)
$("#mainControls").css(
"margin",
"0 " + (window.innerWidth - 376) / 2 + "px"
);
//$("#mainControls").css("width", window.innerWidth + "px");
} else {
$("#songInfoScreen").css("width", "194px");
$("#controlsConainer").css("width", "659px");
$("#syncButton").css("width", "130px");
//$("#GenerateFileButton").css("width", "130px")
$("#mainControls").css("margin", "auto");
//$("#mainControls").css("width", "390px");
}
}
//sets placeholder for the big textarea on the first screen
$("#lyricsTextArea").attr(
"placeholder",
"Copy & paste lyrics into here\nYou can find lyrics from e.g. Musixmatch"
);
//checks whether the required things have been done before allowing access to other screens
//isTopBarButtonAccessible[0] = enterLyricsScreen, corresponding button: topBarEntryLyricsButton
//isTopBarButtonAccessible[1] = uploadFileScreen, corresponding button: topBarUploadFileButton
//isTopBarButtonAccessible[2] = songInfoScreen, corresponding button: topBarSongInfoButton
//isTopBarButtonAccessible[3] = syncLyricsScreen, corresponding button: topBarSyncLinesButton
let isTopBarButtonAccessible = [true, false, false, false];
//buts a line under the chosen topBar button to highlight youre on that screen
function highlightTopBarButton(dehighlightOrHighlight, buttonId) {
//if the element with id buttonId does not exist
if (!document.getElementById(buttonId)) {
// trow error
alert("Error thrown, check logs.");
throw new Error(
"Parameter buttonId has value" +
buttonId +
"\nof which a corresponding element with ID was not found."
);
}
//bit of a *bad* system to highlight and dehighlight but this project is basically dead
//and rolls in the grave once every 6 months. so i cant be bothered fixing it :/
if (dehighlightOrHighlight == "highlight") {
$("#" + buttonId).css("border-bottom", "5px solid #2874ed");
$("#" + buttonId).css("border-bottom-left-radius", "0em");
$("#" + buttonId).css("border-bottom-right-radius", "0em");
} else if (dehighlightOrHighlight == "dehighlight") {
$("#" + buttonId).css("border-bottom", "0px solid #2874ed");
$("#" + buttonId).css("border-bottom-left-radius", "0.5em");
$("#" + buttonId).css("border-bottom-right-radius", "0.5em");
} else {
alert("Error thrown, check logs.");
throw new Error(
"Parameter dehighlightOrHighlight has value " +
dehighlightOrHighlight +
"\nof which is not a valid, only 'highlight' or 'dehighlight' are valid parameters."
);
}
}
//highlights the enterLyricsScreen button because thats the sreen the user is on at the start
highlightTopBarButton("highlight", "topBarEntryLyricsButton");
//just a tracker to keep track of what screen the user is on, set to 1 because thats the sreen the user is on at the start
let whatScreenIsUserCurrentlyOn = 1;
//if the user revisits the first tab then reset the synced lyrics
revisitedFirstTab = true;
//just incase the user wants to come back to screen 1
function topBarEntryLyricsButtonClicked() {
revisitedFirstTab = true;
// !!!RED!!! dehighlight all buttons
redHighlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
redHighlightTopBarButton("dehighlight", "topBarUploadFileButton");
redHighlightTopBarButton("dehighlight", "topBarSongInfoButton");
redHighlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//dehighlight ALL BUTTONS
highlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
highlightTopBarButton("dehighlight", "topBarUploadFileButton");
highlightTopBarButton("dehighlight", "topBarSongInfoButton");
highlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//highlight the button of the currently on screen
highlightTopBarButton("highlight", "topBarEntryLyricsButton");
//whatScreenIsUserCurrentlyOn = 1
whatScreenIsUserCurrentlyOn = 1;
//hide screen 1 and 2 and show screen 3
showScreen1();
hideScreen2();
hideScreen3();
hideScreen4();
}
//highlights the button in red so the user knows they can now click on it
function redHighlightTopBarButton(dehighlightOrHighlight, buttonId) {
//if the element with id buttonId does not exist
if (!document.getElementById(buttonId)) {
// trow error
alert("Error thrown, check logs.");
throw new Error(
"Parameter buttonId has value" +
buttonId +
"\nof which a corresponding element with ID was not found."
);
}
if (dehighlightOrHighlight == "highlight") {
$("#" + buttonId).css("background-color", "#930000");
} else if (dehighlightOrHighlight == "dehighlight") {
$("#" + buttonId).css("background-color", "inherit");
} else {
alert("Error thrown, check logs.");
throw new Error(
"Parameter dehighlightOrHighlight has value" +
dehighlightOrHighlight +
"\nof which a corresponding element with ID was not found."
);
}
}
//to only blip once
let blipped = false;
//when the user enters something into the text area grant them access to the 2nd screen and redHighlight it
j$("lyricsTextArea").addEventListener("input", (event) => {
if (!blipped) {
blip3Times("topBarUploadFileButton");
//break fuse and stop blipping
blipped = true;
}
isTopBarButtonAccessible[1] = true;
});
//warnUsrTheyCanClickButton
async function blip3Times(elementID) {
redHighlightTopBarButton("highlight", elementID);
await asyncReturnPromiseAfter(200);
redHighlightTopBarButton("dehighlight", elementID);
await asyncReturnPromiseAfter(200);
redHighlightTopBarButton("highlight", elementID);
await asyncReturnPromiseAfter(200);
redHighlightTopBarButton("dehighlight", elementID);
await asyncReturnPromiseAfter(200);
redHighlightTopBarButton("highlight", elementID);
return;
}
//Hides and displays time offset menu
$("#topBarSettings").click(function () {
if ($("#topBarSettingsPopup").css("display") == "none") {
$("#topBarSettingsPopup").css("display", "block");
} else {
$("#topBarSettingsPopup").css("display", "none");
}
});
$("#cogPopupCloseX").click(function () {
$("#topBarSettingsPopup").css("display", "none");
});
//hides everything on the first screen
function hideScreen1() {
$("#enterLyricsScreen").css("display", "none");
}
//hides everything on the first screen
function hideScreen2() {
$("#uploadFileScreen").css("display", "none");
}
//hides everything on the first screen
function hideScreen3() {
$("#songInfoScreen").css("display", "none");
}
//hides everything on the first screen
function hideScreen4() {
$("#syncLyricsScreen").css("display", "none");
$("#audioPlayerBar").css("display", "none");
}
function showScreen1() {
$("#enterLyricsScreen").css("display", "block");
}
function showScreen2() {
$("#uploadFileScreen").css("display", "block");
}
function showScreen3() {
$("#songInfoScreen").css("display", "block");
}
function showScreen4() {
$("#syncLyricsScreen").css("display", "block");
$("#audioPlayerBar").css("display", "block");
}
function topBarUploadFileButtonClicked() {
//check whether the user is allowed to click it
if (
isTopBarButtonAccessible[1] == true ||
$("#lyricsTextArea").val().length > 0
) {
//(red) dehighlight al buttons
redHighlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
redHighlightTopBarButton("dehighlight", "topBarUploadFileButton");
redHighlightTopBarButton("dehighlight", "topBarSongInfoButton");
redHighlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//dehighlight ALL BUTTONS
highlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
highlightTopBarButton("dehighlight", "topBarUploadFileButton");
highlightTopBarButton("dehighlight", "topBarSongInfoButton");
highlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//highlight the button of the currently on screen
highlightTopBarButton("highlight", "topBarUploadFileButton");
//whatScreenIsUserCurrentlyOn = current screen (1-4)
whatScreenIsUserCurrentlyOn = 2;
//hide screen 1 and 3 and show screen 2
hideScreen1();
showScreen2();
hideScreen3();
hideScreen4();
}
}
//when the user clicks on the topBarUploadFileButton button,
$("#topBarUploadFileButton").click(() => {
topBarUploadFileButtonClicked();
});
//gobal variable. used to warn user that JSMediaTags automatically read and autofilled the song name and artist name
let autofilled = false;
//when the user successfully uploads a file into the fileSelector
j$("fileSelector").addEventListener("change", (event) => {
//loads the file from fileSelector to audioPlayback
files = event.target.files;
//JSMediaTags is used to read metadata from the file
const jsmediatags = window.jsmediatags;
//try to read metadata to retrieve artist name and song name
console.log("triggered read metadata");
jsmediatags.read(files[0], {
onSuccess: function (tag) {
console.log(tag);
let songName = tag.tags.title;
if (songName.length > 0 && $("#SongNameInput").val() == "") {
$("#SongNameInput").val(songName);
autofilled = true;
}
let artistName = tag.tags.artist;
if (artistName.length > 0 && $("#ArtistNameInput").val() == "") {
$("#ArtistNameInput").val(artistName);
autofilled = true;
}
albumName = tag.tags.album;
if (albumName.length > 0 && $("#AlbumNameInput").val() == "") {
$("#AlbumNameInput").val(albumName);
autofilled = true;
}
},
onError: function (error) {
console.error("JSMediaTag exception:");
console.log(error);
},
});
$("#audioPlaybackAudioSourceID").attr("src", URL.createObjectURL(files[0]));
document.getElementById("audioPlayback").load();
//changes the width of the fileSelector to allow space for the file name
$("#fileSelector").css("width", "260px");
//activate the third button
blip3Times("topBarSongInfoButton");
isTopBarButtonAccessible[2] = true;
});
//when the user clicks on the Song Info button,
j$("topBarSongInfoButton").addEventListener("click", (event) => {
topBarSongInfoButtonClicked();
});
//Only trigger JSMediaTags autofill warning once. DOES NOT DISABLE JSMEDIATAGS
triggeredShowinbgOffJSMEdiaTags = false;
function topBarSongInfoButtonClicked() {
//check whether the user is allowed to click it
if (isTopBarButtonAccessible[2] == true) {
// !!!RED!!! dehighlight all buttons
redHighlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
redHighlightTopBarButton("dehighlight", "topBarUploadFileButton");
redHighlightTopBarButton("dehighlight", "topBarSongInfoButton");
redHighlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//dehighlight ALL BUTTONS
highlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
highlightTopBarButton("dehighlight", "topBarUploadFileButton");
highlightTopBarButton("dehighlight", "topBarSongInfoButton");
highlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//highlight the button of the currently on screen
highlightTopBarButton("highlight", "topBarSongInfoButton");
//whatScreenIsUserCurrentlyOn = current screen (1-4)
whatScreenIsUserCurrentlyOn = 3;
hideScreen1();
hideScreen2();
showScreen3();
hideScreen4();
//alert the users that the firlsds have been automatically filled in ONLY ONCE
if (autofilled == true && !triggeredShowinbgOffJSMEdiaTags) {
triggeredShowinbgOffJSMEdiaTags = true;
displayWarning(
"Some fields have been automatically filled in by grabbing the song's metadata.",
5500,
"#1f6934",
"white"
);
}
//if the SyncLines tab requested the user to fill in a field then flash the
if (blipNextTimeUserVisitsSongInformationTab[0] == true) {
blipNextTimeUserVisitsSongInformationTab[0] = false;
WarnUserToFillOutField("SongNameInput");
}
if (blipNextTimeUserVisitsSongInformationTab[1] == true) {
blipNextTimeUserVisitsSongInformationTab[1] = false;
WarnUserToFillOutField("ArtistNameInput");
}
}
}
//Checks if the required fields have been filled in
async function IsSongInfoEntered() {
allInfoEntered = true;
if ($("#SongNameInput").val() == "") {
allInfoEntered = false;
}
if ($("#ArtistNameInput").val() == "") {
allInfoEntered = false;
}
if (allInfoEntered == true) {
isTopBarButtonAccessible[3] = true;
}
}
previousLyricsEntered = "9832nfqrhgq39g33t$%£$%";
//run when the topBarSyncLinesButton button is clicked
function topBarSyncLinesButtonClicked() {
IsSongInfoEntered();
//check whether the user is allowed to click it
if (isTopBarButtonAccessible[3] == true) {
// !!!RED!!! dehighlight all buttons
redHighlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
redHighlightTopBarButton("dehighlight", "topBarUploadFileButton");
redHighlightTopBarButton("dehighlight", "topBarSongInfoButton");
redHighlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//dehighlight ALL BUTTONS
highlightTopBarButton("dehighlight", "topBarEntryLyricsButton");
highlightTopBarButton("dehighlight", "topBarUploadFileButton");
highlightTopBarButton("dehighlight", "topBarSongInfoButton");
highlightTopBarButton("dehighlight", "topBarSyncLinesButton");
//highlight the button of the currently on screen
highlightTopBarButton("highlight", "topBarSyncLinesButton");
//whatScreenIsUserCurrentlyOn = 4
whatScreenIsUserCurrentlyOn = 4;
//hide screen 1 and 2 and show screen 3
hideScreen1();
hideScreen2();
hideScreen3();
showScreen4();
showHintsAfter();
//do not reset the users progress unless he revisited the first tab and changed its contents
if ($("#lyricsTextArea").val() != previousLyricsEntered) {
previousLyricsEntered = $("#lyricsTextArea").val();
assignLyricsToLinesInTable();
//we want to start off with the first element so
tableLineClicked(0);
}
//determines whether the user is syncing or previewing
beginUserSyncingLinesIntrival();
//begins syncing the playbar
beginSyncingPlaybar();
revisitedFirstTab = false;
}
}
//when the user clicks on the topBarSyncLinesButton button,
$("#topBarSyncLinesButton").click(() => {
console.log("Sync Lines Button Clicked");
if (whatScreenIsUserCurrentlyOn != 4) {
topBarSyncLinesButtonClicked();
}
});
//hints at user to use shift
async function showHintsAfter() {
await asyncReturnPromiseAfter(1500);
displayWarning(
"Hint: Press P/shift to sync lines\ninstead of clicking 'Sync' button",
5500,
"#1f6934",
"white"
);
}
//resolves promise after specified amount of time
function asyncReturnPromiseAfter(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
//screen 1 button clicked
$("#topBarEntryLyricsButton").click(() => {
topBarEntryLyricsButtonClicked();
});
//takes unique id and lyrics as parameters and returns a finished line to insert into the table,
let tableLineSkeleton = (uniqueID, lyrics, timeStamp) => {
let backgroundColor = uniqueID % 2;
//add a class to each row of the table
line =
"<tr class='tableLine' " +
//add a unique id to each row of the table
"id='tableLineIndex" +
uniqueID +
"'>" +
//Table Time Columns:
//add an onclick event to each table columns which also passes its unique id to each function so we can know which item was clicked
"<td <!--onclick='tableLineClicked(" +
uniqueID +
")-->'" +
//add a class of tableColumns and tableTimeColumns
"class='tableColumns tableTimeColumns tableRowBackgroundColour" +
backgroundColor +
"' " +
//add unique id to each time column
"id='tableTimeColumn" +
uniqueID +
"'>" +
timeStamp +
"</td> " +
//Edit Time Table Image
"<td id='tableEditButtonColumn" +
uniqueID +
"' class='tableEditButtonColumnClass tableColumns tableRowBackgroundColour" +
backgroundColor +
"' onclick='tableLineDblClicked(" +
uniqueID +
")'>" +
"<img class='tableEditButtonImageClass' src='pen-icon.png' alt='' >" +
"</td>" +
//Add Time Table Image
"<td id='tableAddButtonColumn" +
uniqueID +
"' class='tableEditButtonColumnClass tableColumns tableRowBackgroundColour" +
backgroundColor +
"' onclick='addNewLineAfterIndex(" +
uniqueID +
")'>" +
"<img class='tableEditButtonImageClass' src='add-icon.png' alt='' >" +
"</td>" +
//Remove Time Table Image
//removeLineAtIndex
"<td id='tableRemoveButtonColumn" +
uniqueID +
"' class='tableEditButtonColumnClass tableColumns tableRowBackgroundColour" +
backgroundColor +
"' onclick='removeLineAtIndex(" +
uniqueID +
")'>" +
"<img class='tableEditButtonImageClass' src='remove-icon.png' alt='' >" +
"</td>" +
//Table Lyrics Columns:
//add an onclick event to each table columns which also passes its unique id to each function so we can know which item was clicked
"<td onclick='tableLineClicked(" +
uniqueID +
")' " +
//add double click event listener
" ondblclick='tableLineDblClicked(" +
uniqueID +
")' " +
//add a class of tableColumns and tableLyricsColumns and an alternating 1 or 0 for backgroundColor
"class='tableColumns tableLyricsColumns tableRowBackgroundColour" +
backgroundColor +
"' " +
//add unique id to each time column
"id='tableLyricsColumn" +
uniqueID +
"'>" +
//insert the lyrics into the tableLyricsColumn
"" +
lyrics +
"</td>" +
"</tr>";
return line;
};
//holds the timespamps
let timeStamps = [];
//new array to store the lyrics lines
let lyricsLines = [];
// Spits each item in array to a div and then assigns an id to them
function assignLyricsToLinesInTable() {
//assign each line in lyricsTextArea to lyricsLines
if (developerTools && developerTools == true) {
//DEVELOPER TOOL
lyricsLines = testLyrics.split("\n");
} else {
lyricsLines = $("#lyricsTextArea").val().split("\n");
}
// just a shortcut to access the lyricsTable which will store the lines
let table = document.getElementById("lyricsTable");
//clearts the innerHTML of the table
table.innerHTML = "";
//go through every element in lyricsLines and uses the tableLineSkeleton function to create valid html and insert it into the table
for (i = 0; i < lyricsLines.length; i++) {
table.innerHTML += tableLineSkeleton(i, lyricsLines[i], "0:00.00");
//console logs out when the for loop ends
if (i == lyricsLines.length) {
console.log("Emptied lyricsLines to table");
}
}
}
function tableLineClicked(rowId) {
//if the user is editing a line do nothing
if (rowId == editingWhatElement) {
//do nothing because we dont want to disturb the editing of a line
} else {
//change the global variable selectedTableRow to roId
selectedTableRow = rowId;
//sets all rows to white
formatTheWholeTable();
//just incase the user was editing another element we finish editing it
finishEditingElement();
}
}
let editingWhatElement = -1;
function tableLineDblClicked(rowId) {
//just incase the user was editing another element we finish editing it
finishEditingElement();
//set editingWhatElement to rowIf
editingWhatElement = rowId;
//get the lyrics inside the line
let lyricsInsideLine = j$("tableLyricsColumn" + rowId).innerHTML;
//make an input type text with no intitial value because any value we set to it till mess up if theres any ' or " inside the value we give"
let inputTypeText =
"<input class='tableLyricsColumnInput' type='text' id='tableLyricsColumnInput" +
rowId +
"'>";
//replace the value of the <td></td> with inputTypeText
j$("tableLyricsColumn" + rowId).innerHTML = inputTypeText;
//add the value now
$("#tableLyricsColumnInput" + rowId).val(lyricsInsideLine);
}
//finishes editing the element after a double click
function finishEditingElement() {
//if editingWhatElement is not -1 and the element with id tableLyricsColumnInput+editingWhatElement exists then continue
if (
editingWhatElement != -1 &&
j$("tableLyricsColumnInput" + editingWhatElement) != null
) {
//take the value of "tableLyricsColumnInput"+editingWhatElement and store it
let tableLyricsColumnInputValue = j$(
"tableLyricsColumnInput" + editingWhatElement
).value;
//replace the contents of "tableLyricsColumn"+editingWhatElement with tableLyricsColumnInputValue
j$("tableLyricsColumn" + editingWhatElement).innerHTML =
tableLyricsColumnInputValue;
//replace the lyrics inside lyricsLines array
lyricsLines[editingWhatElement] = tableLyricsColumnInputValue;
//change the global variable to indicate not editing any lines
editingWhatElement = -1;
}
}
// $(".tableColumns").dblclick(() => {
// alert("You double clicked");
// })
//IMPORTANT GLOBAL VARIABLE
//the table row id of which the user is currently settings the time stamp
let selectedTableRow = 0;
//sets the colour of the row of which the table row the user is setting the time stamp to to BLUE
function colorTableRowBlue(rowId) {
$("#tableTimeColumn" + rowId).css("color", "rgb(0, 174, 255)");
$("#tableLyricsColumn" + rowId).css("color", "rgb(0, 174, 255)");
}
//sets the colour of the row of which the table row the user is setting the time stamp to to WHITE
function colorTableRowWhite(rowId) {
$("#tableTimeColumn" + rowId).css("color", "#ffffff");
$("#tableLyricsColumn" + rowId).css("color", "#ffffff");
}
//sets the colour of the row of which the table row the user is setting the time stamp to to RED
function colorTableRowRed(rowId) {
$("#tableTimeColumn" + rowId).css("color", "#ff2626");
$("#tableLyricsColumn" + rowId).css("color", "#ff2626");
}
function colorTableRowBackgroundBlue(rowId) {
$("#tableTimeColumn" + rowId).css("background-color", "#074685");
$("#tableLyricsColumn" + rowId).css("background-color", "#074685");
}
function decolorTableRowBackgroundBlue(rowId) {
$("#tableTimeColumn" + rowId).css("background-color", "");
$("#tableLyricsColumn" + rowId).css("background-color", "");
}
/////////////////////
//Audio stuff
/////////////////////
//shortcut variable, so we dont type document.getElementById("audioPlayback") every time we use it
let audio = document.getElementById("audioPlayback");
//when the play button is clicked
$("#playButton").click(() => {
playButtonCLicked();
});
function playButtonCLicked() {
// if the audio is Playing and the button is clicked
if ($("#playButton").html() === "Pause") {
//pause the audio
audio.pause();
//change the html of the button to "Play"
$("#playButton").html("Play");
} //if the audio is Paused and the button is clicked
else if ($("#playButton").html() === "Play") {
//play the audio
audio.play();
//change the html of the button to "Pause"
$("#playButton").html("Pause");
}
}
//when the hints button is clicked
$("#hintsButton").click(() => {
alert(
"" +
"Single click on a line to start syncing from it\n" +
"Double click on a line to change its content\n" +
" \n" +
"Space: pause/play\n" +
"J: select next\nK: select prev\n" +
"H: -2s\nL: +2s\n" +
"Arrow right: +5s\n" +
"Arrow left: -5s\n" +
"P/Shift: Sync Line"
);
});
// SEEK FUNCTIONS ---
// Seeks by specifiec time
function seek(t) {
audio.currentTime = audio.currentTime + t;
}
//-10s
$("#backwardButton").click(function () {
seek(-10);
});
//+10s
$("#forwardButton").click(function () {
seek(+10);
});
// NAVIGATING FUNCTIONS ---
function jumpLine(relativeNumber) {
tableLineClicked(
Math.max(0, selectedTableRow + relativeNumber)
)
}
//stores the id of the max line synced in order to keep track of where the user is up to
let maxLineSynced = 0;
///////////////
// Syncing Lines
///////////////
//says whether the line has been synced before
let hasLineBeenSyncedBefore = (lineId) => {
//reads the timespamp inside the table and compares it to "0:00.00"
let syncedBefore = $("#tableTimeColumn" + lineId).html() != "0:00.00";
return syncedBefore;
};
//when the sync button is clicked
$("#syncButton").click(() => {
syncLine();
});
//when the user clicks the sync button or sync shortcut
function syncLine() {
//if a timestamp preceding this one is greater, warn user and dont sync line
if (isGreaterThanAllTimestampsBefore(selectedTableRow)) {
// change the timespamp
$("#tableTimeColumn" + selectedTableRow).html(
formatTimeTommssms(audio.currentTime + getTimeOffset())
);
//adds the timestamp to timeStamps array
timeStamps[selectedTableRow] = timeStampsVerify(
audio.currentTime + getTimeOffset()
);
//sets maxSyncedLine to timeStamps.Length
maxLineSynced = timeStamps.length;
//resets the timestamp on every element after selectedTableRow
resetAllTimestampsAfer(selectedTableRow);
//we move to the next line so
selectedTableRow++;
//resets the table
resetWholeTable();
//formats the whole table
formatTheWholeTable();
//we want to finish editing any elements we were editing also so
finishEditingElement();
//scroll to keep the element in the center of the screen
scrollToTableElement(selectedTableRow)
} else {
displayWarning(
"A timestamp preceding the current one has a" +
" greater timestamp than the current one, please either change that or " +
"wait until the audio reaches a higher timestamp",
4000,
"default",
"default"
);
}
}
//Scrolls to a table element
function scrollToTableElement(tableRow) {
try {
Element.prototype.documentOffsetTop = function () {
return (
this.offsetTop +
(this.offsetParent
? this.offsetParent.documentOffsetTop()
: 0)
);
};
let top =
document
.getElementById("tableTimeColumn" + tableRow)
.documentOffsetTop() -
window.innerHeight / 2;
window.scrollTo({ top: top, behavior: "smooth" });
} catch (e) {
console.log("window.scrollTo error\n\n" + e);
}
}
//resets all the timestamps after the selected element
function resetAllTimestampsAfer(rowId) {
//console.log("reset timestamp elements starting at " + i);
//get the timeStamo of rowId
let initialTimestamp = timeStamps[rowId];
//for all lines after lyricsLines[rowId]
for (let i = rowId + 1; i < lyricsLines.length; i++) {
//if the timestamp of the line being investigated is less than initialTimestamp reset that line
if (timeStamps[i] < initialTimestamp) {
//clears timeStamps after the selected element
timeStamps[i] = null;
$("#tableTimeColumn" + i).html("0:00.00");
colorTableRowWhite(i);
decolorTableRowBackgroundBlue(i);
} else {
//leave it alone
}
}
}
//checks if the current time is greater than all timestamps in timeStamps array
function isGreaterThanAllTimestampsBefore(lineId) {
let currentTime = audio.currentTime + getTimeOffset();
for (let i = 0; i < lineId; i++) {
if (currentTime < timeStamps[i]) {
return false;
}
}
return true;
}
//resets the whole table color to white
function resetWholeTable() {
//sets i to 0
i = 0;
//while the element exists
while (j$("tableLyricsColumn" + i) != null) {
colorTableRowWhite(i);
decolorTableRowBackgroundBlue(i);
i++;
}
}
//We use this function to see whether the user is syncing lines or previewing the syncing they have done
let userSyncingLines = true;
function beginUserSyncingLinesIntrival() {
setInterval(() => {
//if the timestamp of the audio is greater than the gratest value in timestamps array
//if (audio.currentTime ) {
// userSyncingLines = true;
//
//} else if (audio.currentTime < timeStamps[maxLineSynced]) {
highlightPreviewingLines();
//userSyncingLines = false;
//}
}, 200);
}
//Allows the user to preview the syncing they have done
function highlightPreviewingLines() {
//if the length of the timeStamps array is less than 3 errors occure so dont do anything
if (timeStamps.length <= 2) {
//do nothing
} //else
else {
//format the table
formatTheWholeTable();
//stopWhileLoop = false
let stopWhileLoop = false;
//i starting from 0
let i = 0;
//greatest timeStamp met
let greatestTimeStampMet = 0;
//search for the lowest timeStamp which is less than the current
//while stopWhileLoop isnt trigured and the searcher hasnt hit a timestamp less than the previous ones
while (!stopWhileLoop) {
if (timeStamps[i] > greatestTimeStampMet) {
//change the greatest timeStamp met
greatestTimeStampMet = timeStamps[i];
//if the element we are looking for is found
if (audio.currentTime <= timeStamps[i]) {
colorTableRowBackgroundBlue(i - 1);
stopWhileLoop = true;
}
} else {
stopWhileLoop = true;
greatestTimeStampMet = 0;
}
i++;
}
//the reason we look for the greatest timestamp met and reset it as soon as we hit one less is because
//if we go from a 1, 2, 4, 6, 0, 0, 0, 9
//we want it to stop after 6 because just like the real program it will run into an error
}
}
//formats the table
function formatTheWholeTable() {
//sets i to 0
i = 0;
//while the element exists
while (j$("tableLyricsColumn" + i) != null) {
decolorTableRowBackgroundBlue(i);
//if has been synced before color = red
if (hasLineBeenSyncedBefore(i) == true) {
colorTableRowRed(i);
} //if i is the line the user is currently on then set it to blue
else if (i == selectedTableRow) {
} else if (hasLineBeenSyncedBefore(i) == false) {
colorTableRowWhite(i);
}
i++;
}
colorTableRowBlue(selectedTableRow);
}
//Formats seconds into minutes:seconds:ms and returns it that way
function formatTimeTommssms(time) {
let floored, minutes, seconds, ms, formated;
// To not break the code
if (time < 0) {
time = 0;
}
floored = Math.floor(time);
minutes = Math.floor(floored / 60);
seconds = floored % 60;
ms = (time - Math.floor(time)).toFixed(2);
ms = ms * 100;
minutes = Math.floor(minutes);
seconds = Math.floor(seconds);
ms = Math.floor(ms);
// If the offset of time made it negative, makes it 0
if (minutes < 0) {
minutes = 0;
}
if (seconds < 0) {
seconds = 0;
}
if (ms < 0) {
ms = 0;
}
// if seconds or ms are 1 digit only, makes it 2 digit
if (seconds.toString().length == 1) {
seconds = "0" + seconds;
}
if (ms.toString().length == 1) {
ms = ms + "0";
}
formated = minutes + ":" + seconds + "." + ms;
return formated;
}
// Offsets the time as specified by the user
function getTimeOffset() {
return -1 * document.getElementById("cogPopupSelectTimeOffset").value;
}
//is displayWarning already displaying
let isDisplayWarningFree = true;
//Displays a warning message to the user