From 3e201321402866ac6fb201f071e0562bbdc3b18f Mon Sep 17 00:00:00 2001 From: mzd Date: Wed, 30 Apr 2014 20:23:18 -0400 Subject: [PATCH] CRM-14353 js function to store initial form values; php to enable warning on add/edit forms; ---------------------------------------- * CRM-14353: Fix formNavigate to work with multiple forms & ajax https://issues.civicrm.org/jira/browse/CRM-14353 --- CRM/Core/Form.php | 9 +++++++++ js/Common.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 69e1233d51..bd212a6ed0 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -416,6 +416,15 @@ class CRM_Core_Form extends HTML_QuickForm_Page { CRM_Utils_Hook::buildForm(get_class($this), $this); $this->addRules(); + + //Set html data-attribute to enable warning user of unsaved changes + if ($this->unsavedChangesWarn === true + || (!isset($this->unsavedChangesWarn) + && ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) + ) + ) { + $this->setAttribute('data-warn-changes', 'true'); + } } /** diff --git a/js/Common.js b/js/Common.js index 2df825d164..1db3a81090 100644 --- a/js/Common.js +++ b/js/Common.js @@ -238,6 +238,22 @@ CRM.validate = CRM.validate || { }); }; +/** + * Compare Form Input values against cached initial value. + * True if changes have been made. + * @returns {Boolean} + */ + CRM.utils.initialValueChanged = function(el) { + var isDirty = false + $(':input', el).each(function () { + if($(this).data('crm-initial-value') !== undefined + && $(this).data('crm-initial-value') != $(this).val()){ + isDirty = true; + } + }); + return isDirty; + } + /** * Wrapper for select2 initialization function; supplies defaults * @param options object @@ -422,6 +438,10 @@ CRM.validate = CRM.validate || { .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); $('.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', e.target).each( function() { + $(this).data('crm-initial-value', $(e.target).val()); + }); }) .on('dialogopen', function(e) { var $el = $(e.target); -- 2.25.1