Convert error dialog to new Popup class
[infodrom.org/service.infodrom.org] / src / infodrom.js
index fa61440..1190b40 100644 (file)
@@ -22,32 +22,50 @@ function hide_message(text)
     $('#message_div').hide();
 }
 
+var errorwindow = false;
 function show_error(text, timeout)
 {
-    var div = $('#error_div');
-    if (!div.length) {
-       div = $('<div>');
-       div.attr('id', 'error_div').css('z-index','1000');
-       div.hide();
-       div.append($('<p ><img src="/pix/close.gif" title="close" onclick="return hide_error()"/></p>'));
-       div.append($('<div />'));
-       $(document.body).append(div);
+    if (!errorwindow) {
+       errorwindow = new Popup('Fehler', '500px', false, '<div id="errorbody"></div>');
+       errorwindow.setId('errorwindow');
+       $('#errorwindow').css('z-index','1000');
+       errorwindow.centerPopup();
+    } else {
+       errorwindow.openPopup();
     }
 
-    div.find('div').html(text);
-    div.center();
-    div.show();
+    $('#errorbody').html(text);
 
     if (typeof timeout != 'undefined')
-       window.setTimeout(hide_error,timeout*1000);
+       window.setTimeout(function(){errorwindow.closePopup()},timeout*1000);
 }
 
-function hide_error(text)
+function editable_callback(data)
 {
-    $('#error_div').hide();
+    if (data.content) {
+       var elem = $(data.$el);
+        var route = elem.attr('route');
+        var item_id = elem.attr('item_id');
+        if (typeof(route) == 'string' && typeof(item_id) == 'string')
+            $.invoke(route,
+                     {id: item_id,
+                      name: elem.attr('name'),
+                      content: data.content});
+    }
+
     return false;
 }
 
+function make_editable(selector)
+{
+    var list = $(selector);
+    if (list.length)
+       list.editable({
+           closeOnEnter : true,
+           event : 'click',
+           callback: editable_callback
+       });
+}
 (function($){
     $.fn.ltag = function() {
        return this.prop("tagName").toLowerCase();
@@ -103,3 +121,68 @@ $.invoke = function(name, parms, callback) {
                });
 };
 })(jQuery);
+
+function Popup(title, width, height, body) {
+    return this.initialize(title, width, height, body);
+}
+
+Popup.prototype = {
+    initialize: function(title, width, height, body)
+    {
+       this.createPopup(width, height);
+       if (title) this.setTitle(title);
+       if (body) this.setBody(body);
+       return this;
+    },
+
+    createPopup: function(width, height)
+    {
+       this.popup = $('<div class="popup">');
+       this.popup.css('width', width ? width : 'auto');
+       this.popup.css('height', height ? height : 'auto');
+       this.popup.hide().css('z-index', '1000');
+       var titlediv = $('<div class="popup_title" style="position:relative;">');
+       titlediv.append($('<p class="popup_title"></p>'));
+
+       var closeimg = $('<img src="/pix/close.gif" title="Close">');
+       titlediv.append(closeimg);
+
+       this.popup.append(titlediv);
+
+       this.popup.append($('<div class="popup_body" style="clear:both;">'));
+       $('body').append(this.popup);
+
+        this.popup.udraggable({'handle': 'div.popup_title'});
+       this.popup.find('div.popup_title img').bind('click', this.closePopup.bind(this));
+    },
+
+    openPopup: function()
+    {
+       this.popup.show();
+    },
+
+    centerPopup: function()
+    {
+       this.popup.center().show();
+    },
+
+    closePopup: function()
+    {
+       this.popup.hide();
+    },
+
+    setId: function(name)
+    {
+       this.popup.attr('id', name);
+    },
+
+    setTitle: function(title)
+    {
+       this.popup.find('div.popup_title p.popup_title').html(title);
+    },
+
+    setBody: function(body)
+    {
+       this.popup.find('div.popup_body').html(body);
+    }
+}