summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/details/audio.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/js/lib/views/details/audio.js')
-rw-r--r--public/assets/js/lib/views/details/audio.js42
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