1190b4096cd0935ab5f95beac3d6a4cb3e2ed1da
[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 var errorwindow = false;
26 function show_error(text, timeout)
27 {
28     if (!errorwindow) {
29         errorwindow = new Popup('Fehler', '500px', false, '<div id="errorbody"></div>');
30         errorwindow.setId('errorwindow');
31         $('#errorwindow').css('z-index','1000');
32         errorwindow.centerPopup();
33     } else {
34         errorwindow.openPopup();
35     }
36
37     $('#errorbody').html(text);
38
39     if (typeof timeout != 'undefined')
40         window.setTimeout(function(){errorwindow.closePopup()},timeout*1000);
41 }
42
43 function editable_callback(data)
44 {
45     if (data.content) {
46        var elem = $(data.$el);
47         var route = elem.attr('route');
48         var item_id = elem.attr('item_id');
49         if (typeof(route) == 'string' && typeof(item_id) == 'string')
50             $.invoke(route,
51                      {id: item_id,
52                       name: elem.attr('name'),
53                       content: data.content});
54     }
55
56     return false;
57 }
58
59 function make_editable(selector)
60 {
61     var list = $(selector);
62     if (list.length)
63        list.editable({
64            closeOnEnter : true,
65            event : 'click',
66            callback: editable_callback
67        });
68 }
69 (function($){
70     $.fn.ltag = function() {
71         return this.prop("tagName").toLowerCase();
72     };
73
74     $.fn.center = function () {
75         this.css("position","absolute");
76         this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
77                                  $(window).scrollTop()) + "px");
78         this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
79                                   $(window).scrollLeft()) + "px");
80         return this;
81     };
82
83 $.invoke = function(name, parms, callback) {
84     if (typeof(parms) == 'string' && parms.length)
85         parms = 'route='+name+'&'+parms;
86     else if (typeof(parms) == 'object')
87         parms['route'] = name;
88     else
89         parms = 'route='+name;
90
91     $.post('/ajax.php',
92                 parms,
93                 function(data){
94                     if (!data.status) {
95                         if (data.error) {
96                             if (data.redirect_login) {
97                                 show_message(data.error, 5);
98                                 setTimeout(function(){window.location.href = '/';}, 5000);
99                                 return;
100                             } else
101                                 return show_error(data.error);
102                         } else
103                             return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
104                     }
105                     if (typeof(data.html) == 'object')
106                         for (id in data.html) {
107                             var elem = $('#'+id);
108                             if (elem.length) {
109                                 elem.html(data.html[id]);
110                             }
111                         }
112                     if (typeof(data.values) == 'object')
113                         for (id in data.values) {
114                             var elem = $('#'+id);
115                             if (elem.length)
116                                 elem.val(data.values[id]);
117                         }
118
119                     if (typeof(callback) == 'function')
120                         return callback(data);
121                 });
122 };
123 })(jQuery);
124
125 function Popup(title, width, height, body) {
126     return this.initialize(title, width, height, body);
127 }
128
129 Popup.prototype = {
130     initialize: function(title, width, height, body)
131     {
132         this.createPopup(width, height);
133         if (title) this.setTitle(title);
134         if (body) this.setBody(body);
135         return this;
136     },
137
138     createPopup: function(width, height)
139     {
140         this.popup = $('<div class="popup">');
141         this.popup.css('width', width ? width : 'auto');
142         this.popup.css('height', height ? height : 'auto');
143         this.popup.hide().css('z-index', '1000');
144         var titlediv = $('<div class="popup_title" style="position:relative;">');
145         titlediv.append($('<p class="popup_title"></p>'));
146
147         var closeimg = $('<img src="/pix/close.gif" title="Close">');
148         titlediv.append(closeimg);
149
150         this.popup.append(titlediv);
151
152         this.popup.append($('<div class="popup_body" style="clear:both;">'));
153         $('body').append(this.popup);
154
155         this.popup.udraggable({'handle': 'div.popup_title'});
156         this.popup.find('div.popup_title img').bind('click', this.closePopup.bind(this));
157     },
158
159     openPopup: function()
160     {
161         this.popup.show();
162     },
163
164     centerPopup: function()
165     {
166         this.popup.center().show();
167     },
168
169     closePopup: function()
170     {
171         this.popup.hide();
172     },
173
174     setId: function(name)
175     {
176         this.popup.attr('id', name);
177     },
178
179     setTitle: function(title)
180     {
181         this.popup.find('div.popup_title p.popup_title').html(title);
182     },
183
184     setBody: function(body)
185     {
186         this.popup.find('div.popup_body').html(body);
187     }
188 }