42bf6b78cb2581e871f18d1b2ed9092ee5ce9d13
[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
70 function site_url(path)
71 {
72     return SERVICE_ROOT + path;
73 }
74
75 (function($){
76     $.fn.ltag = function() {
77         return this.prop("tagName").toLowerCase();
78     };
79
80     $.fn.center = function () {
81         this.css("position","absolute");
82         this.css("top", Math.max(0, ((window.innerHeight - $(this).outerHeight()) / 2) +
83                                  $(window).scrollTop()) + "px");
84         this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
85                                   $(window).scrollLeft()) + "px");
86         return this;
87     };
88
89     $.fn.positionOn = function(element, align) {
90         return this.each(function() {
91             var target   = $(this);
92             var position = element.offset();
93
94             var x      = position.left;
95             var y      = position.top;
96
97             if(align == 'right') {
98                 x -= (target.outerWidth() - element.outerWidth());
99             } else if(align == 'center') {
100                 x -= target.outerWidth() / 2 - element.outerWidth() / 2;
101             }
102
103             target.css({
104                 position: 'absolute',
105                 zIndex:   5000,
106                 top:      y,
107                 left:     x
108             });
109         });
110     };
111
112 $.invoke = function(name, parms, callback) {
113     if (typeof(parms) == 'string' && parms.length)
114         parms = 'route='+name+'&'+parms;
115     else if (typeof(parms) == 'object')
116         parms['route'] = name;
117     else
118         parms = 'route='+name;
119
120     $.post('/ajax.php',
121                 parms,
122                 function(data){
123                     if (!data.status) {
124                         if (data.error) {
125                             if (data.redirect_login) {
126                                 show_message(data.error, 5);
127                                 setTimeout(function(){window.location.href = '/';}, 5000);
128                                 return;
129                             } else
130                                 return show_error(data.error);
131                         } else
132                             return show_error('Fehler im Backend zu "'+name+'" aufgetreten');
133                     }
134                     if (typeof(data.html) == 'object')
135                         for (id in data.html) {
136                             var elem = $('#'+id);
137                             if (elem.length) {
138                                 elem.html(data.html[id]);
139                             }
140                         }
141                     if (typeof(data.values) == 'object')
142                         for (id in data.values) {
143                             var elem = $('#'+id);
144                             if (elem.length)
145                                 elem.val(data.values[id]);
146                         }
147
148                     if (typeof(callback) == 'function')
149                         return callback(data);
150                 });
151 };
152 })(jQuery);
153
154 function Popup(title, width, height, body) {
155     return this.initialize(title, width, height, body);
156 }
157
158 Popup.prototype = {
159     initialize: function(title, width, height, body)
160     {
161         this.createPopup(width, height);
162         if (title) this.setTitle(title);
163         if (body) this.setBody(body);
164         return this;
165     },
166
167     createPopup: function(width, height)
168     {
169         this.popup = $('<div class="popup">');
170         this.popup.css('width', width ? width : 'auto');
171         this.popup.css('height', height ? height : 'auto');
172         this.popup.hide().css('z-index', '1000');
173         var titlediv = $('<div class="popup_title" style="position:relative;">');
174         titlediv.append($('<p class="popup_title"></p>'));
175
176         var closeimg = $('<img src="/pix/close.gif" title="Close">');
177         titlediv.append(closeimg);
178
179         this.popup.append(titlediv);
180
181         this.popup.append($('<div class="popup_body" style="clear:both;">'));
182         $('body').append(this.popup);
183
184         this.popup.udraggable({'handle': 'div.popup_title'});
185         this.popup.find('div.popup_title img').bind('click', this.closePopup.bind(this));
186     },
187
188     openPopup: function()
189     {
190         this.popup.show();
191     },
192
193     centerPopup: function()
194     {
195         this.popup.center().show();
196     },
197
198     closePopup: function()
199     {
200         this.popup.hide();
201     },
202
203     setId: function(name)
204     {
205         this.popup.attr('id', name);
206     },
207
208     setTitle: function(title)
209     {
210         this.popup.find('div.popup_title p.popup_title').html(title);
211     },
212
213     setBody: function(body)
214     {
215         this.popup.find('div.popup_body').html(body);
216     }
217 }