Escape regexps for autocompletion
[platal.git] / htdocs / javascript / ajax.js
CommitLineData
7743e0e5 1/***************************************************************************
5ddeb07c 2 * Copyright (C) 2003-2007 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 }
1f5f2526 53 this.obj = obj;
54 this.func = func;
55 this.xml_client.abort();
56 this.xml_client.onreadystatechange = this.apply_update_html(this);
57 this.xml_client.open ('GET', src, true);
58 this.xml_client.send (null);
59 return false;
60 }
61
62 this.apply_update_html = function(ajax)
63 {
64 return function()
65 {
66 if(ajax.xml_client.readyState == 4) {
67 if (ajax.xml_client.status == 200) {
68 if (ajax.obj != null) {
69 document.getElementById(ajax.obj).innerHTML = ajax.xml_client.responseText;
70 }
71 if (ajax.func != null) {
72 ajax.func(ajax.xml_client.responseText);
94c63478 73 }
1f5f2526 74 } else if (ajax.xml_client.status == 403) {
75 window.location.reload();
7743e0e5 76 }
1f5f2526 77 }
78 };
7743e0e5 79 }
80}
81
1f5f2526 82var Ajax = new AjaxEngine();
83
b9dcbddd 84var currentTempMessage = 0;
85function setOpacity(obj, opacity)
86{
87 opacity = (opacity == 100)?99:opacity;
88 // IE
89 obj.style.filter = "alpha(opacity:"+opacity+")";
90 // Safari < 1.2, Konqueror
91 obj.style.KHTMLOpacity = opacity/100;
92 // Old Mozilla
93 obj.style.MozOpacity = opacity/100;
94 // Safari >= 1.2, Firefox and Mozilla, CSS3
95 obj.style.opacity = opacity/100
96}
97
98function _showTempMessage(id, state, back)
99{
100 var obj = document.getElementById(id);
101 if (currentTempMessage != state) {
102 return;
103 }
311bc3ad 104 setOpacity(obj, back * 4);
b9dcbddd 105 if (back > 0) {
311bc3ad 106 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + "," + (back-1) + ")", 125);
b9dcbddd 107 } else {
108 obj.innerHTML = "";
109 }
110}
111
112function showTempMessage(id, message, success)
113{
114 var obj = document.getElementById(id);
115 obj.innerHTML = (success ? "<img src='images/icons/wand.gif' alt='' /> "
116 : "<img src='images/icons/error.gif' alt='' /> ") + message;
117 obj.style.fontWeight = "bold";
118 obj.style.color = (success ? "green" : "red");;
119 currentTempMessage++;
120 setOpacity(obj, 100);
311bc3ad 121 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + ", 25)", 1000);
b9dcbddd 122}
123
fdbeba4f 124function previewWiki(idFrom, idTo, withTitle, idShow)
125{
126 var text = encodeURIComponent(document.getElementById(idFrom).value);
127 if (text == "") {
128 return false;
129 }
130 var url = "wiki_preview";
131 if (!withTitle) {
132 url += "/notitle";
133 }
134 Ajax.update_html(idTo, url + "?text=" + text);
135 if (idShow != null) {
136 document.getElementById(idShow).style.display = "";
137 }
138}
139
a14159bf 140// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: