const audio = (function () { const audio = {}; let music = []; let current_index = -1; let links, parent; let selected = false; let playing = false; let built = false; let initted = false; let ui = null; audio.init = function () { if (initted) { audio.index(); return; } parent = document.querySelector("#audio"); audio.index(); audio.build(); if (music.length) { audio.el.src = music[0].href; } initted = true; }; audio.index = function (files) { music = []; current_index = -1; if (files) { music = files .filter((file) => AUDIO_REGEXP.test(file.filename)) .map((file) => ({ href: make_link(file), title: file.filename.replace(AUDIO_REGEXP, ""), })); } else { var links = document.querySelectorAll("a"); Array.prototype.slice.apply(links).forEach(function (link) { if (!link.href.match(/\.(mp3|wav|aiff?|m4a|ogg|opus|flac)$/)) return; link.dataset.index = music.length; music.push({ el: link, href: link.href, title: link.innerText, }); if (playing && link.href === audio.el.src) { current_index = parseInt(link.dataset.index); } }); } // console.log(music); if (playing) { audio.set_cursor(); } }; audio.build = function () { if (built) return; built = true; audio.el = document.createElement("audio"); audio.el.setAttribute("controls", true); // audio.el.addEventListener("loadeddata", () => { if (selected) audio.el.play() }) audio.el.addEventListener("ended", this.nextUnlessFinished.bind(this)); parent.appendChild(audio.el); document.body.addEventListener("keydown", audio.keydown); }; audio.destroy = function () { audio.el.pause(); parent.removeChild(audio.el); audio.el = null; document.body.removeEventListener("keydown", audio.keydown); built = false; }; audio.listen = function (uiController) { ui = uiController; }; audio.play = function (index) { current_index = (parseInt(index) + music.length) % music.length; if (!music[current_index]) { return; } playing = true; audio.el.src = music[current_index].href; audio.el.play(); audio.set_cursor(); if (ui) { ui.onPlay(music[current_index]); } }; audio.pause = function () { audio.el.pause(); if (ui) { ui.onPause(); } }; audio.toggle = function () { if (audio.el.paused) { audio.el.play(); if (ui && music[current_index]) { ui.onPlay(music[current_index]); } } else { audio.el.pause(); if (ui) { ui.onPause(); } } }; audio.set_cursor = function () { selected = document.querySelector(".playing"); if (selected) selected.classList.remove("playing"); if (current_index > -1 && music[current_index].el) { music[current_index].el.classList.add("playing"); } }; audio.prev = function () { audio.play(current_index - 1); }; audio.next = function () { audio.play(current_index + 1); }; audio.nextUnlessFinished = function () { if (current_index < music.length - 1) { audio.next(); } }; audio.keydown = function (event) { function element_is_text_input(element) { var tagName = element.tagName.toLowerCase(); return ( (tagName == "input" && element.type == "text") || tagName == "textarea" || tagName == "button" ); } if (element_is_text_input(document.activeElement)) { return; } if (app.typing || event.ctrlKey || event.altKey || event.metaKey) return; if (!music[current_index]) return; switch (event.keyCode) { case 37: // left if (event.shiftKey) { audio.el.currentTime -= 10; } else { audio.prev(); } break; case 39: // right if (event.shiftKey) { audio.el.currentTime += 10; } else { audio.next(); } break; case 32: // spacebar event.preventDefault(); audio.toggle(); break; } }; return audio; })();