src/pyams_skin/resources/js/myams.js
changeset 179 d381c4c035cd
parent 174 1e8bbcc06a2f
child 184 c9495342d92c
equal deleted inserted replaced
178:7d45e7def548 179:d381c4c035cd
     1 /*
     1 /*
     2  * MyAMS
     2  * MyAMS
     3  * « My Application Management Skin »
     3  * « My Application Management Skin »
     4  *
     4  *
     5  * $Tag$ (rev. 17)
     5  * $Tag$ (rev. 23)
     6  * A bootstrap based application/administration skin
     6  * A bootstrap based application/administration skin
     7  *
     7  *
     8  * Custom administration and application skin tools
     8  * Custom administration and application skin tools
     9  * Released under Zope Public License ZPL 1.1
     9  * Released under Zope Public License ZPL 1.1
    10  * ©2014-2016 Thierry Florac <tflorac@ulthar.net>
    10  * ©2014-2016 Thierry Florac <tflorac@ulthar.net>
   546 	MyAMS.isInDOM = function(element) {
   546 	MyAMS.isInDOM = function(element) {
   547 		element = $(element);
   547 		element = $(element);
   548 		if (!element.exists()) {
   548 		if (!element.exists()) {
   549 			return false;
   549 			return false;
   550 		}
   550 		}
   551 		return globals.document.contains(element[0]);
   551 		return globals.document.body.contains(element[0]);
   552 	};
   552 	};
   553 
   553 
   554 	/**
   554 	/**
   555 	 * Get script or CSS file using browser cache
   555 	 * Get script or CSS file using browser cache
   556 	 * Script or CSS URLs can include variable names, given between braces, as in
   556 	 * Script or CSS URLs can include variable names, given between braces, as in
   757 			}
   757 			}
   758 			var defaults = {
   758 			var defaults = {
   759 				async: typeof(callback) === 'function'
   759 				async: typeof(callback) === 'function'
   760 			};
   760 			};
   761 			var settings = $.extend({}, defaults, options);
   761 			var settings = $.extend({}, defaults, options);
   762 			if (checker === undefined) {
   762 			if (checker instanceof Array) {
   763 				ams.getScript(source, function() {
   763 				var deferred = [];
   764 					if (typeof(callback) === 'function') {
   764 				for (var index = 0; index < checker.length; index++) {
   765 						callback(true, options);
   765 					if (checker[index] === undefined) {
       
   766 						deferred.push(ams.getScript(source[index], {async: true}));
   766 					}
   767 					}
   767 				}, settings);
   768 				}
       
   769 				if (deferred.length > 0) {
       
   770 					$.when.apply($, deferred).then(function () {
       
   771 						if (typeof(callback) === 'function') {
       
   772 							callback(true, options);
       
   773 						}
       
   774 					});
       
   775 				} else if (typeof(callback) === 'function') {
       
   776 					callback(false, options);
       
   777 				}
       
   778 			} else if (checker === undefined) {
       
   779 				if (typeof(source) === 'string') {
       
   780 					ams.getScript(source, function () {
       
   781 						if (typeof(callback) === 'function') {
       
   782 							callback(true, options);
       
   783 						}
       
   784 					}, settings);
       
   785 				}
   768 			} else {
   786 			} else {
   769 				if (typeof(callback) === 'function') {
   787 				if (typeof(callback) === 'function') {
   770 					callback(false, options);
   788 					callback(false, options);
   771 				}
   789 				}
   772 			}
   790 			}
  1423 			$('.alert-danger, SPAN.state-error', form).not('.persistent').remove();
  1441 			$('.alert-danger, SPAN.state-error', form).not('.persistent').remove();
  1424 			$('.state-error', form).removeClassPrefix('state-');
  1442 			$('.state-error', form).removeClassPrefix('state-');
  1425 			// Check submit button
  1443 			// Check submit button
  1426 			var button = $(form.data('ams-submit-button'));
  1444 			var button = $(form.data('ams-submit-button'));
  1427 			if (button && !button.data('ams-form-hide-loading')) {
  1445 			if (button && !button.data('ams-form-hide-loading')) {
       
  1446 				button.data('ams-progress-content', button.html());
  1428 				button.button('loading');
  1447 				button.button('loading');
  1429 			}
  1448 			}
  1430 			ams.ajax.check($.fn.ajaxSubmit,
  1449 			ams.ajax.check($.fn.ajaxSubmit,
  1431 						   ams.baseURL + 'ext/jquery-form-3.49' + ams.devext + '.js',
  1450 						   ams.baseURL + 'ext/jquery-form-3.49' + ams.devext + '.js',
  1432 						   function() {
  1451 						   function() {
  1450 									function _getProgress(handler, progress_id) {
  1469 									function _getProgress(handler, progress_id) {
  1451 
  1470 
  1452 										var interval;
  1471 										var interval;
  1453 
  1472 
  1454 										function _clearProgressStatus() {
  1473 										function _clearProgressStatus() {
  1455 											button.button('reset');
       
  1456 											clearInterval(interval);
  1474 											clearInterval(interval);
       
  1475 											ams.form.resetAfterSubmit(form, button);
       
  1476 											button.html(button.data('ams-progress-content'));
  1457 											ams.executeFunctionByName(progressEndCallback, form, button);
  1477 											ams.executeFunctionByName(progressEndCallback, form, button);
       
  1478 											ams.form.resetChanged(form);
  1458 										}
  1479 										}
  1459 
  1480 
  1460 										function _getProgressStatus() {
  1481 										function _getProgressStatus() {
  1461 											ams.ajax.post(handler,
  1482 											ams.ajax.post(handler,
  1462 														  {progress_id: progress_id},
  1483 														  {progress_id: progress_id},
  1465 															if (status === 'success') {
  1486 															if (status === 'success') {
  1466 																if (result.status === 'running') {
  1487 																if (result.status === 'running') {
  1467 																	if (result.message) {
  1488 																	if (result.message) {
  1468 																		button.text(result.message);
  1489 																		button.text(result.message);
  1469 																	} else {
  1490 																	} else {
  1470 																		button.text(button.data('ams-progress-text') +
  1491 																		var text = button.data('ams-progress-text') || ams.i18n.PROGRESS;
  1471 																					' ' + result.current + ' / ' + result.length);
  1492 																		if (result.current) {
       
  1493 																			text += ': ' + result.current + '/ ' + (result.length || 100);
       
  1494 																		} else {
       
  1495 																			text += '...';
       
  1496 																		}
       
  1497 																		button.text(text);
  1472 																	}
  1498 																	}
  1473 																} else if (result.status === 'finished') {
  1499 																} else if (result.status === 'finished') {
  1474 																	_clearProgressStatus();
  1500 																	_clearProgressStatus();
  1475 																}
  1501 																}
  1476 															} else {
  1502 															} else {
  1672 										var modal = $(form).parents('.modal-dialog');
  1698 										var modal = $(form).parents('.modal-dialog');
  1673 										var keepModal = modal.exists() && button.exists() && button.data('ams-keep-modal');
  1699 										var keepModal = modal.exists() && button.exists() && button.data('ams-keep-modal');
  1674 										if (modal.exists() && (keepModal !== true)) {
  1700 										if (modal.exists() && (keepModal !== true)) {
  1675 											ams.dialog.close(form);
  1701 											ams.dialog.close(form);
  1676 										} else {
  1702 										} else {
  1677 											setTimeout(function() {
  1703 											if (!progressHandler) {
  1678 												ams.form.resetAfterSubmit(form, button);
  1704 												setTimeout(function () {
  1679 												ams.form.resetChanged(form);
  1705 													ams.form.resetAfterSubmit(form, button);
  1680 											}, button.data('ams-form-reset-timeout') || 2000);
  1706 													ams.form.resetChanged(form);
       
  1707 												}, button.data('ams-form-reset-timeout') || 2000);
       
  1708 											}
  1681 										}
  1709 										}
  1682 									}
  1710 									}
  1683 								}
  1711 								}
  1684 
  1712 
  1685 								var hasUpload = (form.data('ams-form-ignore-uploads') !== true) &&
  1713 								var hasUpload = (form.data('ams-form-ignore-uploads') !== true) &&
  2558 					}
  2586 					}
  2559 				}
  2587 				}
  2560 			}
  2588 			}
  2561 
  2589 
  2562 			for (name in plugins) {
  2590 			for (name in plugins) {
       
  2591 				if (!plugins.hasOwnProperty(name)) {
       
  2592 					continue;
       
  2593 				}
  2563 				plugin = plugins[name];
  2594 				plugin = plugins[name];
  2564 				if (ams.plugins.enabled[name] === undefined) {
  2595 				if (ams.plugins.enabled[name] === undefined) {
  2565 					ams.getScript(plugin.src, _loadPlugin, {
  2596 					ams.getScript(plugin.src, _loadPlugin, {
  2566 						async: plugin.async === undefined ? true : plugin.async
  2597 						async: plugin.async === undefined ? true : plugin.async
  2567 					});
  2598 					});
  2614 				var dataElement = $(this);
  2645 				var dataElement = $(this);
  2615 				var data = dataElement.data('ams-data');
  2646 				var data = dataElement.data('ams-data');
  2616 				if (data) {
  2647 				if (data) {
  2617 					for (var name in data) {
  2648 					for (var name in data) {
  2618 						if (data.hasOwnProperty(name)) {
  2649 						if (data.hasOwnProperty(name)) {
  2619 							dataElement.attr('data-' + name, data[name]);
  2650 							var elementData = data[name];
       
  2651 							if (typeof(elementData) !== 'string') {
       
  2652 								elementData = JSON.stringify(elementData);
       
  2653 							}
       
  2654 							dataElement.attr('data-' + name, elementData);
  2620 						}
  2655 						}
  2621 					}
  2656 					}
  2622 				}
  2657 				}
  2623 			});
  2658 			});
  2624 		},
  2659 		},
  4244 								if (table.hasClass('datatable')) {
  4279 								if (table.hasClass('datatable')) {
  4245 									table.dataTable().fnDeleteRow(tr[0]);
  4280 									table.dataTable().fnDeleteRow(tr[0]);
  4246 								} else {
  4281 								} else {
  4247 									tr.remove();
  4282 									tr.remove();
  4248 								}
  4283 								}
       
  4284 							} else {
       
  4285 								MyAMS.ajax.handleJSON(result);
  4249 							}
  4286 							}
  4250 						});
  4287 						});
  4251 					}
  4288 					}
  4252 				});
  4289 				});
  4253 			};
  4290 			};
  4530 				}
  4567 				}
  4531 				menu = $('A[href="' + hash + '"]', nav);
  4568 				menu = $('A[href="' + hash + '"]', nav);
  4532 				if (menu.exists()) {
  4569 				if (menu.exists()) {
  4533 					updateActiveMenus(menu);
  4570 					updateActiveMenus(menu);
  4534 				}
  4571 				}
  4535 				ams.skin.loadURL(url, container);
  4572 				ams.skin.loadURL(url, container, {afterLoadCallback: function() {
  4536 				var prefix = $('html head title').data('ams-title-prefix');
  4573 					var prefix = $('html head title').data('ams-title-prefix');
  4537 				document.title = (prefix ? prefix + ' > ' : '') +
  4574 					document.title = (prefix ? prefix + ' > ' : '') +
  4538 								 ($('[data-ams-page-title]:first', container).data('ams-page-title') ||
  4575 						($('[data-ams-page-title]:first', container).data('ams-page-title') ||
  4539 								  menu.attr('title') ||
  4576 						menu.attr('title') ||
  4540 								  document.title);
  4577 						document.title);
       
  4578 				}});
  4541 			} else {
  4579 			} else {
  4542 				var activeUrl = $('[data-ams-active-menu]').data('ams-active-menu');
  4580 				var activeUrl = $('[data-ams-active-menu]').data('ams-active-menu');
  4543 				if (activeUrl) {
  4581 				if (activeUrl) {
  4544 					menu = $('A[href="' + activeUrl + '"]', nav);
  4582 					menu = $('A[href="' + activeUrl + '"]', nav);
  4545 				} else {
  4583 				} else {
  4601 			if (url.startsWith('#')) {
  4639 			if (url.startsWith('#')) {
  4602 				url = url.substr(1);
  4640 				url = url.substr(1);
  4603 			}
  4641 			}
  4604 			if (typeof(options) === 'function') {
  4642 			if (typeof(options) === 'function') {
  4605 				callback = options;
  4643 				callback = options;
       
  4644 				options = {};
       
  4645 			} else if (options === undefined) {
  4606 				options = {};
  4646 				options = {};
  4607 			}
  4647 			}
  4608 			container = $(container);
  4648 			container = $(container);
  4609 			var defaults = {
  4649 			var defaults = {
  4610 				type: 'GET',
  4650 				type: 'GET',
  4611 				url: url,
  4651 				url: url,
  4612 				dataType: 'html',
  4652 				dataType: 'html',
  4613 				cache: false,
  4653 				cache: false,
  4614 				beforeSend: function() {
  4654 				beforeSend: function() {
  4615 					ams.skin.cleanContainer(container);
  4655 					ams.skin.cleanContainer(container);
  4616 					container.html('<h1 class="loading"><i class="fa fa-cog fa-spin"></i> Loading... </h1>');
  4656 					container.html('<h1 class="loading"><i class="fa fa-cog fa-spin"></i> ' + ams.i18n.LOADING + ' </h1>');
  4617 					if (container[0] === $('#content')[0]) {
  4657 					if (container[0] === $('#content')[0]) {
  4618 						ams.skin._drawBreadCrumb();
  4658 						ams.skin._drawBreadCrumb();
  4619 						var prefix = $('html head title').data('ams-title-prefix');
  4659 						var prefix = $('html head title').data('ams-title-prefix');
  4620 						document.title = (prefix ? prefix + ' > ' : '') + $('.breadcrumb LI:last-child').text();
  4660 						document.title = (prefix ? prefix + ' > ' : '') + $('.breadcrumb LI:last-child').text();
  4621 						$('html, body').animate({scrollTop: 0}, 'fast');
  4661 						$('html, body').animate({scrollTop: 0}, 'fast');
  4664 				error: function(request, options, error) {
  4704 				error: function(request, options, error) {
  4665 					container.html('<h3 class="error"><i class="fa fa-warning txt-color-orangeDark"></i> ' +
  4705 					container.html('<h3 class="error"><i class="fa fa-warning txt-color-orangeDark"></i> ' +
  4666 								   ams.i18n.ERROR + error + '</h3>' +
  4706 								   ams.i18n.ERROR + error + '</h3>' +
  4667 								   request.responseText);
  4707 								   request.responseText);
  4668 				},
  4708 				},
  4669 				async: false
  4709 				async: options.async === undefined ? true : options.async
  4670 			};
  4710 			};
  4671 			var settings = $.extend({}, defaults, options);
  4711 			var settings = $.extend({}, defaults, options);
  4672 			$.ajax(settings);
  4712 			$.ajax(settings);
  4673 		},
  4713 		},
  4674 
  4714 
  5149 				if (data.amsTabLoaded) {
  5189 				if (data.amsTabLoaded) {
  5150 					return;
  5190 					return;
  5151 				}
  5191 				}
  5152 				try {
  5192 				try {
  5153 					link.append('<i class="fa fa-spin fa-cog margin-left-5"></i>');
  5193 					link.append('<i class="fa fa-spin fa-cog margin-left-5"></i>');
  5154 					ams.skin.loadURL(data.amsUrl, link.attr('href'));
  5194 					ams.skin.loadURL(data.amsUrl, link.attr('href'), {afterLoadCallback: function() {
  5155 					if (data.amsTabLoadOnce) {
  5195 						if (data.amsTabLoadOnce) {
  5156 						link.data('ams-tab-loaded', true);
  5196 							link.data('ams-tab-loaded', true);
  5157 					}
  5197 						}
       
  5198 					}});
  5158 				}
  5199 				}
  5159 				finally {
  5200 				finally {
  5160 					$('i', link).remove();
  5201 					$('i', link).remove();
  5161 				}
  5202 				}
  5162 			}
  5203 			}
  5232 
  5273 
  5233 		INFO: "Information",
  5274 		INFO: "Information",
  5234 		WARNING: "!! WARNING !!",
  5275 		WARNING: "!! WARNING !!",
  5235 		ERROR: "ERROR: ",
  5276 		ERROR: "ERROR: ",
  5236 
  5277 
       
  5278 		LOADING: "Loading...",
       
  5279 		PROGRESS: "Processing",
       
  5280 
  5237 		WAIT: "Please wait!",
  5281 		WAIT: "Please wait!",
  5238 		FORM_SUBMITTED: "This form was already submitted...",
  5282 		FORM_SUBMITTED: "This form was already submitted...",
  5239 		NO_SERVER_RESPONSE: "No response from server!",
  5283 		NO_SERVER_RESPONSE: "No response from server!",
  5240 		ERROR_OCCURED: "An error occured!",
  5284 		ERROR_OCCURED: "An error occured!",
  5241 		ERRORS_OCCURED: "Some errors occured!",
  5285 		ERRORS_OCCURED: "Some errors occured!",