Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / catalogue / static / player / 2022_player.js
1 (function($) {
2     $(function() {
3
4         $(".jp-jplayer").each(function() {
5             console.log('starting player')
6         var $self = $(this);
7         var $root = $self.parent();
8
9             //  //  var $number = $('.number', $root);
10         $self.jPlayer({
11             swfPath: "/static/jplayer/",
12             solution: "html,flash",
13             supplied: 'oga,mp3',
14             cssSelectorAncestor: "#" + $self.attr("data-player"),
15
16             ready: function() {
17                 var player = $(this);
18                 console.log(1);
19
20                 var setMedia = function(elem, time=0) {
21                     console.log('setMedia', elem, time);
22                     var media = {}
23
24                     media['mp3'] = elem.attr('data-mp3');
25                     media['oga'] = elem.attr('data-ogg');
26                     media['id'] = elem.attr('data-media-id');
27
28                     $(".c-player__title", $root).html($(".title", elem).html());
29                     $(".c-player__info", $root).html($(".attribution", elem).html());
30                     $(".c-media__caption .content", $root).html($(".project-description", elem).html());
31                     $(".c-media__caption .license", $root).html($(".license", elem).html());
32                     $(".c-media__caption .project-logo", $root).html($(".project-icon", elem).html());
33                     
34                     player.jPlayer("setMedia", media);
35                     player.jPlayer("pause", time);
36                     return player;
37                 };
38
39                 $('.play-next', $root).click(function() {
40                     var next = parseInt($number.text()) + 1;
41                     var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
42                     if (p.length) {
43                         setMedia(p).jPlayer("play");
44                         $number.text(next)
45                     }
46                 });
47                 $('.play-prev', $root).click(function() {
48                     var next = parseInt($number.text()) - 1;
49                     if (next < 1)
50                         return;
51                     var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
52                     setMedia(p).jPlayer("play");
53                     $number.text(next)
54                 });
55
56                 console.log(1);
57
58                 var initialElem = $('.jp-playlist li', $root).first();
59                 var initialTime = 0;
60                 if (true || Modernizr.localstorage) {
61                     try {
62                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
63                     } catch {
64                         audiobooks = {};
65                     }
66                     last = audiobooks[$root.attr("data-book-slug")]
67                     // Fallback for book id;
68                     if (!last) {
69                         last = audiobooks[$root.attr("data-book-id")]
70                     }
71
72                     if (last) {
73                         initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
74                         initialTime = last[2];
75                         $number.text($(".jp-playlist .play", $root).index(initialElem) + 1);
76                     }
77                 }
78                 setMedia(initialElem, initialTime);
79             },
80
81             timeupdate: function(event) {
82                 //event.jPlayer.status.currentTime
83                 
84                 
85                 if (true || (event.jPlayer.status.currentTime && Modernizr.localstorage)) {
86                     try {
87                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
88                     } catch {
89                         audiobooks = {};
90                     }
91                     t = event.jPlayer.status.currentTime;
92                     if (t && event.jPlayer.status.duration - t > 10) {
93                         audiobooks[$root.attr("data-book-slug")] = [
94                             Date.now(),
95                             event.jPlayer.status.media.id,
96                             event.jPlayer.status.currentTime
97                         ];
98                     } else {
99                         delete audiobooks[$root.attr("data-book-slug")];
100                     }
101                     // Remove old book id, if present.
102                     delete audiobooks[$root.attr("data-book-id")];
103                     localStorage["audiobook-history"] = JSON.stringify(audiobooks);
104                 }
105             }
106         });
107       });
108
109
110
111     });
112 })(jQuery)