Skip to content

Commit ef96224

Browse files
authored
Merge pull request #126 from Quix0r/fixes/ruleIsMet-method-cleanups
Add new method and cleanup
2 parents 35ce30f + e8c9165 commit ef96224

File tree

3 files changed

+206
-181
lines changed

3 files changed

+206
-181
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
22
node_modules
33
bower_components
4+
/nbproject/private/

locales/zh-TW.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
'wordLength': '您的密碼太短',
3-
'wordNotEmail': '不要使用電子郵件作為密碼',
4-
'wordSimilarToUsername': '您的密碼不能包含您的用戶名',
5-
'wordTwoCharacterClasses': '使用不同的字元類型 例如: 大小寫混合',
6-
'wordRepetitions': '太多的重複。例如:1111',
7-
'wordSequences': '你的密碼包含連續英/數字 例如:123 or abc',
8-
'errorList': '錯誤:',
9-
'veryWeak': '非常弱',
10-
'weak': '弱',
11-
'normal': '普通',
12-
'medium': '中等',
13-
'strong': '強',
14-
'veryStrong': '非常強',
2+
"wordLength": "您的密碼太短",
3+
"wordNotEmail": "不要使用電子郵件作為密碼",
4+
"wordSimilarToUsername": "您的密碼不能包含您的用戶名",
5+
"wordTwoCharacterClasses": "使用不同的字元類型 例如: 大小寫混合",
6+
"wordRepetitions": "太多的重複。例如:1111",
7+
"wordSequences": "你的密碼包含連續英/數字 例如:123 or abc",
8+
"errorList": "錯誤:",
9+
"veryWeak": "非常弱",
10+
"weak": "",
11+
"normal": "普通",
12+
"medium": "中等",
13+
"strong": "",
14+
"veryStrong": "非常強"
1515
}

src/methods.js

Lines changed: 192 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,192 @@
1-
/*jslint browser: true, unparam: true */
2-
/*global jQuery, ui, rulesEngine, defaultOptions, zxcvbn, console */
3-
4-
/*
5-
* jQuery Password Strength plugin for Twitter Bootstrap
6-
*
7-
* Copyright (c) 2008-2013 Tane Piper
8-
* Copyright (c) 2013 Alejandro Blanco
9-
* Dual licensed under the MIT and GPL licenses.
10-
*/
11-
12-
var methods = {};
13-
14-
(function ($, methods) {
15-
"use strict";
16-
var onKeyUp, onPaste, applyToAll;
17-
18-
onKeyUp = function (event) {
19-
var $el = $(event.target),
20-
options = $el.data("pwstrength-bootstrap"),
21-
word = $el.val(),
22-
userInputs,
23-
verdictText,
24-
verdictLevel,
25-
score;
26-
27-
if (options === undefined) { return; }
28-
29-
options.instances.errors = [];
30-
if (word.length === 0) {
31-
score = undefined;
32-
} else {
33-
if (options.common.zxcvbn) {
34-
userInputs = [];
35-
$.each(options.common.userInputs.concat([options.common.usernameField]), function (idx, selector) {
36-
var value = $(selector).val();
37-
if (value) { userInputs.push(value); }
38-
});
39-
userInputs = userInputs.concat(options.common.zxcvbnTerms);
40-
score = zxcvbn(word, userInputs).guesses;
41-
score = Math.log(score) * Math.LOG2E;
42-
} else {
43-
score = rulesEngine.executeRules(options, word);
44-
}
45-
}
46-
ui.updateUI(options, $el, score);
47-
verdictText = ui.getVerdictAndCssClass(options, score);
48-
verdictLevel = verdictText[1];
49-
verdictText = verdictText[0];
50-
51-
if (options.common.debug) { console.log(score + ' - ' + verdictText); }
52-
53-
if ($.isFunction(options.common.onKeyUp)) {
54-
options.common.onKeyUp(event, {
55-
score: score,
56-
verdictText: verdictText,
57-
verdictLevel: verdictLevel
58-
});
59-
}
60-
};
61-
62-
onPaste = function (event) {
63-
// This handler is necessary because the paste event fires before the
64-
// content is actually in the input, so we cannot read its value right
65-
// away. Therefore, the timeouts.
66-
var $el = $(event.target),
67-
word = $el.val(),
68-
tries = 0,
69-
callback;
70-
71-
callback = function () {
72-
var newWord = $el.val();
73-
74-
if (newWord !== word) {
75-
onKeyUp(event);
76-
} else if (tries < 3) {
77-
tries += 1;
78-
setTimeout(callback, 100);
79-
}
80-
};
81-
82-
setTimeout(callback, 100);
83-
};
84-
85-
methods.init = function (settings) {
86-
this.each(function (idx, el) {
87-
// Make it deep extend (first param) so it extends also the
88-
// rules and other inside objects
89-
var clonedDefaults = $.extend(true, {}, defaultOptions),
90-
localOptions = $.extend(true, clonedDefaults, settings),
91-
$el = $(el);
92-
93-
localOptions.instances = {};
94-
$el.data("pwstrength-bootstrap", localOptions);
95-
96-
$.each(localOptions.common.events, function (idx, eventName) {
97-
var handler = eventName === "paste" ? onPaste : onKeyUp;
98-
$el.on(eventName, handler);
99-
});
100-
101-
ui.initUI(localOptions, $el);
102-
$el.trigger("keyup");
103-
104-
if ($.isFunction(localOptions.common.onLoad)) {
105-
localOptions.common.onLoad();
106-
}
107-
});
108-
109-
return this;
110-
};
111-
112-
methods.destroy = function () {
113-
this.each(function (idx, el) {
114-
var $el = $(el),
115-
options = $el.data("pwstrength-bootstrap"),
116-
elements = ui.getUIElements(options, $el);
117-
elements.$progressbar.remove();
118-
elements.$verdict.remove();
119-
elements.$errors.remove();
120-
$el.removeData("pwstrength-bootstrap");
121-
});
122-
};
123-
124-
methods.forceUpdate = function () {
125-
this.each(function (idx, el) {
126-
var event = { target: el };
127-
onKeyUp(event);
128-
});
129-
};
130-
131-
methods.addRule = function (name, method, score, active) {
132-
this.each(function (idx, el) {
133-
var options = $(el).data("pwstrength-bootstrap");
134-
135-
options.rules.activated[name] = active;
136-
options.rules.scores[name] = score;
137-
options.rules.extra[name] = method;
138-
});
139-
};
140-
141-
applyToAll = function (rule, prop, value) {
142-
this.each(function (idx, el) {
143-
$(el).data("pwstrength-bootstrap").rules[prop][rule] = value;
144-
});
145-
};
146-
147-
methods.changeScore = function (rule, score) {
148-
applyToAll.call(this, rule, "scores", score);
149-
};
150-
151-
methods.ruleActive = function (rule, active) {
152-
applyToAll.call(this, rule, "activated", active);
153-
};
154-
155-
$.fn.pwstrength = function (method) {
156-
var result;
157-
158-
if (methods[method]) {
159-
result = methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
160-
} else if (typeof method === "object" || !method) {
161-
result = methods.init.apply(this, arguments);
162-
} else {
163-
$.error("Method " + method + " does not exist on jQuery.pwstrength-bootstrap");
164-
}
165-
166-
return result;
167-
};
168-
}(jQuery, methods));
1+
/*jslint browser: true, unparam: true */
2+
/*global jQuery, ui, rulesEngine, defaultOptions, zxcvbn, console */
3+
4+
/*
5+
* jQuery Password Strength plugin for Twitter Bootstrap
6+
*
7+
* Copyright (c) 2008-2013 Tane Piper
8+
* Copyright (c) 2013 Alejandro Blanco
9+
* Dual licensed under the MIT and GPL licenses.
10+
*/
11+
12+
var methods = {};
13+
14+
(function ($, methods) {
15+
"use strict";
16+
var onKeyUp, onPaste, applyToAll;
17+
18+
onKeyUp = function (event) {
19+
var $el = $(event.target),
20+
options = $el.data("pwstrength-bootstrap"),
21+
word = $el.val(),
22+
userInputs,
23+
verdictText,
24+
verdictLevel,
25+
score;
26+
27+
if (options === undefined) {
28+
return;
29+
}
30+
31+
options.instances.errors = [];
32+
if (word.length === 0) {
33+
score = undefined;
34+
} else {
35+
if (options.common.zxcvbn) {
36+
userInputs = [];
37+
$.each(options.common.userInputs.concat([options.common.usernameField]), function (idx, selector) {
38+
var value = $(selector).val();
39+
if (value) { userInputs.push(value); }
40+
});
41+
userInputs = userInputs.concat(options.common.zxcvbnTerms);
42+
score = zxcvbn(word, userInputs).guesses;
43+
score = Math.log(score) * Math.LOG2E;
44+
} else {
45+
score = rulesEngine.executeRules(options, word);
46+
}
47+
}
48+
ui.updateUI(options, $el, score);
49+
verdictText = ui.getVerdictAndCssClass(options, score);
50+
verdictLevel = verdictText[1];
51+
verdictText = verdictText[0];
52+
53+
if (options.common.debug) {
54+
console.log(score + ' - ' + verdictText);
55+
}
56+
57+
if ($.isFunction(options.common.onKeyUp)) {
58+
options.common.onKeyUp(event, {
59+
score: score,
60+
verdictText: verdictText,
61+
verdictLevel: verdictLevel
62+
});
63+
}
64+
};
65+
66+
onPaste = function (event) {
67+
// This handler is necessary because the paste event fires before the
68+
// content is actually in the input, so we cannot read its value right
69+
// away. Therefore, the timeouts.
70+
var $el = $(event.target),
71+
word = $el.val(),
72+
tries = 0,
73+
callback;
74+
75+
callback = function () {
76+
var newWord = $el.val();
77+
78+
if (newWord !== word) {
79+
onKeyUp(event);
80+
} else if (tries < 3) {
81+
tries += 1;
82+
setTimeout(callback, 100);
83+
}
84+
};
85+
86+
setTimeout(callback, 100);
87+
};
88+
89+
methods.init = function (settings) {
90+
this.each(function (idx, el) {
91+
// Make it deep extend (first param) so it extends also the
92+
// rules and other inside objects
93+
var clonedDefaults = $.extend(true, {}, defaultOptions),
94+
localOptions = $.extend(true, clonedDefaults, settings),
95+
$el = $(el);
96+
97+
localOptions.instances = {};
98+
$el.data("pwstrength-bootstrap", localOptions);
99+
100+
$.each(localOptions.common.events, function (idx, eventName) {
101+
var handler = eventName === "paste" ? onPaste : onKeyUp;
102+
$el.on(eventName, handler);
103+
});
104+
105+
ui.initUI(localOptions, $el);
106+
$el.trigger("keyup");
107+
108+
if ($.isFunction(localOptions.common.onLoad)) {
109+
localOptions.common.onLoad();
110+
}
111+
});
112+
113+
return this;
114+
};
115+
116+
methods.destroy = function () {
117+
this.each(function (idx, el) {
118+
var $el = $(el),
119+
options = $el.data("pwstrength-bootstrap"),
120+
elements = ui.getUIElements(options, $el);
121+
elements.$progressbar.remove();
122+
elements.$verdict.remove();
123+
elements.$errors.remove();
124+
$el.removeData("pwstrength-bootstrap");
125+
});
126+
};
127+
128+
methods.forceUpdate = function () {
129+
this.each(function (idx, el) {
130+
var event = { target: el };
131+
onKeyUp(event);
132+
});
133+
};
134+
135+
methods.addRule = function (name, method, score, active) {
136+
this.each(function (idx, el) {
137+
var options = $(el).data("pwstrength-bootstrap");
138+
139+
options.rules.activated[name] = active;
140+
options.rules.scores[name] = score;
141+
options.rules.extra[name] = method;
142+
});
143+
};
144+
145+
applyToAll = function (rule, prop, value) {
146+
this.each(function (idx, el) {
147+
$(el).data("pwstrength-bootstrap").rules[prop][rule] = value;
148+
});
149+
};
150+
151+
methods.changeScore = function (rule, score) {
152+
applyToAll.call(this, rule, "scores", score);
153+
};
154+
155+
methods.ruleActive = function (rule, active) {
156+
applyToAll.call(this, rule, "activated", active);
157+
};
158+
159+
methods.ruleIsMet = function (rule) {
160+
if ($.isFunction(rulesEngine.validation[rule])) {
161+
if (rule === "wordLength") {
162+
rule = "wordLengthStaticScore";
163+
}
164+
165+
var rulesMetCnt = 0;
166+
167+
this.each(function (idx, el) {
168+
var options = $(el).data("pwstrength-bootstrap");
169+
170+
rulesMetCnt += rulesEngine.validation[rule](options, $(el).val(), 1);
171+
});
172+
173+
return (rulesMetCnt === this.length);
174+
}
175+
176+
$.error("Rule " + rule + " does not exist on jQuery.pwstrength-bootstrap.validation");
177+
};
178+
179+
$.fn.pwstrength = function (method) {
180+
var result;
181+
182+
if (methods[method]) {
183+
result = methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
184+
} else if (typeof method === "object" || !method) {
185+
result = methods.init.apply(this, arguments);
186+
} else {
187+
$.error("Method " + method + " does not exist on jQuery.pwstrength-bootstrap");
188+
}
189+
190+
return result;
191+
};
192+
}(jQuery, methods));

0 commit comments

Comments
 (0)