X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/8c60a1ba0c4b0d563a641ed2c4853cf17690f0ad..60123f2c3cb55d31b9ce973a794a0edbe2a282b9:/src/catalogue/static/player/player.js diff --git a/src/catalogue/static/player/player.js b/src/catalogue/static/player/player.js index d80446318..69870bb34 100644 --- a/src/catalogue/static/player/player.js +++ b/src/catalogue/static/player/player.js @@ -19,16 +19,17 @@ media['mp3'] = li.attr('data-mp3'); media['oga'] = li.attr('data-ogg'); + media['id'] = li.attr('data-media-id'); $(".title", $root).html(li.html()); - $root.attr('data-media-id', li.attr('data-media-id')); player.jPlayer("setMedia", media); player.jPlayer("pause", time); + return player; }; $('.play-next', $root).click(function() { var next = parseInt($number.text()) + 1; - var p = $('.play:eq(' + next + ')', $root); + var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root); if (p.length) { setMedia(p).jPlayer("play"); $number.text(next) @@ -38,32 +39,53 @@ var next = parseInt($number.text()) - 1; if (next < 1) return; - var p = $('.play:eq(' + next + ')', $root); + var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root); setMedia(p).jPlayer("play"); $number.text(next) }); - var initialElem = $('.play', $root).first(); + var initialElem = $('.jp-playlist .play', $root).first(); var initialTime = 0; if (Modernizr.localstorage) { - audiobooks = JSON.parse(localStorage["audiobook-history"]); - last = audiobooks[$root.attr("data-book-id")] + try { + audiobooks = JSON.parse(localStorage["audiobook-history"]); + } catch { + audiobooks = {}; + } + last = audiobooks[$root.attr("data-book-slug")] + // Fallback for book id; + if (!last) { + last = audiobooks[$root.attr("data-book-id")] + } + if (last) { initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first(); initialTime = last[2]; + $number.text($(".jp-playlist .play", $root).index(initialElem) + 1); } } setMedia(initialElem, initialTime); }, timeupdate: function(event) { - if (Modernizr.localstorage) { + if (event.jPlayer.status.currentTime && Modernizr.localstorage) { try { audiobooks = JSON.parse(localStorage["audiobook-history"]); } catch { audiobooks = {}; } - audiobooks[$root.attr("data-book-id")] = [Date.now(), $root.attr("data-media-id"), event.jPlayer.status.currentTime]; + t = event.jPlayer.status.currentTime; + if (t && event.jPlayer.status.duration - t > 10) { + audiobooks[$root.attr("data-book-slug")] = [ + Date.now(), + event.jPlayer.status.media.id, + event.jPlayer.status.currentTime + ]; + } else { + delete audiobooks[$root.attr("data-book-slug")]; + } + // Remove old book id, if present. + delete audiobooks[$root.attr("data-book-id")]; localStorage["audiobook-history"] = JSON.stringify(audiobooks); } }