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