2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for time 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/jonthornton/jquery-timepicker#options
Drupal . webform = Drupal . webform || { } ;
Drupal . webform . timePicker = Drupal . webform . timePicker || { } ;
Drupal . webform . timePicker . options = Drupal . webform . timePicker . options || { } ;
2017-03-16 15:29:07 +00:00
/ * *
* Attach timepicker fallback on time elements .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior to time elements .
* /
Drupal . behaviors . webformTime = {
attach : function ( context , settings ) {
2018-11-23 12:29:20 +00:00
if ( ! $ . fn . timepicker ) {
2017-03-16 15:29:07 +00:00
return ;
}
2018-11-23 12:29:20 +00:00
$ ( context ) . find ( 'input[data-webform-time-format]' ) . once ( 'webformTimePicker' ) . each ( function ( ) {
2017-03-16 15:29:07 +00:00
var $input = $ ( this ) ;
2018-11-23 12:29:20 +00:00
// Skip if time inputs are supported by the browser and input is not a text field.
// @see \Drupal\webform\Element\WebformDatetime
if ( window . Modernizr && Modernizr . inputtypes . time === true && $input . attr ( 'type' ) !== 'text' ) {
return ;
2017-03-16 15:29:07 +00:00
}
2018-11-23 12:29:20 +00:00
var options = { } ;
options . timeFormat = $input . data ( 'webformTimeFormat' ) ;
2017-03-16 15:29:07 +00:00
if ( $input . attr ( 'min' ) ) {
options . minTime = $input . attr ( 'min' ) ;
}
if ( $input . attr ( 'max' ) ) {
options . maxTime = $input . attr ( 'max' ) ;
}
// HTML5 time element steps is in seconds but for the timepicker
// fallback it needs to be in minutes.
// Note: The 'datetime' element uses the #date_increment which defaults
// to 1 (second).
// @see \Drupal\Core\Datetime\Element\Datetime::processDatetime
// Only use step if it is greater than 60 seconds.
if ( $input . attr ( 'step' ) && ( $input . attr ( 'step' ) > 60 ) ) {
options . step = Math . round ( $input . attr ( 'step' ) / 60 ) ;
}
else {
options . step = 1 ;
}
2018-11-23 12:29:20 +00:00
options = $ . extend ( options , Drupal . webform . timePicker . options ) ;
2017-03-16 15:29:07 +00:00
$input . timepicker ( options ) ;
} ) ;
}
2018-11-23 12:29:20 +00:00
} ;
2017-03-16 15:29:07 +00:00
} ) ( jQuery , Drupal ) ;