Improves jQuery extension in xorg.js.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 7 Nov 2010 15:48:17 +0000 (16:48 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 7 Nov 2010 15:48:17 +0000 (16:48 +0100)
This provides the following Ajax-based API:
  $.xget(url, data, onSuccess, onError, type);
  $.xgetJSON(url, data, onSuccess, onError);
  $.xgetScript(url, onSuccess, onError);
  $.xpost(url, data, onSuccess, onError, type);
These functions support does the same as the jQuery's default ajax
function (without 'x' prefix) but distinguish the case of http success and
the case of http error (500, 40x). By default onSuccess is null, while
onError display an alert.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/igoogle.js
htdocs/javascript/profile.js
htdocs/javascript/xorg.js
templates/emails/redirect.tpl

index 67889c7..ba16f9b 100644 (file)
@@ -30,7 +30,7 @@ function markEventAsRead(event_id)
     _toggle(_gel("mark-read-" + event_id));
     _gel("evt-" + event_id).setAttribute("class", "read");
     _gel("link-" + event_id).setAttribute("href", "events/unread/" + event_id);
-    $.xget("events/read/" + event_id, 'text');
+    $.xget("events/read/" + event_id);
     return false;
 }
 
index 9ba2071..32efb1f 100644 (file)
@@ -113,7 +113,7 @@ function updateNameDisplay(isFemale)
             searchnames += $('#search_name_' + i).find(':text').val() + ';;';
         }
     }
-    $.xget('profile/ajax/buildnames/' + searchnames + '/' + isFemale, 'text',
+    $.xget('profile/ajax/buildnames/' + searchnames + '/' + isFemale,
            function(data){
                var name = data.split(';');
                $('#public_name').html(name[0]);
index d804403..185e8fb 100644 (file)
@@ -361,74 +361,113 @@ 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({
+        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');
+        },
+
+        xpost: function(source, data, onSuccess, onError, type) {
+            return $.xajax(source, 'POST', data, onSuccess, onError, type);
         }
-        $.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;
         }
-        var $this = this;
-        $.post(url, { text: text },
-               function (data) {
-                   $this.html(data);
-               }, 'text');
-        return this;
-    }
+    });
 })(jQuery);
 
 // }}}
index c2cbc08..6e4512f 100644 (file)
     {
         activeEnable();
         $.xget('emails/redirect/' + (checked ? '' : 'in') + 'active/' + email,
-               'text', redirectUpdate);
+               redirectUpdate);
     }
 
     function rewriteUpdate(mail, allow, box)