CRM-14353 js function to store initial form values; php to enable warning on add...
authormzd <mzd@ginkgostreet.com>
Thu, 1 May 2014 00:23:18 +0000 (20:23 -0400)
committermzd <mzd@ginkgostreet.com>
Thu, 1 May 2014 18:59:15 +0000 (14:59 -0400)
----------------------------------------
* CRM-14353: Fix formNavigate to work with multiple forms & ajax
  https://issues.civicrm.org/jira/browse/CRM-14353

CRM/Core/Form.php
js/Common.js

index 69e1233d5164dfd787ece9d6368c8034287917fa..bd212a6ed0d04774c9a5afd75c57c484c9b5491f 100644 (file)
@@ -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');
+    }
   }
 
   /**
index 2df825d16443ca3e0251397b43802f47812be68c..1db3a81090db9fa62ab98a974d64f241cbfd6465 100644 (file)
@@ -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);