Skip to content

Commit e8c9165

Browse files
author
Roland Häder
committed
Some changes:
- changed back to foo === undefined (requested by reviewer). - unneccessary else-block removed
1 parent 93e6b6e commit e8c9165

File tree

1 file changed

+192
-192
lines changed

1 file changed

+192
-192
lines changed

src/methods.js

Lines changed: 192 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -1,192 +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 (typeof(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-
} else {
175-
$.error("Rule " + rule + " does not exist on jQuery.pwstrength-bootstrap.validation");
176-
}
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));
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)