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