-
Notifications
You must be signed in to change notification settings - Fork 1
/
html5-playlist.js
53 lines (50 loc) · 2.23 KB
/
html5-playlist.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
(function (document, window) {
// html5-playlist.js
// (c) Severák 2021
// MIT-licensed
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
document.querySelectorAll('audio').forEach(function (audio) {
if (audio.getAttribute('data-playlist-id') && document.getElementById(audio.getAttribute('data-playlist-id'))) {
var playlist = document.getElementById(audio.getAttribute('data-playlist-id')).querySelectorAll('li a');
var playlistTag = document.getElementById(audio.getAttribute('data-playlist-id'));
if (playlist.length) {
playlist[0].classList.add('current-track');
var playingSongNo = 0;
audio.addEventListener('ended', function () {
// track ended, skip to next:
playlist.forEach(function (elem) {
elem.classList.remove('current-track');
});
playingSongNo++;
if (playlist[playingSongNo]) {
audio.src = playlist[playingSongNo].href;
playlist[playingSongNo].classList.add('current-track');
audio.play();
}
});
playlistTag.addEventListener('click', function (event) {
// track name clicked, play it:
event.preventDefault();
var ord = 0;
playlist.forEach(function (elem) {
elem.classList.remove('current-track');
if (elem == event.target) {
playingSongNo = ord;
audio.src = playlist[playingSongNo].href;
playlist[playingSongNo].classList.add('current-track');
audio.play();
}
ord++;
});
})
}
}
});
})(document, window);