eb0893b5838ba5de2e228a4346d046d7a5a2e4b6
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
;
41 var ajaxParams
= function(onSuccess
, onError
, extraParameters
) {
43 success
: function(data
, textStatus
, xhr
) {
44 if (textStatus
=== 'success') {
46 onSuccess(data
, textStatus
, xhr
);
48 } else if (textStatus
=== 'error') {
50 onError(data
, textStatus
, xhr
);
52 alert("Une error s'est produite lors du traitement de la requête.\n"
53 + "Ta session a peut-être expiré");
60 /* Add new functions to jQuery namesapce */
66 return function(url
) {
67 if (url
.startsWith('http', true)) {
70 if (typeof base
=== 'undefined') {
71 base
= $('head base');
72 if (base
.length
> 0) {
73 base
= base
.attr('href');
74 if (!base
.endsWith('/')) {
85 /* The goal of the following functions is to provide an AJAX API that
86 * take a different callback in case of HTTP success code (2XX) and in
90 xajax
: function(source
, method
, data
, onSuccess
, onError
, type
) {
92 if ($.isFunction(data
)) {
93 type
= type
|| onError
;
98 if (onError
&& !$.isFunction(onError
)) {
99 type
= type
|| onError
;
103 return $.ajax(ajaxParams(onSuccess
, onError
, {
104 url
: $.plURL(source
),
111 xapi
: function(apicall
, payload
, onSuccess
, onError
) {
112 if ($.isFunction(payload
)) {
117 return $.ajax(ajaxParams(onSuccess
, onError
, {
118 url
: $.plURL('api/' + $.xapiVersion + '/' + apicall
),
120 data
: JSON
.stringify(payload
),
122 contentType
: 'text/javascript'
126 xget
: function(source
, data
, onSuccess
, onError
, type
) {
127 return $.xajax(source
, 'GET', data
, onSuccess
, onError
, type
);
130 xgetJSON
: function(source
, data
, onSuccess
, onError
) {
131 return $.xget(source
, data
, onSuccess
, onError
, 'json');
134 xgetScript
: function(source
, onSuccess
, onError
) {
135 return $.xget(source
, null, onSuccess
, onError
, 'script');
138 xgetText
: function(source
, data
, onSuccess
, onError
) {
139 return $.xget(source
, data
, onSuccess
, onError
, 'text');
142 xpost
: function(source
, data
, onSuccess
, onError
, type
) {
143 return $.xajax(source
, 'POST', data
, onSuccess
, onError
, type
);
146 closeOnEsc
: function() {
147 return $(window
).keydown(function (e
) {
148 if (e
.keyCode
=== 27) {
154 dynPost
: function(action
, key
, value
) {
159 if (!$.isArray(key
)) {
170 if (values
.hasOwnProperty(k
)) {
178 $('body').appendTo(form
);
185 /* Add new functions to jQuery objects */
187 tmpMessage
: function(message
, success
) {
189 this.html("<img src='images/icons/wand.gif' alt='' /> " + message
)
190 .css('color', 'green');
192 this.html("<img src='images/icons/error.gif' alt='' /> " + message
)
193 .css('color', 'red');
195 return this.css('fontWeight', 'bold')
201 updateHtml
: function(source
, callback
) {
203 function handler(data
) {
209 $.xget(source
, handler
, 'text');
213 successMessage
: function(source
, message
) {
215 $.xget(source
, function() {
216 elements
.tmpMessage(message
, true);
221 wiki
: function(text
, withTitle
) {
223 return this.html('');
225 var url
= 'wiki_preview';
230 $.post(url
, { text
: text
},
237 popWin
: function(w
, h
) {
238 return this.click(function() {
239 window
.open(this.href
, '_blank',
240 'toolbar=0,location=0,directories=0,status=0,'
241 +'menubar=0,scrollbars=1,resizable=1,'
242 +'width='+w
+',height='+h
);
249 assertLength
: function(len
) {
250 return this.assert(function() {
251 return $(this).length
=== len
;
255 assertId
: function(id
) {
256 return this.assert(function() {
257 return $(this).attr('id') === id
;
261 assertClass
: function(clazz
) {
262 return this.assert(function() {
263 return $(this).hasClass(clazz
);
270 // {{{ function RegExp.escape()
272 RegExp
.escape
= function(text
) {
273 if (!arguments
.callee
.sRE
) {
275 '/', '.', '*', '+', '?', '|',
276 '(', ')', '[', ']', '{', '}',
279 arguments
.callee
.sRE
= new RegExp(
280 '(\\' + specials
.join('|\\') + ')', 'g'
283 return text
.replace(arguments
.callee
.sRE
, '\\$1');
287 // {{{ String extension
289 String
.prototype.startsWith
= function(str
, caseInsensitive
) {
292 if (str
.length
> this.length
) {
295 if (caseInsensitive
) {
296 str
= str
.toLowerCase();
297 cmp
= cmp
.toLowerCase();
299 return cmp
.substr(0, str
.length
) === str
;
302 String
.prototype.endsWith
= function(str
, caseInsensitive
) {
305 if (str
.length
> this.length
) {
308 if (caseInsensitive
) {
309 str
= str
.toLowerCase();
310 cmp
= cmp
.toLowerCase();
312 return cmp
.substr(cmp
.length
- str
.length
, str
.length
) === str
;
315 String
.prototype.htmlEntities
= function() {
316 return this.replace(/&/g,'&')
317 .replace(new RegExp('<','g'),'<')
318 .replace(/>/g,'>');
321 String
.prototype.contains
= function(str
, caseInsensitive
) {
323 if (str
.length
> this.length
) {
326 if (caseInsensitive
) {
327 str
= str
.toLowerCase();
328 cmp
= cmp
.toLowerCase();
330 return cmp
.indexOf(str
) >= 0;
334 // {{{ PmWiki decoding
339 convert
: function(a
) {
342 for (i
= 0; i
< a
.length
; i
++) {
344 s
+= ((b
>= 'A' && b
<= 'Z') || (b
>= 'a' && b
<= 'z') ? Nix
.map
[b
] : b
);
353 if (Nix
.map
!== null) {
357 s
= 'abcdefghijklmnopqrstuvwxyz';
358 for (i
= 0; i
< s
.length
; i
++) {
359 map
[s
.charAt(i
)] = s
.charAt((i
+13) % 26);
361 for (i
=0; i
< s
.length
; i
++) {
362 map
[s
.charAt(i
).toUpperCase()] = s
.charAt((i
+13) % 26).toUpperCase();
367 decode
: function(a
) {
368 document
.write(Nix
.convert(a
));
375 function previewWiki(idFrom
, idTo
, withTitle
, idShow
)
377 $('#' + idTo
).wiki($('#' + idFrom
).val(), withTitle
);
379 $('#' + idShow
).show();
385 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: