X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/3be2da7359a9e68a577aa28a6a9d2ff0d3e0d8e4..09ac89c30a2ab3eea1995ac78e72eae94c7a3f39:/src/catalogue/static/player/player.js diff --git a/src/catalogue/static/player/player.js b/src/catalogue/static/player/player.js index a41d2c0ae..64a21d281 100644 --- a/src/catalogue/static/player/player.js +++ b/src/catalogue/static/player/player.js @@ -13,15 +13,18 @@ ready: function() { var player = $(this); - var setMedia = function(elem) { + var setMedia = function(elem, time=0) { var li = $(elem).parent(); var media = {} media['mp3'] = li.attr('data-mp3'); media['oga'] = li.attr('data-ogg'); + media['id'] = li.attr('data-media-id'); $(".title", $root).html(li.html()); - return player.jPlayer("setMedia", media); + player.jPlayer("setMedia", media); + player.jPlayer("pause", time); + return player; }; $('.play-next', $root).click(function() { @@ -41,8 +44,49 @@ $number.text(next) }); - setMedia($('.play', $root).first()); + var initialElem = $('.play', $root).first(); + var initialTime = 0; + if (Modernizr.localstorage) { + 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]; + } + } + setMedia(initialElem, initialTime); + }, + timeupdate: function(event) { + if (event.jPlayer.status.currentTime && Modernizr.localstorage) { + try { + audiobooks = JSON.parse(localStorage["audiobook-history"]); + } catch { + audiobooks = {}; + } + 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); + } } }); });