fix
[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
78         (function() {
79             var $current = null;
80             var menu_loaded = false;
81             $('.hidden-box-wrapper').each(function() {
82                 var $hidden = $('.hidden-box', this);
83                 $('.hidden-box-trigger', this).click(function(event) {
84                     event.preventDefault();
85                     if ($current == $hidden) {
86                         $current = null;
87                         $hidden.hide('fast');
88                     } else {
89                         $current && $current.hide('fast');
90                         $hidden.show('fast');
91                         $current = $hidden;
92                         if ($(this).hasClass('load-menu') && !menu_loaded) {
93                             $.ajax({
94                                 url: '/katalog/' + LANGUAGE_CODE + '.json',
95                                 dataType: "json"
96                             }).done(function(data) {
97                                 $.each(data, function(index, value) {
98                                     var $menuitem = $('#menu-' + index);
99                                     $menuitem.html(value);
100                                     var $minisearch = $("<input class='mini-search' style='margin-bottom: 1em' />");
101                                     $minisearch.keyup(function() {
102                                         var s = $(this).val().toLowerCase();
103                                         if (s) {
104                                             $("li", $menuitem).each(function() {
105                                                 if ($("a", this).text().toLowerCase().indexOf(s) != -1)
106                                                     $(this).show();
107                                                 else
108                                                     $(this).hide();
109                                             });
110                                         }
111                                         else {
112                                             $("li", $menuitem).css("display", "");
113                                         }
114                                     });
115                                     $menuitem.prepend($minisearch);
116                                 });
117                                 menu_loaded = true;
118                             });
119                         }
120                     }
121                 });
122             });
123             /* this kinda breaks the whole page. */
124             $('body').click(function(e) {
125                 if ($current == null) return;
126                 var p = $(e.target);
127                 while (p.length) {
128                     if (p == $current)
129                         return;
130                     if (p.hasClass('hidden-box-trigger')
131                         || p.hasClass('simple-toggler')
132                         || p.hasClass('mini-search'))
133                         return;
134                     p = p.parent();
135                 }
136                 $current.hide('fast');
137                 $current = null;
138             });
139         })();
140
141
142         $('#show-menu').click(function(event) {
143             event.preventDefault();
144             //$('#menu').toggle('fast');
145             $('body').toggleClass('menu-on');
146         });
147
148
149         $('#book-list-nav').find('h2').click(function(event) {
150             event.preventDefault();
151             $('#book-list-nav-index').toggle();
152         });
153
154
155         $('#themes-list-toggle').click(function(event) {
156             event.preventDefault();
157             $('#themes-list').toggle('fast');
158         });
159
160
161         $('.book-list-index').click(function(){
162             $('.book-list-show-index').hide('fast');
163             var books_ul = $(this).parent().next().children().first();
164             if(books_ul.first().is(':hidden')){
165                 books_ul.toggle('fast');
166             }
167             return false;
168         });
169
170         $('.hoverclick').click(function() {$(this).closest('.hoverget').toggleClass('hover');});
171
172         $(function(){
173             $("#search").search();
174         });
175
176         $('body').on('click', '.simple-toggler' , function(ev) {
177             ev.preventDefault();
178             var scope = $(this).closest('.simple-toggler-scope');
179             scope.find('.simple-hidden-box').each(function() {
180                 var $this = $(this);
181                 if ($this.is(':hidden')) {
182                     $this.show();
183                 } else {
184                     $this.hide();
185                 }
186             });
187         });
188
189
190         $('.tabbed-filter').each(function() {
191             var tf = this;
192             $('.tab').click(function() {
193                 if ($(this).hasClass('active')) {
194                     $(this).removeClass('active');
195                     $('#' + $(this).attr('data-id')).hide();
196                 }
197                 else {
198                     var $active = $('.active', tf);
199                     $active.removeClass('active');
200                     $('#' + $active.attr('data-id')).hide();
201                     $(this).addClass('active');
202                     $('#' + $(this).attr('data-id')).show();
203                 }
204             });
205         });
206
207
208         $('.plain-list-paged').each(function() {
209             // should change on resize?
210             var $plc = $(this);
211             var $pl = $('.plain-list', this);
212
213             var $items = $('p', $pl);
214
215             if ($items.length > 40) {
216                 $items.hide();
217                 var prev = [0, 0];
218
219                 $('.pager', $plc).paging($items.length, {
220                     format: '[< ncnnn >]', // define how the navigation should look like and in which order onFormat() get's called
221                     perpage: 40,
222                     lapping: 0, // don't overlap pages for the moment
223                     page: 1, // start at page, can also be "null" or negative
224                     onSelect: function (page) {
225                         var data = this.slice;
226                         $items.slice(prev[0], prev[1]).hide();
227                         $items.slice(data[0], data[1]).show();
228                         prev = data;
229                     },
230                     onFormat: function (type) {
231                         switch (type) {
232                             case 'block': // n and c
233                                 return ' <li><a href="#"' + (this.value == this.page ? ' class="current"' : '') + '>' +
234                                     this.value + '</a></li>';
235                             case 'next': // >
236                                 return '<li><a href="#">&rsaquo;</a></li>';
237                             case 'prev': // <
238                                 return '<li><a href="#">&lsaquo;</a></li>';
239                             case 'first': // [
240                                 return '<li><a href="#">&laquo;</a></li>';
241                             case 'last': // ]
242                                 return '<li><a href="#">&raquo;</a></li>';
243                         }
244                     }
245                 });
246             }
247         });
248
249         /* global tlite */
250         tlite(function (el) {
251             return $(el).hasClass('tlite-tooltip');
252         });
253
254         /* more/less switch from https://codepen.io/JoshBlackwood/pen/pEwHe */
255         // Hide the extra content initially, using JS so that if JS is disabled, no problemo:
256         $('.read-more-content').addClass('hide');
257         $('.read-more-show, .read-more-hide').removeClass('hide');
258
259         // Set up the toggle effect:
260         $('.read-more-show').on('click', function(e) {
261           $(this).next('.read-more-content').removeClass('hide');
262           $(this).addClass('hide');
263           e.preventDefault();
264         });
265
266         // Changes contributed by @diego-rzg
267         $('.read-more-hide').on('click', function(e) {
268           var p = $(this).parent('.read-more-content');
269           p.addClass('hide');
270           p.prev('.read-more-show').removeClass('hide'); // Hide only the preceding "Read More"
271           e.preventDefault();
272         });
273     });
274 })(jQuery);
275