2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for webforms .
2017-03-16 15:29:07 +00:00
* /
( function ( $ , Drupal ) {
'use strict' ;
2018-11-23 12:29:20 +00:00
/ * *
* Remove single submit event listener .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior for removing single submit event listener .
*
* @ see Drupal . behaviors . formSingleSubmit
* /
Drupal . behaviors . weformRemoveFormSingleSubmit = {
attach : function attach ( ) {
function onFormSubmit ( e ) {
var $form = $ ( e . currentTarget ) ;
$form . removeAttr ( 'data-drupal-form-submit-last' ) ;
}
$ ( 'body' )
. once ( 'webform-single-submit' )
. on ( 'submit.singleSubmit' , 'form.webform-remove-single-submit:not([method~="GET"])' , onFormSubmit ) ;
}
} ;
2017-03-16 15:29:07 +00:00
/ * *
* Autofocus first input .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior for the webform autofocusing .
* /
Drupal . behaviors . webformAutofocus = {
attach : function ( context ) {
$ ( context ) . find ( '.webform-submission-form.js-webform-autofocus :input:visible:enabled:first' ) . focus ( ) ;
}
} ;
/ * *
* Prevent webform autosubmit on wizard pages .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior for disabling webform autosubmit .
2018-11-23 12:29:20 +00:00
* Wizard pages need to be progressed with the Previous or Next buttons , not by pressing Enter .
2017-03-16 15:29:07 +00:00
* /
Drupal . behaviors . webformDisableAutoSubmit = {
attach : function ( context ) {
// @see http://stackoverflow.com/questions/11235622/jquery-disable-form-submit-on-enter
2018-11-23 12:29:20 +00:00
$ ( context ) . find ( '.webform-submission-form.js-webform-disable-autosubmit input' )
. not ( ':button, :submit, :reset, :image, :file' )
. once ( 'webform-disable-autosubmit' )
. on ( 'keyup keypress' , function ( e ) {
var keyCode = e . keyCode || e . which ;
if ( keyCode === 13 ) {
e . preventDefault ( ) ;
return false ;
}
} ) ;
2017-03-16 15:29:07 +00:00
}
} ;
/ * *
* Skip client - side validation when submit button is pressed .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior for the skipping client - side validation .
* /
Drupal . behaviors . webformSubmitNoValidate = {
attach : function ( context ) {
2018-11-23 12:29:20 +00:00
$ ( context ) . find ( ':submit.js-webform-novalidate' ) . once ( 'webform-novalidate' ) . on ( 'click' , function ( ) {
2017-03-16 15:29:07 +00:00
$ ( this . form ) . attr ( 'novalidate' , 'novalidate' ) ;
} ) ;
}
} ;
/ * *
2018-11-23 12:29:20 +00:00
* Attach behaviors to trigger submit button from input onchange .
2017-03-16 15:29:07 +00:00
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
2018-11-23 12:29:20 +00:00
* Attaches form trigger submit events .
2017-03-16 15:29:07 +00:00
* /
2018-11-23 12:29:20 +00:00
Drupal . behaviors . webformSubmitTrigger = {
2017-03-16 15:29:07 +00:00
attach : function ( context ) {
2018-11-23 12:29:20 +00:00
$ ( '[data-webform-trigger-submit]' ) . once ( 'webform-trigger-submit' ) . on ( 'change' , function ( ) {
var submit = $ ( this ) . attr ( 'data-webform-trigger-submit' ) ;
$ ( submit ) . mousedown ( ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
/ * *
2018-11-23 12:29:20 +00:00
* Custom required error message .
2017-03-16 15:29:07 +00:00
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
2018-11-23 12:29:20 +00:00
* Attaches the behavior for the webform custom required error message .
*
* @ see http : //stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message
2017-03-16 15:29:07 +00:00
* /
2018-11-23 12:29:20 +00:00
Drupal . behaviors . webformRequiredError = {
attach : function ( context ) {
$ ( context ) . find ( ':input[data-webform-required-error]' ) . once ( 'webform-required-error' )
. on ( 'invalid' , function ( ) {
this . setCustomValidity ( '' ) ;
if ( ! this . valid ) {
this . setCustomValidity ( $ ( this ) . attr ( 'data-webform-required-error' ) ) ;
}
} )
. on ( 'input, change' , function ( ) {
// Find all related elements by name and reset custom validity.
// This specifically applies to required radios and checkboxes.
var name = $ ( this ) . attr ( 'name' ) ;
$ ( this . form ) . find ( ':input[name="' + name + '"]' ) . each ( function ( ) {
this . setCustomValidity ( '' ) ;
2017-03-16 15:29:07 +00:00
} ) ;
2018-11-23 12:29:20 +00:00
} ) ;
2017-03-16 15:29:07 +00:00
}
} ;
2018-11-23 12:29:20 +00:00
// When #state:required is triggered we need to reset the target elements
// custom validity.
$ ( document ) . on ( 'state:required' , function ( e ) {
$ ( e . target ) . filter ( '[data-webform-required-error]' )
. each ( function ( ) { this . setCustomValidity ( '' ) ; } ) ;
} ) ;
if ( window . imceInput ) {
window . imceInput . processUrlInput = function ( i , el ) {
var button = imceInput . createUrlButton ( el . id , el . getAttribute ( 'data-imce-type' ) ) ;
el . parentNode . insertAfter ( button , el ) ;
} ;
}
2017-03-16 15:29:07 +00:00
} ) ( jQuery , Drupal ) ;