diff options
| -rw-r--r-- | public/assets/js/lib/views/details/audio.js | 154 |
1 files changed, 80 insertions, 74 deletions
diff --git a/public/assets/js/lib/views/details/audio.js b/public/assets/js/lib/views/details/audio.js index 6087b6b..0f81bb9 100644 --- a/public/assets/js/lib/views/details/audio.js +++ b/public/assets/js/lib/views/details/audio.js @@ -1,104 +1,110 @@ -var audio = (function(){ - var audio = {} - - var el, music = [], current_index = -1 - var links, comment, parent - var selected = false - var playing = false - var built = false +var audio = (function () { + var audio = {}; + + var el, + music = [], + current_index = -1; + var links, comment, parent; + var selected = false; + var playing = false; + var built = false; audio.init = function () { - comment = document.querySelector("#comment") - parent = document.querySelector("#audio") - - audio.index() - audio.build() - el.src = music[0] - } + comment = document.querySelector("#comment"); + parent = document.querySelector("#audio"); + + audio.index(); + audio.build(); + el.src = music[0]; + }; audio.index = function () { - music = [] - 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(link) + music = []; + 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(link); if (playing && link.href === el.src) { - current_index = parseInt(link.dataset.index) + current_index = parseInt(link.dataset.index); } - }) + }); if (playing) { - audio.set_cursor() + audio.set_cursor(); } - } + }; audio.build = function () { - if (built) return - built = true - el = audio.el = document.createElement("audio") - el.setAttribute("controls", true) - el.addEventListener("loadeddata", () => { if (selected) el.play() }) - el.addEventListener("ended", audio.next) - parent.appendChild(el) - document.body.addEventListener("keydown", audio.keydown) - } + if (built) return; + built = true; + el = audio.el = document.createElement("audio"); + el.setAttribute("controls", true); + // el.addEventListener("loadeddata", () => { if (selected) el.play() }) + el.addEventListener("ended", audio.next); + parent.appendChild(el); + document.body.addEventListener("keydown", audio.keydown); + }; audio.destroy = function () { - el.pause() - el = null - parent.removeChild(el) - document.body.removeEventListener("keydown", audio.keydown) - built = false - } + el.pause(); + el = null; + parent.removeChild(el); + document.body.removeEventListener("keydown", audio.keydown); + built = false; + }; audio.play = function (index) { - playing = true - current_index = (parseInt(index) + music.length) % music.length - el.src = music[current_index].href - audio.set_cursor() - } + playing = true; + current_index = (parseInt(index) + music.length) % music.length; + el.src = music[current_index].href; + el.play(); + audio.set_cursor(); + }; audio.set_cursor = function () { - selected = document.querySelector(".playing") - if (selected) selected.classList.remove("playing") - music[current_index].classList.add("playing") - } - audio.prev = function (){ - audio.play(current_index - 1) - } - audio.next = function (){ - audio.play(current_index + 1) - } - audio.toggle = function (){ - if (el.paused) el.play() - else el.pause() - } - audio.keydown = function(e){ + selected = document.querySelector(".playing"); + if (selected) selected.classList.remove("playing"); + music[current_index].classList.add("playing"); + }; + audio.prev = function () { + audio.play(current_index - 1); + }; + audio.next = function () { + audio.play(current_index + 1); + }; + audio.toggle = function () { + if (el.paused) el.play(); + else el.pause(); + }; + audio.keydown = function (e) { function element_is_text_input(el) { - var tagName = el.tagName.toLowerCase() - return (tagName == 'input' && el.type == 'text' || tagName == 'textarea' || tagName == 'button') + var tagName = el.tagName.toLowerCase(); + return ( + (tagName == "input" && el.type == "text") || + tagName == "textarea" || + tagName == "button" + ); } if (element_is_text_input(document.activeElement)) { - return + return; } - if (app.typing || e.ctrlKey || e.altKey || e.metaKey) return + if (app.typing || e.ctrlKey || e.altKey || e.metaKey) return; switch (e.keyCode) { case 37: // left if (e.shiftKey) { - el.currentTime -= 10 + el.currentTime -= 10; } else { - audio.prev() + audio.prev(); } break; case 39: // right if (e.shiftKey) { - el.currentTime += 10 + el.currentTime += 10; } else { - audio.next() + audio.next(); } break; case 32: // spacebar - e.preventDefault() - audio.toggle() + e.preventDefault(); + audio.toggle(); break; } - } - - return audio + }; -})()
\ No newline at end of file + return audio; +})(); |
