Merge commit 'origin/master' into hruid
[platal.git] / htdocs / javascript / ajax.js
CommitLineData
7743e0e5 1/***************************************************************************
179afa7f 2 * Copyright (C) 2003-2008 Polytechnique.org *
7743e0e5 3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
1f5f2526 21function AjaxEngine()
22{
23 this.xml_client = null;
24 this.init = false;
25 this.obj = null;
26 this.func = null;
7743e0e5 27
1f5f2526 28 this.prepare_client = function()
7743e0e5 29 {
1f5f2526 30 if (!this.init) {
7743e0e5 31 if (window.XMLHttpRequest) {
1f5f2526 32 this.xml_client = new XMLHttpRequest();
7743e0e5 33 } else if (window.ActiveXObject) {
34 try {
1f5f2526 35 this.xml_client = new ActiveXObject("Msxml2.XMLHTTP");
7743e0e5 36 } catch (e) {
1f5f2526 37 this.xml_client = new ActiveXObject("Microsoft.XMLHTTP");
7743e0e5 38 }
39 }
1f5f2526 40 if (this.xml_client == null) {
a14159bf 41 alert("Ton client ne supporte pas Ajax, nécessaire pour certaines fonctionalités de cette page");
7743e0e5 42 }
43 }
1f5f2526 44 this.init = true;
45 }
7743e0e5 46
1f5f2526 47 this.update_html = function(obj, src, func)
7743e0e5 48 {
1f5f2526 49 this.prepare_client();
50 if (this.xml_client == null) {
7743e0e5 51 return true;
52 }
9807a8ee 53 if (src.match(/^http/i) == null) {
54 src = platal_baseurl + src;
55 }
1f5f2526 56 this.obj = obj;
57 this.func = func;
58 this.xml_client.abort();
59 this.xml_client.onreadystatechange = this.apply_update_html(this);
60 this.xml_client.open ('GET', src, true);
61 this.xml_client.send (null);
62 return false;
63 }
64
65 this.apply_update_html = function(ajax)
66 {
67 return function()
68 {
5ff3f06b
FB
69 if(ajax.xml_client.readyState == 4) {
70 if (ajax.xml_client.status == 200) {
71 if (ajax.obj != null) {
72 document.getElementById(ajax.obj).innerHTML = ajax.xml_client.responseText;
1f5f2526 73 }
5ff3f06b
FB
74 if (ajax.func != null) {
75 ajax.func(ajax.xml_client.responseText);
94c63478 76 }
5ff3f06b
FB
77 } else if (ajax.xml_client.status == 403) {
78 window.location.reload();
4654187f
FB
79 } else if (ajax.xml_client.status >= 500) {
80 alert("Une erreur s'est produite lors du traitement de la requête");
7743e0e5 81 }
1f5f2526 82 }
83 };
7743e0e5 84 }
85}
86
1f5f2526 87var Ajax = new AjaxEngine();
88
b9dcbddd 89var currentTempMessage = 0;
90function setOpacity(obj, opacity)
91{
92 opacity = (opacity == 100)?99:opacity;
93 // IE
94 obj.style.filter = "alpha(opacity:"+opacity+")";
95 // Safari < 1.2, Konqueror
96 obj.style.KHTMLOpacity = opacity/100;
97 // Old Mozilla
98 obj.style.MozOpacity = opacity/100;
99 // Safari >= 1.2, Firefox and Mozilla, CSS3
100 obj.style.opacity = opacity/100
101}
102
103function _showTempMessage(id, state, back)
104{
105 var obj = document.getElementById(id);
106 if (currentTempMessage != state) {
107 return;
5e70bf24 108 }
311bc3ad 109 setOpacity(obj, back * 4);
b9dcbddd 110 if (back > 0) {
311bc3ad 111 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + "," + (back-1) + ")", 125);
b9dcbddd 112 } else {
113 obj.innerHTML = "";
114 }
115}
116
117function showTempMessage(id, message, success)
118{
119 var obj = document.getElementById(id);
120 obj.innerHTML = (success ? "<img src='images/icons/wand.gif' alt='' /> "
121 : "<img src='images/icons/error.gif' alt='' /> ") + message;
122 obj.style.fontWeight = "bold";
123 obj.style.color = (success ? "green" : "red");;
124 currentTempMessage++;
125 setOpacity(obj, 100);
311bc3ad 126 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + ", 25)", 1000);
b9dcbddd 127}
128
fdbeba4f 129function previewWiki(idFrom, idTo, withTitle, idShow)
130{
131 var text = encodeURIComponent(document.getElementById(idFrom).value);
132 if (text == "") {
133 return false;
5ff3f06b 134 }
9807a8ee 135 var url = "wiki_preview";
fdbeba4f 136 if (!withTitle) {
137 url += "/notitle";
5ff3f06b 138 }
fdbeba4f 139 Ajax.update_html(idTo, url + "?text=" + text);
140 if (idShow != null) {
141 document.getElementById(idShow).style.display = "";
5ff3f06b
FB
142 }
143}
144
46504fb3 145function sendTestEmail(token, forlife)
5ff3f06b 146{
46504fb3 147 Ajax.update_html(null, 'emails/test' + (forlife == null ? '' : '/' + forlife) + '?token=' + token,
5ff3f06b 148 function() {
faefdbb7 149 showTempMessage('mail_sent', "Un email a été envoyé avec succès"
5ff3f06b
FB
150 + (forlife == null ? " sur ton adresse." : " sur l'adresse de " + forlife),
151 true); });
152 return false;
fdbeba4f 153}
154
a14159bf 155// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: