Ensure total is always updated when other amount is selected
authoreileen <emcnaughton@wikimedia.org>
Sat, 27 Jan 2024 20:04:39 +0000 (09:04 +1300)
committereileen <emcnaughton@wikimedia.org>
Sat, 27 Jan 2024 20:14:43 +0000 (09:14 +1300)
CRM/Price/BAO/PriceField.php
templates/CRM/Contribute/Form/Contribution/Main.tpl

index a161fae72659c69566b4909ea0cc8576b6ac2556..38629f0f03d5e24551ce95f3e4671a7ef6f8bd8b 100644 (file)
@@ -389,6 +389,8 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             'price' => json_encode([$elementName, '0|0']),
             'data-currency' => $currencyName,
             'onclick' => 'clearAmountOther();',
+            'data-amount' => 0,
+            'data-is-null-option' => TRUE,
           ];
         }
 
@@ -408,6 +410,8 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $choiceAttrs['0'] = [
             'price' => json_encode([$elementName, '0']),
             'data-membership-type-id' => NULL,
+            'data-amount' => 0,
+            'data-is-null-option' => TRUE,
           ] + $incomingExtra;
         }
 
index 054776ad3df1e25bbc011cf75521fee02bbd28ef..b516313aea5ea2cf0a5c7644d2e3f1f40800152f 100644 (file)
 {literal}
   <script type="text/javascript">
 
-    // Putting these functions directly in template so they are available for standalone forms
+    // Putting these functions directly in template for historical reasons.
     function useAmountOther(mainPriceFieldName) {
-     for( i=0; i < document.Main.elements.length; i++ ) {
-        element = document.Main.elements[i];
-        if ( element.type == 'radio' && element.name === mainPriceFieldName ) {
-          if (element.value == '0' ) {
-            element.checked = true;
-            // Copied from `updatePriceSetHighlight()` below which isn't available here.
-            cj('#priceset .price-set-row span').removeClass('highlight');
-            cj('#priceset .price-set-row input:checked').parent().addClass('highlight');
-          }
-          else {
-            element.checked = false;
+      var currentFocus = CRM.$(':focus');
+      CRM.$('input[name=' + mainPriceFieldName + ']:radio:unchecked').each(
+        function () {
+          if (CRM.$(this).data('is-null-option') !== undefined) {
+            // Triggering this click here because over in Calculate.tpl
+            // a blur action is attached
+            CRM.$(this).prop('checked', true).trigger('click');
           }
         }
-      }
+      );
+      // Copied from `updatePriceSetHighlight()` below which isn't available here.
+      // @todo - consider adding this to the actions assigned in Calculate.tpl
+      CRM.$('#priceset .price-set-row span').removeClass('highlight');
+      CRM.$('#priceset .price-set-row input:checked').parent().addClass('highlight');
+      // Return the focus we blurred earlier.
+      currentFocus.trigger('focus');
+
     }
 
     function clearAmountOther(otherPriceFieldName) {
-      cj('#' + otherPriceFieldName).val('');
-      cj('#' + otherPriceFieldName).blur();
-      // @todo - remove the next 2 lines - they seems to relate to a field that is never present
-      // as amount_other will be (e.g) price_4
-      if (document.Main.amount_other == null) return; // other_amt field not present; do nothing
-      document.Main.amount_other.value = "";
+      cj('#' + otherPriceFieldName).val('').trigger('blur');
     }
 
   </script>