2017-03-16 15:29:07 +00:00
/ * *
* @ file
2018-11-23 12:29:20 +00:00
* JavaScript behaviors for unsaved webforms .
2017-03-16 15:29:07 +00:00
* /
( function ( $ , Drupal ) {
'use strict' ;
var unsaved = false ;
/ * *
* Unsaved changes .
*
* @ type { Drupal ~ behavior }
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches the behavior for unsaved changes .
* /
Drupal . behaviors . webformUnsaved = {
attach : function ( context ) {
// Look for the 'data-webform-unsaved' attribute which indicates that the
// multi-step webform has unsaved data.
// @see \Drupal\webform\WebformSubmissionForm::buildForm
if ( $ ( '.js-webform-unsaved[data-webform-unsaved]' ) . length ) {
unsaved = true ;
}
else {
2018-11-23 12:29:20 +00:00
$ ( '.js-webform-unsaved :input:not(:button, :submit, :reset)' , context ) . once ( 'webform-unsaved' ) . on ( 'change keypress' , function ( event , param1 ) {
// Ignore events triggered when #states API is changed,
// which passes 'webform.states' as param1.
// @see webform.states.js ::triggerEventHandlers().
if ( param1 !== 'webform.states' ) {
unsaved = true ;
}
2017-03-16 15:29:07 +00:00
} ) ;
}
2018-11-23 12:29:20 +00:00
$ ( '.js-webform-unsaved button, .js-webform-unsaved input[type="submit"]' , context ) . once ( 'webform-unsaved' ) . on ( 'click' , function ( event ) {
// For reset button we must confirm unsaved changes before the
// before unload event handler.
if ( $ ( this ) . hasClass ( 'webform-button--reset' ) && unsaved ) {
if ( ! window . confirm ( Drupal . t ( 'Changes you made may not be saved.' ) + '\n\n' + Drupal . t ( 'Press OK to leave this page or Cancel to stay.' ) ) ) {
return false ;
}
}
2017-03-16 15:29:07 +00:00
unsaved = false ;
} ) ;
2018-11-23 12:29:20 +00:00
// Track all CKEditor change events.
// @see https://ckeditor.com/old/forums/Support/CKEditor-jQuery-change-event
if ( window . CKEDITOR && ! CKEDITOR . webformUnsaved ) {
CKEDITOR . webformUnsaved = true ;
CKEDITOR . on ( 'instanceCreated' , function ( event ) {
event . editor . on ( 'change' , function ( evt ) {
unsaved = true ;
} ) ;
} ) ;
}
2017-03-16 15:29:07 +00:00
}
} ;
$ ( window ) . on ( 'beforeunload' , function ( ) {
if ( unsaved ) {
return true ;
}
} ) ;
2018-11-23 12:29:20 +00:00
/ * *
2017-03-16 15:29:07 +00:00
* An experimental shim to partially emulate onBeforeUnload on iOS .
* Part of https : //github.com/codedance/jquery.AreYouSure/
*
* Copyright ( c ) 2012 - 2014 , Chris Dance and PaperCut Software http : //www.papercut.com/
* Dual licensed under the MIT or GPL Version 2 licenses .
* http : //jquery.org/license
*
* Author : chris . dance @ papercut . com
* Date : 19 th May 2014
* /
$ ( function ( ) {
if ( ! navigator . userAgent . toLowerCase ( ) . match ( /iphone|ipad|ipod|opera/ ) ) {
return ;
}
$ ( 'a' ) . bind ( 'click' , function ( evt ) {
var href = $ ( evt . target ) . closest ( 'a' ) . attr ( 'href' ) ;
2018-11-23 12:29:20 +00:00
if ( typeof href !== 'undefined' && ! ( href . match ( /^#/ ) || href . trim ( ) === '' ) ) {
2017-03-16 15:29:07 +00:00
if ( $ ( window ) . triggerHandler ( 'beforeunload' ) ) {
2018-11-23 12:29:20 +00:00
if ( ! window . confirm ( Drupal . t ( 'Changes you made may not be saved.' ) + '\n\n' + Drupal . t ( 'Press OK to leave this page or Cancel to stay.' ) ) ) {
2017-03-16 15:29:07 +00:00
return false ;
}
}
window . location . href = href ;
return false ;
}
} ) ;
} ) ;
} ) ( jQuery , Drupal ) ;