SHARE
TWEET

Untitled

a guest Jun 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', function() {
  2.     (function(forms, $, undefined) {
  3.         var $forms, $submitButtons;
  4.  
  5.         var bindScopeVariables = function() {
  6.             $submitButtons = $('input[type=submit], button[type=submit]');
  7.             $forms = $('form');
  8.         };
  9.  
  10.         var addEventListeners = function() {
  11.             $submitButtons.click(function(e) {
  12.                 if (e.target.classList.contains('FormSubmitButton')) {
  13.                     return;
  14.                 }
  15.                 checkForm($(this));
  16.             });
  17.         };
  18.  
  19.         var resetFormState = function() {
  20.             // Fire the show/hide event in case we're returning to the page
  21.             $('.to-show').change();
  22.             console.log('resetFormState')
  23.         };
  24.  
  25.         var checkCustomValidation = function($form) {
  26.             var isValidCustom = true;
  27.  
  28.             // future date checking
  29.             $form.find('div.threepartfuturedate select.threepartyear').each(function() {
  30.                 var isRequiredAttr = $(this).attr('data-val-required');
  31.                 var isRequired = typeof isRequiredAttr !== typeof undefined && isRequiredAttr !== false;
  32.  
  33.                 if (!futureDateCheck($(this), isRequired)) {
  34.                     isValidCustom = false;
  35.                     return false;
  36.                 }
  37.             });
  38.  
  39.             return isValidCustom;
  40.         };
  41.  
  42.         var checkForm = function($submitButton) {
  43.             var $parentForm = $submitButton.closest('form');
  44.  
  45.             if ($parentForm.length > 0) {
  46.                 var isValid = $parentForm.valid();
  47.                 var isValidCustom = checkCustomValidation($parentForm);
  48.  
  49.                 if (isValid && isValidCustom) {
  50.                     // Button has custom data-loading
  51.                     if ($submitButton.data('loading-text') || $submitButton.data('content')) {
  52.                         return;
  53.                     }
  54.  
  55.                     $submitButton.button('loading');
  56.  
  57.                 } else {
  58.                     forms.scrollToFurthestFormError($parentForm);
  59.                 }
  60.             }
  61.         };
  62.  
  63.         window.forms.init = function() {
  64.             bindScopeVariables();
  65.             addEventListeners();
  66.             resetFormState();
  67.         };
  68.  
  69.        
  70.  
  71.         window.forms.resetButtonLoading = function() {
  72.             $submitButtons.button('reset');
  73.         };
  74.  
  75.         window.forms.scrollToFurthestFormError = function($form) {
  76.             var offsetTop = 0;
  77.             var $element = [];
  78.             var errorList = $form.data('validator').errorList;
  79.  
  80.             if (errorList.length > 0) {
  81.                 $element = $form.data('validator').errorList[0].element;
  82.                 offsetTop = $(errorList[0].element).offset().top - 20;
  83.             }
  84.  
  85.             // Custom datetime.js validation is not part of JQuery Validate errorList so do custom scrolling here
  86.             $form.find('div.threepartfuturedate select.threepartyear').each(function() {
  87.                 var isRequiredAttr = $(this).attr('data-val-required');
  88.                 var isRequired = typeof isRequiredAttr !== typeof undefined && isRequiredAttr !== false;
  89.  
  90.                 var isValidDate = futureDateCheck($(this), isRequired);
  91.  
  92.                 if (!isValidDate) {
  93.                     var newOffsetTop = $(this).offset().top - 20;
  94.  
  95.                     // If the invalid element is higher on the page OR there are no other errors...
  96.                     // Scroll to this invalid field
  97.                     if (newOffsetTop < offsetTop || offsetTop === 0) {
  98.                         $element = $(this);
  99.                         offsetTop = newOffsetTop;
  100.                         return false;
  101.                     }
  102.                 }
  103.             });
  104.  
  105.             // sets focus on the first field that has an error
  106.             $element.focus();
  107.             mariecurie.helpers.scrollTo(offsetTop, 1000);
  108.         };
  109.  
  110.         $(window).bind("pageshow", function () {
  111.             resetFormState();
  112.         });
  113.  
  114.     })((window.forms = window.forms || {}), jQuery);
  115.  
  116.     window.forms.init();
  117.    
  118. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top