2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for jQuery Text Counter 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/ractoon/jQuery-Text-Counter#options
Drupal . webform = Drupal . webform || { } ;
Drupal . webform . counter = Drupal . webform . counter || { } ;
Drupal . webform . counter . options = Drupal . webform . counter . options || { } ;
2017-03-16 15:29:07 +00:00
/ * *
* Initialize text field and textarea word and character counter .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformCounter = {
attach : function ( context ) {
2018-11-23 12:29:20 +00:00
if ( ! $ . fn . textcounter ) {
return ;
}
2017-03-16 15:29:07 +00:00
$ ( context ) . find ( '.js-webform-counter' ) . once ( 'webform-counter' ) . each ( function ( ) {
var options = {
2018-11-23 12:29:20 +00:00
type : $ ( this ) . data ( 'counter-type' ) ,
max : $ ( this ) . data ( 'counter-maximum' ) ,
min : $ ( this ) . data ( 'counter-minimum' ) || 0 ,
counterText : $ ( this ) . data ( 'counter-minimum-message' ) ,
countDownText : $ ( this ) . data ( 'counter-maximum-message' ) ,
inputErrorClass : 'webform-counter-warning' ,
counterErrorClass : 'webform-counter-warning' ,
countSpaces : true ,
stopInputAtMaximum : false ,
// Don't display min/max message since server-side validation will
// display these messages.
minimumErrorText : '' ,
maximumErrorText : ''
2017-03-16 15:29:07 +00:00
} ;
2018-11-23 12:29:20 +00:00
options . countDown = ( options . max ) ? true : false ;
if ( ! options . counterText ) {
options . counterText = ( options . type === 'word' ) ? Drupal . t ( '%d word(s) entered' ) : Drupal . t ( '%d characters(s) entered' ) ;
}
if ( ! options . countDownText ) {
options . countDownText = ( options . type === 'word' ) ? Drupal . t ( '%d word(s) remaining' ) : Drupal . t ( '%d characters(s) remaining' ) ;
2017-03-16 15:29:07 +00:00
}
2018-11-23 12:29:20 +00:00
options = $ . extend ( options , Drupal . webform . counter . options ) ;
2017-03-16 15:29:07 +00:00
2018-11-23 12:29:20 +00:00
$ ( this ) . textcounter ( options ) ;
} ) ;
2017-03-16 15:29:07 +00:00
}
} ;
} ) ( jQuery , Drupal ) ;