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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
41
|
this.$helpBlock = $('<div class="help-block password-help"></div>').appendTo(this.$row);
|
42
|
|
43
|
return this;
|
44
|
};
|
45
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50
|
|
51
|
|
52
|
|
53
|
|
54
|
|
55
|
|
56
|
|
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
|
|
70
|
|
71
|
Drupal.BootstrapPassword.prototype.validateStrength = function () {
|
72
|
var result = Drupal.evaluatePasswordStrength(this.password.$input.val(), Drupal.settings.password);
|
73
|
|
74
|
|
75
|
this.$helpBlock.show();
|
76
|
this.strength.$label.show();
|
77
|
this.strength.$bar.show();
|
78
|
|
79
|
|
80
|
this.$helpBlock.html(result.message);
|
81
|
|
82
|
|
83
|
this.$helpBlock[result.strength === 100 ? 'hide' : 'show']();
|
84
|
|
85
|
|
86
|
this.strength.$label.html(result.indicatorText);
|
87
|
|
88
|
|
89
|
this.strength.$bar.attr('aria-valuenow', result.strength);
|
90
|
this.strength.$bar.css('width', result.strength + '%');
|
91
|
|
92
|
|
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
|
|
121
|
|
122
|
Drupal.BootstrapPassword.prototype.validateMatch = function () {
|
123
|
var password = this.password.$input.val();
|
124
|
|
125
|
|
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
|
|
136
|
else if (password) {
|
137
|
this.switchClass(this.confirm.$wrapper, 'error', 'has');
|
138
|
this.confirm.$icon.addClass('glyphicon-remove').removeClass('glyphicon-ok');
|
139
|
}
|
140
|
|
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
|
|
150
|
|
151
|
|
152
|
|
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);
|