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