stars and tags instead of shelves, move to social app
[wolnelektury.git] / wolnelektury / static / js / dialogs.js
1 (function($) {
2     $(function() {
3
4         // create containers for all ajaxable form links
5         $('.ajaxable').each(function() {
6             var $window = $("#ajaxable-window").clone();
7             $window.attr("id", this.id + "-window");
8             $('body').append($window);
9
10             var $trigger = $(this)
11             var trigger = '#' + this.id;
12
13             $window.jqm({
14                 ajax: '@href',
15                 ajaxText: '<p><img src="' + STATIC_URL + 'img/indicator.gif" alt="*"/> ' + gettext("Loading") + '</p>',
16                 target: $('.target', $window)[0],
17                 overlay: 60,
18                 trigger: trigger,
19                 onShow: function(hash) {
20                     var offset = $(hash.t).offset();
21                     hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
22                     var width = $(hash.t).width();
23                     width = width > 50 ? width : 50;
24                     $('.header', hash.w).css({width: width});
25                     hash.w.show();
26                 },
27                 onLoad: function(hash) {
28                     $('form', hash.w).ajaxForm({
29                         dataType: 'json',
30                         target: $('.target', $window),
31                         success: function(response) {
32                             if (response.success) {
33                                 $('.target', $window).text(response.message);
34                                 setTimeout(function() { $window.jqmHide() }, 1000);
35                                 callback = ajaxable_callbacks[$trigger.attr('data-callback')];
36                                 callback && callback($trigger, response);
37                                 if (response.redirect)
38                                     window.location = response.redirect;
39                             }
40                             else {
41                                 $('.error', $window).remove();
42                                 $.each(response.errors, function(id, errors) {
43                                     $('#id_' + id, $window).before('<span class="error">' + errors[0] + '</span>');
44                                 });
45                                 $('input[type=submit]', $window).removeAttr('disabled');
46                                 return false;
47                             }
48                         }
49                     });
50                 }
51             });
52         });
53
54
55         var login_and_retry = function($form) {
56             var $window = $("#ajaxable-window").clone();
57             $window.attr("id", "context-login-window");
58             $('body').append($window);
59
60             $window.jqm({
61                 ajax: '/uzytkownicy/zaloguj-utworz/',
62                 ajaxText: '<p><img src="' + STATIC_URL + 'img/indicator.gif" alt="*"/> ' + gettext("Loading") + '</p>',
63                 target: $('.target', $window)[0],
64                 overlay: 60,
65                 onShow: function(hash) {
66                     var offset = $form.offset();
67                     hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $form.width(), top: offset.top});
68                     var width = $form.width();
69                     width = width > 50 ? width : 50;
70                     $('.header', hash.w).css({width: width});
71                     hash.w.show();
72                 },
73                 onLoad: function(hash) {
74                     $('form', hash.w).ajaxForm({
75                         dataType: 'json',
76                         target: $('.target', $window),
77                         success: function(response) {
78                             if (response.success) {
79                                 $('.target', $window).text(response.message);
80                                 setTimeout(function() { $window.jqmHide() }, 1000);
81                                 $form.submit();
82                             }
83                             else {
84                                 $('.error', $window).remove();
85                                 $.each(response.errors, function(id, errors) {
86                                     $('#id_' + id, $window).before('<span class="error">' + errors[0] + '</span>');
87                                 });
88                                 $('input[type=submit]', $window).removeAttr('disabled');
89                                 return false;
90                             }
91                         }
92                     });
93                 }
94             }).jqmShow();
95             
96         };
97
98
99         $('.ajax-form').each(function() {
100             var $form = $(this);
101             $form.ajaxForm({
102                 dataType: 'json',
103                 beforeSubmit: function() {
104                     $('input[type=submit]', $form)
105                         .attr('disabled', 'disabled')
106                         .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
107                 },
108                 error: function(response) {
109                         if (response.status == 403)
110                             login_and_retry($form);
111                     },
112                 success: function(response) {
113                     if (response.success) {
114                         callback = ajax_form_callbacks[$form.attr('data-callback')];
115                         callback && callback($form, response);
116
117                     } else {
118                         $('span.error', $form).remove();
119                         $.each(response.errors, function(id, errors) {
120                             $('#id_' + id, $form).before('<span class="error">' + errors[0] + '</span>');
121                         });
122                         $('input[type=submit]', $form).removeAttr('disabled');
123                         $('img', $form).remove();
124                     }
125                 }
126             });
127         });
128
129
130         var update_star = function($elem, response) {
131             /* updates the star after successful ajax */
132             var $star = $elem.closest('.star');
133             if (response.like) {
134                 $star.addClass('like');
135                 $star.removeClass('unlike');
136             }
137             else {
138                 $star.addClass('unlike');
139                 $star.removeClass('like');
140             }
141         };
142
143         var ajax_form_callbacks = {
144             'social-like-book': update_star
145         };
146
147         var ajaxable_callbacks = {
148             'social-book-sets': update_star
149         };
150
151
152     });
153 })(jQuery)
154