2015-08-17 17:00:26 -07:00
/ * *
2018-11-23 12:29:20 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
* https : //www.drupal.org/node/2815083
* @ preserve
* * /
2015-08-17 17:00:26 -07:00
( function ( $ , Drupal , drupalSettings ) {
Drupal . behaviors . password = {
2018-11-23 12:29:20 +00:00
attach : function attach ( context , settings ) {
2015-08-17 17:00:26 -07:00
var $passwordInput = $ ( context ) . find ( 'input.js-password-field' ) . once ( 'password' ) ;
if ( $passwordInput . length ) {
var translate = settings . password ;
var $passwordInputParent = $passwordInput . parent ( ) ;
var $passwordInputParentWrapper = $passwordInputParent . parent ( ) ;
2018-11-23 12:29:20 +00:00
var $passwordSuggestions = void 0 ;
2015-08-17 17:00:26 -07:00
$passwordInputParent . addClass ( 'password-parent' ) ;
2018-11-23 12:29:20 +00:00
$passwordInputParentWrapper . find ( 'input.js-password-confirm' ) . parent ( ) . append ( '<div aria-live="polite" aria-atomic="true" class="password-confirm js-password-confirm">' + translate . confirmTitle + ' <span></span></div>' ) . addClass ( 'confirm-parent' ) ;
2015-08-17 17:00:26 -07:00
var $confirmInput = $passwordInputParentWrapper . find ( 'input.js-password-confirm' ) ;
var $confirmResult = $passwordInputParentWrapper . find ( 'div.js-password-confirm' ) ;
var $confirmChild = $confirmResult . find ( 'span' ) ;
if ( settings . password . showStrengthIndicator ) {
2015-09-04 13:20:09 -07:00
var passwordMeter = '<div class="password-strength"><div class="password-strength__meter"><div class="password-strength__indicator js-password-strength__indicator"></div></div><div aria-live="polite" aria-atomic="true" class="password-strength__title">' + translate . strengthTitle + ' <span class="password-strength__text js-password-strength__text"></span></div></div>' ;
2015-08-17 17:00:26 -07:00
$confirmInput . parent ( ) . after ( '<div class="password-suggestions description"></div>' ) ;
$passwordInputParent . append ( passwordMeter ) ;
$passwordSuggestions = $passwordInputParentWrapper . find ( 'div.password-suggestions' ) . hide ( ) ;
}
2018-11-23 12:29:20 +00:00
var passwordCheckMatch = function passwordCheckMatch ( confirmInputVal ) {
2015-08-17 17:00:26 -07:00
var success = $passwordInput . val ( ) === confirmInputVal ;
var confirmClass = success ? 'ok' : 'error' ;
2018-11-23 12:29:20 +00:00
$confirmChild . html ( translate [ 'confirm' + ( success ? 'Success' : 'Failure' ) ] ) . removeClass ( 'ok error' ) . addClass ( confirmClass ) ;
2015-08-17 17:00:26 -07:00
} ;
2018-11-23 12:29:20 +00:00
var passwordCheck = function passwordCheck ( ) {
2015-08-17 17:00:26 -07:00
if ( settings . password . showStrengthIndicator ) {
var result = Drupal . evaluatePasswordStrength ( $passwordInput . val ( ) , settings . password ) ;
if ( $passwordSuggestions . html ( ) !== result . message ) {
$passwordSuggestions . html ( result . message ) ;
}
$passwordSuggestions . toggle ( result . strength !== 100 ) ;
2018-11-23 12:29:20 +00:00
$passwordInputParent . find ( '.js-password-strength__indicator' ) . css ( 'width' , result . strength + '%' ) . removeClass ( 'is-weak is-fair is-good is-strong' ) . addClass ( result . indicatorClass ) ;
2015-08-17 17:00:26 -07:00
$passwordInputParent . find ( '.js-password-strength__text' ) . html ( result . indicatorText ) ;
}
if ( $confirmInput . val ( ) ) {
passwordCheckMatch ( $confirmInput . val ( ) ) ;
2018-11-23 12:29:20 +00:00
$confirmResult . css ( { visibility : 'visible' } ) ;
} else {
$confirmResult . css ( { visibility : 'hidden' } ) ;
2015-08-17 17:00:26 -07:00
}
} ;
$passwordInput . on ( 'input' , passwordCheck ) ;
$confirmInput . on ( 'input' , passwordCheck ) ;
}
}
} ;
Drupal . evaluatePasswordStrength = function ( password , translate ) {
password = password . trim ( ) ;
2018-11-23 12:29:20 +00:00
var indicatorText = void 0 ;
var indicatorClass = void 0 ;
2015-08-17 17:00:26 -07:00
var weaknesses = 0 ;
var strength = 100 ;
var msg = [ ] ;
var hasLowercase = /[a-z]/ . test ( password ) ;
var hasUppercase = /[A-Z]/ . test ( password ) ;
var hasNumbers = /[0-9]/ . test ( password ) ;
var hasPunctuation = /[^a-zA-Z0-9]/ . test ( password ) ;
var $usernameBox = $ ( 'input.username' ) ;
2018-11-23 12:29:20 +00:00
var username = $usernameBox . length > 0 ? $usernameBox . val ( ) : translate . username ;
2015-08-17 17:00:26 -07:00
2015-10-08 11:40:12 -07:00
if ( password . length < 12 ) {
2015-08-17 17:00:26 -07:00
msg . push ( translate . tooShort ) ;
2018-11-23 12:29:20 +00:00
strength -= ( 12 - password . length ) * 5 + 30 ;
2015-08-17 17:00:26 -07:00
}
if ( ! hasLowercase ) {
msg . push ( translate . addLowerCase ) ;
weaknesses ++ ;
}
if ( ! hasUppercase ) {
msg . push ( translate . addUpperCase ) ;
weaknesses ++ ;
}
if ( ! hasNumbers ) {
msg . push ( translate . addNumbers ) ;
weaknesses ++ ;
}
if ( ! hasPunctuation ) {
msg . push ( translate . addPunctuation ) ;
weaknesses ++ ;
}
switch ( weaknesses ) {
case 1 :
strength -= 12.5 ;
break ;
case 2 :
strength -= 25 ;
break ;
case 3 :
strength -= 40 ;
break ;
case 4 :
strength -= 40 ;
break ;
}
if ( password !== '' && password . toLowerCase ( ) === username . toLowerCase ( ) ) {
msg . push ( translate . sameAsUsername ) ;
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
strength = 5 ;
}
if ( strength < 60 ) {
indicatorText = translate . weak ;
indicatorClass = 'is-weak' ;
2018-11-23 12:29:20 +00:00
} else if ( strength < 70 ) {
2015-08-17 17:00:26 -07:00
indicatorText = translate . fair ;
indicatorClass = 'is-fair' ;
2018-11-23 12:29:20 +00:00
} else if ( strength < 80 ) {
2015-08-17 17:00:26 -07:00
indicatorText = translate . good ;
indicatorClass = 'is-good' ;
2018-11-23 12:29:20 +00:00
} else if ( strength <= 100 ) {
2015-08-17 17:00:26 -07:00
indicatorText = translate . strong ;
indicatorClass = 'is-strong' ;
}
msg = translate . hasWeaknesses + '<ul><li>' + msg . join ( '</li><li>' ) + '</li></ul>' ;
return {
strength : strength ,
message : msg ,
indicatorText : indicatorText ,
indicatorClass : indicatorClass
} ;
} ;
2018-11-23 12:29:20 +00:00
} ) ( jQuery , Drupal , drupalSettings ) ;