src/pyams_skin/resources/js/myams.js
changeset 179 d381c4c035cd
parent 174 1e8bbcc06a2f
child 184 c9495342d92c
--- a/src/pyams_skin/resources/js/myams.js	Tue Apr 18 11:35:26 2017 +0200
+++ b/src/pyams_skin/resources/js/myams.js	Tue Apr 18 11:38:32 2017 +0200
@@ -2,7 +2,7 @@
  * MyAMS
  * « My Application Management Skin »
  *
- * $Tag$ (rev. 17)
+ * $Tag$ (rev. 23)
  * A bootstrap based application/administration skin
  *
  * Custom administration and application skin tools
@@ -548,7 +548,7 @@
 		if (!element.exists()) {
 			return false;
 		}
-		return globals.document.contains(element[0]);
+		return globals.document.body.contains(element[0]);
 	};
 
 	/**
@@ -759,12 +759,30 @@
 				async: typeof(callback) === 'function'
 			};
 			var settings = $.extend({}, defaults, options);
-			if (checker === undefined) {
-				ams.getScript(source, function() {
-					if (typeof(callback) === 'function') {
-						callback(true, options);
+			if (checker instanceof Array) {
+				var deferred = [];
+				for (var index = 0; index < checker.length; index++) {
+					if (checker[index] === undefined) {
+						deferred.push(ams.getScript(source[index], {async: true}));
 					}
-				}, settings);
+				}
+				if (deferred.length > 0) {
+					$.when.apply($, deferred).then(function () {
+						if (typeof(callback) === 'function') {
+							callback(true, options);
+						}
+					});
+				} else if (typeof(callback) === 'function') {
+					callback(false, options);
+				}
+			} else if (checker === undefined) {
+				if (typeof(source) === 'string') {
+					ams.getScript(source, function () {
+						if (typeof(callback) === 'function') {
+							callback(true, options);
+						}
+					}, settings);
+				}
 			} else {
 				if (typeof(callback) === 'function') {
 					callback(false, options);
@@ -1425,6 +1443,7 @@
 			// Check submit button
 			var button = $(form.data('ams-submit-button'));
 			if (button && !button.data('ams-form-hide-loading')) {
+				button.data('ams-progress-content', button.html());
 				button.button('loading');
 			}
 			ams.ajax.check($.fn.ajaxSubmit,
@@ -1452,9 +1471,11 @@
 										var interval;
 
 										function _clearProgressStatus() {
-											button.button('reset');
 											clearInterval(interval);
+											ams.form.resetAfterSubmit(form, button);
+											button.html(button.data('ams-progress-content'));
 											ams.executeFunctionByName(progressEndCallback, form, button);
+											ams.form.resetChanged(form);
 										}
 
 										function _getProgressStatus() {
@@ -1467,8 +1488,13 @@
 																	if (result.message) {
 																		button.text(result.message);
 																	} else {
-																		button.text(button.data('ams-progress-text') +
-																					' ' + result.current + ' / ' + result.length);
+																		var text = button.data('ams-progress-text') || ams.i18n.PROGRESS;
+																		if (result.current) {
+																			text += ': ' + result.current + '/ ' + (result.length || 100);
+																		} else {
+																			text += '...';
+																		}
+																		button.text(text);
 																	}
 																} else if (result.status === 'finished') {
 																	_clearProgressStatus();
@@ -1674,10 +1700,12 @@
 										if (modal.exists() && (keepModal !== true)) {
 											ams.dialog.close(form);
 										} else {
-											setTimeout(function() {
-												ams.form.resetAfterSubmit(form, button);
-												ams.form.resetChanged(form);
-											}, button.data('ams-form-reset-timeout') || 2000);
+											if (!progressHandler) {
+												setTimeout(function () {
+													ams.form.resetAfterSubmit(form, button);
+													ams.form.resetChanged(form);
+												}, button.data('ams-form-reset-timeout') || 2000);
+											}
 										}
 									}
 								}
@@ -2560,6 +2588,9 @@
 			}
 
 			for (name in plugins) {
+				if (!plugins.hasOwnProperty(name)) {
+					continue;
+				}
 				plugin = plugins[name];
 				if (ams.plugins.enabled[name] === undefined) {
 					ams.getScript(plugin.src, _loadPlugin, {
@@ -2616,7 +2647,11 @@
 				if (data) {
 					for (var name in data) {
 						if (data.hasOwnProperty(name)) {
-							dataElement.attr('data-' + name, data[name]);
+							var elementData = data[name];
+							if (typeof(elementData) !== 'string') {
+								elementData = JSON.stringify(elementData);
+							}
+							dataElement.attr('data-' + name, elementData);
 						}
 					}
 				}
@@ -4246,6 +4281,8 @@
 								} else {
 									tr.remove();
 								}
+							} else {
+								MyAMS.ajax.handleJSON(result);
 							}
 						});
 					}
@@ -4532,12 +4569,13 @@
 				if (menu.exists()) {
 					updateActiveMenus(menu);
 				}
-				ams.skin.loadURL(url, container);
-				var prefix = $('html head title').data('ams-title-prefix');
-				document.title = (prefix ? prefix + ' > ' : '') +
-								 ($('[data-ams-page-title]:first', container).data('ams-page-title') ||
-								  menu.attr('title') ||
-								  document.title);
+				ams.skin.loadURL(url, container, {afterLoadCallback: function() {
+					var prefix = $('html head title').data('ams-title-prefix');
+					document.title = (prefix ? prefix + ' > ' : '') +
+						($('[data-ams-page-title]:first', container).data('ams-page-title') ||
+						menu.attr('title') ||
+						document.title);
+				}});
 			} else {
 				var activeUrl = $('[data-ams-active-menu]').data('ams-active-menu');
 				if (activeUrl) {
@@ -4604,6 +4642,8 @@
 			if (typeof(options) === 'function') {
 				callback = options;
 				options = {};
+			} else if (options === undefined) {
+				options = {};
 			}
 			container = $(container);
 			var defaults = {
@@ -4613,7 +4653,7 @@
 				cache: false,
 				beforeSend: function() {
 					ams.skin.cleanContainer(container);
-					container.html('<h1 class="loading"><i class="fa fa-cog fa-spin"></i> Loading... </h1>');
+					container.html('<h1 class="loading"><i class="fa fa-cog fa-spin"></i> ' + ams.i18n.LOADING + ' </h1>');
 					if (container[0] === $('#content')[0]) {
 						ams.skin._drawBreadCrumb();
 						var prefix = $('html head title').data('ams-title-prefix');
@@ -4666,7 +4706,7 @@
 								   ams.i18n.ERROR + error + '</h3>' +
 								   request.responseText);
 				},
-				async: false
+				async: options.async === undefined ? true : options.async
 			};
 			var settings = $.extend({}, defaults, options);
 			$.ajax(settings);
@@ -5151,10 +5191,11 @@
 				}
 				try {
 					link.append('<i class="fa fa-spin fa-cog margin-left-5"></i>');
-					ams.skin.loadURL(data.amsUrl, link.attr('href'));
-					if (data.amsTabLoadOnce) {
-						link.data('ams-tab-loaded', true);
-					}
+					ams.skin.loadURL(data.amsUrl, link.attr('href'), {afterLoadCallback: function() {
+						if (data.amsTabLoadOnce) {
+							link.data('ams-tab-loaded', true);
+						}
+					}});
 				}
 				finally {
 					$('i', link).remove();
@@ -5234,6 +5275,9 @@
 		WARNING: "!! WARNING !!",
 		ERROR: "ERROR: ",
 
+		LOADING: "Loading...",
+		PROGRESS: "Processing",
+
 		WAIT: "Please wait!",
 		FORM_SUBMITTED: "This form was already submitted...",
 		NO_SERVER_RESPONSE: "No response from server!",