Merge close_on_esc.js into xorg.js.
[platal.git] / htdocs / javascript / xorg.js
index d804403..7b49f17 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -129,15 +129,6 @@ RegExp.escape = function(text) {
  * POPUP THINGS
  */
 
-// {{{ function popWin()
-
-function popWin(theNode, w, h) {
-    window.open(theNode.href, '_blank',
-        'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h);
-    return false;
-}
-
-// }}}
 // {{{ function goodiesPopup()
 
 var __goodies_active = true;
@@ -216,43 +207,44 @@ function auto_links() {
     var light = (url.indexOf('display=light') > url.indexOf('?'));
     var resource_page = (url.indexOf('rss') > -1 || url.indexOf('ical') > -1);
 
-    $("a,link").each(
-        function(i) {
-            node = $(this);
-            href =  this.href;
-            if(!href || node.hasClass('xdx')
-               || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
-                return;
-            }
-            if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
-                node.click(function () { window.open(this.href); return false; });
-            }
-            if (href.indexOf(fqdn) > -1 && light) {
-                href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
-                this.href = href;
-            }
-            rss  = href.indexOf('rss') > -1;
-            ical = href.indexOf('ical') > -1;
-            if (rss || ical) {
-                if (href.indexOf('http') < 0) {
-                    href = 'http://' + fqdn + '/' + href;
-                }
-            }
-            if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
-                if (rss && href.indexOf('prefs/rss') < 0 &&  (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
-                    goodiesPopup(this, __goodies_rss);
-                } else if (ical) {
-                    goodiesPopup(this, __goodies_ical);
-                }
+    $("a,link").each(function(i) {
+        var node = $(this);
+        var href = this.href;
+        if(!href || node.hasClass('xdx')
+           || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
+            return;
+        }
+        if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
+            node.click(function () {
+                window.open(href);
+                return false;
+            });
+        }
+        if (href.indexOf(fqdn) > -1 && light) {
+            href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
+            this.href = href;
+        }
+        var rss  = href.indexOf('rss') > -1;
+        var ical = href.indexOf('ical') > -1;
+        if (rss || ical) {
+            if (href.indexOf('http') < 0) {
+                href = 'http://' + fqdn + '/' + href;
             }
-            if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
-                var w = matches[1], h = matches[2];
-                node.click(function() { return popWin(this, w, h); });
+        }
+        if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
+            if (rss && href.indexOf('prefs/rss') < 0 &&  (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
+                goodiesPopup(this, __goodies_rss);
+            } else if (ical) {
+                goodiesPopup(this, __goodies_ical);
             }
         }
-    );
-    $('.popup2').click(function() { return popWin(this, 840, 600); });
-    $('.popup3').click(function() { return popWin(this, 640, 800); });
+        if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
+            var w = matches[1], h = matches[2];
+            node.popWin(w, h);
+        }
+    });
+    $('.popup2').popWin(840, 600);
+    $('.popup3').popWin(640, 800);
 }
 
 
@@ -361,74 +353,140 @@ function checkPassword(box, okLabel) {
 // {{{ jQuery object extension
 
 (function($) {
-    $.xget = function(source, type, onSuccess, onError) {
-        function ajaxHandler(data, textStatus) {
-            if (textStatus == 'success') {
-                if (onSuccess) {
-                    onSuccess(data);
-                }
-            } else if (textStatus == 'error') {
-                if (onError) {
-                    onError(data);
-                } else {
-                    alert("Une error s'est produite lors du traitement de la requête.\n"
-                        + "Ta session a peut-être expiré");
-                }
+    /* Add new functions to jQuery namesapce */
+    $.extend({
+        /* The goal of the following functions is to provide an AJAX API that
+         * take a different callback in case of HTTP success code (2XX) and in
+         * other cases.
+         */
+
+        xajax: function(source, method, data, onSuccess, onError, type) {
+            /* Shift argument */
+            if ($.isFunction(data)) {
+                type = type || onError;
+                onError = onSuccess;
+                onSuccess = data;
+                data = null;
+            }
+            if (onError != null && !$.isFunction(onError)) {
+                type = type || onError;
+                onError = null;
             }
-        }
-        $.get(source, ajaxHandler, type);
-        return false;
-    }
-
-    $.fn.tmpMessage = function(message, success) {
-        if (success) {
-            this.html("<img src='images/icons/wand.gif' alt='' /> " + message)
-                .css('color', 'green');
-        } else {
-            this.html("<img src='images/icons/error.gif' alt='' /> " + message)
-                .css('color', 'red');
-        }
-        return this.css('fontWeight', 'bold')
-                   .show()
-                   .delay(1000)
-                   .fadeOut(500);
-    }
 
-    $.fn.updateHtml = function(source, callback) {
-        var elements = this;
-        function handler(data) {
-            elements.html(data);
-            if (callback) {
-                callback(data);
+            function ajaxHandler(data, textStatus, xhr) {
+                if (textStatus == 'success') {
+                    if (onSuccess) {
+                        onSuccess(data, textStatus, xhr);
+                    }
+                } else if (textStatus == 'error') {
+                    if (onError) {
+                        onError(data, textStatus, xhr);
+                    } else {
+                        alert("Une error s'est produite lors du traitement de la requête.\n"
+                            + "Ta session a peut-être expiré");
+                    }
+                }
             }
+            return $.ajax({
+                url: source,
+                type: method,
+                success: ajaxHandler,
+                data : data,
+                dataType: type
+            });
+        },
+
+        xget: function(source, data, onSuccess, onError, type) {
+            return $.xajax(source, 'GET', data, onSuccess, onError, type);
+        },
+
+        xgetJSON: function(source, data, onSuccess, onError) {
+            return $.xget(source, data, onSuccess, onError, 'json');
+        },
+
+        xgetScript: function(source, onSuccess, onError) {
+            return $.xget(source, null, onSuccess, onError, 'script');
+        },
+
+        xgetText: function(source, data, onSuccess, onError) {
+            return $.xget(source, data, onSuccess, onError, 'text');
+        },
+
+        xpost: function(source, data, onSuccess, onError, type) {
+            return $.xajax(source, 'POST', data, onSuccess, onError, type);
+        },
+
+        closeOnEsc: function() {
+            return $(window).keydown(function (e) {
+                if (e.keyCode == 27) {
+                    window.close();
+                }
+            });
         }
-        $.xget(source, 'text', handler);
-        return this;
-    }
-
-    $.fn.successMessage = function(source, message) {
-        var elements = this;
-        $.xget(source, 'text', function() {
-            elements.tmpMessage(message, true);
-        });
-        return this;
-    }
+    });
 
-    $.fn.wiki = function(text, withTitle) {
-        if (text == '') {
-            return this.html('');
-        }
-        var url = 'wiki_preview';
-        if (!withTitle) {
-            url += '/notitile';
+    /* Add new functions to jQuery objects */
+    $.fn.extend({
+        tmpMessage: function(message, success) {
+            if (success) {
+                this.html("<img src='images/icons/wand.gif' alt='' /> " + message)
+                    .css('color', 'green');
+            } else {
+                this.html("<img src='images/icons/error.gif' alt='' /> " + message)
+                    .css('color', 'red');
+            }
+            return this.css('fontWeight', 'bold')
+                       .show()
+                       .delay(1000)
+                       .fadeOut(500);
+        },
+
+        updateHtml: function(source, callback) {
+            var elements = this;
+            function handler(data) {
+                elements.html(data);
+                if (callback) {
+                    callback(data);
+                }
+            }
+            $.xget(source, handler, 'text');
+            return this;
+        },
+
+        successMessage: function(source, message) {
+            var elements = this;
+            $.xget(source, function() {
+                elements.tmpMessage(message, true);
+            });
+            return this;
+        },
+
+        wiki: function(text, withTitle) {
+            if (text == '') {
+                return this.html('');
+            }
+            var url = 'wiki_preview';
+            if (!withTitle) {
+                url += '/notitile';
+            }
+            var $this = this;
+            $.post(url, { text: text },
+                   function (data) {
+                       $this.html(data);
+                   }, 'text');
+            return this;
+        },
+
+        popWin: function(w, h) {
+            return this.click(function() {
+                window.open(this.href, '_blank',
+                            'toolbar=0,location=0,directories=0,status=0,'
+                           +'menubar=0,scrollbars=1,resizable=1,'
+                           +'width='+w+',height='+h);
+                return false;
+            });
         }
-        var $this = this;
-        $.post(url, { text: text },
-               function (data) {
-                   $this.html(data);
-               }, 'text');
-        return this;
-    }
+    });
 })(jQuery);
 
 // }}}
@@ -466,7 +524,7 @@ function sendTestEmail(token, hruid)
  * The real OnLoad
  */
 
-$(document).ready(function() {
+$(function() {
     auto_links();
     getNow();
     setInterval(getNow, 1000);