d9f38f3cd0a8f7d206bf65e6fe69b6b627c08ab9
[platal.git] / htdocs / javascript / core.js
1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
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
21 // {{{ jQuery object extension
22
23 (function($) {
24 /* Add new functions to jQuery namesapce */
25 $.extend({
26 /* The goal of the following functions is to provide an AJAX API that
27 * take a different callback in case of HTTP success code (2XX) and in
28 * other cases.
29 */
30
31 xajax: function(source, method, data, onSuccess, onError, type) {
32 /* Shift argument */
33 if ($.isFunction(data)) {
34 type = type || onError;
35 onError = onSuccess;
36 onSuccess = data;
37 data = null;
38 }
39 if (onError != null && !$.isFunction(onError)) {
40 type = type || onError;
41 onError = null;
42 }
43
44 function ajaxHandler(data, textStatus, xhr) {
45 if (textStatus == 'success') {
46 if (onSuccess) {
47 onSuccess(data, textStatus, xhr);
48 }
49 } else if (textStatus == 'error') {
50 if (onError) {
51 onError(data, textStatus, xhr);
52 } else {
53 alert("Une error s'est produite lors du traitement de la requête.\n"
54 + "Ta session a peut-être expiré");
55 }
56 }
57 }
58 return $.ajax({
59 url: source,
60 type: method,
61 success: ajaxHandler,
62 data : data,
63 dataType: type
64 });
65 },
66
67 xget: function(source, data, onSuccess, onError, type) {
68 return $.xajax(source, 'GET', data, onSuccess, onError, type);
69 },
70
71 xgetJSON: function(source, data, onSuccess, onError) {
72 return $.xget(source, data, onSuccess, onError, 'json');
73 },
74
75 xgetScript: function(source, onSuccess, onError) {
76 return $.xget(source, null, onSuccess, onError, 'script');
77 },
78
79 xgetText: function(source, data, onSuccess, onError) {
80 return $.xget(source, data, onSuccess, onError, 'text');
81 },
82
83 xpost: function(source, data, onSuccess, onError, type) {
84 return $.xajax(source, 'POST', data, onSuccess, onError, type);
85 },
86
87 closeOnEsc: function() {
88 return $(window).keydown(function (e) {
89 if (e.keyCode == 27) {
90 window.close();
91 }
92 });
93 },
94
95 dynPost: function(action, key, value) {
96 var values;
97 if (!$.isArray(key)) {
98 values = { };
99 values[key] = value;
100 } else {
101 values = key;
102 }
103 var form = $('<form>', {
104 action: action,
105 method: 'post'
106 });
107 for (var k in values) {
108 $('<input>', {
109 type: 'hidden',
110 name: k,
111 value: values[k]
112 }).appendTo(form);
113 }
114 $('body').appendTo(form);
115 form.submit();
116 }
117 });
118
119 /* Add new functions to jQuery objects */
120 $.fn.extend({
121 tmpMessage: function(message, success) {
122 if (success) {
123 this.html("<img src='images/icons/wand.gif' alt='' /> " + message)
124 .css('color', 'green');
125 } else {
126 this.html("<img src='images/icons/error.gif' alt='' /> " + message)
127 .css('color', 'red');
128 }
129 return this.css('fontWeight', 'bold')
130 .show()
131 .delay(1000)
132 .fadeOut(500);
133 },
134
135 updateHtml: function(source, callback) {
136 var elements = this;
137 function handler(data) {
138 elements.html(data);
139 if (callback) {
140 callback(data);
141 }
142 }
143 $.xget(source, handler, 'text');
144 return this;
145 },
146
147 successMessage: function(source, message) {
148 var elements = this;
149 $.xget(source, function() {
150 elements.tmpMessage(message, true);
151 });
152 return this;
153 },
154
155 wiki: function(text, withTitle) {
156 if (text == '') {
157 return this.html('');
158 }
159 var url = 'wiki_preview';
160 if (!withTitle) {
161 url += '/notitile';
162 }
163 var $this = this;
164 $.post(url, { text: text },
165 function (data) {
166 $this.html(data);
167 }, 'text');
168 return this;
169 },
170
171 popWin: function(w, h) {
172 return this.click(function() {
173 window.open(this.href, '_blank',
174 'toolbar=0,location=0,directories=0,status=0,'
175 +'menubar=0,scrollbars=1,resizable=1,'
176 +'width='+w+',height='+h);
177 return false;
178 });
179 }
180 });
181 })(jQuery);
182
183 // }}}
184 // {{{ function RegExp.escape()
185
186 RegExp.escape = function(text) {
187 if (!arguments.callee.sRE) {
188 var specials = [
189 '/', '.', '*', '+', '?', '|',
190 '(', ')', '[', ']', '{', '}',
191 '\\', '^' , '$'
192 ];
193 arguments.callee.sRE = new RegExp(
194 '(\\' + specials.join('|\\') + ')', 'g'
195 );
196 }
197 return text.replace(arguments.callee.sRE, '\\$1');
198 }
199
200 // }}}
201 // {{{ PmWiki decoding
202
203 Nix = {
204 map: null,
205 convert: function(a) {
206 Nix.init();
207 var s = '';
208 for (i = 0; i < a.length ; i++) {
209 var b = a.charAt(i);
210 s += ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') ? Nix.map[b] : b);
211 }
212 return s;
213 },
214 init: function() {
215 if (Nix.map != null)
216 return;
217 var map = new Array();
218 var s='abcdefghijklmnopqrstuvwxyz';
219 for (i = 0; i < s.length; i++)
220 map[s.charAt(i)] = s.charAt((i+13)%26);
221 for (i=0; i<s.length; i++)map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
222 Nix.map = map;
223 },
224 decode: function(a) {
225 document.write(Nix.convert(a));
226 }
227 }
228
229 // }}}
230 // {{{ preview wiki
231
232 function previewWiki(idFrom, idTo, withTitle, idShow)
233 {
234 $('#' + idTo).wiki($('#' + idFrom).val(), withTitle);
235 if (idShow != null) {
236 $('#' + idShow).show();
237 }
238 }
239
240 // }}}
241
242 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: