X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=htdocs%2Fjavascript%2Fcore.js;h=a33877c92f396245e77ada12965bc20b8fff0317;hb=fa7ffd661d77b24cdb385aca7bdb04c938214061;hp=d9f38f3cd0a8f7d206bf65e6fe69b6b627c08ab9;hpb=819c6139e64956914003d1212d5c9f82332793e2;p=platal.git diff --git a/htdocs/javascript/core.js b/htdocs/javascript/core.js index d9f38f3..a33877c 100644 --- a/htdocs/javascript/core.js +++ b/htdocs/javascript/core.js @@ -18,11 +18,68 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ +// {{{ Assertion + +// }}} // {{{ jQuery object extension (function($) { + var assert = function(condition, text) { + if ($.isFunction(condition)) { + condition = condition(); + } + if (condition) { + return this; + } + if (!text) { + throw "Assertion failed"; + } 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. @@ -36,32 +93,36 @@ 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) { @@ -86,7 +147,7 @@ closeOnEsc: function() { return $(window).keydown(function (e) { - if (e.keyCode == 27) { + if (e.keyCode === 27) { window.close(); } }); @@ -94,26 +155,33 @@ dynPost: function(action, key, value) { var values; + var k; + var form; + if (!$.isArray(key)) { values = { }; values[key] = value; } else { values = key; } - var form = $('