2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for toggle integration .
2017-03-16 15:29:07 +00:00
* /
( function ( $ , Drupal ) {
'use strict' ;
2018-11-23 12:29:20 +00:00
// @see https://github.com/simontabor/jquery-toggles
Drupal . webform = Drupal . webform || { } ;
Drupal . webform . toggles = Drupal . webform . toggles || { } ;
Drupal . webform . toggles . options = Drupal . webform . toggles . options || { } ;
2017-03-16 15:29:07 +00:00
/ * *
* Initialize toggle element using Toggles .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformToggle = {
attach : function ( context ) {
2018-11-23 12:29:20 +00:00
if ( ! $ . fn . toggles ) {
return ;
}
2017-03-16 15:29:07 +00:00
$ ( context ) . find ( '.js-webform-toggle' ) . once ( 'webform-toggle' ) . each ( function ( ) {
var $toggle = $ ( this ) ;
var $wrapper = $toggle . parent ( ) ;
var $checkbox = $wrapper . find ( 'input[type="checkbox"]' ) ;
var $label = $wrapper . find ( 'label' ) ;
2018-11-23 12:29:20 +00:00
var options = $ . extend ( {
2017-03-16 15:29:07 +00:00
checkbox : $checkbox ,
on : $checkbox . is ( ':checked' ) ,
clicker : $label ,
text : {
on : $toggle . attr ( 'data-toggle-text-on' ) || '' ,
off : $toggle . attr ( 'data-toggle-text-off' ) || ''
}
2018-11-23 12:29:20 +00:00
} , Drupal . webform . toggles . options ) ;
$toggle . toggles ( options ) ;
// Trigger change event for #states API.
// @see Drupal.states.Trigger.states.checked.change
$toggle . on ( 'toggle' , function ( ) {
$checkbox . trigger ( 'change' ) ;
2017-03-16 15:29:07 +00:00
} ) ;
// If checkbox is disabled then add the .disabled class to the toggle.
if ( $checkbox . attr ( 'disabled' ) || $checkbox . attr ( 'readonly' ) ) {
$toggle . addClass ( 'disabled' ) ;
}
// Add .clearfix to the wrapper.
2018-11-23 12:29:20 +00:00
$wrapper . addClass ( 'clearfix' ) ;
2017-03-16 15:29:07 +00:00
} ) ;
}
} ;
// Track the disabling of a toggle's checkbox using states.
2018-11-23 12:29:20 +00:00
if ( $ . fn . toggles ) {
$ ( document ) . on ( 'state:disabled' , function ( event ) {
$ ( '.js-webform-toggle' ) . each ( function ( ) {
var $toggle = $ ( this ) ;
var $wrapper = $toggle . parent ( ) ;
var $checkbox = $wrapper . find ( 'input[type="checkbox"]' ) ;
var isDisabled = ( $checkbox . attr ( 'disabled' ) || $checkbox . attr ( 'readonly' ) ) ;
$toggle [ isDisabled ? 'disabled' : 'disabled' ] ( ) ;
} ) ;
2017-03-16 15:29:07 +00:00
} ) ;
2018-11-23 12:29:20 +00:00
}
2017-03-16 15:29:07 +00:00
} ) ( jQuery , Drupal ) ;