# HG changeset patch # User Thierry Florac # Date 1545327636 -3600 # Node ID f08f5a2ffaa6bf73e28b309b776508a7ce21f1b9 # Parent 24e2893a96308733925e799a7b6cc2cefe00c349 Allow download of several files in a single AJAX check diff -r 24e2893a9630 -r f08f5a2ffaa6 src/pyams_skin/resources/js/myams-ajax.js --- a/src/pyams_skin/resources/js/myams-ajax.js Thu Dec 20 14:15:59 2018 +0100 +++ b/src/pyams_skin/resources/js/myams-ajax.js Thu Dec 20 18:40:36 2018 +0100 @@ -43,10 +43,11 @@ var defaults = { async: typeof(callback) === 'function' }; - var settings = $.extend({}, defaults, options); + var settings = $.extend({}, defaults, options), + deferred = [], + index; if (checker instanceof Array) { - var deferred = []; - for (var index = 0; index < checker.length; index++) { + for (index = 0; index < checker.length; index++) { if (checker[index] === undefined) { deferred.push(ams.getScript(source[index], {async: true})); } @@ -59,7 +60,18 @@ callCallbacks(false, options); } } else if (checker === undefined) { - if (typeof(source) === 'string') { + if (source instanceof Array) { + for (index = 0; index < source.length; index++) { + deferred.push(ams.getScript(source[index], {async: true})); + } + if (deferred.length > 0) { + $.when.apply($, deferred).then(function () { + callCallbacks(true, options); + }); + } else { + callCallbacks(false, options); + } + } else if (typeof(source) === 'string') { ams.getScript(source, function () { callCallbacks(true, options); }, settings); diff -r 24e2893a9630 -r f08f5a2ffaa6 src/pyams_skin/resources/js/myams-ajax.min.js --- a/src/pyams_skin/resources/js/myams-ajax.min.js Thu Dec 20 14:15:59 2018 +0100 +++ b/src/pyams_skin/resources/js/myams-ajax.min.js Thu Dec 20 18:40:36 2018 +0100 @@ -1,1 +1,1 @@ -!function(p,g){var h=g.MyAMS;h.ajax={check:function(e,t,a,o){function n(e,t){if(void 0!==a){a instanceof Array||(a=[a]);for(var o=0;o=e.total||console&&console.log&&console.log(parseInt(e.loaded/e.total*100,10)+"%"))},getJSON:function(){return function(e){var t=e.url;delete e.url,h.ajax.post(t,e,function(e,t,o){h.ajax.handleJSON(e)})}},post:function(e,t,o,n){var a,s;a=e.startsWith(window.location.protocol)?e:this.getAddr()+e,"function"==typeof o?(n=o,o={}):o||(o={}),void 0===n&&(n=o.callback),"string"==typeof n&&(n=h.getFunctionByName(n)),delete o.callback;var i={url:a,type:"post",cache:!1,async:"function"==typeof n,data:p.param(t),dataType:"json",beforeSend:function(e,t){if(void 0!==g.Cookies){var o=Cookies.get(h.csrfCookieName);o&&e.setRequestHeader(h.csrfHeaderName,o)}},success:n||function(e){s=e.result}},r=p.extend({},i,o);return p.ajax(r),s},getResponse:function(t){var o,n,e=t.getResponseHeader("content-type");if(e)if(e.startsWith("application/javascript"))o="script",n=t.responseText;else if(e.startsWith("text/html"))o="html",n=t.responseText;else if(e.startsWith("text/xml"))o="xml",n=t.responseText;else if(n=t.responseJSON)o="json";else try{n=JSON.parse(t.responseText),o="json"}catch(e){n=t.responseText,o="text"}else o="json",n={status:"alert",alert:{title:h.i18n.ERROR_OCCURED,content:h.i18n.NO_SERVER_RESPONSE}};return{contentType:o,data:n}},handleJSON:function(e,t,o){var n,a,s,i,r,l,c,f=e.status;switch(f){case"alert":g.alert&&g.alert(e.alert.title+"\n\n"+e.alert.content);break;case"error":h.form&&h.form.showErrors(t,e);break;case"info":case"success":void 0!==t&&(h.form&&h.form.resetChanged(t),!1!==e.close_form&&h.dialog&&h.dialog.close(t));break;case"message":case"messagebox":break;case"notify":case"callback":case"callbacks":void 0!==t&&(h.form&&h.form.resetChanged(t),!1!==e.close_form&&h.dialog&&h.dialog.close(t));break;case"modal":h.dialog&&h.dialog.open(e.location);break;case"reload":void 0!==t&&(h.form&&h.form.resetChanged(t),!1!==e.close_form&&h.dialog&&h.dialog.close(t)),(n=e.location||window.location.hash).startsWith("#")&&(n=n.substr(1));var d=p(e.target||o||"#content");h.skin&&h.skin.loadURL(n,d,{preLoadCallback:h.getFunctionByName(e.pre_reload)||function(){p("[data-ams-pre-reload]",d).each(function(){h.executeFunctionByName(p(this).data("ams-pre-reload"))})},preLoadCallbackOptions:e.pre_reload_options,afterLoadCallback:h.getFunctionByName(e.post_reload)||function(){p("[data-ams-post-reload]",d).each(function(){h.executeFunctionByName(p(this).data("ams-post-reload"))})},afterLoadCallbackOptions:e.post_reload_options});break;case"redirect":void 0!==t&&(h.form&&h.form.resetChanged(t),!0===e.close_form&&h.dialog&&h.dialog.close(t)),(n=e.location||window.location.href).endsWith("##")&&(n=n.replace(/##/,window.location.hash)),e.window?window.open(n,e.window,e.options):window.location.href===n?window.location.reload(!0):window.location.href=n;break;default:console&&console.log&&console.log("Unhandled status: "+f)}if(e.content&&(s=e.content,i=p(s.target||o||t||"#content"),!0===s.raw?i.text(s.text):(i.html(s.html),h.initContent&&h.initContent(i)),s.keep_hidden||i.removeClass("hidden")),e.contents){var u=e.contents;for(a=0;a0?e.when.apply(e,f).then(function(){i(!0,s)}):i(!1,s)}else if(void 0===t)if(n instanceof Array){for(r=0;r0?e.when.apply(e,f).then(function(){i(!0,s)}):i(!1,s)}else"string"==typeof n&&o.getScript(n,function(){i(!0,s)},c);else i(!1,s)},getAddr:function(t){var o=t||e("HTML HEAD BASE").attr("href")||window.location.href;return o.substr(0,o.lastIndexOf("/")+1)},start:function(){e("#ajax-gear").show()},stop:function(){e("#ajax-gear").hide()},progress:function(e){e.lengthComputable&&(e.loaded>=e.total||console&&console.log&&console.log(parseInt(e.loaded/e.total*100,10)+"%"))},getJSON:function(){return function(e){var t=e.url;delete e.url,o.ajax.post(t,e,function(e,t,n){o.ajax.handleJSON(e)})}},post:function(n,a,s,i){var r;r=n.startsWith(window.location.protocol)?n:this.getAddr()+n,"function"==typeof s?(i=s,s={}):s||(s={}),void 0===i&&(i=s.callback),"string"==typeof i&&(i=o.getFunctionByName(i)),delete s.callback;var l,c={url:r,type:"post",cache:!1,async:"function"==typeof i,data:e.param(a),dataType:"json",beforeSend:function(e,n){if(void 0!==t.Cookies){var a=Cookies.get(o.csrfCookieName);a&&e.setRequestHeader(o.csrfHeaderName,a)}},success:i||function(e){l=e.result}},f=e.extend({},c,s);return e.ajax(f),l},getResponse:function(e){var t,n,a=e.getResponseHeader("content-type");if(a)if(a.startsWith("application/javascript"))t="script",n=e.responseText;else if(a.startsWith("text/html"))t="html",n=e.responseText;else if(a.startsWith("text/xml"))t="xml",n=e.responseText;else if(n=e.responseJSON)t="json";else try{n=JSON.parse(e.responseText),t="json"}catch(o){n=e.responseText,t="text"}else t="json",n={status:"alert",alert:{title:o.i18n.ERROR_OCCURED,content:o.i18n.NO_SERVER_RESPONSE}};return{contentType:t,data:n}},handleJSON:function(n,a,s){var i,r=n.status;switch(r){case"alert":t.alert&&t.alert(n.alert.title+"\n\n"+n.alert.content);break;case"error":o.form&&o.form.showErrors(a,n);break;case"info":case"success":void 0!==a&&(o.form&&o.form.resetChanged(a),!1!==n.close_form&&o.dialog&&o.dialog.close(a));break;case"message":case"messagebox":break;case"notify":case"callback":case"callbacks":void 0!==a&&(o.form&&o.form.resetChanged(a),!1!==n.close_form&&o.dialog&&o.dialog.close(a));break;case"modal":o.dialog&&o.dialog.open(n.location);break;case"reload":void 0!==a&&(o.form&&o.form.resetChanged(a),!1!==n.close_form&&o.dialog&&o.dialog.close(a)),(i=n.location||window.location.hash).startsWith("#")&&(i=i.substr(1));var l=e(n.target||s||"#content");o.skin&&o.skin.loadURL(i,l,{preLoadCallback:o.getFunctionByName(n.pre_reload)||function(){e("[data-ams-pre-reload]",l).each(function(){o.executeFunctionByName(e(this).data("ams-pre-reload"))})},preLoadCallbackOptions:n.pre_reload_options,afterLoadCallback:o.getFunctionByName(n.post_reload)||function(){e("[data-ams-post-reload]",l).each(function(){o.executeFunctionByName(e(this).data("ams-post-reload"))})},afterLoadCallbackOptions:n.post_reload_options});break;case"redirect":void 0!==a&&(o.form&&o.form.resetChanged(a),!0===n.close_form&&o.dialog&&o.dialog.close(a)),(i=n.location||window.location.href).endsWith("##")&&(i=i.replace(/##/,window.location.hash)),n.window?window.open(i,n.window,n.options):window.location.href===i?window.location.reload(!0):window.location.href=i;break;default:console&&console.log&&console.log("Unhandled status: "+r)}var c,f,d;if(n.content&&(f=n.content,d=e(f.target||s||a||"#content"),!0===f.raw?d.text(f.text):(d.html(f.html),o.initContent&&o.initContent(d)),f.keep_hidden||d.removeClass("hidden")),n.contents){var u=n.contents;for(c=0;c