Projet

Général

Profil

Paste
Télécharger (6,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / js / modules / user / user.js @ 388c412d

1
(function ($, Drupal) {
2

    
3
  Drupal.BootstrapPassword = function (element) {
4
    var self = this;
5
    var $element = $(element);
6
    this.settings = Drupal.settings.password;
7
    this.$wrapper = $element.parent().parent();
8
    this.$row = $('<div class="row">').prependTo(this.$wrapper);
9

    
10
    // The password object.
11
    this.password = {
12
      $input: $element,
13
      $label: $element.parent().find('label'),
14
      $wrapper: $element.parent().addClass('col-sm-6 col-md-4 has-feedback').appendTo(self.$row)
15
    };
16
    this.password.$icon = $('<span class="glyphicon form-control-feedback"></span>').appendTo(this.password.$wrapper);
17

    
18
    // Strength meter.
19
    this.strength = {
20
      $label: $('<div class="label" aria-live="assertive"></div>').appendTo(this.password.$label),
21
      $progress: $('<div class="progress"><div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div></div>').appendTo(this.password.$wrapper)
22
    };
23
    this.strength.$bar = this.strength.$progress.find('.progress-bar');
24

    
25
    // The confirmation object.
26
    this.confirm = {
27
      $input: this.$wrapper.find('input.password-confirm')
28
    };
29
    this.confirm.$wrapper = this.confirm.$input.parent().addClass('col-sm-6 col-md-4 has-feedback').appendTo(self.$row);
30
    this.confirm.$icon = $('<span class="glyphicon form-control-feedback"></span>').appendTo(this.confirm.$wrapper);
31

    
32
    // Bind events.
33
    this.password.$input.on('keyup focus blur', function () {
34
      self.validateStrength();
35
    });
36
    this.confirm.$input.on('keyup blur', function () {
37
      self.validateMatch();
38
    });
39

    
40
    // Add password help at the of row.
41
    this.$helpBlock = $('<div class="help-block password-help"></div>').appendTo(this.$row);
42

    
43
    return this;
44
  };
45

    
46
  /**
47
   * Helper method to switch classes on elements based on status.
48
   *
49
   * @param {jQuery} $element
50
   *   The jQuery element to modify.
51
   * @param {string} type
52
   *   The name of the class to switch to. Can be one of: "danger", "info",
53
   *   "success" or "warning".
54
   * @param {string} prefix
55
   *   The prefix to use. Typically this would be something like "label" or
56
   *   "progress-bar".
57
   */
58
  Drupal.BootstrapPassword.prototype.switchClass = function ($element, type, prefix) {
59
    prefix = prefix + '-' || '';
60
    var types = prefix === 'has-' ? ['error', 'warning', 'success'] : ['danger', 'info', 'success', 'warning'];
61
    if (type) {
62
      type = types.splice($.inArray(type, types), 1).shift();
63
      $element.addClass(prefix + type);
64
    }
65
    $element.removeClass(prefix + types.join(' ' + prefix));
66
  };
67

    
68
  /**
69
   * Validates the strength of a password.
70
   */
71
  Drupal.BootstrapPassword.prototype.validateStrength = function () {
72
    var result = Drupal.evaluatePasswordStrength(this.password.$input.val(), Drupal.settings.password);
73

    
74
    // Ensure visibility.
75
    this.$helpBlock.show();
76
    this.strength.$label.show();
77
    this.strength.$bar.show();
78

    
79
    // Update the suggestions for how to improve the password.
80
    this.$helpBlock.html(result.message);
81

    
82
    // Only show the description box if there is a weakness in the password.
83
    this.$helpBlock[result.strength === 100 ? 'hide' : 'show']();
84

    
85
    // Update the strength indication text.
86
    this.strength.$label.html(result.indicatorText);
87

    
88
    // Adjust the length of the strength indicator.
89
    this.strength.$bar.attr('aria-valuenow', result.strength);
90
    this.strength.$bar.css('width', result.strength + '%');
91

    
92
    // Change the classes (color) of the strength meter based on result level.
93
    switch (result.indicatorText) {
94
      case this.settings.weak:
95
        this.switchClass(this.password.$wrapper, 'error', 'has');
96
        this.switchClass(this.strength.$label, 'danger', 'label');
97
        this.switchClass(this.strength.$bar, 'danger', 'progress-bar');
98
        this.password.$icon.addClass('glyphicon-remove').removeClass('glyphicon-warning-sign glyphicon-ok');
99
        break;
100

    
101
      case this.settings.fair:
102
      case this.settings.good:
103
        this.switchClass(this.password.$wrapper, 'warning', 'has');
104
        this.switchClass(this.strength.$label, 'warning', 'label');
105
        this.switchClass(this.strength.$bar, 'warning', 'progress-bar');
106
        this.password.$icon.addClass('glyphicon-warning-sign').removeClass('glyphicon-remove glyphicon-ok');
107
        break;
108

    
109
      case this.settings.strong:
110
        this.switchClass(this.password.$wrapper, 'success', 'has');
111
        this.switchClass(this.strength.$label, 'success', 'label');
112
        this.switchClass(this.strength.$bar, 'success', 'progress-bar');
113
        this.password.$icon.addClass('glyphicon-ok').removeClass('glyphicon-warning-sign glyphicon-remove');
114
        break;
115
    }
116
    this.validateMatch();
117
  };
118

    
119
  /**
120
   * Validates both original and confirmation passwords to ensure they match.
121
   */
122
  Drupal.BootstrapPassword.prototype.validateMatch = function () {
123
    var password = this.password.$input.val();
124

    
125
    // Passwords match.
126
    if (password && password === this.confirm.$input.val()) {
127
      this.switchClass(this.password.$wrapper, 'success', 'has');
128
      this.switchClass(this.confirm.$wrapper, 'success', 'has');
129
      this.$helpBlock.hide();
130
      this.strength.$label.hide();
131
      this.strength.$bar.hide();
132
      this.password.$icon.addClass('glyphicon-ok').removeClass('glyphicon-warning-sign glyphicon-remove');
133
      this.confirm.$icon.addClass('glyphicon-ok').removeClass('glyphicon-remove');
134
    }
135
    // Passwords do not match.
136
    else if (password) {
137
      this.switchClass(this.confirm.$wrapper, 'error', 'has');
138
      this.confirm.$icon.addClass('glyphicon-remove').removeClass('glyphicon-ok');
139
    }
140
    // No password.
141
    else {
142
      this.confirm.$icon.removeClass('glyphicon-ok glyphicon-remove');
143
      this.confirm.$input.val('');
144
      this.switchClass(this.confirm.$wrapper, '', 'has');
145
    }
146
  };
147

    
148
  /**
149
   * Overrides core JS for password strength and confirmation.
150
   *
151
   * Attach handlers to evaluate the strength of any password fields and to check
152
   * that its confirmation is correct.
153
   */
154
  Drupal.behaviors.password = {
155
    attach: function (context) {
156
      $('input.password-field', context).once('password', function () {
157
        new Drupal.BootstrapPassword(this);
158
      });
159
    }
160
  };
161

    
162
})(jQuery, window.Drupal);