2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for details element .
2017-03-16 15:29:07 +00:00
* /
( function ( $ , Drupal ) {
'use strict' ;
/ * *
* Attach handler to save details open / close state .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformDetailsSave = {
attach : function ( context ) {
if ( ! window . localStorage ) {
return ;
}
// Summary click event handler.
$ ( 'details > summary' , context ) . once ( 'webform-details-summary-save' ) . click ( function ( ) {
var $details = $ ( this ) . parent ( ) ;
2018-11-23 12:29:20 +00:00
// @see https://css-tricks.com/snippets/jquery/make-an-jquery-hasattr/
if ( $details [ 0 ] . hasAttribute ( 'data-webform-details-nosave' ) ) {
return ;
}
2017-03-16 15:29:07 +00:00
var name = Drupal . webformDetailsSaveGetName ( $details ) ;
if ( ! name ) {
return ;
}
2018-11-23 12:29:20 +00:00
var open = ( $details . attr ( 'open' ) !== 'open' ) ? '1' : '0' ;
2017-03-16 15:29:07 +00:00
localStorage . setItem ( name , open ) ;
} ) ;
// Initialize details open state via local storage.
$ ( 'details' , context ) . once ( 'webform-details-save' ) . each ( function ( ) {
var $details = $ ( this ) ;
var name = Drupal . webformDetailsSaveGetName ( $details ) ;
if ( ! name ) {
return ;
}
var open = localStorage . getItem ( name ) ;
if ( open === null ) {
return ;
}
2018-11-23 12:29:20 +00:00
if ( open === '1' ) {
2017-03-16 15:29:07 +00:00
$details . attr ( 'open' , 'open' ) ;
}
else {
$details . removeAttr ( 'open' ) ;
}
} ) ;
}
} ;
/ * *
* Get the name used to store the state of details element .
*
2018-11-23 12:29:20 +00:00
* @ param { jQuery } $details
2017-03-16 15:29:07 +00:00
* A details element .
*
2018-11-23 12:29:20 +00:00
* @ return { string }
2017-03-16 15:29:07 +00:00
* The name used to store the state of details element .
* /
Drupal . webformDetailsSaveGetName = function ( $details ) {
if ( ! window . localStorage ) {
return '' ;
}
// Any details element not included a webform must have define its own id.
var webformId = $details . attr ( 'data-webform-element-id' ) ;
if ( webformId ) {
return 'Drupal.webform.' + webformId . replace ( '--' , '.' ) ;
}
var detailsId = $details . attr ( 'id' ) ;
if ( ! detailsId ) {
return '' ;
}
var $form = $details . parents ( 'form' ) ;
if ( ! $form . length || ! $form . attr ( 'id' ) ) {
return '' ;
}
var formId = $form . attr ( 'id' ) ;
if ( ! formId ) {
return '' ;
}
2018-11-23 12:29:20 +00:00
// ISSUE: When Drupal renders a webform in a modal dialog it appends a unique
// identifier to webform ids and details ids. (i.e. my-form--FeSFISegTUI)
2017-03-16 15:29:07 +00:00
// WORKAROUND: Remove the unique id that delimited using double dashes.
formId = formId . replace ( /--.+?$/ , '' ) . replace ( /-/g , '_' ) ;
detailsId = detailsId . replace ( /--.+?$/ , '' ) . replace ( /-/g , '_' ) ;
return 'Drupal.webform.' + formId + '.' + detailsId ;
2018-11-23 12:29:20 +00:00
} ;
2017-03-16 15:29:07 +00:00
} ) ( jQuery , Drupal ) ;