Add a "Previous" button to the player
[app-musicexpo.git] / player.js
index c81e8fabcecd78ae2b205460ed5162e19f8d1e1a..7adcdd838d4cf4b58754f0174a8f0ca8a2e7f0e8 100644 (file)
--- a/player.js
+++ b/player.js
@@ -6,7 +6,9 @@ var TYPES = {
        "MP3": "audio/mpeg"
 };
 
-var audio, details, start, data;
+var audio, details, start, prev, data;
+var hash_to_id = {}, inhibit_handle_hash = false;
+var hist = []
 
 function load_song (id) {
        audio.style.display = "inline";
@@ -24,30 +26,56 @@ function load_song (id) {
        }
 
        details.innerHTML = "Now playing: " + song.artist + " - " + song.title;
+       inhibit_handle_hash = true;
+       location.hash = song.hash;
+       inhibit_handle_hash = false;
        audio.load();
 }
 
 function play_random () {
        start.innerHTML = "Next";
        var id = Math.floor(Math.random() * data.length);
+       hist.push(id);
+       if(hist.length > 1)
+               prev.disabled = false;
        load_song(id);
        audio.play();
 }
 
-function make_onclick_handler (id){
-       return function () {
-               load_song(id);
-               start.innerHTML = "Next";
-               audio.play();
-       }
+function play_random_after_error () {
+       hist.pop(); /* Purge offending song from history */
+       play_random();
+}
+
+function play_prev () {
+       hist.pop();
+       var song = hist[hist.length - 1];
+       console.log("Will now play: " + song)
+       if(hist.length <= 1)
+               prev.disabled = true;
+       load_song(song);
+       audio.play();
+}
+
+function handle_hash(first_run){
+       if(!hash_to_id.hasOwnProperty(location.hash) || inhibit_handle_hash)
+               return;
+       var id = hash_to_id[location.hash]
+       if(first_run === true)
+               hist.push(id)
+       load_song(id);
+       start.innerHTML = "Next";
+       audio.play();
 }
 
 window.onload = function () {
        var container = document.getElementById("player");
-       container.innerHTML = '<div id="details"></div> <button id="start_player">Play a random song</button> (or click a song title)<br><audio id="audio" controls></audio>';
+       container.innerHTML = '<div id="details"></div> <button id="prev_player">Previous</button><button id="start_player">Play a random song</button> (or click a song title)<br><audio id="audio" controls></audio>';
        audio = document.getElementById("audio");
        details = document.getElementById("details");
        start = document.getElementById("start_player");
+       prev  = document.getElementById("prev_player")
+       prev.disabled = true;
 
        data = [];
        var trs = document.querySelectorAll("tbody tr");
@@ -56,6 +84,7 @@ window.onload = function () {
                var song = {
                        "artist": tr.getElementsByClassName("artist")[0].textContent,
                        "title": tr.getElementsByClassName("title")[0].textContent,
+                       "hash": tr.getElementsByTagName("a")[0].dataset.hash,
                        "formats": []
                };
                var formats = tr.getElementsByClassName("formats")[0].getElementsByTagName("a");
@@ -67,11 +96,14 @@ window.onload = function () {
                        });
                }
                data.push(song);
-               tr.getElementsByClassName("title")[0].onclick = make_onclick_handler(i);
+               hash_to_id[song.hash] = i;
        }
 
        audio.style.display = "none";
        audio.addEventListener('ended', play_random);
-       audio.addEventListener('error', play_random);
+       audio.addEventListener('error', play_random_after_error);
        start.addEventListener('click', play_random);
+       prev.addEventListener('click', play_prev);
+       window.onhashchange = handle_hash;
+       handle_hash(true);
 };
This page took 0.011358 seconds and 4 git commands to generate.