1/** 2 * @file 3 * JavaScript behaviors for webforms. 4 */ 5 6(function ($, Drupal, once) { 7 /** 8 * Remove single submit event listener. 9 * 10 * @type {Drupal~behavior} 11 * 12 * @prop {Drupal~behaviorAttach} attach 13 * Attaches the behavior for removing single submit event listener. 14 * 15 * @see Drupal.behaviors.formSingleSubmit 16 */ 17 Drupal.behaviors.webformRemoveFormSingleSubmit = { 18 attach: function attach() { 19 function onFormSubmit(e) { 20 var $form = $(e.currentTarget); 21 $form.removeAttr('data-drupal-form-submit-last'); 22 } 23 $(once('webform-single-submit', 'body')) 24 .on('submit.singleSubmit', 'form.webform-remove-single-submit', onFormSubmit); 25 } 26 }; 27 28 /** 29 * Prevent webform autosubmit on wizard pages. 30 * 31 * @type {Drupal~behavior} 32 * 33 * @prop {Drupal~behaviorAttach} attach 34 * Attaches the behavior for disabling webform autosubmit. 35 * Wizard pages need to be progressed with the Previous or Next buttons, 36 * not by pressing Enter. 37 */ 38 Drupal.behaviors.webformDisableAutoSubmit = { 39 attach(context) { 40 // Not using context so that inputs loaded via Ajax will have autosubmit 41 // disabled. 42 // @see http://stackoverflow.com/questions/11235622/jquery-disable-form-submit-on-enter 43 $(once('webform-disable-autosubmit', $('.js-webform-disable-autosubmit input').not(':button, :submit, :reset, :image, :file'))) 44 .on('keyup keypress', function (e) { 45 if (e.which === 13) { 46 e.preventDefault(); 47 return false; 48 } 49 }); 50 } 51 }; 52 53 /** 54 * Custom required and pattern validation error messages. 55 * 56 * @type {Drupal~behavior} 57 * 58 * @prop {Drupal~behaviorAttach} attach 59 * Attaches the behavior for the webform custom required and pattern 60 * validation error messages. 61 * 62 * @see http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message 63 **/ 64 Drupal.behaviors.webformRequiredError = { 65 attach(context) { 66 $(once('webform-required-error', $(context).find(':input[data-webform-required-error], :input[data-webform-pattern-error]'))) 67 .on('invalid', function () { 68 this.setCustomValidity(''); 69 if (this.valid) { 70 return; 71 } 72 73 if (this.validity.patternMismatch && $(this).attr('data-webform-pattern-error')) { 74 this.setCustomValidity($(this).attr('data-webform-pattern-error')); 75 } 76 else if (this.validity.valueMissing && $(this).attr('data-webform-required-error')) { 77 this.setCustomValidity($(this).attr('data-webform-required-error')); 78 } 79 }) 80 .on('input change', function () { 81 // Find all related elements by name and reset custom validity. 82 // This specifically applies to required radios and checkboxes. 83 var name = $(this).attr('name'); 84 $(this.form).find(':input[name="' + name + '"]').each(function () { 85 this.setCustomValidity(''); 86 }); 87 }); 88 } 89 }; 90 91 // When #state:required is triggered we need to reset the target elements 92 // custom validity. 93 $(document).on('state:required', function (e) { 94 $(e.target).filter(':input[data-webform-required-error]') 95 .each(function () {this.setCustomValidity('');}); 96 }); 97 98})(jQuery, Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.