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