From: Radek Czajka Date: Thu, 2 Jul 2020 15:39:01 +0000 (+0200) Subject: Remember audiobook position in localStorage. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/8c60a1ba0c4b0d563a641ed2c4853cf17690f0ad?ds=inline Remember audiobook position in localStorage. --- diff --git a/src/catalogue/static/player/player.js b/src/catalogue/static/player/player.js index a41d2c0ae..d80446318 100644 --- a/src/catalogue/static/player/player.js +++ b/src/catalogue/static/player/player.js @@ -13,7 +13,7 @@ ready: function() { var player = $(this); - var setMedia = function(elem) { + var setMedia = function(elem, time=0) { var li = $(elem).parent(); var media = {} @@ -21,7 +21,9 @@ media['oga'] = li.attr('data-ogg'); $(".title", $root).html(li.html()); - return player.jPlayer("setMedia", media); + $root.attr('data-media-id', li.attr('data-media-id')); + player.jPlayer("setMedia", media); + player.jPlayer("pause", time); }; $('.play-next', $root).click(function() { @@ -41,8 +43,29 @@ $number.text(next) }); - setMedia($('.play', $root).first()); + var initialElem = $('.play', $root).first(); + var initialTime = 0; + if (Modernizr.localstorage) { + audiobooks = JSON.parse(localStorage["audiobook-history"]); + 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 (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]; + localStorage["audiobook-history"] = JSON.stringify(audiobooks); + } } }); }); diff --git a/src/catalogue/templates/catalogue/snippets/jplayer.html b/src/catalogue/templates/catalogue/snippets/jplayer.html index 50e024206..96d359141 100644 --- a/src/catalogue/templates/catalogue/snippets/jplayer.html +++ b/src/catalogue/templates/catalogue/snippets/jplayer.html @@ -1,6 +1,6 @@ {% load i18n catalogue_tags %} {% if audiobooks %}{% if book|status:request.user != 'closed' %} -
+