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
) {
42 function errorHandler()
45 return onError
.apply(this, arguments
);
47 alert("Une error s'est produite lors du traitement de la requête.\n"
48 + "Ta session a peut-être expiré");
58 /* Add new functions to jQuery namesapce */
64 return function(url
) {
65 if (url
.startsWith('http', true)) {
68 if (typeof base
=== 'undefined') {
69 base
= $('head base');
70 if (base
.length
> 0) {
71 base
= base
.attr('href');
72 if (!base
.endsWith('/')) {
83 /* The goal of the following functions is to provide an AJAX API that
84 * take a different callback in case of HTTP success code (2XX) and in
88 xajax
: function(source
, method
, data
, onSuccess
, onError
, type
) {
90 if ($.isFunction(data
)) {
91 type
= type
|| onError
;
96 if (onError
&& !$.isFunction(onError
)) {
97 type
= type
|| onError
;
101 return $.ajax(ajaxParams(onSuccess
, onError
, {
102 url
: $.plURL(source
),
109 xapi
: function(apicall
, payload
, onSuccess
, onError
) {
110 if ($.isFunction(payload
)) {
116 apicall
+= '?token=' + $.xsrf_token
;
119 return $.ajax(ajaxParams(onSuccess
, onError
, {
120 url
: $.plURL('api/' + $.xapiVersion + '/' + apicall
),
122 data
: JSON
.stringify(payload
),
124 contentType
: 'text/javascript'
128 xget
: function(source
, data
, onSuccess
, onError
, type
) {
129 return $.xajax(source
, 'GET', data
, onSuccess
, onError
, type
);
132 xgetJSON
: function(source
, data
, onSuccess
, onError
) {
133 return $.xget(source
, data
, onSuccess
, onError
, 'json');
136 xgetScript
: function(source
, onSuccess
, onError
) {
137 return $.xget(source
, null, onSuccess
, onError
, 'script');
140 xgetText
: function(source
, data
, onSuccess
, onError
) {
141 return $.xget(source
, data
, onSuccess
, onError
, 'text');
144 xpost
: function(source
, data
, onSuccess
, onError
, type
) {
145 return $.xajax(source
, 'POST', data
, onSuccess
, onError
, type
);
148 closeOnEsc
: function() {
149 return $(window
).keydown(function (e
) {
150 if (e
.keyCode
=== 27) {
156 dynPost
: function(action
, key
, value
) {
161 if (!$.isArray(key
)) {
172 if (values
.hasOwnProperty(k
)) {
180 $('body').append(form
);
187 /* Add new functions to jQuery objects */
189 tmpMessage
: function(message
, success
) {
191 this.html("<img src='images/icons/wand.gif' alt='' /> " + message
)
192 .css('color', 'green');
194 this.html("<img src='images/icons/error.gif' alt='' /> " + message
)
195 .css('color', 'red');
197 return this.css('fontWeight', 'bold')
203 updateHtml
: function(source
, callback
) {
205 function handler(data
) {
211 $.xget(source
, handler
, 'text');
215 successMessage
: function(source
, message
) {
217 $.xget(source
, function() {
218 elements
.tmpMessage(message
, true);
223 wiki
: function(text
, withTitle
) {
225 return this.html('');
227 var url
= 'wiki_preview';
232 $.post(url
, { text
: text
},
239 popWin
: function(w
, h
) {
240 return this.click(function() {
241 window
.open(this.href
, '_blank',
242 'toolbar=0,location=0,directories=0,status=0,'
243 +'menubar=0,scrollbars=1,resizable=1,'
244 +'width='+w
+',height='+h
);
251 assertLength
: function(len
) {
252 return this.assert(function() {
253 return $(this).length
=== len
;
257 assertId
: function(id
) {
258 return this.assert(function() {
259 return $(this).attr('id') === id
;
263 assertClass
: function(clazz
) {
264 return this.assert(function() {
265 return $(this).hasClass(clazz
);
272 // {{{ function RegExp.escape()
274 RegExp
.escape
= function(text
) {
275 if (!arguments
.callee
.sRE
) {
277 '/', '.', '*', '+', '?', '|',
278 '(', ')', '[', ']', '{', '}',
281 arguments
.callee
.sRE
= new RegExp(
282 '(\\' + specials
.join('|\\') + ')', 'g'
285 return text
.replace(arguments
.callee
.sRE
, '\\$1');
289 // {{{ String extension
291 String
.prototype.startsWith
= function(str
, caseInsensitive
) {
294 if (str
.length
> this.length
) {
297 if (caseInsensitive
) {
298 str
= str
.toLowerCase();
299 cmp
= cmp
.toLowerCase();
301 return cmp
.substr(0, str
.length
) === str
;
304 String
.prototype.endsWith
= function(str
, caseInsensitive
) {
307 if (str
.length
> this.length
) {
310 if (caseInsensitive
) {
311 str
= str
.toLowerCase();
312 cmp
= cmp
.toLowerCase();
314 return cmp
.substr(cmp
.length
- str
.length
, str
.length
) === str
;
317 String
.prototype.htmlEntities
= function() {
318 return this.replace(/&/g,'&')
319 .replace(new RegExp('<','g'),'<')
320 .replace(/>/g,'>');
323 String
.prototype.contains
= function(str
, caseInsensitive
) {
325 if (str
.length
> this.length
) {
328 if (caseInsensitive
) {
329 str
= str
.toLowerCase();
330 cmp
= cmp
.toLowerCase();
332 return cmp
.indexOf(str
) >= 0;
336 // {{{ PmWiki decoding
341 convert
: function(a
) {
344 for (i
= 0; i
< a
.length
; i
++) {
346 s
+= ((b
>= 'A' && b
<= 'Z') || (b
>= 'a' && b
<= 'z') ? Nix
.map
[b
] : b
);
355 if (Nix
.map
!== null) {
359 s
= 'abcdefghijklmnopqrstuvwxyz';
360 for (i
= 0; i
< s
.length
; i
++) {
361 map
[s
.charAt(i
)] = s
.charAt((i
+13) % 26);
363 for (i
=0; i
< s
.length
; i
++) {
364 map
[s
.charAt(i
).toUpperCase()] = s
.charAt((i
+13) % 26).toUpperCase();
369 decode
: function(a
) {
370 document
.write(Nix
.convert(a
));
377 function previewWiki(idFrom
, idTo
, withTitle
, idShow
)
379 $('#' + idTo
).wiki($('#' + idFrom
).val(), withTitle
);
381 $('#' + idShow
).show();
387 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: