bc9463ee9a7b350570896d839d21d2fb519cdd62
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 function assert(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 */
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
49 xajax
: function(source
, method
, data
, onSuccess
, onError
, type
) {
51 if ($.isFunction(data
)) {
52 type
= type
|| onError
;
57 if (onError
!= null && !$.isFunction(onError
)) {
58 type
= type
|| onError
;
62 function ajaxHandler(data
, textStatus
, xhr
) {
63 if (textStatus
== 'success') {
65 onSuccess(data
, textStatus
, xhr
);
67 } else if (textStatus
== 'error') {
69 onError(data
, textStatus
, xhr
);
71 alert("Une error s'est produite lors du traitement de la requête.\n"
72 + "Ta session a peut-être expiré");
85 xget
: function(source
, data
, onSuccess
, onError
, type
) {
86 return $.xajax(source
, 'GET', data
, onSuccess
, onError
, type
);
89 xgetJSON
: function(source
, data
, onSuccess
, onError
) {
90 return $.xget(source
, data
, onSuccess
, onError
, 'json');
93 xgetScript
: function(source
, onSuccess
, onError
) {
94 return $.xget(source
, null, onSuccess
, onError
, 'script');
97 xgetText
: function(source
, data
, onSuccess
, onError
) {
98 return $.xget(source
, data
, onSuccess
, onError
, 'text');
101 xpost
: function(source
, data
, onSuccess
, onError
, type
) {
102 return $.xajax(source
, 'POST', data
, onSuccess
, onError
, type
);
105 closeOnEsc
: function() {
106 return $(window
).keydown(function (e
) {
107 if (e
.keyCode
== 27) {
113 dynPost
: function(action
, key
, value
) {
115 if (!$.isArray(key
)) {
121 var form
= $('<form>', {
125 for (var k
in values
) {
132 $('body').appendTo(form
);
139 /* Add new functions to jQuery objects */
141 tmpMessage
: function(message
, success
) {
143 this.html("<img src='images/icons/wand.gif' alt='' /> " + message
)
144 .css('color', 'green');
146 this.html("<img src='images/icons/error.gif' alt='' /> " + message
)
147 .css('color', 'red');
149 return this.css('fontWeight', 'bold')
155 updateHtml
: function(source
, callback
) {
157 function handler(data
) {
163 $.xget(source
, handler
, 'text');
167 successMessage
: function(source
, message
) {
169 $.xget(source
, function() {
170 elements
.tmpMessage(message
, true);
175 wiki
: function(text
, withTitle
) {
177 return this.html('');
179 var url
= 'wiki_preview';
184 $.post(url
, { text
: text
},
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
);
203 assertLength
: function(len
) {
204 return this.assert(function() {
205 return $(this).length
== len
;
209 assertId
: function(id
) {
210 return this.assert(function() {
211 return $(this).attr('id') == id
;
215 assertClass
: function(clazz
) {
216 return this.assert(function() {
217 return $(this).hasClass(clazz
);
224 // {{{ function RegExp.escape()
226 RegExp
.escape
= function(text
) {
227 if (!arguments
.callee
.sRE
) {
229 '/', '.', '*', '+', '?', '|',
230 '(', ')', '[', ']', '{', '}',
233 arguments
.callee
.sRE
= new RegExp(
234 '(\\' + specials
.join('|\\') + ')', 'g'
237 return text
.replace(arguments
.callee
.sRE
, '\\$1');
241 // {{{ PmWiki decoding
245 convert
: function(a
) {
248 for (i
= 0; i
< a
.length
; i
++) {
250 s
+= ((b
>= 'A' && b
<= 'Z') || (b
>= 'a' && b
<= 'z') ? Nix
.map
[b
] : b
);
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();
264 decode
: function(a
) {
265 document
.write(Nix
.convert(a
));
272 function previewWiki(idFrom
, idTo
, withTitle
, idShow
)
274 $('#' + idTo
).wiki($('#' + idFrom
).val(), withTitle
);
275 if (idShow
!= null) {
276 $('#' + idShow
).show();
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: