eink fixes
[wolnelektury.git] / src / wolnelektury / static / js / base.js
1 (function($) {
2     $(function() {
3         $.fn.toggle_slide = function(p) {
4             var cont = $(this);
5             var short_el = p['short_el'] || $(':first-child', this);
6             var long_el = p['long_el'] || short_el.next();
7             var button = p['button'];
8             var short_text = p['short_text'];
9             var long_text = p['long_text'];
10
11             var toggle_fun = function(cont, short_el, long_el, button, short_text, long_text) {
12                 return function () {
13                     if (cont.hasClass('short')) {
14                         cont.animate({"height": long_el.attr("cont_h") + 'px'}, {duration: "fast"})
15                             .removeClass('short');
16                         short_el.hide();
17                         long_el.show();
18                         if (button && long_text) button.html(long_text);
19                     } else {
20                         cont.animate({"height": short_el.attr("cont_h") + 'px'}, {duration: "fast"}).addClass('short');
21                         long_el.hide();
22                         short_el.show();
23                         if (button && short_text) button.html(short_text);
24                     }
25                     return false;
26                 };
27             };
28             if (long_el.html().length <= short_el.html().length)
29                 return;
30
31             // ensure long element shown first
32             long_el.show();short_el.hide();
33             long_el.attr("cont_h", $(this).height()).hide();
34             short_el.show().attr("cont_h", $(this).height());
35             $(this).addClass('short');
36
37             if (button && short_text)
38                 button.html(short_text);
39             if (button)
40                 button.click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
41         };
42
43
44         // Fragments
45         $('.fragment-with-short').each(function() {
46             $(this).toggle_slide({
47                 short_el: $('.fragment-short-text', this),
48                 long_el: $('.fragment-long-text', this),
49                 button: $('.toggle', this)
50             })
51         });
52         $('#description').each(function() {
53             $(this).toggle_slide({
54                 short_el: $('#description-short', this),
55                 long_el: $('#description-long', this),
56                 button: $(this)
57             })
58         });
59
60         $('.more-expand').each(function () {
61             $(this).shorten({
62                 showChars: 150,
63                 moreText: "wiÄ™cej",
64                 lessText: "mniej"
65             });
66         });
67
68
69         $('.carousel').on('cycle-before', function(event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) {
70             $("iframe", outgoingSlideEl).attr("src", '');
71             $("iframe", incomingSlideEl).attr("src", $("iframe", incomingSlideEl).attr('data-src'));
72         });
73         $('.carousel section').first().each(function() {
74             $("iframe", this).attr("src", $("iframe", this).attr('data-src'));
75         });
76
77         $(".media-eink .carousel").cycle({fx: "none"});
78
79
80         (function() {
81             var $current = null;
82             var menu_loaded = false;
83             $('.hidden-box-wrapper').each(function() {
84                 var $hidden = $('.hidden-box', this);
85                 $('.hidden-box-trigger', this).click(function(event) {
86                     event.preventDefault();
87                     if ($current == $hidden) {
88                         $current = null;
89                         $hidden.hide('fast');
90                     } else {
91                         $current && $current.hide('fast');
92                         $hidden.show('fast');
93                         $current = $hidden;
94                         if ($(this).hasClass('load-menu') && !menu_loaded) {
95                             $.ajax({
96                                 url: '/katalog/' + LANGUAGE_CODE + '.json',
97                                 dataType: "json"
98                             }).done(function(data) {
99                                 $.each(data, function(index, value) {
100                                     var $menuitem = $('#menu-' + index);
101                                     $menuitem.html(value);
102                                     var $minisearch = $("<input class='mini-search' style='margin-bottom: 1em' />");
103                                     $minisearch.keyup(function() {
104                                         var s = $(this).val().toLowerCase();
105                                         if (s) {
106                                             $("li", $menuitem).each(function() {
107                                                 if ($("a", this).text().toLowerCase().indexOf(s) != -1)
108                                                     $(this).show();
109                                                 else
110                                                     $(this).hide();
111                                             });
112                                         }
113                                         else {
114                                             $("li", $menuitem).css("display", "");
115                                         }
116                                     });
117                                     $menuitem.prepend($minisearch);
118                                 });
119                                 menu_loaded = true;
120                             });
121                         }
122                     }
123                 });
124             });
125             /* this kinda breaks the whole page. */
126             $('body').click(function(e) {
127                 if ($current == null) return;
128                 var p = $(e.target);
129                 while (p.length) {
130                     if (p == $current)
131                         return;
132                     if (p.hasClass('hidden-box-trigger')
133                         || p.hasClass('simple-toggler')
134                         || p.hasClass('mini-search'))
135                         return;
136                     p = p.parent();
137                 }
138                 $current.hide('fast');
139                 $current = null;
140             });
141         })();
142
143
144         $('#show-menu').click(function(event) {
145             event.preventDefault();
146             //$('#menu').toggle('fast');
147             $('body').toggleClass('menu-on');
148         });
149
150
151         $('#book-list-nav').find('h2').click(function(event) {
152             event.preventDefault();
153             $('#book-list-nav-index').toggle();
154         });
155
156
157         $('#themes-list-toggle').click(function(event) {
158             event.preventDefault();
159             $('#themes-list').toggle('fast');
160         });
161
162
163         $('.book-list-index').click(function(){
164             $('.book-list-show-index').hide('fast');
165             var books_ul = $(this).parent().next().children().first();
166             if(books_ul.first().is(':hidden')){
167                 books_ul.toggle('fast');
168             }
169             return false;
170         });
171
172         $('.hoverclick').click(function() {$(this).closest('.hoverget').toggleClass('hover');});
173
174         $(function(){
175             $("#search").search();
176         });
177
178         $('body').on('click', '.simple-toggler' , function(ev) {
179             ev.preventDefault();
180             var scope = $(this).closest('.simple-toggler-scope');
181             scope.find('.simple-hidden-box').each(function() {
182                 var $this = $(this);
183                 if ($this.is(':hidden')) {
184                     $this.show();
185                 } else {
186                     $this.hide();
187                 }
188             });
189         });
190
191
192         $('.tabbed-filter').each(function() {
193             var tf = this;
194             $('.tab').click(function() {
195                 if ($(this).hasClass('active')) {
196                     $(this).removeClass('active');
197                     $('#' + $(this).attr('data-id')).hide();
198                 }
199                 else {
200                     var $active = $('.active', tf);
201                     $active.removeClass('active');
202                     $('#' + $active.attr('data-id')).hide();
203                     $(this).addClass('active');
204                     $('#' + $(this).attr('data-id')).show();
205                 }
206             });
207         });
208
209
210         $('.plain-list-paged').each(function() {
211             // should change on resize?
212             var $plc = $(this);
213             var $pl = $('.plain-list', this);
214
215             var $items = $('p', $pl);
216
217             if ($items.length > 40) {
218                 $items.hide();
219                 var prev = [0, 0];
220
221                 $('.pager', $plc).paging($items.length, {
222                     format: '[< ncnnn >]', // define how the navigation should look like and in which order onFormat() get's called
223                     perpage: 40,
224                     lapping: 0, // don't overlap pages for the moment
225                     page: 1, // start at page, can also be "null" or negative
226                     onSelect: function (page) {
227                         var data = this.slice;
228                         $items.slice(prev[0], prev[1]).hide();
229                         $items.slice(data[0], data[1]).show();
230                         prev = data;
231                     },
232                     onFormat: function (type) {
233                         switch (type) {
234                             case 'block': // n and c
235                                 return ' <li><a href="#"' + (this.value == this.page ? ' class="current"' : '') + '>' +
236                                     this.value + '</a></li>';
237                             case 'next': // >
238                                 return '<li><a href="#">&rsaquo;</a></li>';
239                             case 'prev': // <
240                                 return '<li><a href="#">&lsaquo;</a></li>';
241                             case 'first': // [
242                                 return '<li><a href="#">&laquo;</a></li>';
243                             case 'last': // ]
244                                 return '<li><a href="#">&raquo;</a></li>';
245                         }
246                     }
247                 });
248             }
249         });
250
251         /* global tlite */
252         tlite(function (el) {
253             return $(el).hasClass('tlite-tooltip');
254         });
255
256         /* more/less switch from https://codepen.io/JoshBlackwood/pen/pEwHe */
257         // Hide the extra content initially, using JS so that if JS is disabled, no problemo:
258         $('.read-more-content').addClass('hide');
259         $('.read-more-show, .read-more-hide').removeClass('hide');
260
261         // Set up the toggle effect:
262         $('.read-more-show').on('click', function(e) {
263           $(this).next('.read-more-content').removeClass('hide');
264           $(this).addClass('hide');
265           e.preventDefault();
266         });
267
268         // Changes contributed by @diego-rzg
269         $('.read-more-hide').on('click', function(e) {
270           var p = $(this).parent('.read-more-content');
271           p.addClass('hide');
272           p.prev('.read-more-show').removeClass('hide'); // Hide only the preceding "Read More"
273           e.preventDefault();
274         });
275
276
277         function update_info() {
278             var amount = parseInt($("#id_amount").val());
279             var monthly =  $("#id_monthly").val() == 'True';
280             if (monthly) slug = "monthly";
281             else if (amount >= parseInt($("#plan-single").attr('data-min-for-year'))) slug = 'single-year';
282             else slug = 'single';
283
284             var chunk = $('.club-form-info .chunk-' + slug);
285             if (chunk.css('display') == 'none') {
286                 $('.chunk-alt').css('height', $('.chunk-alt').height());
287                 $('.chunk-alt .chunk').css('position', 'absolute');
288
289                 $('.club-form-info .chunk').fadeOut();
290                 $('.club-form-info .chunk.chunk-' + slug).fadeIn(function() {
291                     $('.chunk-alt .chunk').css('position', 'static');
292                     $('.chunk-alt').css('height', 'auto');
293                 });
294                 $('.chunk-alt').animate({height: chunk.height()}, 100);
295             }
296         }
297         
298         $("#id_amount").val($("#plan-monthly").attr('data-amount'));
299         
300         $(".button.kwota").click(function() {
301             var plan = $(this).closest('.plan');
302             $('.kwota', plan).removeClass('active')
303             $('.inna', plan).removeClass('active')
304             $(this).addClass('active');
305
306             var amount = $(this).text();
307             plan.attr("data-amount", amount);
308             $("#id_amount").val(amount);
309
310             update_info();
311             return false;
312         });
313
314         $(".plan-toggle").click(function() {
315             $(".plan-toggle").removeClass('active');
316             $(this).addClass('active')
317             $(".plan").hide();
318             var plan = $("#" + $(this).attr('data-plan'));
319             plan.show();
320             $("#id_amount").val(plan.attr('data-amount'));
321             $("#id_monthly").val(plan.attr('data-monthly'));
322
323             update_info();
324             return false;
325         });
326
327         $(".inna .button").click(function() {
328             var plan = $(this).closest('.plan');
329             $('.kwota', plan).removeClass('active');
330             $(this).parent().addClass('active');
331             $('input', plan).focus();
332
333             var amount = $('input', $(this).parent()).val();
334             plan.attr("data-amount", amount);
335             $("#id_amount").val(amount);
336
337             update_info();
338             return false;
339         });
340         
341         $(".inna input").on('input', function() {
342             var plan = $(this).closest('.plan');
343             $('.kwota', plan).removeClass('active');
344             var amount = $(this).val();
345             plan.attr("data-amount", amount);
346             $("#id_amount").val(amount);
347
348             update_info();
349             return false;
350         });
351
352     });
353 })(jQuery);
354