e76b2aefa0b435ecb30ac61cabdc10de314809a4
[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);
132
133 function Popup(title, width, height, body) {
134     return this.initialize(title, width, height, body);
135 }
136
137 Popup.prototype = {
138     initialize: function(title, width, height, body)
139     {
140         this.createPopup(width, height);
141         if (title) this.setTitle(title);
142         if (body) this.setBody(body);
143         return this;
144     },
145
146     createPopup: function(width, height)
147     {
148         this.popup = $('<div class="popup">');
149         this.popup.css('width', width ? width : 'auto');
150         this.popup.css('height', height ? height : 'auto');
151         this.popup.hide().css('z-index', '1000');
152         var titlediv = $('<div class="popup_title" style="position:relative;">');
153         titlediv.append($('<p class="popup_title"></p>'));
154
155         var closeimg = $('<img src="/pix/close.gif" title="Close">');
156         titlediv.append(closeimg);
157
158         this.popup.append(titlediv);
159
160         this.popup.append($('<div class="popup_body" style="clear:both;">'));
161         $('body').append(this.popup);
162
163         this.popup.udraggable({'handle': 'div.popup_title'});
164         this.popup.find('div.popup_title img').bind('click', this.closePopup.bind(this));
165     },
166
167     openPopup: function()
168     {
169         this.popup.show();
170     },
171
172     centerPopup: function()
173     {
174         this.popup.center().show();
175     },
176
177     closePopup: function()
178     {
179         this.popup.hide();
180     },
181
182     setTitle: function(title)
183     {
184         this.popup.find('div.popup_title p.popup_title').html(title);
185     },
186
187     setBody: function(body)
188     {
189         this.popup.find('div.popup_body').html(body);
190     }
191 }