bf9d94b54aeae378c9620552151b48d3df760ba2
[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.id;
11
12             var href = $(this).attr('href');
13             if (href.search('\\?') != -1)
14                 href += '&ajax=1';
15             else href += '?ajax=1';
16
17             $window.jqm({
18                 ajax: href,
19                 ajaxText: '<p><img src="' + STATIC_URL + 'img/indicator.gif" alt="*"/> ' + gettext("Loading") + '</p>',
20                 target: $('.target', $window)[0],
21                 overlay: 60,
22                 trigger: trigger,
23                 onShow: function(hash) {
24                     var offset = $(hash.t).offset();
25                     hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
26                     $('.header', hash.w).css({width: $(hash.t).width()});
27                     hash.w.show();
28                 },
29                 onLoad: function(hash) {
30                     $('form', hash.w).each(function() {
31                         if (this.action.search('[\\?&]ajax=1') != -1)
32                             return;
33                         if (this.action.search('\\?') != -1)
34                             this.action += '&ajax=1';
35                         else this.action += '?ajax=1';
36                     });
37                     $('form', hash.w).ajaxForm({
38                         dataType: 'json',
39                         target: $('.target', $window),
40                         success: function(response) {
41                             if (response.success) {
42                                 $('.target', $window).text(response.message);
43                                 setTimeout(function() { $window.jqmHide() }, 1000);
44                                 if (response.redirect)
45                                     window.location = response.redirect;
46                             }
47                             else {
48                                 $('.error', $window).remove();
49                                 $.each(response.errors, function(id, errors) {
50                                     $('#id_' + id, $window).before('<span class="error">' + errors[0] + '</span>');
51                                 });
52                                 $('input[type=submit]', $window).removeAttr('disabled');
53                                 return false;
54                             }
55                         }
56                     });
57                 }
58             });
59         });
60
61
62     });
63 })(jQuery)
64