1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
24 // {{{ jQuery object extension
27 var assert
= function(condition
, text
) {
28 if ($.isFunction(condition
)) {
29 condition
= condition();
35 throw "Assertion failed";
37 throw "Assertion failed: " + text
;
42 /* Add new functions to jQuery namesapce */
46 return function(url
) {
47 if (url
.startsWith('http', true)) {
50 if (typeof base
=== 'undefined') {
51 base
= $('head base');
52 if (base
.length
> 0) {
53 base
= base
.attr('href');
54 if (!base
.endsWith('/')) {
65 /* The goal of the following functions is to provide an AJAX API that
66 * take a different callback in case of HTTP success code (2XX) and in
70 xajax
: function(source
, method
, data
, onSuccess
, onError
, type
) {
72 if ($.isFunction(data
)) {
73 type
= type
|| onError
;
78 if (onError
&& !$.isFunction(onError
)) {
79 type
= type
|| onError
;
83 function ajaxHandler(data
, textStatus
, xhr
) {
84 if (textStatus
=== 'success') {
86 onSuccess(data
, textStatus
, xhr
);
88 } else if (textStatus
=== 'error') {
90 onError(data
, textStatus
, xhr
);
92 alert("Une error s'est produite lors du traitement de la requête.\n"
93 + "Ta session a peut-être expiré");
100 success
: ajaxHandler
,
106 xget
: function(source
, data
, onSuccess
, onError
, type
) {
107 return $.xajax(source
, 'GET', data
, onSuccess
, onError
, type
);
110 xgetJSON
: function(source
, data
, onSuccess
, onError
) {
111 return $.xget(source
, data
, onSuccess
, onError
, 'json');
114 xgetScript
: function(source
, onSuccess
, onError
) {
115 return $.xget(source
, null, onSuccess
, onError
, 'script');
118 xgetText
: function(source
, data
, onSuccess
, onError
) {
119 return $.xget(source
, data
, onSuccess
, onError
, 'text');
122 xpost
: function(source
, data
, onSuccess
, onError
, type
) {
123 return $.xajax(source
, 'POST', data
, onSuccess
, onError
, type
);
126 closeOnEsc
: function() {
127 return $(window
).keydown(function (e
) {
128 if (e
.keyCode
=== 27) {
134 dynPost
: function(action
, key
, value
) {
139 if (!$.isArray(key
)) {
150 if (values
.hasOwnProperty(k
)) {
158 $('body').appendTo(form
);
165 /* Add new functions to jQuery objects */
167 tmpMessage
: function(message
, success
) {
169 this.html("<img src='images/icons/wand.gif' alt='' /> " + message
)
170 .css('color', 'green');
172 this.html("<img src='images/icons/error.gif' alt='' /> " + message
)
173 .css('color', 'red');
175 return this.css('fontWeight', 'bold')
181 updateHtml
: function(source
, callback
) {
183 function handler(data
) {
189 $.xget(source
, handler
, 'text');
193 successMessage
: function(source
, message
) {
195 $.xget(source
, function() {
196 elements
.tmpMessage(message
, true);
201 wiki
: function(text
, withTitle
) {
203 return this.html('');
205 var url
= 'wiki_preview';
210 $.post(url
, { text
: text
},
217 popWin
: function(w
, h
) {
218 return this.click(function() {
219 window
.open(this.href
, '_blank',
220 'toolbar=0,location=0,directories=0,status=0,'
221 +'menubar=0,scrollbars=1,resizable=1,'
222 +'width='+w
+',height='+h
);
229 assertLength
: function(len
) {
230 return this.assert(function() {
231 return $(this).length
=== len
;
235 assertId
: function(id
) {
236 return this.assert(function() {
237 return $(this).attr('id') === id
;
241 assertClass
: function(clazz
) {
242 return this.assert(function() {
243 return $(this).hasClass(clazz
);
250 // {{{ function RegExp.escape()
252 RegExp
.escape
= function(text
) {
253 if (!arguments
.callee
.sRE
) {
255 '/', '.', '*', '+', '?', '|',
256 '(', ')', '[', ']', '{', '}',
259 arguments
.callee
.sRE
= new RegExp(
260 '(\\' + specials
.join('|\\') + ')', 'g'
263 return text
.replace(arguments
.callee
.sRE
, '\\$1');
267 // {{{ String extension
269 String
.prototype.startsWith
= function(str
, caseInsensitive
) {
272 if (str
.length
> this.length
) {
275 if (caseInsensitive
) {
276 str
= str
.toLowerCase();
277 cmp
= cmp
.toLowerCase();
279 return cmp
.substr(0, str
.length
) === str
;
282 String
.prototype.endsWith
= function(str
, caseInsensitive
) {
285 if (str
.length
> this.length
) {
288 if (caseInsensitive
) {
289 str
= str
.toLowerCase();
290 cmp
= cmp
.toLowerCase();
292 return cmp
.substr(cmp
.length
- str
.length
, str
.length
) === str
;
295 String
.prototype.htmlEntities
= function() {
296 return this.replace(/&/g,'&')
297 .replace(new RegExp('<','g'),'<')
298 .replace(/>/g,'>');
301 String
.prototype.contains
= function(str
, caseInsensitive
) {
303 if (str
.length
> this.length
) {
306 if (caseInsensitive
) {
307 str
= str
.toLowerCase();
308 cmp
= cmp
.toLowerCase();
310 return cmp
.indexOf(str
) >= 0;
314 // {{{ PmWiki decoding
319 convert
: function(a
) {
322 for (i
= 0; i
< a
.length
; i
++) {
324 s
+= ((b
>= 'A' && b
<= 'Z') || (b
>= 'a' && b
<= 'z') ? Nix
.map
[b
] : b
);
333 if (Nix
.map
!== null) {
337 s
= 'abcdefghijklmnopqrstuvwxyz';
338 for (i
= 0; i
< s
.length
; i
++) {
339 map
[s
.charAt(i
)] = s
.charAt((i
+13) % 26);
341 for (i
=0; i
< s
.length
; i
++) {
342 map
[s
.charAt(i
).toUpperCase()] = s
.charAt((i
+13) % 26).toUpperCase();
347 decode
: function(a
) {
348 document
.write(Nix
.convert(a
));
355 function previewWiki(idFrom
, idTo
, withTitle
, idShow
)
357 $('#' + idTo
).wiki($('#' + idFrom
).val(), withTitle
);
359 $('#' + idShow
).show();
365 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: