X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=htdocs%2Fjavascript%2Fajax.js;h=bc913035cc326b27358bd1ae60340259601e382b;hb=3be64a1586fc6c744a888e2e882d7b9330fe2d73;hp=2bf3c0227f688cb055eb001c82d5275ed58b6c23;hpb=db3bd1464d42de8adbd48a8b3a22af70f0912129;p=platal.git diff --git a/htdocs/javascript/ajax.js b/htdocs/javascript/ajax.js index 2bf3c02..bc91303 100644 --- a/htdocs/javascript/ajax.js +++ b/htdocs/javascript/ajax.js @@ -18,55 +18,136 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ -Ajax = { - xml_client: null, - init: false, +function AjaxEngine() +{ + this.xml_client = null; + this.init = false; + this.obj = null; + this.func = null; - prepare_client: function() + this.prepare_client = function() { - if (!Ajax.init) { + if (!this.init) { if (window.XMLHttpRequest) { - Ajax.xml_client = new XMLHttpRequest(); + this.xml_client = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { - Ajax.xml_client = new ActiveXObject("Msxml2.XMLHTTP"); + this.xml_client = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { - Ajax.xml_client = new ActiveXObject("Microsoft.XMLHTTP"); + this.xml_client = new ActiveXObject("Microsoft.XMLHTTP"); } } - if (Ajax.xml_client == null) { + if (this.xml_client == null) { alert("Ton client ne supporte pas Ajax, nécessaire pour certaines fonctionalités de cette page"); } } - Ajax.init = true; - }, + this.init = true; + } - update_html: function(obj, src, func) + this.update_html = function(obj, src, func) { - Ajax.prepare_client(); - if (Ajax.xml_client == null) { + this.prepare_client(); + if (this.xml_client == null) { return true; } - Ajax.xml_client.abort(); - Ajax.xml_client.onreadystatechange = function() - { - if(Ajax.xml_client.readyState == 4) { - if (Ajax.xml_client.status == 200) { - if (obj != null) { - document.getElementById(obj).innerHTML = Ajax.xml_client.responseText; - } - if (func != null) { - func(Ajax.xml_client.responseText); - } - } else if (Ajax.xml_client.status == 403) { - window.location.reload(); + if (src.match(/^http/i) == null) { + src = platal_baseurl + src; + } + 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; } + if (ajax.func != null) { + ajax.func(ajax.xml_client.responseText); + } + } else if (ajax.xml_client.status == 403) { + window.location.reload(); } - }; - Ajax.xml_client.open ('GET', src, true); - Ajax.xml_client.send (null); + } + }; + } +} + +var Ajax = new AjaxEngine(); + +var currentTempMessage = 0; +function setOpacity(obj, opacity) +{ + opacity = (opacity == 100)?99:opacity; + // IE + obj.style.filter = "alpha(opacity:"+opacity+")"; + // Safari < 1.2, Konqueror + obj.style.KHTMLOpacity = opacity/100; + // Old Mozilla + obj.style.MozOpacity = opacity/100; + // Safari >= 1.2, Firefox and Mozilla, CSS3 + obj.style.opacity = opacity/100 +} + +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); + } else { + obj.innerHTML = ""; + } +} + +function showTempMessage(id, message, success) +{ + var obj = document.getElementById(id); + obj.innerHTML = (success ? " " + : " ") + message; + obj.style.fontWeight = "bold"; + obj.style.color = (success ? "green" : "red");; + currentTempMessage++; + setOpacity(obj, 100); + setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + ", 25)", 1000); +} + +function previewWiki(idFrom, idTo, withTitle, idShow) +{ + var text = encodeURIComponent(document.getElementById(idFrom).value); + if (text == "") { return false; } + var url = "wiki_preview"; + if (!withTitle) { + url += "/notitle"; + } + Ajax.update_html(idTo, url + "?text=" + text); + if (idShow != null) { + document.getElementById(idShow).style.display = ""; + } +} + +function sendTestEmail(forlife) +{ + Ajax.update_html(null, 'emails/test' + (forlife == null ? '' : '/' + forlife), + function() { + showTempMessage('mail_sent', "Un mail a été envoyé avec succès" + + (forlife == null ? " sur ton adresse." : " sur l'adresse de " + forlife), + true); }); + return false; } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: