Introduce new AJAX framework with improved class use
[infodrom.org/service.infodrom.org] / src / infodrom.js
1 function show_message(text, timeout)
2 {
3     if (typeof timeout == 'undefined') timeout = 3;
4
5     var div = $('#message_div');
6     if (!div.length) {
7         div = $('<div>');
8         div.attr('id', 'message_div').css('z-index','1000');
9         div.hide();
10         $(document.body).append(div);
11     }
12
13     div.text(text);
14     div.center();
15     div.show();
16
17     window.setTimeout(hide_message,timeout*1000);
18 }
19
20 function hide_message(text)
21 {
22     $('#message_div').hide();
23 }
24
25 function show_error(text, timeout)
26 {
27     var div = $('#error_div');
28     if (!div.length) {
29         div = $('<div>');
30         div.attr('id', 'error_div').css('z-index','1000');
31         div.hide();
32         div.append($('<p ><img src="/pix/close.gif" title="close" onclick="return hide_error()"/></p>'));
33         div.append($('<div />'));
34         $(document.body).append(div);
35     }
36
37     div.find('div').html(text);
38     div.center();
39     div.show();
40
41     if (typeof timeout != 'undefined')
42         window.setTimeout(hide_error,timeout*1000);
43 }
44
45 function hide_error(text)
46 {
47     $('#error_div').hide();
48     return false;
49 }
50
51 (function($){
52     $.fn.ltag = function() {
53         return this.prop("tagName").toLowerCase();
54     };
55
56     $.fn.center = function () {
57         this.css("position","absolute");
58         this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
59                                  $(window).scrollTop()) + "px");
60         this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
61                                   $(window).scrollLeft()) + "px");
62         return this;
63     };
64
65 $.invoke = function(name, parms, callback) {
66     if (typeof(parms) == 'string' && parms.length)
67         parms = 'route='+name+'&'+parms;
68     else if (typeof(parms) == 'object')
69         parms['route'] = name;
70     else
71         parms = 'route='+name;
72
73     $.post('/ajax.php',
74                 parms,
75                 function(data){
76                     if (!data.status) {
77                         if (data.error) {
78                             if (data.redirect_login) {
79                                 show_message(data.error, 5);
80                                 setTimeout(function(){window.location.href = '/';}, 5000);
81                                 return;
82                             } else
83                                 return show_error(data.error);
84                         } else
85                             return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
86                     }
87                     if (typeof(data.html) == 'object')
88                         for (id in data.html) {
89                             var elem = $('#'+id);
90                             if (elem.length) {
91                                 elem.html(data.html[id]);
92                             }
93                         }
94                     if (typeof(data.values) == 'object')
95                         for (id in data.values) {
96                             var elem = $('#'+id);
97                             if (elem.length)
98                                 elem.val(data.values[id]);
99                         }
100
101                     if (typeof(callback) == 'function')
102                         return callback(data);
103                 });
104 };
105 })(jQuery);