claro-8.x-1.x-dev/js/user.es6.js

js/user.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * @file
 * Overrides Drupal core user.js that provides password strength indicator.
 *
 * @todo remove these overrides after
 */
 
(($, Drupal) => {
  /**
   * This overrides the default Drupal.behaviors.password functionality.
   *
   * - Markup has been moved to theme functions so that to enable customizations
   *   needed for matching Claro's design requirements
   * - Modified classes so that same class names are not being used for different
   */
  Drupal.behaviors.password = {
    attach(context, settings) {
      const $passwordInput = $(context)
        .find("input.js-password-field")
        .once("password");
 
      if ($passwordInput.length) {
        // Settings and translated messages added by
        // user_form_process_password_confirm().
        const translate = settings.password;
 
        // The form element object of the password input.
        const $passwordInputParent = $passwordInput.parent();
 
        // The password_confirm form element object.
        const $passwordWidget = $passwordInput.closest(
          ".js-form-type-password-confirm"
        );
 
        // The password confirm input.
        const $passwordConfirmInput = $passwordWidget.find(
          "input.js-password-confirm"
        );
 
        // The strength feedback element for the password input.
        const $passwordInputHelp = $(
          Drupal.theme.passwordInputHelp(translate.strengthTitle)
        );
 
        // The password match feedback for the password confirm input.
        const $passwordConfirmHelp = $(
          Drupal.theme.passwordConfirmHelp(translate.confirmTitle)
        );
 
        const $passwordInputStrengthBar = $passwordInputHelp.find(
          ".js-password-strength-bar"
        );
        const $passwordInputStrengthMessageWrapper = $passwordInputHelp.find(
          ".js-password-strength-text"
        );
        const $passwordConfirmMatch = $passwordConfirmHelp.find(
          ".js-password-match-text"
        );
        let $passwordSuggestionsTips = $(
          Drupal.theme.passwordSuggestionsTips("", "")
        ).hide();
 
        // If the password strength indicator is enabled, add its markup.
        if (settings.password.showStrengthIndicator) {
          $passwordConfirmInput
            .after($passwordConfirmHelp)
            .parent()
            .after($passwordSuggestionsTips);
 
          $passwordInputParent.append($passwordInputHelp);
        }
 
        // Check that password and confirmation inputs match.
        const passwordCheckMatch = confirmInputVal => {
          if (confirmInputVal) {
            const success = $passwordInput.val() === confirmInputVal;
            const confirmClass = success ? "ok" : "error";
            const confirmMatchMessage = success
              ? translate.confirmSuccess
              : translate.confirmFailure;
 
            // Update the success message and set the class accordingly if
            // needed.
            if (
              !$passwordConfirmMatch.hasClass(confirmClass) ||
              !$passwordConfirmMatch.html() === confirmMatchMessage
            ) {
              $passwordConfirmMatch
                .html(confirmMatchMessage)
                .removeClass("ok error")
                .addClass(confirmClass);
            }
          }
        };
 
        // Check the password strength.
        const passwordCheck = () => {
          if (settings.password.showStrengthIndicator) {
            // Evaluate the password strength.
            const result = Drupal.evaluatePasswordStrength(
              $passwordInput.val(),
              settings.password
            );
            const $newSuggestions = $(
              Drupal.theme.passwordSuggestionsTips(
                translate.hasWeaknesses,
                result.tips
              )
            );
 
            // Update the suggestions for how to improve the password.
            if ($newSuggestions.html() !== $passwordSuggestionsTips.html()) {
              $passwordSuggestionsTips.replaceWith($newSuggestions);
              $passwordSuggestionsTips = $newSuggestions;
 
              // Only show the description box if a weakness exists in the
              // password.
              $passwordSuggestionsTips.toggle(result.strength !== 100);
            }
 
            // Adjust the length of the strength indicator.
            $passwordInputStrengthBar
              .css("width", `${result.strength}%`)
              .removeClass("is-weak is-fair is-good is-strong")
              .addClass(result.indicatorClass);
 
            // Update the strength indication text if needed.
            if (
              !$passwordInputStrengthMessageWrapper.hasClass(
                result.indicatorClass
              ) ||
              !$passwordInputStrengthMessageWrapper.html() ===
                result.indicatorText
            ) {
              $passwordInputStrengthMessageWrapper
                .html(result.indicatorText)
                .removeClass("is-weak is-fair is-good is-strong")
                .addClass(result.indicatorClass);
            }
          }
 
          $passwordWidget
            .removeClass("is-initial")
            .removeClass("is-password-empty is-password-filled")
            .removeClass("is-confirm-empty is-confirm-filled");
 
          // Check the value of the password input and add the proper classes.
          $passwordWidget.addClass(
            $passwordInput.val() ? "is-password-filled" : "is-password-empty"
          );
 
          // Check the value in the confirm input and show results.
          passwordCheckMatch($passwordConfirmInput.val());
          $passwordWidget.addClass(
            $passwordConfirmInput.val()
              ? "is-confirm-filled"
              : "is-confirm-empty"
          );
        };
 
        // Add initial classes.
        $passwordWidget
          .addClass(
            $passwordInput.val() ? "is-password-filled" : "is-password-empty"
          )
          .addClass(
            $passwordConfirmInput.val()
              ? "is-confirm-filled"
              : "is-confirm-empty"
          );
 
        // Monitor input events.
        $passwordInput.on("input", passwordCheck);
        $passwordConfirmInput.on("input", passwordCheck);
      }
    }
  };
 
  /**
   * Override the default Drupal.evaluatePasswordStrength.
   *
   * The default implementation of this function hard codes some markup inside
   * this function. Rendering markup is now handled by
   * Drupal.behaviors.password.
   *
   * @param {string} password
   *   Password to evaluate the strength.
   *
   * @param {Array.<string>} translate
   *   Settings and translated messages added by user_form_process_password_confirm().
   *
   * @return {Array.<string>}
   *   Array containing the strength, tips, indicators text and class.
   */
  Drupal.evaluatePasswordStrength = (password, translate) => {
    password = password.trim();
    let indicatorText;
    let indicatorClass;
    let weaknesses = 0;
    let strength = 100;
    const tips = [];
    const hasLowercase = /[a-z]/.test(password);
    const hasUppercase = /[A-Z]/.test(password);
    const hasNumbers = /[0-9]/.test(password);
    const hasPunctuation = /[^a-zA-Z0-9]/.test(password);
 
    // If there is a username edit box on the page, compare password to that,
    // otherwise use value from the database.
    const $usernameBox = $("input.username");
    const username =
      $usernameBox.length > 0 ? $usernameBox.val() : translate.username;
 
    // Lose 5 points for every character less than 12, plus a 30 point penalty.
    if (password.length < 12) {
      tips.push(translate.tooShort);
      strength -= (12 - password.length) * 5 + 30;
    }
 
    // Count weaknesses.
    if (!hasLowercase) {
      tips.push(translate.addLowerCase);
      weaknesses += 1;
    }
    if (!hasUppercase) {
      tips.push(translate.addUpperCase);
      weaknesses += 1;
    }
    if (!hasNumbers) {
      tips.push(translate.addNumbers);
      weaknesses += 1;
    }
    if (!hasPunctuation) {
      tips.push(translate.addPunctuation);
      weaknesses += 1;
    }
 
    // Apply penalty for each weakness (balanced against length penalty).
    switch (weaknesses) {
      case 1:
        strength -= 12.5;
        break;
 
      case 2:
        strength -= 25;
        break;
 
      case 3:
        strength -= 40;
        break;
 
      case 4:
        strength -= 40;
        break;
 
      default:
        // Default: 0. Nothing to do.
        break;
    }
 
    // Check if password is the same as the username.
    if (password !== "" && password.toLowerCase() === username.toLowerCase()) {
      tips.push(translate.sameAsUsername);
      // Passwords the same as username are always very weak.
      strength = 5;
    }
 
    // Based on the strength, work out what text should be shown by the
    // password strength meter.
    if (strength < 60) {
      indicatorText = translate.weak;
      indicatorClass = "is-weak";
    } else if (strength < 70) {
      indicatorText = translate.fair;
      indicatorClass = "is-fair";
    } else if (strength < 80) {
      indicatorText = translate.good;
      indicatorClass = "is-good";
    } else if (strength <= 100) {
      indicatorText = translate.strong;
      indicatorClass = "is-strong";
    }
 
    return {
      strength,
      tips,
      indicatorText,
      indicatorClass
    };
  };
 
  /**
   * Password strenght feedback for password confirm's main input.
   *
   * @param {string} message
   *   The prefix text for the strength feedback word.
   *
   * @return {string}
   *   The string representing the DOM fragment.
   */
  Drupal.theme.passwordInputHelp = message =>
    `<div class="password-strength">
      <div class="password-strength__track">
        <div class="password-strength__bar js-password-strength-bar"></div>
      </div>
      <div aria-live="polite" aria-atomic="true" class="password-strength__title">
        ${message} <span class="password-strength__text js-password-strength-text"></span>
      </div>
    </div>`;
 
  /**
   * Password match feedback for password confirm input.
   *
   * @param {string} message
   *   The message that precedes the yes|no text.
   *
   * @return {string}
   *   A string representing the DOM fragment.
   */
  Drupal.theme.passwordConfirmHelp = message =>
    `<div aria-live="polite" aria-atomic="true" class="password-match-message">${message} <span class="password-match-message__text js-password-match-text"></span></div>`;
 
  /**
   * Password suggestions tips.
   *
   * @param {string} title
   *   The title that precedes tips.
   * @param {Array.<string>} tips
   *   Array containing the tips.
   *
   * @return {string}
   *   A string representing the DOM fragment.
   */
  Drupal.theme.passwordSuggestionsTips = (title, tips) =>
    `<div class="password-suggestions">${
      tips.length
        ? `${title}<ul class="password-suggestions__tips"><li class="password-suggestions__tip">${tips.join(
            '</li><li class="password-suggestions__tip">'
          )}</li></ul>`
        : ""
    }</div>`;
})(jQuery, Drupal);

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc