About page: Cache our zxcvbn calls and limit the variation of the animation.

Diff is mostly whitespace.

props jorbin, azaozz.
fixes #25603.


git-svn-id: https://develop.svn.wordpress.org/trunk@25884 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2013-10-23 19:50:30 +00:00
parent 1d33726b1a
commit ed15997210

View File

@ -1,66 +1,80 @@
(function($){ (function($){
var password = 'Gosh, WordPress is grand.', var password = 'Gosh, WordPress is grand.',
$input = $('#pass'), $input = $('#pass'),
shouldAnimate = true, shouldAnimate = true,
indicatorString = $('#pass-strength-result').text(); timesForAnimation = [280, 300, 305, 310, 315, 325, 330, 345, 360, 370, 380, 400, 450, 500, 600],
resultsCache = {},
indicatorString = $('#pass-strength-result').text();
function updateResult(){ function updateResult(){
var strength = wp.passwordStrength.meter($input.val(), [], $input.val()); var strength;
$('#pass-strength-result').removeClass('short bad good strong'); if ( typeof( resultsCache[ $input.val() ]) === 'undefined') {
switch ( strength ) { strength = wp.passwordStrength.meter($input.val(), [], $input.val());
case 2: resultsCache[ $input.val() ] = strength;
$('#pass-strength-result').addClass('bad').html( pwsL10n['bad'] ); } else {
break; strength = resultsCache[ $input.val() ];
case 3: }
$('#pass-strength-result').addClass('good').html( pwsL10n['good'] );
break;
case 4:
$('#pass-strength-result').addClass('strong').html( pwsL10n['strong'] );
break;
default:
$('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
}
}
function resetMeter(){
$input.val('');
$('#pass-strength-result').text(indicatorString);
$('#pass-strength-result').removeClass('short bad good strong');
}
function animate(){ $('#pass-strength-result').removeClass('short bad good strong');
if (shouldAnimate === false) switch ( strength ) {
return; case 2:
if ($input.val().length < password.length){ $('#pass-strength-result').addClass('bad').html( pwsL10n['bad'] );
$input.val( password.substr(0, $input.val().length + 1) ); break;
updateResult(); case 3:
} else { $('#pass-strength-result').addClass('good').html( pwsL10n['good'] );
resetMeter(); break;
} case 4:
// Look like real typing by changing the speed new letters are added each time $('#pass-strength-result').addClass('strong').html( pwsL10n['strong'] );
setTimeout(animate, 220 + Math.floor(Math.random() * ( 800 - 220)) ); break;
} default:
// $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
function begin(){ }
// we async load zxcvbn, so we need to make sure it's loaded before starting }
if (typeof(zxcvbn) !== 'undefined') function resetMeter(){
animate(); $input.val('');
else $('#pass-strength-result').text(indicatorString);
setTimeout(begin,800); $('#pass-strength-result').removeClass('short bad good strong');
} }
// Turn off the animation on focus function animate(){
$input.on('focus', function(){ if (shouldAnimate === false)
shouldAnimate = false; return;
resetMeter(); if ($input.val().length < password.length){
}); $input.val( password.substr(0, $input.val().length + 1) );
updateResult();
// Act like a normal password strength meter // Look like real typing by changing the speed new letters are added each time
$input.on('keyup', function(){ setTimeout( animate, ( timesForAnimation[ Math.floor( Math.random() * timesForAnimation.length ) ] ) );
updateResult(); } else {
}); resetMeter();
// Start the animation // When we reset, let's wait a bit longer than normal to start again
begin(); setTimeout(animate, 700);
}
}
function begin(){
// we async load zxcvbn, so we need to make sure it's loaded before starting
if (typeof(zxcvbn) !== 'undefined')
animate();
else
setTimeout(begin,800);
}
// Turn off the animation on focus
$input.on('focus', function(){
shouldAnimate = false;
resetMeter();
});
// Act like a normal password strength meter
$input.on('keyup', function(){
updateResult();
});
// Start the animation
begin();
})(jQuery); })(jQuery);