Rewrite our pour implementation of Ajax using jQuery.
[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{
1f5f2526 23 this.update_html = function(obj, src, func)
7743e0e5 24 {
053c41fc
FB
25 $.get(src,
26 function(data, textStatus) {
27 if (textStatus == "success") {
28 if (obj) {
29 $('#' + obj).html(data);
1f5f2526 30 }
053c41fc
FB
31 if (func) {
32 func(data);
94c63478 33 }
053c41fc
FB
34 } else if (textStatus == "error") {
35 alert("Une erreur s'est produite lors du traitement de la requête.\n"
36 +"Ta session a peut-être expirée.");
7743e0e5 37 }
053c41fc
FB
38 }, 'text');
39 return false;
7743e0e5 40 }
41}
42
1f5f2526 43var Ajax = new AjaxEngine();
44
b9dcbddd 45var currentTempMessage = 0;
46function setOpacity(obj, opacity)
47{
48 opacity = (opacity == 100)?99:opacity;
49 // IE
50 obj.style.filter = "alpha(opacity:"+opacity+")";
51 // Safari < 1.2, Konqueror
52 obj.style.KHTMLOpacity = opacity/100;
53 // Old Mozilla
54 obj.style.MozOpacity = opacity/100;
55 // Safari >= 1.2, Firefox and Mozilla, CSS3
56 obj.style.opacity = opacity/100
57}
58
59function _showTempMessage(id, state, back)
60{
61 var obj = document.getElementById(id);
62 if (currentTempMessage != state) {
63 return;
5e70bf24 64 }
311bc3ad 65 setOpacity(obj, back * 4);
b9dcbddd 66 if (back > 0) {
311bc3ad 67 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + "," + (back-1) + ")", 125);
b9dcbddd 68 } else {
69 obj.innerHTML = "";
70 }
71}
72
73function showTempMessage(id, message, success)
74{
75 var obj = document.getElementById(id);
76 obj.innerHTML = (success ? "<img src='images/icons/wand.gif' alt='' /> "
77 : "<img src='images/icons/error.gif' alt='' /> ") + message;
78 obj.style.fontWeight = "bold";
79 obj.style.color = (success ? "green" : "red");;
80 currentTempMessage++;
81 setOpacity(obj, 100);
311bc3ad 82 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + ", 25)", 1000);
b9dcbddd 83}
84
fdbeba4f 85function previewWiki(idFrom, idTo, withTitle, idShow)
86{
41ec982a 87 var text = document.getElementById(idFrom).value;
fdbeba4f 88 if (text == "") {
89 return false;
5ff3f06b 90 }
9807a8ee 91 var url = "wiki_preview";
fdbeba4f 92 if (!withTitle) {
93 url += "/notitle";
5ff3f06b 94 }
41ec982a
FB
95 $.post(url, { text: text },
96 function(data) {
97 $("#" + idTo).html(data);
98 },
99 'text');
fdbeba4f 100 if (idShow != null) {
101 document.getElementById(idShow).style.display = "";
5ff3f06b
FB
102 }
103}
104
90c614cd 105function sendTestEmail(token, hruid)
5ff3f06b 106{
90c614cd 107 Ajax.update_html(null, 'emails/test' + (hruid == null ? '' : '/' + hruid) + '?token=' + token,
5ff3f06b 108 function() {
faefdbb7 109 showTempMessage('mail_sent', "Un email a été envoyé avec succès"
90c614cd 110 + (hruid == null ? " sur ton adresse." : " sur l'adresse de " + hruid),
5ff3f06b
FB
111 true); });
112 return false;
fdbeba4f 113}
114
a14159bf 115// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: