4 $(".book-right-column").remove();
6 if ($("#player-bar").length) {
7 $("h1").first().after($("<div class='dynamic-insert'><a href='#' class='enable-player-bar'><div class='text'><i class='icon icon-play' style='border: 1px solid black; border-radius: 100%; padding: 1em; margin-right: 1em;'></i> Możesz jednocześnie czytać i słuchać tej lektury!</div></a></div>"));
10 $(".enable-player-bar").click(function() {
11 $('body').addClass('with-player-bar');
12 $('.jp-play').click();
17 var smil = $("#smil").text();
19 smil = $.parseJSON(smil);
21 $.each(smil, function(i, e) {
22 $('#' + e).addClass('syncable');
28 /*$(window).on('scroll', function() {
30 $("#locator").removeClass('snap');
36 scrollTo = function() {
37 if (!scrolling && $('.playing-highlight').length && $('.playing-highlight')[0] != lastscroll) {
38 lastscroll = $('.playing-highlight')[0];
41 scrollTop: $('.playing-highlight').offset().top,
53 $.jPlayer.timeFormat.showHour = true;
55 $(".jp-jplayer").each(function() {
58 var $root = $self.parent();
59 var $currentMedia = null
60 var currentDuration = 0;
62 var totalDurationLeft = 0;
63 var totalDurationBefore = 0;
66 var doesUpdateSynchro = true;
68 // TODO: will need class for attach
69 // may be added from sync data
70 $(".syncable").click(function() {
71 if (!$('body').hasClass('with-player-bar')) return;
72 let id = $(this).attr('id');
74 for (let i=0; i<smil.length; ++i) {
75 if (smil[i][0] == id) {
76 setMediaFromTime(smil[i][1], 'play');
77 //player.jPlayer('play');
83 var setMediaFromTime = function(time, cmd='pause') {
84 $('.jp-playlist li', $root).each((i, e) => {
85 d = parseFloat($(e).data('duration'));
87 setMedia($(e), time, cmd);
95 var setMedia = function(elem, time=0, cmd='pause') {
98 media['mp3'] = elem.attr('data-mp3');
99 media['oga'] = elem.attr('data-ogg');
100 media['id'] = elem.attr('data-media-id');
102 $(".c-player__head", $root).html(
103 $(".attribution", elem).html())
105 $(".c-player__info", $root).html(
106 $(".title", elem).html()
108 $(".c-media__caption .content", $root).html($(".project-description", elem).html());
109 $(".c-media__caption .license", $root).html($(".license", elem).html());
110 $(".c-media__caption .project-logo", $root).html($(".project-icon", elem).html());
113 doesUpdateSynchro = false;
114 if (!$currentMedia || $currentMedia[0] != elem[0]) {
115 console.log('set', player.jPlayer("setMedia", media))
116 player.jPlayer("option", "playbackRate", speed);
118 doesUpdateSynchro = true;
119 player.jPlayer(cmd, time);
121 $currentMedia = elem;
122 $(".play-next", $root).prop("disabled", !elem.next().length);
124 let du = parseFloat(elem.data('duration'));
125 currentDuration = du;
126 elem.nextAll().each(function() {
127 du += parseFloat($(this).data('duration'));
129 totalDurationLeft = du;
132 elem.prevAll().each(function() {
133 pdu += parseFloat($(this).data('duration'));
135 totalDurationBefore = pdu;
136 console.log('sm 3', du, pdu);
142 var updateSynchrotext = function(position) {
143 if (!doesUpdateSynchro) return;
145 let curElemId = null;
146 for (let i=0; i<smil.length; ++i) {
148 if (smil[i][1] <= position) curElemId = smil[i][0];
151 $(".playing-highlight").removeClass("playing-highlight");
152 if (curElemId !== null) {
153 let curElem = $("#" + curElemId);
154 curElem.addClass("playing-highlight");
156 let miny = window.scrollY;
157 let maxy = miny + window.innerHeight;
158 let y = curElem.offset().top;
160 let locator = $("#locator");
161 // TODO: if snap then roll
162 locator.removeClass('up').removeClass('down');
163 if (locator.hasClass('snap')) {
164 console.log('SCROLL!');
168 locator.addClass('up');
171 locator.addClass('down');
178 swfPath: "/static/js/contrib/jplayer/",
179 solution: "html,flash",
181 cssSelectorAncestor: "#" + $self.attr("data-player"),
182 useStateClassSkin: true,
187 let selectItem = $('.c-select li');
188 selectItem.on('click', function() {
189 let speedStr = $(this).data('speed');
190 speed = parseFloat(speedStr);
191 player.jPlayer("option", "playbackRate", speed);
192 localStorage['audiobook-speed'] = speedStr;
193 _paq.push(['trackEvent', 'audiobook', 'speed', speedStr]);
196 $('.jp-play', $root).click(function() {
197 _paq.push(['trackEvent', 'audiobook', 'play']);
199 $('.jp-seek-bar', $root).click(function() {
200 _paq.push(['trackEvent', 'audiobook', 'seek']);
202 $('.jp-mute', $root).click(function() {
203 _paq.push(['trackEvent', 'audiobook', 'mute']);
205 $('.jp-volume-bar', $root).click(function() {
206 _paq.push(['trackEvent', 'audiobook', 'volume']);
209 $('.play-next', $root).click(function() {
210 let p = $currentMedia.next();
212 setMedia(p).jPlayer("play");
213 _paq.push(['trackEvent', 'audiobook', 'next']);
216 $('.play-prev', $root).click(function() {
217 let p = $currentMedia.prev();
219 setMedia(p).jPlayer("play");
220 _paq.push(['trackEvent', 'audiobook', 'prev']);
222 // If in first part, restart it.
223 setMedia($currentMedia).jPlayer("play");
224 _paq.push(['trackEvent', 'audiobook', 'rewind']);
228 $('.jp-playlist li', $root).click(function() {
229 setMedia($(this)).jPlayer("play");
230 $('.c-player__chapters').removeClass('is-active');
231 _paq.push(['trackEvent', 'audiobook', 'chapter']);
234 console.log('READY 3!');
235 var initialElem = $('.jp-playlist li', $root).first();
237 console.log('READY 4!');
238 if (true || Modernizr.localstorage) {
240 let speedStr = localStorage['audiobook-speed'];
242 speed = parseFloat(speedStr);
243 $(".speed .is-active").removeClass("is-active");
244 $(".speed [data-speed='" + speedStr + "']").addClass("is-active");
249 audiobooks = JSON.parse(localStorage["audiobook-history"]);
253 last = audiobooks[$root.attr("data-book-slug")]
254 // Fallback for book id;
256 last = audiobooks[$root.attr("data-book-id")]
260 initialElem = $('[data-media-id="' + last[1] + '"]', $root).first();
261 initialTime = last[2];
264 console.log('READY 5!', initialElem, initialTime);
265 setMedia($(initialElem), initialTime);
266 console.log('READY 6!');
269 timeupdate: function(event) {
270 t = event.jPlayer.status.currentTime;
272 updateSynchrotext(totalDurationBefore + t);
275 ttl = (totalDurationLeft - t) / speed;
276 ttl = $.jPlayer.convertTime(ttl);
277 $(".total-time-left").text('Czas do końca: ' + ttl);
279 $(".time-left").text('– ' + $.jPlayer.convertTime(
284 if (Math.abs(t - lastUpdate) > 3) {
286 audiobooks = JSON.parse(localStorage["audiobook-history"]);
290 if (t && event.jPlayer.status.duration - t > 10) {
291 audiobooks[$root.attr("data-book-slug")] = [
293 event.jPlayer.status.media.id,
294 event.jPlayer.status.currentTime
297 delete audiobooks[$root.attr("data-book-slug")];
299 // Remove old book id, if present.
300 delete audiobooks[$root.attr("data-book-id")];
301 localStorage["audiobook-history"] = JSON.stringify(audiobooks);
307 ended: function(event) {
308 let p = $currentMedia.next();
310 setMedia(p).jPlayer("play");
317 $('#locator').on('click', function() {
318 $(this).toggleClass('snap');