Update to the way last audiobooks are stored.
authorRadek Czajka <rczajka@rczajka.pl>
Tue, 14 Jul 2020 13:16:04 +0000 (15:16 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 10 Sep 2020 12:15:11 +0000 (14:15 +0200)
src/catalogue/static/player/player.js
src/catalogue/templates/catalogue/snippets/jplayer.html

index d93bbad..6399379 100644 (file)
@@ -19,9 +19,9 @@
 
                     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);
                 };
                     } catch {
                         audiobooks = {};
                     }
-                    last = audiobooks[$root.attr("data-book-id")]
+                    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];
             },
 
             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);
                 }
             }
index 96d3591..4398746 100644 (file)
@@ -1,6 +1,6 @@
 {% load i18n catalogue_tags %}
 {% if audiobooks %}{% if book|status:request.user != 'closed' %}
-  <div class="jp-type-playlist" data-book-id="{{ book.pk }}">
+  <div class="jp-type-playlist" data-book-id="{{ book.pk }}" data-book-slug="{{ book.slug }}">
     <div id="jplayer" class="jp-jplayer" data-player="jp_container_{{ book.pk }}"
          data-supplied="oga,mp3"></div>
     <div id="jp_container_{{ book.pk }}" class="jp-audio">