|
1 (function($){ |
|
2 var password = 'Gosh, WordPress is grand.', |
|
3 $input = $('#pass'), |
|
4 shouldAnimate = true, |
|
5 timesForAnimation = [280, 300, 305, 310, 315, 325, 330, 345, 360, 370, 380, 400, 450, 500, 600], |
|
6 resultsCache = {}, |
|
7 indicatorString = $('#pass-strength-result').text(); |
|
8 |
|
9 function updateResult(){ |
|
10 var strength; |
|
11 |
|
12 if ( typeof( resultsCache[ $input.val() ]) === 'undefined') { |
|
13 strength = wp.passwordStrength.meter($input.val(), [], $input.val()); |
|
14 resultsCache[ $input.val() ] = strength; |
|
15 } else { |
|
16 strength = resultsCache[ $input.val() ]; |
|
17 } |
|
18 |
|
19 $('#pass-strength-result').removeClass('short bad good strong'); |
|
20 switch ( strength ) { |
|
21 case 2: |
|
22 $('#pass-strength-result').addClass('bad').html( pwsL10n['bad'] ); |
|
23 break; |
|
24 case 3: |
|
25 $('#pass-strength-result').addClass('good').html( pwsL10n['good'] ); |
|
26 break; |
|
27 case 4: |
|
28 $('#pass-strength-result').addClass('strong').html( pwsL10n['strong'] ); |
|
29 break; |
|
30 default: |
|
31 $('#pass-strength-result').addClass('short').html( pwsL10n['short'] ); |
|
32 } |
|
33 } |
|
34 function resetMeter(){ |
|
35 $input.val(''); |
|
36 $('#pass-strength-result').text(indicatorString); |
|
37 $('#pass-strength-result').removeClass('short bad good strong'); |
|
38 } |
|
39 |
|
40 function animate(){ |
|
41 if (shouldAnimate === false) |
|
42 return; |
|
43 if ($input.val().length < password.length){ |
|
44 $input.val( password.substr(0, $input.val().length + 1) ); |
|
45 updateResult(); |
|
46 |
|
47 // Look like real typing by changing the speed new letters are added each time |
|
48 setTimeout( animate, ( timesForAnimation[ Math.floor( Math.random() * timesForAnimation.length ) ] ) ); |
|
49 } else { |
|
50 resetMeter(); |
|
51 |
|
52 // When we reset, let's wait a bit longer than normal to start again |
|
53 setTimeout(animate, 700); |
|
54 } |
|
55 |
|
56 } |
|
57 |
|
58 function begin(){ |
|
59 // we async load zxcvbn, so we need to make sure it's loaded before starting |
|
60 if (typeof(zxcvbn) !== 'undefined') |
|
61 animate(); |
|
62 else |
|
63 setTimeout(begin,800); |
|
64 } |
|
65 |
|
66 // Turn off the animation on focus |
|
67 $input.on('focus', function(){ |
|
68 shouldAnimate = false; |
|
69 resetMeter(); |
|
70 }); |
|
71 |
|
72 // Act like a normal password strength meter |
|
73 $input.on('keyup', function(){ |
|
74 updateResult(); |
|
75 }); |
|
76 |
|
77 // Start the animation |
|
78 begin(); |
|
79 |
|
80 })(jQuery); |