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.js101
1 files changed, 62 insertions, 39 deletions
diff --git a/public/assets/js/lib/views/details/audio.js b/public/assets/js/lib/views/details/audio.js
index fb72e0b..2329886 100644
--- a/public/assets/js/lib/views/details/audio.js
+++ b/public/assets/js/lib/views/details/audio.js
@@ -1,26 +1,27 @@
-var audio = (function () {
- var audio = {};
+const audio = (function () {
+ const audio = {};
- var el,
- music = [],
- current_index = -1;
- var links, comment, parent;
- var selected = false;
- var playing = false;
- var built = false;
- var initted = false;
+ 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;
}
- comment = document.querySelector("#comment");
parent = document.querySelector("#audio");
audio.index();
audio.build();
- el.src = music[0];
+ if (music.length) {
+ audio.el.src = music[0];
+ }
initted = true;
};
@@ -32,7 +33,7 @@ var audio = (function () {
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) {
+ if (playing && link.href === audio.el.src) {
current_index = parseInt(link.dataset.index);
}
});
@@ -44,28 +45,55 @@ var audio = (function () {
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);
+ audio.el = document.createElement("audio");
+ audio.el.setAttribute("controls", true);
+ // audio.el.addEventListener("loadeddata", () => { if (selected) audio.el.play() })
+ parent.appendChild(audio.el);
document.body.addEventListener("keydown", audio.keydown);
};
audio.destroy = function () {
- el.pause();
- el = null;
- parent.removeChild(el);
+ 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) {
playing = true;
current_index = (parseInt(index) + music.length) % music.length;
- el.src = music[current_index].href;
- el.play();
+ 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) {
+ ui.onPlay(music[current_index]);
+ }
+ } else {
+ audio.el.pause();
+ if (ui) {
+ ui.onPause();
+ }
+ }
};
audio.set_cursor = function () {
@@ -84,16 +112,11 @@ var audio = (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();
+ audio.keydown = function (event) {
+ function element_is_text_input(element) {
+ var tagName = element.tagName.toLowerCase();
return (
- (tagName == "input" && el.type == "text") ||
+ (tagName == "input" && element.type == "text") ||
tagName == "textarea" ||
tagName == "button"
);
@@ -101,24 +124,24 @@ var audio = (function () {
if (element_is_text_input(document.activeElement)) {
return;
}
- if (app.typing || e.ctrlKey || e.altKey || e.metaKey) return;
- switch (e.keyCode) {
+ if (app.typing || event.ctrlKey || event.altKey || event.metaKey) return;
+ switch (event.keyCode) {
case 37: // left
- if (e.shiftKey) {
- el.currentTime -= 10;
+ if (event.shiftKey) {
+ audio.el.currentTime -= 10;
} else {
audio.prev();
}
break;
case 39: // right
- if (e.shiftKey) {
- el.currentTime += 10;
+ if (event.shiftKey) {
+ audio.el.currentTime += 10;
} else {
audio.next();
}
break;
case 32: // spacebar
- e.preventDefault();
+ event.preventDefault();
audio.toggle();
break;
}