Serves javascript file from a per-version directory, to prevent cross-version caching...
[platal.git] / htdocs / javascript / ajax.js
index 8912dd6..fa2849d 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 
 function AjaxEngine()
 {
-    this.xml_client = null;
-    this.init = false;
-    this.obj  = null;
-    this.func = null;
-
-    this.prepare_client = function()
-    {
-        if (!this.init) {
-            if (window.XMLHttpRequest) {
-                this.xml_client = new XMLHttpRequest();
-            } else if (window.ActiveXObject) {
-                try {
-                    this.xml_client = new ActiveXObject("Msxml2.XMLHTTP");
-                } catch (e) {
-                    this.xml_client = new ActiveXObject("Microsoft.XMLHTTP");
-                }
-            }
-            if (this.xml_client == null) {
-                alert("Ton client ne supporte pas Ajax, nécessaire pour certaines fonctionalités de cette page");
-            }
-        }
-        this.init = true;
-    }
-
     this.update_html = function(obj, src, func)
     {
-        this.prepare_client();
-        if (this.xml_client == null) {
-            return true;
-        }
-        this.obj = obj;
-        this.func = func;
-        this.xml_client.abort();
-        this.xml_client.onreadystatechange = this.apply_update_html(this);
-        this.xml_client.open ('GET', src, true);
-        this.xml_client.send (null);
-        return false;
-    }
-
-    this.apply_update_html = function(ajax)
-    {
-        return function()
-        {
-            if(ajax.xml_client.readyState == 4) { 
-                if (ajax.xml_client.status == 200) { 
-                    if (ajax.obj != null) {  
-                        document.getElementById(ajax.obj).innerHTML = ajax.xml_client.responseText; 
+        $.get(src,
+            function(data, textStatus) {
+                if (textStatus == "success") {
+                    if (obj) {
+                        $('#' + obj).html(data);
                     }
-                    if (ajax.func != null) { 
-                        ajax.func(ajax.xml_client.responseText); 
+                    if (func) {
+                        func(data);
                     }
-                } else if (ajax.xml_client.status == 403) { 
-                    window.location.reload(); 
+                } else if (textStatus == "error") {
+                    alert("Une erreur s'est produite lors du traitement de la requête.\n"
+                         +"Ta session a peut-être expirée.");
                 }
-            }
-        };
+            }, 'text');
+        return false;
     }
 }
 
@@ -100,7 +61,7 @@ function _showTempMessage(id, state, back)
     var obj = document.getElementById(id);
     if (currentTempMessage != state) {
         return;
-    }   
+    }
     setOpacity(obj, back * 4);
     if (back > 0) {
         setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + "," + (back-1) + ")", 125);
@@ -123,18 +84,32 @@ function showTempMessage(id, message, success)
 
 function previewWiki(idFrom, idTo, withTitle, idShow)
 {
-    var text = encodeURIComponent(document.getElementById(idFrom).value);
+    var text = document.getElementById(idFrom).value;
     if (text == "") {
         return false;
-    }   
-    var url  = platal_baseurl + "wiki_preview";
+    }
+    var url  = "wiki_preview";
     if (!withTitle) {
         url += "/notitle";
-    }   
-    Ajax.update_html(idTo, url + "?text=" + text);
+    }
+    $.post(url, { text: text },
+        function(data) {
+            $("#" + idTo).html(data);
+        },
+        'text');
     if (idShow != null) {
         document.getElementById(idShow).style.display = "";
-    }   
+    }
+}
+
+function sendTestEmail(token, hruid)
+{
+    Ajax.update_html(null, 'emails/test' + (hruid == null ? '' : '/' + hruid) + '?token=' + token,
+                     function() {
+                        showTempMessage('mail_sent', "Un email a été envoyé avec succès"
+                                        + (hruid == null ? " sur ton adresse." : " sur l'adresse de " + hruid),
+                                        true); });
+    return false;
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: