|
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