diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-15 05:36:50 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-15 05:36:50 +0100 |
| commit | 7ad469291c015b33a2d20587db26b9621ed82d00 (patch) | |
| tree | 83e2a56822033a638d03ff7ddf4bfee3181631e6 /public/assets/js/lib/views/details/audio.js | |
| parent | cc585396a85e3107bb7b4298098b84b738919c8f (diff) | |
sort file list by name or date, updates audio player
Diffstat (limited to 'public/assets/js/lib/views/details/audio.js')
| -rw-r--r-- | public/assets/js/lib/views/details/audio.js | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/public/assets/js/lib/views/details/audio.js b/public/assets/js/lib/views/details/audio.js index 42f5376..e328a07 100644 --- a/public/assets/js/lib/views/details/audio.js +++ b/public/assets/js/lib/views/details/audio.js @@ -3,24 +3,35 @@ var audio = (function(){ var el, music = [], current_index = -1 var links, comment, parent + var selected = false var playing = false audio.init = function () { - links = document.querySelectorAll("a") comment = document.querySelector("#comment") parent = document.querySelector("#audio") - - Array.prototype.slice.apply(links).forEach(function(url){ - if (! url.href.match(/(mp3|wav|ogg)/)) return - url.dataset.index = music.length - music.push(url) - }) + + audio.index() audio.build() } + audio.index = function () { + music = [] + var links = document.querySelectorAll("a") + Array.prototype.slice.apply(links).forEach(function(link){ + if (! link.href.match(/(mp3|wav|ogg)/)) return + link.dataset.index = music.length + music.push(link) + if (playing && link.href === el.src) { + current_index = parseInt(link.dataset.index) + } + }) + if (playing) { + audio.set_cursor() + } + } audio.build = function () { el = document.createElement("audio") el.setAttribute("controls", true) - el.addEventListener("loadeddata", () => { if (playing) el.play() }) + el.addEventListener("loadeddata", () => { if (selected) el.play() }) el.addEventListener("ended", audio.next) el.src = music[0] parent.appendChild(el) @@ -33,10 +44,14 @@ var audio = (function(){ document.body.removeEventListener("keydown", audio.keydown) } audio.play = function (index) { + playing = true current_index = (parseInt(index) + music.length) % music.length el.src = music[current_index].href - playing = document.querySelector(".playing") - if (playing) playing.classList.remove("playing") + 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 (){ @@ -50,6 +65,13 @@ var audio = (function(){ 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') + } + if (element_is_text_input(document.activeElement)) { + return + } if (app.typing || e.ctrlKey || e.altKey || e.metaKey || e.shiftKey) return switch (e.keyCode) { case 37: // left |
