From 88e9380e1f4ad961a1d6343b753d4365d068f032 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 1 May 2014 16:50:04 -0700 Subject: [PATCH] CRM-14353 - Add confirm to popup forms --- js/Common.js | 18 +++++++++--------- js/crm.ajax.js | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/js/Common.js b/js/Common.js index c92c1e7815..cebf87a446 100644 --- a/js/Common.js +++ b/js/Common.js @@ -240,16 +240,16 @@ CRM.validate = CRM.validate || { /** * Compare Form Input values against cached initial value. - * True if changes have been made. - * @returns {Boolean} + * + * @return {Boolean} true if changes have been made. */ CRM.utils.initialValueChanged = function(el) { - var isDirty = false - $(':input:visible', el).each(function () { - if($(this).data('crm-initial-value') !== undefined - && $(this).data('crm-initial-value') != $(this).val()){ - isDirty = true; - } + var isDirty = false; + $(':input:visible, :input.select2-offscreen', el).each(function () { + var initialValue = $(this).data('crm-initial-value'); + if (initialValue !== undefined && initialValue != $(this).val()) { + isDirty = true; + } }); return isDirty; } @@ -439,7 +439,7 @@ CRM.validate = CRM.validate || { $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); // Cache Form Input initial values - $('form[data-warn-changes] :input:visible', e.target).each( function() { + $('form[data-warn-changes] :input', e.target).each(function() { $(this).data('crm-initial-value', $(this).val()); }); }) diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 066269b15a..d98018a290 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -337,12 +337,27 @@ var widget = CRM.loadPage(url, settings).off('.crmForm'); + function cancelAction() { + if (CRM.utils.initialValueChanged(widget)) { + return confirm(ts('You have unsaved changes.')); + } + return true; + } + if (widget.data('uiDialog')) { + // This is a bit harsh but we are removing jQuery UI's event handler from the button and adding our own + $('.ui-dialog-titlebar-close').first().off().click(function() { + if (cancelAction()) { + widget.dialog('close'); + } + }); + } + widget.on('crmFormLoad.crmForm', function(event, data) { var $el = $(this); var settings = $el.crmSnippet('option', 'crmForm'); settings.cancelButton && $(settings.cancelButton, this).click(function(event) { var returnVal = settings.onCancel.call($el, event); - if (returnVal !== false) { + if (returnVal !== false && cancelAction()) { $el.trigger('crmFormCancel', event); if ($el.data('uiDialog') && settings.autoClose) { $el.dialog('close'); @@ -350,8 +365,9 @@ else if (!settings.autoClose) { $el.crmSnippet('resetUrl').crmSnippet('refresh'); } + } else { + event.preventDefault(); } - return returnVal === false; }); if (settings.validate) { $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); -- 2.25.1