Stop editing when clicking on sum row
[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 editable_callback(data)
52 {
53     if (data.content) {
54        var elem = $(data.$el);
55         var route = elem.attr('route');
56         var item_id = elem.attr('item_id');
57         if (typeof(route) == 'string' && typeof(item_id) == 'string')
58             $.invoke(route,
59                      {id: item_id,
60                       name: elem.attr('name'),
61                       content: data.content});
62     }
63
64     return false;
65 }
66
67 function make_editable(selector)
68 {
69     var list = $(selector);
70     if (list.length)
71        list.editable({
72            closeOnEnter : true,
73            event : 'click',
74            callback: editable_callback
75        });
76 }
77 (function($){
78     $.fn.ltag = function() {
79         return this.prop("tagName").toLowerCase();
80     };
81
82     $.fn.center = function () {
83         this.css("position","absolute");
84         this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
85                                  $(window).scrollTop()) + "px");
86         this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
87                                   $(window).scrollLeft()) + "px");
88         return this;
89     };
90
91 $.invoke = function(name, parms, callback) {
92     if (typeof(parms) == 'string' && parms.length)
93         parms = 'route='+name+'&'+parms;
94     else if (typeof(parms) == 'object')
95         parms['route'] = name;
96     else
97         parms = 'route='+name;
98
99     $.post('/ajax.php',
100                 parms,
101                 function(data){
102                     if (!data.status) {
103                         if (data.error) {
104                             if (data.redirect_login) {
105                                 show_message(data.error, 5);
106                                 setTimeout(function(){window.location.href = '/';}, 5000);
107                                 return;
108                             } else
109                                 return show_error(data.error);
110                         } else
111                             return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
112                     }
113                     if (typeof(data.html) == 'object')
114                         for (id in data.html) {
115                             var elem = $('#'+id);
116                             if (elem.length) {
117                                 elem.html(data.html[id]);
118                             }
119                         }
120                     if (typeof(data.values) == 'object')
121                         for (id in data.values) {
122                             var elem = $('#'+id);
123                             if (elem.length)
124                                 elem.val(data.values[id]);
125                         }
126
127                     if (typeof(callback) == 'function')
128                         return callback(data);
129                 });
130 };
131 })(jQuery);