summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/details/audio.js
blob: 30c5efdfd6877286c3589d80b175f7f6e53507c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 initted = false;

  audio.init = function () {
    if (initted) {
      audio.index();
      return;
    }
    comment = document.querySelector("#comment");
    parent = document.querySelector("#audio");

    audio.index();
    audio.build();
    el.src = music[0];
    initted = true;
  };

  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);
      if (playing && link.href === el.src) {
        current_index = parseInt(link.dataset.index);
      }
    });
    if (playing) {
      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);
  };

  audio.destroy = function () {
    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;
    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) {
    function element_is_text_input(el) {
      var tagName = el.tagName.toLowerCase();
      return (
        (tagName == "input" && el.type == "text") ||
        tagName == "textarea" ||
        tagName == "button"
      );
    }
    if (element_is_text_input(document.activeElement)) {
      return;
    }
    if (app.typing || e.ctrlKey || e.altKey || e.metaKey) return;
    switch (e.keyCode) {
      case 37: // left
        if (e.shiftKey) {
          el.currentTime -= 10;
        } else {
          audio.prev();
        }
        break;
      case 39: // right
        if (e.shiftKey) {
          el.currentTime += 10;
        } else {
          audio.next();
        }
        break;
      case 32: // spacebar
        e.preventDefault();
        audio.toggle();
        break;
    }
  };

  return audio;
})();