2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for signature pad 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/szimek/signature_pad#options
Drupal . webform = Drupal . webform || { } ;
Drupal . webform . signaturePad = Drupal . webform . signaturePad || { } ;
Drupal . webform . signaturePad . options = Drupal . webform . signaturePad . options || { } ;
2017-03-16 15:29:07 +00:00
/ * *
* Initialize signature element .
*
* @ type { Drupal ~ behavior }
* /
Drupal . behaviors . webformSignature = {
attach : function ( context ) {
2018-11-23 12:29:20 +00:00
if ( ! window . SignaturePad ) {
return ;
}
2017-03-16 15:29:07 +00:00
$ ( context ) . find ( 'input.js-webform-signature' ) . once ( 'webform-signature' ) . each ( function ( ) {
var $input = $ ( this ) ;
var value = $input . val ( ) ;
var $wrapper = $input . parent ( ) ;
var $canvas = $wrapper . find ( 'canvas' ) ;
2018-11-23 12:29:20 +00:00
var $button = $wrapper . find ( ':button, :submit' ) ;
2017-03-16 15:29:07 +00:00
var canvas = $canvas [ 0 ] ;
2018-11-23 12:29:20 +00:00
var calculateDimensions = function ( ) {
$canvas . attr ( 'width' , $wrapper . width ( ) ) ;
$canvas . attr ( 'height' , $wrapper . width ( ) / 3 ) ;
} ;
2017-03-16 15:29:07 +00:00
// Set height.
$canvas . attr ( 'width' , $wrapper . width ( ) ) ;
$canvas . attr ( 'height' , $wrapper . width ( ) / 3 ) ;
$ ( window ) . resize ( function ( ) {
2018-11-23 12:29:20 +00:00
calculateDimensions ( ) ;
2017-03-16 15:29:07 +00:00
// Resizing clears the canvas so we need to reset the signature pad.
signaturePad . clear ( ) ;
var value = $input . val ( ) ;
if ( value ) {
signaturePad . fromDataURL ( value ) ;
}
} ) ;
// Initialize signature canvas.
2018-11-23 12:29:20 +00:00
var options = $ . extend ( {
onEnd : function ( ) {
2017-03-16 15:29:07 +00:00
$input . val ( signaturePad . toDataURL ( ) ) ;
}
2018-11-23 12:29:20 +00:00
} , Drupal . webform . signaturePad . options ) ;
var signaturePad = new SignaturePad ( canvas , options ) ;
2017-03-16 15:29:07 +00:00
// Set value.
if ( value ) {
signaturePad . fromDataURL ( value ) ;
}
// Set reset handler.
$button . on ( 'click' , function ( ) {
signaturePad . clear ( ) ;
2018-11-23 12:29:20 +00:00
$input . val ( '' ) ;
2017-03-16 15:29:07 +00:00
this . blur ( ) ;
return false ;
} ) ;
// Input onchange clears signature pad if value is empty.
2018-11-23 12:29:20 +00:00
// Onchange events handlers are triggered when a webform is
// hidden or shown.
2017-03-16 15:29:07 +00:00
// @see webform.states.js
2018-11-23 12:29:20 +00:00
// @see triggerEventHandlers()
2017-03-16 15:29:07 +00:00
$input . on ( 'change' , function ( ) {
if ( ! $input . val ( ) ) {
signaturePad . clear ( ) ;
}
2018-11-23 12:29:20 +00:00
setTimeout ( function ( ) {
calculateDimensions ( ) ;
} , 1 ) ;
2017-03-16 15:29:07 +00:00
} ) ;
// Turn signature pad off/on when the input is disabled/enabled.
// @see webform.states.js
$input . on ( 'webform:disabled' , function ( ) {
if ( $input . is ( ':disabled' ) ) {
signaturePad . off ( ) ;
}
else {
signaturePad . on ( ) ;
}
} ) ;
} ) ;
}
} ;
} ) ( jQuery , Drupal ) ;