2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for other elements .
2017-03-16 15:29:07 +00:00
* /
( function ( $ , Drupal ) {
2018-11-23 12:29:20 +00:00
'use strict' ;
2017-03-16 15:29:07 +00:00
/ * *
* Toggle other input ( text ) field .
*
* @ param { boolean } show
* TRUE will display the text field . FALSE with hide and clear the text field .
* @ param { object } $element
* The input ( text ) field to be toggled .
2018-11-23 12:29:20 +00:00
* @ param { string } effect
* Effect .
2017-03-16 15:29:07 +00:00
* /
2018-11-23 12:29:20 +00:00
function toggleOther ( show , $element , effect ) {
2017-03-16 15:29:07 +00:00
var $input = $element . find ( 'input' ) ;
2018-11-23 12:29:20 +00:00
var hideEffect = ( effect === false ) ? 'hide' : 'slideUp' ;
var showEffect = ( effect === false ) ? 'show' : 'slideDown' ;
2017-03-16 15:29:07 +00:00
if ( show ) {
// Limit the other inputs width to the parent's container.
2018-11-23 12:29:20 +00:00
// If the parent container is not visible it's width will be 0
// and ignored.
var width = $element . parent ( ) . width ( ) ;
if ( width ) {
$element . width ( width ) ;
}
2017-03-16 15:29:07 +00:00
// Display the element.
2018-11-23 12:29:20 +00:00
$element [ showEffect ] ( ) ;
// If not initializing, then focus the other element.
if ( effect !== false ) {
$input . focus ( ) ;
}
// Require the input.
$input . prop ( 'required' , true ) . attr ( 'aria-required' , 'true' ) ;
2017-03-16 15:29:07 +00:00
// Restore the input's value.
var value = $input . data ( 'webform-value' ) ;
2018-11-23 12:29:20 +00:00
if ( typeof value !== 'undefined' ) {
2017-03-16 15:29:07 +00:00
$input . val ( value ) ;
2018-11-23 12:29:20 +00:00
var input = $input . get ( 0 ) ;
// Move cursor to the beginning of the other text input.
// @see https://stackoverflow.com/questions/21177489/selectionstart-selectionend-on-input-type-number-no-longer-allowed-in-chrome
if ( $ . inArray ( input . type , [ 'text' , 'search' , 'url' , 'tel' , 'password' ] ) !== - 1 ) {
input . setSelectionRange ( 0 , 0 ) ;
}
2017-03-16 15:29:07 +00:00
}
// Refresh CodeMirror used as other element.
$element . parent ( ) . find ( '.CodeMirror' ) . each ( function ( index , $element ) {
$element . CodeMirror . refresh ( ) ;
} ) ;
}
else {
2018-11-23 12:29:20 +00:00
// Hide the element.
$element [ hideEffect ] ( ) ;
2017-03-16 15:29:07 +00:00
// Save the input's value.
$input . data ( 'webform-value' , $input . val ( ) ) ;
// Empty and un-required the input.
2018-11-23 12:29:20 +00:00
$input . val ( '' ) . prop ( 'required' , false ) . removeAttr ( 'aria-required' ) ;
2017-03-16 15:29:07 +00:00
}
}
/ * *
* Attach handlers to select other elements .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformSelectOther = {
attach : function ( context ) {
$ ( context ) . find ( '.js-webform-select-other' ) . once ( 'webform-select-other' ) . each ( function ( ) {
var $element = $ ( this ) ;
var $select = $element . find ( 'select' ) ;
var $otherOption = $element . find ( 'option[value="_other_"]' ) ;
var $input = $element . find ( '.js-webform-select-other-input' ) ;
$select . on ( 'change' , function ( ) {
toggleOther ( $otherOption . is ( ':selected' ) , $input ) ;
} ) ;
2018-11-23 12:29:20 +00:00
toggleOther ( $otherOption . is ( ':selected' ) , $input , false ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
/ * *
* Attach handlers to checkboxes other elements .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformCheckboxesOther = {
attach : function ( context ) {
$ ( context ) . find ( '.js-webform-checkboxes-other' ) . once ( 'webform-checkboxes-other' ) . each ( function ( ) {
var $element = $ ( this ) ;
var $checkbox = $element . find ( 'input[value="_other_"]' ) ;
var $input = $element . find ( '.js-webform-checkboxes-other-input' ) ;
$checkbox . on ( 'change' , function ( ) {
toggleOther ( this . checked , $input ) ;
} ) ;
2018-11-23 12:29:20 +00:00
toggleOther ( $checkbox . is ( ':checked' ) , $input , false ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
/ * *
* Attach handlers to radios other elements .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformRadiosOther = {
attach : function ( context ) {
$ ( context ) . find ( '.js-webform-radios-other' ) . once ( 'webform-radios-other' ) . each ( function ( ) {
var $element = $ ( this ) ;
var $radios = $element . find ( 'input[type="radio"]' ) ;
var $input = $element . find ( '.js-webform-radios-other-input' ) ;
$radios . on ( 'change' , function ( ) {
toggleOther ( ( $radios . filter ( ':checked' ) . val ( ) === '_other_' ) , $input ) ;
} ) ;
2018-11-23 12:29:20 +00:00
toggleOther ( ( $radios . filter ( ':checked' ) . val ( ) === '_other_' ) , $input , false ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
/ * *
* Attach handlers to buttons other elements .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformButtonsOther = {
attach : function ( context ) {
$ ( context ) . find ( '.js-webform-buttons-other' ) . once ( 'webform-buttons-other' ) . each ( function ( ) {
var $element = $ ( this ) ;
var $buttons = $element . find ( 'input[type="radio"]' ) ;
var $input = $element . find ( '.js-webform-buttons-other-input' ) ;
2018-11-23 12:29:20 +00:00
var $container = $ ( this ) . find ( '.js-webform-webform-buttons' ) ;
2017-03-16 15:29:07 +00:00
2018-11-23 12:29:20 +00:00
// Create set onchange handler.
$container . change ( function ( ) {
2017-03-16 15:29:07 +00:00
toggleOther ( ( $ ( this ) . find ( ':radio:checked' ) . val ( ) === '_other_' ) , $input ) ;
} ) ;
2018-11-23 12:29:20 +00:00
toggleOther ( ( $buttons . filter ( ':checked' ) . val ( ) === '_other_' ) , $input , false ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
} ) ( jQuery , Drupal ) ;