// {{{ jQuery object extension
(function($) {
- function assert(condition, text) {
+ var assert = function(condition, text) {
if ($.isFunction(condition)) {
condition = condition();
}
} else {
throw "Assertion failed: " + text;
}
- }
+ };
+ var ajaxParams = function(onSuccess, onError, extraParameters) {
+ function errorHandler()
+ {
+ if (onError) {
+ return onError.apply(this, arguments);
+ } else {
+ alert("Une error s'est produite lors du traitement de la requête.\n"
+ + "Ta session a peut-être expiré");
+ }
+ }
+
+ return $.extend({
+ success: onSuccess,
+ error: errorHandler
+ }, extraParameters);
+ };
/* Add new functions to jQuery namesapce */
$.extend({
+ xapiVersion: '1',
+
+ plURL: (function() {
+ var base;
+ return function(url) {
+ if (url.startsWith('http', true)) {
+ return url;
+ }
+ if (typeof base === 'undefined') {
+ base = $('head base');
+ if (base.length > 0) {
+ base = base.attr('href');
+ if (!base.endsWith('/')) {
+ base += '/';
+ }
+ } else {
+ base = '';
+ }
+ }
+ return base + url;
+ };
+ }()),
+
/* The goal of the following functions is to provide an AJAX API that
* take a different callback in case of HTTP success code (2XX) and in
* other cases.
onSuccess = data;
data = null;
}
- if (onError != null && !$.isFunction(onError)) {
+ if (onError && !$.isFunction(onError)) {
type = type || onError;
onError = null;
}
- function ajaxHandler(data, textStatus, xhr) {
- if (textStatus == 'success') {
- if (onSuccess) {
- onSuccess(data, textStatus, xhr);
- }
- } else if (textStatus == 'error') {
- if (onError) {
- onError(data, textStatus, xhr);
- } else {
- alert("Une error s'est produite lors du traitement de la requête.\n"
- + "Ta session a peut-être expiré");
- }
- }
- }
- return $.ajax({
- url: source,
+ return $.ajax(ajaxParams(onSuccess, onError, {
+ url: $.plURL(source),
type: method,
- success: ajaxHandler,
data : data,
dataType: type
- });
+ }));
+ },
+
+ xapi: function(apicall, payload, onSuccess, onError) {
+ if ($.isFunction(payload)) {
+ onError = onSuccess;
+ onSuccess = payload;
+ }
+
+ if ($.xsrf_token) {
+ apicall += '?token=' + $.xsrf_token;
+ }
+
+ return $.ajax(ajaxParams(onSuccess, onError, {
+ url: $.plURL('api/' + $.xapiVersion + '/' + apicall),
+ type: 'POST',
+ data: JSON.stringify(payload),
+ dataType: 'json',
+ contentType: 'text/javascript'
+ }));
},
xget: function(source, data, onSuccess, onError, type) {
closeOnEsc: function() {
return $(window).keydown(function (e) {
- if (e.keyCode == 27) {
+ if (e.keyCode === 27) {
window.close();
}
});
dynPost: function(action, key, value) {
var values;
+ var k;
+ var form;
+
if (!$.isArray(key)) {
values = { };
values[key] = value;
} else {
values = key;
}
- var form = $('<form>', {
+ form = $('<form>', {
action: action,
method: 'post'
});
- for (var k in values) {
- $('<input>', {
- type: 'hidden',
- name: k,
- value: values[k]
- }).appendTo(form);
+ for (k in values) {
+ if (values.hasOwnProperty(k)) {
+ $('<input>', {
+ type: 'hidden',
+ name: k,
+ value: values[k]
+ }).appendTo(form);
+ }
}
- $('body').appendTo(form);
+ $('body').append(form);
form.submit();
},
},
wiki: function(text, withTitle) {
- if (text == '') {
+ if (!text) {
return this.html('');
}
var url = 'wiki_preview';
assertLength: function(len) {
return this.assert(function() {
- return $(this).length == len;
+ return $(this).length === len;
});
},
assertId: function(id) {
return this.assert(function() {
- return $(this).attr('id') == id;
+ return $(this).attr('id') === id;
});
},
});
}
});
-})(jQuery);
+}(jQuery));
// }}}
// {{{ function RegExp.escape()
RegExp.escape = function(text) {
- if (!arguments.callee.sRE) {
- var specials = [
- '/', '.', '*', '+', '?', '|',
- '(', ')', '[', ']', '{', '}',
- '\\', '^' , '$'
- ];
- arguments.callee.sRE = new RegExp(
- '(\\' + specials.join('|\\') + ')', 'g'
- );
- }
- return text.replace(arguments.callee.sRE, '\\$1');
-}
+ if (!arguments.callee.sRE) {
+ var specials = [
+ '/', '.', '*', '+', '?', '|',
+ '(', ')', '[', ']', '{', '}',
+ '\\', '^' , '$'
+ ];
+ arguments.callee.sRE = new RegExp(
+ '(\\' + specials.join('|\\') + ')', 'g'
+ );
+ }
+ return text.replace(arguments.callee.sRE, '\\$1');
+};
+
+// }}}
+// {{{ String extension
+
+String.prototype.startsWith = function(str, caseInsensitive) {
+ var cmp = this;
+
+ if (str.length > this.length) {
+ return false;
+ }
+ if (caseInsensitive) {
+ str = str.toLowerCase();
+ cmp = cmp.toLowerCase();
+ }
+ return cmp.substr(0, str.length) === str;
+};
+
+String.prototype.endsWith = function(str, caseInsensitive) {
+ var cmp = this;
+
+ if (str.length > this.length) {
+ return false;
+ }
+ if (caseInsensitive) {
+ str = str.toLowerCase();
+ cmp = cmp.toLowerCase();
+ }
+ return cmp.substr(cmp.length - str.length, str.length) === str;
+};
+
+String.prototype.htmlEntities = function() {
+ return this.replace(/&/g,'&')
+ .replace(new RegExp('<','g'),'<')
+ .replace(/>/g,'>');
+};
+
+String.prototype.contains = function(str, caseInsensitive) {
+ var cmp = this;
+ if (str.length > this.length) {
+ return false;
+ }
+ if (caseInsensitive) {
+ str = str.toLowerCase();
+ cmp = cmp.toLowerCase();
+ }
+ return cmp.indexOf(str) >= 0;
+};
// }}}
// {{{ PmWiki decoding
Nix = {
- map: null,
- convert: function(a) {
- Nix.init();
- var s = '';
- for (i = 0; i < a.length ; i++) {
- var b = a.charAt(i);
- s += ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') ? Nix.map[b] : b);
- }
- return s;
- },
- init: function() {
- if (Nix.map != null)
- return;
- var map = new Array();
- var s='abcdefghijklmnopqrstuvwxyz';
- for (i = 0; i < s.length; i++)
- map[s.charAt(i)] = s.charAt((i+13)%26);
- for (i=0; i<s.length; i++)map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
- Nix.map = map;
- },
- decode: function(a) {
- document.write(Nix.convert(a));
- }
-}
+ map: null,
+
+ convert: function(a) {
+ var s = '';
+ Nix.init();
+ for (i = 0; i < a.length ; i++) {
+ var b = a.charAt(i);
+ s += ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') ? Nix.map[b] : b);
+ }
+ return s;
+ },
+
+ init: function() {
+ var map;
+ var s;
+
+ if (Nix.map !== null) {
+ return;
+ }
+ map = [ ];
+ s = 'abcdefghijklmnopqrstuvwxyz';
+ for (i = 0; i < s.length; i++) {
+ map[s.charAt(i)] = s.charAt((i+13) % 26);
+ }
+ for (i=0; i< s.length; i++) {
+ map[s.charAt(i).toUpperCase()] = s.charAt((i+13) % 26).toUpperCase();
+ }
+ Nix.map = map;
+ },
+
+ decode: function(a) {
+ document.write(Nix.convert(a));
+ }
+};
// }}}
// {{{ preview wiki
function previewWiki(idFrom, idTo, withTitle, idShow)
{
$('#' + idTo).wiki($('#' + idFrom).val(), withTitle);
- if (idShow != null) {
+ if (idShow) {
$('#' + idShow).show();
}
}
// }}}
+// {{{ register error report
+
+$(function() {
+ $(window).error(function(error) {
+ if ($.xsrf_token) {
+ $.ajax({
+ url: $.plURL('site_errors/register'),
+ type: 'POST',
+ data: {
+ url: "" + window.location,
+ token: $.xsrf_token,
+ error: JSON.stringify({
+ message: error.originalEvent.message,
+ file: error.originalEvent.filename,
+ line: error.originalEvent.lineno
+ })
+ }
+ });
+ }
+ });
+});
+
+// }}}
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: