Dodanie możliwości wyboru formatów przy ściąganiu półki.
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 var BANNER_TEXTS = [
2     'Przekaż 1% żeby ukryć ten baner.',
3     'Jak dobrze wydać 1% swojego podatku? <strong>Poradnik dla opornych</strong>.',
4     'Wiadomość systemowa: wystąpił błąd brak funduszy. Wykonaj procedurę 1%.',
5     '<strong>FREE!</strong> Wygraj darmowe lektury!',
6     'Confidential business offer. Not scam! 1% for you.',
7     'Biblioteka Wolne Lektury wymaga aktualizacji. Kliknij dalej.',
8     '1000 lektur. <strong>1 procent</strong>.'
9 ]
10
11
12 function changeBannerText() {
13     var index = Math.floor(Math.random() * BANNER_TEXTS.length);
14     if (BANNER_TEXTS[index] == $('#onepercent-text').html()) {
15         // try again
16         changeBannerText();
17     } else {
18         $('#onepercent-text').fadeOut('slow', function() {
19             $(this).html(BANNER_TEXTS[index]);
20             $(this).fadeIn('slow');
21         });
22         
23         setTimeout(changeBannerText, 30 * 1000);
24     }
25 }
26
27
28 (function($) {
29     $(function() {
30         $('form input').labelify({labelledClass: 'blur'});
31         
32         target = $('#login-register-window div.target');
33         
34         $('#show-registration-form').click(function() {
35             $('#login-form').hide();
36             $('#registration-form').show();
37         });
38         
39         $('#show-login-form').click(function() {
40             $('#registration-form').hide();
41             $('#login-form').show();
42         });
43         
44         // Fragments
45         $('.fragment-text').each(function() {
46             if ($(this).prev().filter('.fragment-short-text').length) {
47                 $(this).hover(
48                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
49                     function() { $(this).css({background: '#FFF'}); }
50                 ).click(function() {
51                     $(this).fadeOut(function() {
52                         $(this).prev().fadeIn()
53                     });
54                 })
55             }
56         });
57         
58         $('.fragment-short-text').click(function() {
59             $(this).fadeOut(function() { $(this).next().fadeIn() });
60         }).hover(
61             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
62             function() { $(this).css({background: '#FFF'}); }
63         );
64         
65         $('.show-all-tags').click(function() {
66             $(this).parent().parent().fadeOut(function() { 
67                 $(this).next().fadeIn();
68             });
69             return false;
70         });
71         
72         $('.hide-all-tags').click(function() {
73            $(this).parent().parent().fadeOut(function() {
74                $(this).prev().fadeIn();
75            });
76            return false; 
77         });
78         
79         $('#registration-form').ajaxForm({
80             dataType: 'json',
81             beforeSubmit: function() {
82                 $('#registration-form input[type=submit]')
83                     .attr('disabled', 'disabled')
84                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
85             },
86             success: function(response) {
87                 if (response.success) {
88                     location.reload(true);
89                 } else {
90                     $('#registration-form span.error').remove();
91                     $.each(response.errors, function(id, errors) {
92                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
93                     });
94                     $('#registration-form input[type=submit]').removeAttr('disabled');
95                     $('#registration-form img').remove();
96                 }
97             }
98         });
99         
100         $('#login-form').ajaxForm({
101             dataType: 'json',
102             beforeSubmit: function() {
103                 $('#login-form input[type=submit]')
104                     .attr('disabled', 'disabled')
105                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
106             },
107             success: function(response) {
108                 if (response.success) {
109                     location.reload(true);
110                 } else {
111                     $('#login-form span.error').remove();
112                     $.each(response.errors, function(id, errors) {
113                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
114                     });
115                     $('#login-form input[type=submit]').removeAttr('disabled');
116                     $('#login-form img').remove();
117                 }
118             }
119         });
120         
121         $('#login-register-window').jqm({
122             target: target[0],
123             overlay: 60,
124             trigger: '.login-register-link',
125             onShow: function(hash) {
126                 var offset = $(hash.t).offset();
127                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
128                 $('div.header', hash.w).css({width: $(hash.t).width()});
129                 hash.w.show();
130             }
131         });
132         
133         $('ul.shelf-list li').hover(function() {
134             $(this).css({background: '#EEE', cursor: 'pointer'});
135         }, function() {
136             $(this).css({background: 'transparent'});
137         }).click(function() {
138             location.href = $('a.visit-shelf', this).attr('href');
139         });
140         
141         $('.delete-shelf').click(function() { 
142             var link = $(this);
143             var shelf_name = $('.visit-shelf', link.parent()).text();
144             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
145                 $.post(link.attr('href'), function(data, textStatus) {
146                     link.parent().remove();
147                 });
148             }
149             return false;
150         });
151         
152         $('#user-shelves-window').jqm({
153             ajax: '@href',
154             target: $('#user-shelves-window div.target')[0],
155             overlay: 60,
156             trigger: '#user-shelves-link',
157             onShow: function(hash) {
158                 var offset = $(hash.t).offset();
159                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
160                 $('div.header', hash.w).css({width: $(hash.t).width()});
161                 hash.w.show();
162             },
163             onLoad: function(hash) { 
164                 $('form', hash.w).ajaxForm({
165                     target: $('#user-shelves-window div.target'),
166                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
167                 });
168                 
169                 $('input', hash.w).labelify({labelledClass: 'blur'});
170                 
171                 $('ul.shelf-list li', hash.w).hover(function() {
172                     $(this).css({background: '#EEE', cursor: 'pointer'});
173                 }, function() {
174                     $(this).css({background: 'transparent'});
175                 }).click(function() {
176                     location.href = $('a.visit-shelf', this).attr('href');
177                 });
178                 
179                 $('.delete-shelf').click(function() {
180                     var link = $(this);
181                     var shelf_name = $('.visit-shelf', link.parent()).text();
182                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
183                         $.post(link.attr('href'), function(data, textStatus) {
184                             link.parent().remove();
185                         });
186                     }
187                     return false;
188                 });
189             }
190         });
191     
192         $('#books-list .book').hover(
193             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
194             function() { $(this).css({background: '#FFF'}); }
195         ).click(function() {
196             location.href = $('h2 a', this).attr('href');
197         });
198     
199         $('#toggle-description').hover(
200             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
201             function() { $(this).css({background: '#EEE'}); }
202         ).click(function() {
203             if ($('#description').hasClass('hidden')) {
204                 $('#description').slideDown('fast').removeClass('hidden');
205                 $.cookie('description-state', 'opened', {path: '/', expires: 30});
206                 $('p', this).html('Zwiń opis ▲');
207             } else {
208                 $('#description').slideUp('fast').addClass('hidden');
209                 $.cookie('description-state', 'closed', {path: '/', expires: 30});
210                 $('p', this).html('Rozwiń opis ▼');
211             }
212         });
213     
214         var target = $('#set-window div.target');
215     
216         $('#set-window').jqm({
217             ajax: '@href', 
218             target: target[0],
219             overlay: 60,
220             trigger: 'a.jqm-trigger', 
221             onShow: function(hash) { 
222                 var offset = $(hash.t).offset();
223                 target.html('<p><img src="/static/img/indicator.gif" /> Ładowanie</p>');
224                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
225             onLoad: function(hash) { 
226                 $('form', hash.w).ajaxForm({
227                     target: target,
228                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
229                 });
230             }
231         });
232         
233         $('a.remove-from-shelf').click(function(event) {
234             event.preventDefault();
235             link = $(this);
236             $.post(link.attr('href'), function(data, textStatus) {
237                 link.parent().remove();
238             });
239         });
240         
241         if ($.cookie('description-state') == 'closed') {
242             $('#description').hide().addClass('hidden');
243             $('#toggle-description p').html('Rozwiń opis ▼');
244         }
245                 
246         $('#user-info').show();
247         changeBannerText();
248         $('#onepercent-banner').slideDown('slow');
249         
250         var formatsDownloaded = false;
251         $('#download-shelf').click(function() {
252             $('#download-shelf-menu').slideDown('fast');
253             
254             if (!formatsDownloaded) {
255                 // Pobierz dane o formatach
256                 formatsDownloaded = true;
257                 $.ajax({
258                     url: $('#download-formats-form').attr('data-formats-feed'),
259                     type: 'GET',
260                     dataType: 'json',
261                     complete: function() {
262                         $('#download-formats-form-submit').attr('disabled', null);
263                         $('#download-formats-form-submit-li img').remove();
264                         $('#updating-formats').fadeOut('fast', function() {
265                             $('#formats-updated').fadeIn('fast');
266                         });
267                     },
268                     success: function(data) {
269                         $('#download-formats-form li').each(function() {
270                             var item = $(this);
271                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
272                                 item.fadeOut('fast', function() {
273                                     item.remove();
274                                 });
275                             }
276                         });
277                     }
278                 });
279             }
280             return false;
281         });
282         
283         $('#download-formats-form-cancel').click(function() {
284             $('#download-shelf-menu').slideUp('fast');
285             return false;
286         });
287     });
288 })(jQuery)