Allow download of several files in a single AJAX check
authorThierry Florac <thierry.florac@onf.fr>
Thu, 20 Dec 2018 18:40:36 +0100
changeset 500 f08f5a2ffaa6
parent 499 24e2893a9630
child 501 2f0eb9a77324
Allow download of several files in a single AJAX check
src/pyams_skin/resources/js/myams-ajax.js
src/pyams_skin/resources/js/myams-ajax.min.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);
--- 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<a.length;o++){var n=h.getFunctionByName(a[o]);"function"==typeof n&&n(e,t)}}}a instanceof Array||"object"==typeof a&&(o=a,a=void 0);var s={async:"function"==typeof a},i=p.extend({},s,o);if(e instanceof Array){for(var r=[],l=0;l<e.length;l++)void 0===e[l]&&r.push(h.getScript(t[l],{async:!0}));0<r.length?p.when.apply(p,r).then(function(){n(!0,o)}):n(!1,o)}else void 0===e?"string"==typeof t&&h.getScript(t,function(){n(!0,o)},i):n(!1,o)},getAddr:function(e){var t=e||p("HTML HEAD BASE").attr("href")||window.location.href;return t.substr(0,t.lastIndexOf("/")+1)},start:function(){p("#ajax-gear").show()},stop:function(){p("#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,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;a<u.length;a++)s=u[a],i=p(s.target),!0===s.raw?i.text(s.text):(i.html(s.html),h.initContent&&h.initContent(i)),s.keep_hidden||i.removeClass("hidden")}if(e.message&&("string"==typeof(r=e.message)?"info"===f||"success"===f?h.skin&&h.skin.smallBox(f,{title:r,icon:"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:3e3}):h.skin&&h.skin.alert(p(t||"#content"),f,r):h.skin&&h.skin.alert(p(r.target||o||t||"#content"),r.status||"success",r.header,r.body,r.subtitle)),e.smallbox&&("string"==typeof(r=e.smallbox)?h.skin&&h.skin.smallBox(e.smallbox_status||f,{title:e.smallbox,icon:e.smallbox_icon||"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:e.smallbox_timeout||3e3}):h.skin&&h.skin.smallBox(r.status||f,{title:r.message,icon:r.icon||"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:r.timeout||3e3})),e.messagebox)if("string"==typeof(r=e.messagebox))h.skin&&h.skin.messageBox("info",{title:h.i18n.ERROR_OCCURED,content:r,timeout:1e4});else{var m=r.status||"info";"error"===m&&t&&o&&h.executeFunctionByName(t.data("ams-form-submit-error")||"MyAMS.form.finalizeSubmitOnError",t,o),h.skin&&h.skin.messageBox(m,{title:r.title||h.i18n.ERROR_OCCURED,content:r.content,icon:r.icon,number:r.number,timeout:null===r.timeout?void 0:r.timeout||1e4})}if(e.event&&t.trigger(e.event,e.event_options),e.events)for(void 0===t&&(t=p(document)),a=0;a<e.events.length;a++)null!==(l=e.events[a])&&("string"==typeof l?t.trigger(l,e.events_options):t.trigger(l.event,l.options));if(e.callback&&h.executeFunctionByName(e.callback,t,e.options),e.callbacks)for(a=0;a<e.callbacks.length;a++)"function"==typeof(c=e.callbacks[a])?h.executeFunctionByName(c,t,c.options):h.executeFunctionByName(c.callback,t,c.options)}}}(jQuery,this);
+!function(e,t){var o=t.MyAMS;o.ajax={check:function(t,n,a,s){function i(e,t){if(void 0!==a){a instanceof Array||(a=[a]);for(var n=0;n<a.length;n++){var s=o.getFunctionByName(a[n]);"function"==typeof s&&s(e,t)}}}a instanceof Array||"object"==typeof a&&(s=a,a=void 0);var r,l={async:"function"==typeof a},c=e.extend({},l,s),f=[];if(t instanceof Array){for(r=0;r<t.length;r++)void 0===t[r]&&f.push(o.getScript(n[r],{async:!0}));f.length>0?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;r<n.length;r++)f.push(o.getScript(n[r],{async:!0}));f.length>0?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<u.length;c++)f=u[c],d=e(f.target),!0===f.raw?d.text(f.text):(d.html(f.html),o.initContent&&o.initContent(d)),f.keep_hidden||d.removeClass("hidden")}var m;if(n.message&&("string"==typeof(m=n.message)?"info"===r||"success"===r?o.skin&&o.skin.smallBox(r,{title:m,icon:"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:3e3}):o.skin&&o.skin.alert(e(a||"#content"),r,m):o.skin&&o.skin.alert(e(m.target||s||a||"#content"),m.status||"success",m.header,m.body,m.subtitle)),n.smallbox&&("string"==typeof(m=n.smallbox)?o.skin&&o.skin.smallBox(n.smallbox_status||r,{title:n.smallbox,icon:n.smallbox_icon||"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:n.smallbox_timeout||3e3}):o.skin&&o.skin.smallBox(m.status||r,{title:m.message,icon:m.icon||"fa fa-fw fa-info-circle font-xs align-top margin-top-10",timeout:m.timeout||3e3})),n.messagebox)if("string"==typeof(m=n.messagebox))o.skin&&o.skin.messageBox("info",{title:o.i18n.ERROR_OCCURED,content:m,timeout:1e4});else{var p=m.status||"info";"error"===p&&a&&s&&o.executeFunctionByName(a.data("ams-form-submit-error")||"MyAMS.form.finalizeSubmitOnError",a,s),o.skin&&o.skin.messageBox(p,{title:m.title||o.i18n.ERROR_OCCURED,content:m.content,icon:m.icon,number:m.number,timeout:null===m.timeout?void 0:m.timeout||1e4})}if(n.event&&a.trigger(n.event,n.event_options),n.events){var g;for(void 0===a&&(a=e(document)),c=0;c<n.events.length;c++)null!==(g=n.events[c])&&("string"==typeof g?a.trigger(g,n.events_options):a.trigger(g.event,g.options))}if(n.callback&&o.executeFunctionByName(n.callback,a,n.options),n.callbacks){var h;for(c=0;c<n.callbacks.length;c++)"function"==typeof(h=n.callbacks[c])?o.executeFunctionByName(h,a,h.options):o.executeFunctionByName(h.callback,a,h.options)}}}}(jQuery,this);