polishing map fixes
[wolnelektury.git] / src / catalogue / static / player / player.js
old mode 100755 (executable)
new mode 100644 (file)
index 69dceed..69870bb
@@ -1,34 +1,98 @@
 (function($) {
     $(function() {
 
 (function($) {
     $(function() {
 
-      $("#jplayer").each(function() {
+      $(".jp-jplayer").each(function() {
        var $self = $(this);
        var $self = $(this);
+        var $root = $self.parent();
+        var $number = $('.number', $root);
         $self.jPlayer({
             swfPath: "/static/jplayer/",
             solution: "html,flash",
             supplied: $self.attr('data-supplied'),
         $self.jPlayer({
             swfPath: "/static/jplayer/",
             solution: "html,flash",
             supplied: $self.attr('data-supplied'),
-    
+            cssSelectorAncestor: "#" + $self.attr("data-player"),
+
             ready: function() {
                 var player = $(this);
             ready: function() {
                 var player = $(this);
-                var setMedia = function(elem) {
+                var setMedia = function(elem, time=0) {
                     var li = $(elem).parent();
                     var li = $(elem).parent();
-                    $('.jp-playlist-current').removeClass('jp-playlist-current');
-                    $(li).addClass('jp-playlist-current');
                     var media = {}
                     var media = {}
-    
-                    $('.mp3', li).each(function() {media['mp3'] = $(this).attr('href')});
-                    $('.ogg', li).each(function() {media['oga'] = $(this).attr('href')});
-    
-                    return player.jPlayer("setMedia", 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());
+                    player.jPlayer("setMedia", media);
+                    player.jPlayer("pause", time);
+                    return player;
                 };
                 };
-                setMedia($('.play').first()).jPlayer("play");
-    
-                $('.play').click(function() {
-                    setMedia(this).jPlayer("play");
+
+                $('.play-next', $root).click(function() {
+                    var next = parseInt($number.text()) + 1;
+                    var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
+                    if (p.length) {
+                        setMedia(p).jPlayer("play");
+                        $number.text(next)
+                    }
+                });
+                $('.play-prev', $root).click(function() {
+                    var next = parseInt($number.text()) - 1;
+                    if (next < 1)
+                        return;
+                    var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
+                    setMedia(p).jPlayer("play");
+                    $number.text(next)
                 });
                 });
+
+                var initialElem = $('.jp-playlist .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];
+                        $number.text($(".jp-playlist .play", $root).index(initialElem) + 1);
+                    }
+                }
+                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);
+                }
             }
         });
       });
 
             }
         });
       });
 
+
+
     });
     });
-})(jQuery)
\ No newline at end of file
+})(jQuery)