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