-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-SimplePlayer.js
More file actions
1041 lines (803 loc) · 31.1 KB
/
MMM-SimplePlayer.js
File metadata and controls
1041 lines (803 loc) · 31.1 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
Module.register("MMM-SimplePlayer", {
defaults: {
autoplay: true,
playTracks: true, // play from directory
musicDirectory: "modules/MMM-SimplePlayer/music",
usePlaylist: false,
playlistName: "examplePlaylist.m3u",
DLNAPlaylistName: "dlnaPlaylist.m3u",
playlist: [],
images: [], // contains any images (photos) returned from the DLNA server
playlistOrder: [],//Shuffled/Random ordering of the playlist
showDLNA: false, //enables DLNA support will load any DLNA servers found initially
DLNAPlaylist: [], //holds the DLNA playlist
art: [], //matching art for the DLNA play list
showEvents: false,
showMeta: true,
showAlbumArt: false, //if art is available (usually just DLNA) show it as background to the DIV
showSlideShowControls: false,
slideShowDuration: 10000, //10 seconds
slideShowFadeDuration:2000, //2 seconds
showSlideShow: false,
slideShowSize: "medium",
startMuted: false,
shuffle: false,
repeat: false,
showMini:false, //start with miniplayer
miniplayercontrols: ['Back', 'Play', 'Next', 'MiniPlayer'],
controlsSize: "medium",
defaultplayercontrols: ['Back', 'Play', 'Stop', 'Next', 'Volume', 'Shuffle', 'Repeat', 'MiniPlayer', 'DLNA'],
supportedAudioExt: ['MP3', 'WAV', 'OGG'],
showVisualiser:false,
useProxy: false,
DLNAs: [], //add servers when found here
proxyBase: "http://localhost:8080/proxy", //the URL of the proxy endpoint
autoHideControls: false,
debug: false,
},
startAudio()
{
this.requestTracks();
if (this.config.showDLNA) {
this.requestServers();
}
},
reset()
{
this.currentTrack = 0;
this.isPlaying = false;
this.DLNAItems = [];
this.DLNAimages = [];
this.DLNAIdxs = [0]; //will contain the idx of item displayed in the current level , used when scrolling left and right, set when scrolling up and down
this.DLNACurrentIdx = 0; // points to the current level, ++ -- left and right scrolling
this.returnedDLNAItems = [{ is: "-99", type: "server", name: "No DLNA Servers" }];
this.loadDLNAItems();
this.DLNAServersLoaded = false;
this.currentServerID = 0;
},
start()
{
this.masterplayercontrols = ['Back', 'Play', 'Stop', 'Next', 'Volume', 'Shuffle', 'Repeat', 'MiniPlayer', 'DLNA'];
this.sendNotificationToNodeHelper("CONFIG", { config: { debug: this.config.debug } });
this.reset();
this.trackInfoMsg = "";
this.slideShow = null;
this.showingDLNA = this.config.showDLNA; //used to toggle the DLNA button
this.showingMini = this.config.showMini; //used to toggle the Miniplayer button
this.startAudio();
this.iconMap =
{
Back: ["fa-backward", true],
Play: ["fa-play", true],
Stop: ["fa-stop", true],
Next: ["fa-forward", true],
Volume: [this.config.startMuted ? "fa-volume-off" : "fa-volume-low", true],
Shuffle: ["fa-random", this.config.shuffle],
Repeat: ["fa-redo", this.config.repeat],
MiniPlayer: ["fa-minimize", true],
}
if (this.config.showDLNA)
{
this.iconMap["DLNA"] = ["fa-server", this.showingDLNA];
}
else
{
this.iconMap["DLNA_Not_Available"] = ["fa-ban", false];
const index = this.config.defaultplayercontrols.indexOf('DLNA');
if (index !== -1)
{
this.config.defaultplayercontrols[index] = 'DLNA_Not_Available';
}
}
this.DLNAIconMap =
{
ScrollDown: ["fa-arrow-down", true], //only if entries on the tree on same level are below this one
ScrollUp: ["fa-arrow-up", true], //only if entries on the tree on same level are above this one
Add: ["fa-plus", true], //add the current displayed node and all children to the DLNA playlist
Remove: ["fa-minus", true], //removes the current displayed node and all children from the DLNA playlist
Clear: ["fa-trash", true], //clears the current DLNA playlist
Save: ["fa-file-export", true], //only when entries are in the DLNA playlist
Open: ["fa-file-import", true], //only if a saved playlist exists, loads into the DLNA playlist
ScrollLeft: ["fa-arrow-left", true], //only if entries in the tree to the left
ScrollRight: ["fa-arrow-right", true], //only if entries in the tree to the right; if no entries, will attempt to get new folders from the DLNA client using the ID of the displayed node
}
this.addDLNAServers = false; //used to indicate if we need to add DLNA servers when found
if (this.config.useProxy && this.config.DLNAs.length == 0)
{
this.addDLNAServers = true;
}
},
sendNotificationToNodeHelper: function (notification, payload) { //special to send the discrete module id each time
this.sendSocketNotification(notification, { identifier: this.identifier, payload: payload });
},
isSupportedAudio(url) {
//add ability to check a radio url where the type is in the url (-mp3)
var extMatch = url.match(/\.([a-zA-Z0-9]+)(?:\?|#|$)/); //check .mp3
if (!extMatch) {
var extMatch = url.match(/\-([a-zA-Z0-9]+)(?:\?|#|$)/); //check -mp3
if (!extMatch) { return false; }
}
const ext = extMatch[1].toUpperCase();
return this.config.supportedAudioExt.includes(ext);
},
setAudioSrc(src) //validates it is playable in HTML5 audio player
{
//html audio player only currently supports MP3 WAV OGG NOT native windows WMA
if (this.config.showEvents) { this.addLogEntry(`Audio: ${src}`); }
this.trackInfoMsg = "";
//console.log("src:", src);
if (this.isSupportedAudio(src))
{
if (this.config.useProxy) {
this.audio.src = Utilities.getProxyAudioSrc(src, this.config);
}
else {
this.audio.src = src;
}
this.getTrackInfo();
if (this.config.showAlbumArt) {
//console.log("showing art");
this.showAlbumArt();
}
}
else
{
this.trackInfoMsg = " - Unsupported audio";
this.audio.src = "";
}
},
socketNotificationReceived(fullnotification, payload)
{
this.addLogEntry(`Received: ${fullnotification}`);
var identifier = fullnotification.split(">")[0];
var notification = fullnotification.split(">")[1];
if (identifier != this.identifier) { return; }
if (notification === "PLAYLIST_READY")
{
this.config.playlist = payload[0];
this.config.images = payload[1];
this.config.art = payload[2];
this.radomisePlaylist(this.config.shuffle);
if (this.config.autoplay && this.config.playlist.length > 0)
{
this.setAudioSrc(this.config.playlist[this.config.playlistOrder[this.currentTrack]]);
}
if (this.config.showSlideShow && this.config.images && this.config.images.length > 0)
{
this.slideShow.setPlaylist(this.config.images);
this.slideShow.play();
}
}
//handle DLNA - a list of DLNA items is returned from node helper with the notification "NEW_DLNA_ITEMS"
//these replace the DLNA_items list held locally in the module
//the DLNA items display logic is then handled through the handleDLNAevents function and the available DLNA list.
if (notification === "NEW_DLNA_ITEMS") {
this.returnedDLNAItems = payload;
//this.DLNAItemsCurrentDisplayIdx = 0;
if (this.config.showEvents) {
this.addLogEntry(`DLNA Playlist Updated:${this.DLNAItems.length} items`);
}
this.loadDLNAItems();
this.showDLNAItems();
}
if (notification === "METADATA_RESULT") {
this.trackInfo.setAttribute("data-text", payload.common.artist + " - " + payload.common.album + " - #" + payload.common.track.no + " - " + payload.common.title + this.trackInfoMsg);
}
},
radomisePlaylist: function (shuffle)
{
//setup the play order as 0 - n initially
for (let i = 0; i < this.config.playlist.length; i++)
{
this.config.playlistOrder[i] = i;
}
//now randomise if needed
//use a seedable random function to shuffle the playlist
if (shuffle)
{
this.config.playlistOrder = this.seededShuffleRange(0, this.config.playlist.length - 1)
}
},
getStyles: function ()
{
//stuff and nonsense
if (this.config.showVisualiser || this.config.autoHideControls)
{
return ["MMM-SimplePlayer.css", 'font-awesome.css',"modules/MMM-ButterMeNoParsnips/butterMeNoParsnips.css"]
}
else
{
return ["MMM-SimplePlayer.css", 'font-awesome.css'];
}
},
getScripts: function ()
{
// if using butterme, then the butterme script might be loaded by audioproxy.
if (this.config.showVisualiser)// && Utilities == null)
{
return [
this.file('touchToolTipHandler.js'), // this file will be loaded straight from the module folder.
this.file('slideShow.js'), // this file will be loaded straight from the module folder.
'modules/MMM-ButterMeNoParsnips/helpers.js',
]
}
else
{
return [
this.file('touchToolTipHandler.js'), // this file will be loaded straight from the module folder.
this.file('slideShow.js'), // this file will be loaded straight from the module folder.
]
}
},
addAudioEvent(msg)
{
const audioError = this.audio.error ? ` ( Error ${this.audio.error.code}: ${this.audio.error.message})` : "";
this.addLogEntry(`${msg}${audioError}`);
},
addLogEntry(msg)
{
if (!this.config.showEvents) { return };
const eventLog = document.getElementById("eventLogbody"+this.identifier);
if (!eventLog)
{
if (this.config.debug) console.error("Event log element not found.");
return;
}
const event = document.createElement("p");
event.className = "event-log-body";
event.innerText = msg;
eventLog.prepend(event);
},
loadDLNAItems()
{
this.DLNAItems = [];
this.returnedDLNAItems.forEach(DLNAItem =>
{
if (DLNAItem.type == "server") {
this.DLNAServersLoaded = true;
this.DLNACurrentIdx = 0;
} //received a server
this.DLNAItems.push({ id: DLNAItem.id, type: DLNAItem.type, item: DLNAItem.name, content: DLNAItem.content });
if (this.config.useProxy)
{
if (DLNAItem.address != null && this.addDLNAServers) //only add a valid address if the original config was empty
{
this.addDLNAServer(DLNAItem.address)
}
}
});
if (this.DLNAIdxs[this.DLNACurrentIdx] > this.DLNAItems.length - 1) { this.DLNAIdxs[this.DLNACurrentIdx] = 0; };
//console.log("Showing DLNAitems 1", this.DLNAItems.length, " ", JSON.stringify(this.DLNAItems))
},
addDLNAServer(server)
{
this.config.DLNAs.push(server);
},
showDLNAItems() {
//show the item in this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]] in the DLNAitem div
var showItem = this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]];
if (!showItem) {
var x = 1;
}
if (showItem.type == "server") {
this.currentServerID = showItem.id;
//this.DLNAIdxs[0] = 0; //seems to defat the proper scolling of servers
//this.DLNACurrentIdx = 0;
}
var showContent = ` ${((showItem.content) ? (showItem.content.album) ? showItem.content.album : "" : "")}`;
this.DLNAItem.setAttribute("data-text", showItem.item + showContent);
if (this.config.showAlbumArt && showItem.content && showItem.content.albumArt) { this.showDLNAAlbumArt(showItem.content.albumArt) }
},
isTouchDevice() {
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
},
addButtons(controlList,controls)
{
controlList.forEach(action => {
var [icon, unDimmed] = ["", ""]
if (action == "DLNA") {
[icon, unDimmed] = ["fa-server", this.showingDLNA]; //special case as we need to be dynamic with showingDLNA
}
else {
[icon, unDimmed] = this.iconMap[action];
}
const button = document.createElement("button");
button.id = action.toLowerCase() + "Button" + this.identifier;
button.className = "fa-button tooltip-container";
button.addEventListener("click", () => this.handleAction(action));
this.setupButton(action, icon, unDimmed, button); //pass button as it may not be available yet
button.addEventListener('touchstart', handleTouchStart);
button.addEventListener('touchend', handleTouchEnd);
controls.appendChild(button);
this.addTooltip(button, action);
if (action == "Volume") //need some special handling for volume as it is not a button action
{
this.handleAction("volumechange");
} //force the correct volume icon to be showed
});
},
buildDom(controlList)
{
const wrapper = document.createElement("div");
wrapper.className = "simple-player";
wrapper.id = "simple-player";
if (this.config.showVisualiser) {
butterMeDiv = document.createElement("div");
butterMeDiv.id = "butterme";
butterMeDiv.className = "butterme";
Helpers.addBMConfig("audioPlayer", true, "butterme"); //set the config for butterme only
Helpers.addBMScript(); //writes the butterme to the page as it has to be type module!
wrapper.appendChild(butterMeDiv);
}
if (this.config.showAlbumArt) { wrapper.setAttribute("art", ""); }
if (this.config.showSlideShow)
{
//add the slidshow as a discrete div
const slideShow = document.createElement("div");
slideShow.id = "slideShow";
slideShow.className = "slideShow " + this.config.slideShowSize + "-img";
this.slideShow = new ImageSlideshow(slideShow);
this.slideShow.setDuration(this.config.slideShowDuration);
this.slideShow.setFade(this.config.slideShowFadeDuration);
if (this.config.showSlideShowControls)
{
slideShow.appendChild(this.slideShow.addControls(this));
}
wrapper.appendChild(slideShow);
}
if (this.config.showEvents || this.config.showAlbumArt) {
const eventLog = document.createElement("div");
eventLog.className = "small muted-background";
eventLog.id = "eventLog" + this.identifier;
eventLog.style.maxHeight = "6em";
eventLog.style.height = "5em";
eventLog.style.overflowY = "auto";
eventLog.style.padding = "1em";
eventLog.innerHTML = " ";
if (this.config.showEvents)
{
eventLog.innerHTML = "<strong>Event Log:</strong>";
eventLog.style.border = "1px solid #ccc";
const eventLogBody = document.createElement("div");
eventLogBody.id = "eventLogbody"+this.identifier;
eventLog.appendChild(eventLogBody);
}
wrapper.appendChild(eventLog);
}
this.audio = document.createElement("audio");
this.audio.id = "audioPlayer";
this.audio.crossOrigin = "anonymous";
const audioEvents = [
{ action: 'abort', required: false }, { action: 'canplay', required: false },
{ action: 'canplaythrough', required: false }, { action: 'durationchange', required: false },
{ action: 'emptied', required: false }, { action: 'ended', required: true },
{ action: 'suspend', required: false }, { action: 'timeupdate', required: false },
{ action: 'volumechange', required: true }, { action: 'waiting', required: false },
{ action: 'playing', required: true }, { action: 'progress', required: false },
{ action: 'ratechange', required: false }, { action: 'seeked', required: false },
{ action: 'seeking', required: false }, { action: 'stalled', required: false },
{ action: 'error', required: false }, { action: 'loadeddata', required: false },
{ action: 'loadedmetadata', required: false }, { action: 'loadstart', required: false },
{ action: 'pause', required: true }, { action: 'play', required: false },
];
var preEventType = "";
// Attach listeners for each event if required
audioEvents.forEach(eventType => {
if (eventType.required || this.config.showEvents) {
this.audio.addEventListener(eventType.action, (e) => {
if (this.config.showEvents && !(preEventType == eventType.action)) {
const timestamp = new Date().toLocaleTimeString();
this.addAudioEvent(`${timestamp} — ${eventType.action}`);
}
preEventType = eventType.action;
if (eventType.required) { this.handleAction(eventType.action); } //only handle actions that are required
});
}
});
this.audio.controls = false;
this.audio.volume = this.config.startMuted ? 0 : 0.5;
this.audio.autoplay = this.config.autoplay;
wrapper.appendChild(this.audio);
this.isPlaying = this.audio.paused ? false : true;
const controls = document.createElement("div");
controls.className = "controls " + this.config.controlsSize;
if (this.config.autoHideControls) {
controls.classList.add("SP-button-overlay");
if (!this.isTouchDevice()) { controls.classList.add("SP-button-transparent"); }
}
controls.id = "controls" + this.identifier;
if (!this.config.showMeta) { controls.className += this.audio.playing ? " pulsing-border" : " still-border"; }
const controlButtons = document.createElement("div");
controlButtons.id = "controlButtons" + this.identifier;
this.addButtons(controlList, controlButtons);
controls.appendChild(controlButtons);
wrapper.appendChild(controls);
if (this.config.showMeta) {
this.trackInfo = document.createElement("div");
this.trackInfo.id = "trackInfo";
this.trackInfo.className = "track-info small";
this.trackInfo.setAttribute("data-text", "Artist - Song Title");
this.trackInfo.innerHTML = `<i class="fas fa-music"></i>`;
controls.appendChild(this.trackInfo);
}
if (this.config.showDLNA) {
this.DLNAItem = document.createElement("div");
this.DLNAItem.id = "DLNAItem";
this.DLNAItem.className = 'track-info ' + this.config.controlsSize;
//console.log("Showing DLNAitems 2", this.DLNAItems.length)
this.showDLNAItems();
this.DLNAItem.innerHTML = ` `;
controls.appendChild(this.DLNAItem);
const DLNAControls = document.createElement("div");
DLNAControls.className = "controls " + this.config.controlsSize;;
DLNAControls.id = "DLNAControls" + this.identifier;
if (this.config.autoHideControls) {
DLNAControls.classList.add("SP-button-overlay");
if (!this.isTouchDevice()) { DLNAControls.classList.add("SP-button-transparent"); }
}
Object.entries(this.DLNAIconMap).forEach(([action, [icon, unDimmed]]) => {
const button = document.createElement("button");
button.id = action.toLowerCase() + "Button";
button.className = "fa-button tooltip-container";
button.addEventListener("click", () => this.handleDLNAAction(action));
this.setupButton(action, icon, unDimmed, button); //pass button as it may not be available yet
DLNAControls.appendChild(button);
this.addTooltip(button, action);
});
controls.appendChild(DLNAControls);
}
return wrapper;
},
getDom()
{
if (this.config.debug) console.log("GetDom");
let wrapper = document.getElementById("simple-player");
//if the wrapper doesnt exist then build it
if (wrapper == null) {
wrapper = this.buildDom(this.config.defaultplayercontrols);
}
else {
if (this.showingMini) {
this.updateControls (this.config.miniplayercontrols);
}
else {
this.updateControls(this.config.defaultplayercontrols);
}
}
return wrapper;
},
updateControls(controlList)
{
//clear the relevant button area and add those required only
let controls = document.getElementById("controlButtons" + this.identifier);
if (!controls) { return; }
while (controls.firstChild) {
// The list is LIVE so it will re-index each call
controls.removeChild(controls.firstChild);
}
this.addButtons(controlList, controls);
},
addTooltip(buttonElement, toolTip)
{
const tooltip = document.createElement("span");
tooltip.className = "tooltip-text";
tooltip.innerText = toolTip.replace(/([A-Z])/g, ' $1');
buttonElement.appendChild(tooltip);
},
showPlayPause(action)
{
//get the Play/Pause button element so we can change its inner html to the correct icon
const playPauseButton = document.getElementById("playButton" + this.identifier);
//need the tooltip added here!!
if (action === "playing")
{
playPauseButton.innerHTML = '<i class="fas fa-pause" aria-hidden="true"></i>';
this.addTooltip(playPauseButton, 'Pause');
}
else if (action === "pause" || action === "Stop")
{
playPauseButton.innerHTML = '<i class="fas fa-play" aria-hidden="true"></i>';
this.addTooltip(playPauseButton, 'Play');
}
},
setupButton(action, icon, unDimmed, buttonElement,visible=false)
{
//if button element passed always use it
if (buttonElement) {
buttonT = buttonElement;
}
else
{
var buttonT = document.getElementById(action.toLowerCase() + "Button" + this.identifier);
}
//use the order of classes to ensure that the undimmed comes at the correct point
buttonT.innerHTML = `<i id="${action}icon${this.identifier}" class="fas ${unDimmed ? "" : "dimmedButton"} controlButton ${icon}" aria-hidden="true"></i>`;
this.addTooltip(buttonT, action);
},
handleDLNAAction(action)
{
if (this.config.showEvents) { this.addLogEntry(`Handling: ${action}`); }
//the following actions control how the DLNAItems are displayed within the DLNAItem div
//if there are no DLNA items then the DLNAItem div will not be displayed
//the DLNA items is a one dimension list of names of the DLNA items, that can be scrolled up and down using the ScrollDown and ScrollUp actions.
//if the scrollRight or scrollLeft action is handled, then the current displayed item and the action will be passed to the node helper to get the next list DLNA items
switch (action) {
case "ScrollDown":
//scroll down the DLNA items, if there are more items to display using the idx and length
this.DLNAIdxs[this.DLNACurrentIdx]++;
//this.DLNAItemsCurrentDisplayIdx++
this.DLNAIdxs[this.DLNACurrentIdx] = (this.DLNAIdxs[this.DLNACurrentIdx] > (this.DLNAItems.length - 1)) ? 0 : this.DLNAIdxs[this.DLNACurrentIdx]
//console.log("Showing DLNAitems 3", this.DLNAItems.length)
this.showDLNAItems();
return;
case "ScrollUp":
//scroll up the DLNA items, if there are more items to display
this.DLNAIdxs[this.DLNACurrentIdx]--;
this.DLNAIdxs[this.DLNACurrentIdx] = (this.DLNAIdxs[this.DLNACurrentIdx] < 0) ? (this.DLNAItems.length - 1) : this.DLNAIdxs[this.DLNACurrentIdx];
//console.log("Showing DLNAitems 4", this.DLNAItems.length)
this.showDLNAItems();
return;
case "Add":
//add the current displayed item and all children to the DLNA playlist
this.sendNotificationToNodeHelper("ADD_DLNA_ITEM", { action: action, currentServerID: this.currentServerID, item: this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]], returnPlaylist: this.showingDLNA });
return;
case "Remove":
//removes the current displayed item and all children from the DLNA playlist
this.sendNotificationToNodeHelper("REMOVE_DLNA_ITEM", { action: action, currentServerID: this.currentServerID, item: this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]], returnPlaylist: this.showingDLNA });
return;
case "Clear":
//clears the current DLNA playlist
this.DLNACurrentIdx = 0;
this.DLNAIdxs = [0];
this.sendNotificationToNodeHelper("CLEAR_DLNA_PLAYLIST", { returnPlaylist: this.showingDLNA });
return;
case "Save":
//saves the current DLNA playlist to a file
this.sendNotificationToNodeHelper("SAVE_DLNA_PLAYLIST", { musicDirectory:this.config.musicDirectory, DLNAPlaylistName:this.config.DLNAPlaylistName });
return;
case "Open":
//opens a saved DLNA playlist file and loads it into the DLNA playlist
this.sendNotificationToNodeHelper("OPEN_DLNA_PLAYLIST", { musicDirectory: this.config.musicDirectory, DLNAPlaylistName: this.config.DLNAPlaylistName, returnPlaylist: this.showingDLNA });
return;
case "ScrollLeft":
//if the current item displayed is a server cant scroll left
if (this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]].type == "server" || !this.DLNAServersLoaded) { return; }
this.sendNotificationToNodeHelper("DLNA_ACTION", { action: action, currentServerID: this.currentServerID, item: this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]], returnPlaylist: this.showingDLNA });
this.DLNACurrentIdx--;
return;
case "ScrollRight":
//if current type is media, or no servers loaded yet, cant scroll right
if (this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]].type == "media" || !this.DLNAServersLoaded) { return; }
this.sendNotificationToNodeHelper("DLNA_ACTION", { action: action, currentServerID: this.currentServerID, item: this.DLNAItems[this.DLNAIdxs[this.DLNACurrentIdx]], returnPlaylist: this.showingDLNA });
this.DLNACurrentIdx++;
if (this.DLNACurrentIdx > this.DLNAIdxs.length-1) { this.DLNAIdxs[this.DLNACurrentIdx] = 0; }
return;
}
},
handleAction(action)
{
if (this.config.showEvents)
{
this.addLogEntry(`Handling: ${action}`);
}
//as some actions are triggered by actions on a button that we want to change, then map action to button name
//some buttons may not loaded due to config; check them when attempting to set icon values
var bAction = action;
if (action == "volumechange") { bAction = "Volume"; } //volumechange is a special case as it is triggered by the audio element, not a button)
const Icon = document.getElementById(bAction+"icon"+this.identifier);
switch (action) {
case "ssNext":
this.slideShow.next();
return;
case "ssPrev":
this.slideShow.prev();
return;
case "ssStop":
this.slideShow.stop();
return;
case "ssPlay":
this.slideShow.play();
return;
case "ssPlay":
this.slideShow.pause();
return;
case "MiniPlayer":
this.showingMini = !this.showingMini;
this.updateDom();
return;
case "ended":
//play the next track in the playlist, if at end start again if repeat is enabled
if (this.currentTrack + 1 >= this.config.playlist.length) {
if (this.config.repeat) {
this.handleAction("Next");
}
else {
this.currentTrack = 0; //reset to the first track
}
}
else
{
this.handleAction("Next");
}
return;
case "Shuffle":
// Toggles the shuffle mode, and randomises the playlist if shuffle is now enabled
this.config.shuffle = !this.config.shuffle;
this.radomisePlaylist(this.config.shuffle);
if (this.config.showEvents) { this.addLogEntry(`Handling: ${this.config.playlistOrder}`); }
this.setupButton(action, this.iconMap[action][0], this.config.shuffle,null);
return;
case "Repeat":
// Toggles the repeat mode
this.config.repeat = !this.config.repeat;
this.setupButton(action, this.iconMap[action][0], this.config.repeat,null);
return;
case "playing":
this.showPlayPause(action);
this.isPlaying = true;
if (!this.config.showMeta) { this.setBorder(); }
return;
case "pause":
this.showPlayPause(action);
this.isPlaying = false;
if (!this.config.showMeta) { this.setBorder(); }
return;
case "volumechange":
if (this.config.showEvents) { this.addLogEntry(`Event volumeChange: ${this.audio.volume}`); }
if (Icon)
{
if (this.audio.volume > 0 && this.audio.volume < 0.51) {
Icon.className = "fas fa-volume-low";
}
else if (this.audio.volume > 0.5) {
Icon.className = "fas fa-volume-high";
}
else {
Icon.className = "fas fa-volume-off";
}
}
return;
case "Volume":
if (this.audio.volume == 0)
{
this.audio.volume = 0.5;
}
else if (this.audio.volume > 0.5)
{
this.audio.volume = 0;
}
else
{
this.audio.volume = 1;
}
return;
case "Back":
this.currentTrack = (this.currentTrack - 1 + this.config.playlist.length) % this.config.playlist.length;
this.setAudioSrc(this.config.playlist[this.config.playlistOrder[this.currentTrack]]);
//if not autoplay then make sure that the play control is showing the play icon and the track is teeded up ready to go
if (!this.config.autoplay) {
this.handleAction("Stop");
}
return;
case "Next":
this.currentTrack = (this.currentTrack + 1) % this.config.playlist.length;
this.setAudioSrc(this.config.playlist[this.config.playlistOrder[this.currentTrack]]);
//if not autoplay then make sure that the play control is showing the play icon and the track is teeded up ready to go
if (!this.config.autoplay) {
this.handleAction("Stop");
}
return;
case "Play":
if (!this.audio.error) {
if (this.audio.paused) {
if (this.audio.volume == 0) { this.audio.volume = 0.5; } // Set volume to 0.5 when playing if it was muted
if (!this.audio.error)
this.audio.play();
this.isPlaying = true;
} else {
this.audio.pause();
this.isPlaying = false;
}
}
return;
case "Stop":
this.audio.pause();
this.audio.currentTime = 0;
this.showPlayPause(action);
this.isPlaying = false;
return;
case "DLNA":
if (!this.config.showDLNA) { return;}
this.showingDLNA = !this.showingDLNA;
this.setupButton(action, this.iconMap[action][0], this.showingDLNA, null);
//now tell node helper to toggle the DLNA playlist depending on the showingDLNA state
this.requestTracks();
return;
}
},
showAlbumArt()
{
if (!this.config.art || !this.config.art[this.config.playlistOrder[this.currentTrack]]) { return; } //check we have actually got some art to load
var sp = document.getElementById("eventLog" + this.identifier);
sp.setAttribute("art", this.config.art[this.config.playlistOrder[this.currentTrack]]);
sp.style.backgroundImage = `url("${this.config.art[this.config.playlistOrder[this.currentTrack]]}")`;
sp.style.backgroundSize = 'contain';
sp.style.backgroundPosition = 'center';
sp.style.backgroundRepeat = 'no-repeat';
},
showDLNAAlbumArt(url)
{
var sp = document.getElementById("eventLog" + this.identifier);
sp.setAttribute("art", url);
sp.style.backgroundImage = `url("${url}")`;
sp.style.backgroundSize = 'contain';
sp.style.backgroundPosition = 'center';
sp.style.backgroundRepeat = 'no-repeat';
},
requestServers()
{
this.sendNotificationToNodeHelper("GET_DLNA_SERVERS", null);
},
requestTracks() {
if (!this.showingDLNA) {
if (this.config.playTracks) {
if (this.config.showEvents) { this.addLogEntry(`sending: SCAN_DIRECTORY`); }
this.sendNotificationToNodeHelper("SCAN_DIRECTORY", this.config.musicDirectory);
}
else if (this.config.usePlaylist) {
if (this.config.showEvents) { this.addLogEntry(`sending: LOAD_PLAYLIST`); }
this.sendNotificationToNodeHelper("LOAD_PLAYLIST", [this.config.musicDirectory, this.config.playlistName]);
}
else {
this.config.playlist = [];
this.handleAction("Stop");
}
}
else {
if (this.config.showEvents) { this.addLogEntry(`sending: GET_DLNA_PLAYLIST`); }
this.sendNotificationToNodeHelper("GET_DLNA_PLAYLIST", null);
}
},
setBorder() {
const controls = document.getElementById("controls" + this.identifier);
//replace the border class old,with new
controls.classList.replace("still-border", !this.audio.paused ? "pulsing-border" : "still-border")
controls.classList.replace("pulsing-border", !this.audio.paused ? "pulsing-border" : "still-border")
;
},
getTrackInfo() {
if (!this.config.playlist || !this.config.playlist[this.currentTrack]) { return; }
if (this.config.showMeta) {
this.sendNotificationToNodeHelper("GET_METADATA", this.config.playlist[this.config.playlistOrder[this.currentTrack]]);