Updated Javascript API
authorThierry Florac <thierry.florac@onf.fr>
Wed, 02 Jul 2014 10:28:37 +0200
changeset 49 f830ae04d473
parent 48 501bdc011ad6
child 50 ed38f50045e7
Updated Javascript API
src/ztfy/myams/resources/js/myams.js
--- a/src/ztfy/myams/resources/js/myams.js	Wed Jul 02 10:28:23 2014 +0200
+++ b/src/ztfy/myams/resources/js/myams.js	Wed Jul 02 10:28:37 2014 +0200
@@ -2,7 +2,7 @@
  * MyAMS
  * « My Application Management Skin »
  *
- * $Tag: 0.1.2 $
+ * $Tag$
  * A bootstrap based application/administration skin
  *
  * Custom administration and application skin tools
@@ -1294,7 +1294,7 @@
 			var callback = request.getResponseHeader('X-AMS-Callback');
 			if (callback) {
 				var options = request.getResponseHeader('X-AMS-Callback-Options');
-				ams.executeFunctionByName(callback, form, options === undefined ? {} : JSON.parse(options));
+				ams.executeFunctionByName(callback, form, options === undefined ? {} : JSON.parse(options), request);
 			}
 		},
 
@@ -1409,7 +1409,7 @@
 						   ams.baseURL + 'ext/bootstrap-modalmanager' + (ams.devmode ? '.js' : '.min.js'),
 						   function() {
 								ams.ajax.check($.fn.modal.defaults,
-								ams.baseURL + 'ext/bootstrap-modal' + (ams.devmode ? '.js' : '.min.js'),
+											   ams.baseURL + 'ext/bootstrap-modal' + (ams.devmode ? '.js' : '.min.js'),
 								function(first_load) {
 									if (first_load) {
 										$(document).off('click.modal');
@@ -1420,10 +1420,12 @@
 												'</div>' +
 											'</div>';
 									}
-									if (typeof(source) == 'string')
+									if (typeof(source) == 'string') {
+										var source_data = {};
 										var url = source;
-									else {
-										url = source.attr('href') || source.data('ams-url');
+									} else {
+										source_data = source.data();
+										url = source.attr('href') || source_data.amsUrl;
 										var url_getter = ams.getFunctionByName(url);
 										if (typeof(url_getter) == 'function') {
 											url = url_getter.call(source);
@@ -1437,41 +1439,47 @@
 										$(url).modal('show');
 									} else {
 										// Remote URL modal dialog
-										$.get(url, options, function(data, status, request) {
-											$('body').modalmanager('removeLoading');
-											var request_data = ams.ajax.getResponse(request);
-											var data_type = request_data.content_type;
-											var result = request_data.data;
-											switch (data_type) {
-												case 'json':
-													ams.ajax.handleJSON(result, $($(source).data('ams-json-target') || '#content'));
-													break;
-												case 'script':
-													break;
-												case 'xml':
-													break;
-												case 'html':
-												case 'text':
-												default:
-													var content = $(result);
-													var dialog = $('.modal-dialog', content.wrap('<div></div>').parent());
-													var dialog_data = dialog.data();
-													var data_options = {
-														overflow: dialog_data.amsModalOverflow || '.modal-viewport',
-														maxHeight: dialog_data.amsModalMaxHeight === undefined
-																? function() {
-																	return $(window).height() -
-																				$('.modal-header', content).outerHeight(true) -
-																				$('footer', content).outerHeight(true) - 85;
-																}
-																: ams.getFunctionByName(dialog_data.amsModalMaxHeight)
-													};
-													var settings = $.extend({}, data_options, dialog_data.amsModalOptions);
-													settings = ams.executeFunctionByName(dialog_data.amsModalInitCallback, dialog, settings) || settings;
-													$('<div>').addClass('modal fade')
-															  .append(content)
-															  .modal(settings);
-													ams.initContent(content);
+										$.ajax({
+											url: url,
+											type: 'get',
+											cache: source_data.amsAllowCache === undefined ? false : source_data.amsAllowCache,
+											data: options,
+											success: function(data, status, request) {
+												$('body').modalmanager('removeLoading');
+												var request_data = ams.ajax.getResponse(request);
+												var data_type = request_data.content_type;
+												var result = request_data.data;
+												switch (data_type) {
+													case 'json':
+														ams.ajax.handleJSON(result, $($(source).data('ams-json-target') || '#content'));
+														break;
+													case 'script':
+														break;
+													case 'xml':
+														break;
+													case 'html':
+													case 'text':
+													default:
+														var content = $(result);
+														var dialog = $('.modal-dialog', content.wrap('<div></div>').parent());
+														var dialog_data = dialog.data();
+														var data_options = {
+															overflow: dialog_data.amsModalOverflow || '.modal-viewport',
+															maxHeight: dialog_data.amsModalMaxHeight === undefined
+																	? function() {
+																		return $(window).height() -
+																					$('.modal-header', content).outerHeight(true) -
+																					$('footer', content).outerHeight(true) - 85;
+																	}
+																	: ams.getFunctionByName(dialog_data.amsModalMaxHeight)
+														};
+														var settings = $.extend({}, data_options, dialog_data.amsModalOptions);
+														settings = ams.executeFunctionByName(dialog_data.amsModalInitCallback, dialog, settings) || settings;
+														$('<div>').addClass('modal fade')
+																  .append(content)
+																  .modal(settings);
+														ams.initContent(content);
+												}
 											}
 										});
 									}
@@ -1824,15 +1832,20 @@
 						var checker = $('<label class="checkbox"></label>');
 						var fieldname = data.amsCheckerFieldname || ('checker_'+ams.generateId());
 						var prefix = data.amsCheckerHiddenPrefix;
+						var hidden = null;
+						var checkedValue = data.amsCheckerHiddenValueOn || 'true';
+						var uncheckedValue = data.amsCheckerHiddenValueOff || 'false';
 						if (prefix) {
-							$('<input type="hidden">').attr('name', prefix + fieldname)
-													  .val(true)
-													  .appendTo(checker);
+							hidden = $('<input type="hidden">').attr('name', prefix + fieldname)
+															   .val(data.amsCheckerState == 'on' ? checkedValue : uncheckedValue)
+															   .prependTo(legend);
 						}
 						var input = $('<input type="checkbox">').attr('name', fieldname)
 																.attr('id', fieldname.replace(/\./, '_'))
+																.data('ams-checker-hidden-input', hidden)
 																.data('ams-checker-init', true)
-																.val(data.amsCheckerState == 'on');
+																.val(true)
+																.attr('checked', data.amsCheckerState == 'on' ? 'checked' : null);
 						if (data.amsCheckerReadonly) {
 							input.attr('disabled', 'disabled');
 						} else {
@@ -1842,20 +1855,29 @@
 								legend.trigger('ams.checker.before-switch', [legend, veto]);
 								if (veto.veto)
 									return;
-								if ($(this).is(':checked')) {
-									if (data.amsCheckerMode == 'disable')
-										fieldset.removeAttr('disabled')
-									else
-										fieldset.removeClass('switched');
-									$('[data-required]', fieldset).attr('required', 'required');
-									legend.trigger('ams.checker.opened', [legend]);
-								} else {
-									if (data.amsCheckerMode == 'disable')
-										fieldset.attr('disabled', 'disabled');
-									else
-										fieldset.addClass('switched');
-									$('[data-required]', fieldset).removeAttr('required');
-									legend.trigger('ams.checker.closed', [legend]);
+								var isChecked = $(this).is(':checked');
+								ams.executeFunctionByName(data.amsCheckerChangeHandler, legend, isChecked);
+								if (!data.amsCheckerCancelDefault) {
+									var hidden = input.data('ams-checker-hidden-input');
+									if (isChecked) {
+										if (data.amsCheckerMode == 'disable')
+											fieldset.removeAttr('disabled');
+										else
+											fieldset.removeClass('switched');
+										if (hidden)
+											hidden.val(checkedValue);
+										$('[data-required]', fieldset).attr('required', 'required');
+										legend.trigger('ams.checker.opened', [legend]);
+									} else {
+										if (data.amsCheckerMode == 'disable')
+											fieldset.attr('disabled', 'disabled');
+										else
+											fieldset.addClass('switched');
+										if (hidden)
+											hidden.val(uncheckedValue);
+										$('[data-required]', fieldset).removeAttr('required');
+										legend.trigger('ams.checker.closed', [legend]);
+									}
 								}
 							});
 						}
@@ -3072,6 +3094,7 @@
 		// Logout button
 		$('#logout a').click(function(e) {
 			e.preventDefault();
+			e.stopPropagation();
 			//get the link
 			ams.loginURL = $(this).attr('href');
 			// ask verification