From: Coleman Watts Date: Sun, 17 Nov 2013 23:09:48 +0000 (-0800) Subject: CRM-10693 - Ajax - Support resetButton in CRM.loadForm X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=83df6b4a519e14458c95046d4f19b8dfb9583e8b;p=civicrm-core.git CRM-10693 - Ajax - Support resetButton in CRM.loadForm --- diff --git a/js/Common.js b/js/Common.js index cd1cda4ec7..de2861b853 100644 --- a/js/Common.js +++ b/js/Common.js @@ -825,7 +825,7 @@ CRM.validate = CRM.validate || { url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; var settings = { target: '#crm-ajax-dialog', - dialogSettings: { + dialog: { modal: true, minWidth: 600, close: function() { @@ -834,19 +834,22 @@ CRM.validate = CRM.validate || { } }, onLoad: null, - type: 'Page' + type: 'Page', + url: url }; $.extend(true, settings, options); - if (settings.target == '#crm-ajax-dialog') { - $('
' + ts('Loading') + '...
').dialog(settings.dialogSettings); + // Create new dialog + if (settings.dialog && settings.target[0] == '#') { + $('
' + ts('Loading') + '...
').dialog(settings.dialog); } $.getJSON(url, function(data) { - if (settings.target == '#crm-ajax-dialog' && !settings.dialogSettings.title && data.title) { + if (settings.dialog && !settings.dialog.title && data.title) { $(settings.target).dialog('option', 'title', data.title); } + settings.content = data.content; $(settings.target).html(data.content).trigger('crm' + settings.type + 'Load', data); if (typeof(settings.onLoad) == 'function') { - settings.onLoad(data, url); + settings.onLoad(data, settings); } }); }; @@ -858,43 +861,55 @@ CRM.validate = CRM.validate || { validate: true, onLoad: null, onCancel: function(e) { - $(settings.target).dialog('close'); + if (settings.dialog !== false) { + $(settings.target).dialog('close'); + } return false; }, - onError: function(data) { + onError: function(data, settings) { $(settings.target).html(data.content).trigger('crmFormReload', data); if (typeof(data.errors) == 'object') { $.each(data.errors, function(target, msg) { $('[name="'+target+'"]').crmError(msg); }); } - settings.onLoad(data, url); + settings.onLoad(data, settings); }, - onSuccess: function(data) { - $(settings.target).dialog('close'); + onSuccess: function(data, settings) { + if (settings.dialog !== false) { + $(settings.target).dialog('close'); + } } }; $.extend(settings, options, {type: 'Form'}); - settings.onLoad = function(data, url) { + settings.onLoad = function(data, settings) { $(".cancel.form-submit", settings.target).click(settings.onCancel); if (settings.validate) { - $("form", settings.target).validate(CRM.validate.params); + $("form", settings.target).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); } $("form", settings.target).ajaxForm({ - url: url, + url: settings.url, dataType: 'json', success: function(response) { if (response.status == 'success') { - settings.onSuccess(response); + settings.onSuccess(response, settings); + $(settings.target).unblock().trigger('crmFormSuccess', data); + if (settings.resetButton && response.buttonName == settings.resetButton) { + $(settings.target).html(settings.content).trigger('crmFormLoad', data); + settings.onLoad(data, settings); + } } else { - settings.onError(response); + settings.onError(response, settings); } + }, + beforeSubmit: function() { + $(settings.target).block().trigger('crmFormSubmit', data); } }); // Call original onLoad fn if (typeof(options.onLoad) == 'function') { - options.onLoad(data, url); + options.onLoad(data, settings); } }; CRM.loadPage(url, settings);