| 1 |
/** |
| 2 |
* Small style tweaks for login form, and show a counter if user triggered login |
| 3 |
* protections (too many failed attempts). |
| 4 |
*/ |
| 5 |
|
| 6 |
/* global document */ |
| 7 |
/* eslint-env browser */ |
| 8 |
( function( $ ) { |
| 9 |
|
| 10 |
$( document ).ready( function() { |
| 11 |
// Move any external service buttons into top of login form. |
| 12 |
var loginform = document.getElementById( 'loginform' ); |
| 13 |
var externals = document.getElementById( 'auth-external-service-login' ); |
| 14 |
if ( null !== loginform && null !== externals ) { |
| 15 |
loginform.insertBefore( externals, loginform.firstChild ); |
| 16 |
} |
| 17 |
|
| 18 |
// Decrement seconds counter if it exists |
| 19 |
var secondsElement = document.getElementById( 'seconds_remaining' ); |
| 20 |
if ( null !== secondsElement ) { |
| 21 |
var secondsInterval = setInterval( function() { |
| 22 |
var seconds = secondsElement.getAttribute( 'data-seconds' ); |
| 23 |
if ( 1 > seconds ) { |
| 24 |
clearInterval( secondsInterval ); |
| 25 |
return; |
| 26 |
} |
| 27 |
seconds = parseInt( seconds, 10 ) - 1; |
| 28 |
secondsElement.innerHTML = secondsAsSentence( seconds ); |
| 29 |
secondsElement.setAttribute( 'data-seconds', seconds ); |
| 30 |
}, 1000 ); |
| 31 |
} |
| 32 |
}); |
| 33 |
|
| 34 |
function secondsAsSentence( seconds ) { |
| 35 |
var units = { |
| 36 |
week: 3600 * 24 * 7, |
| 37 |
day: 3600 * 24, |
| 38 |
hour: 3600, |
| 39 |
minute: 60, |
| 40 |
second: 1, |
| 41 |
}; |
| 42 |
|
| 43 |
// specifically handle zero |
| 44 |
if ( 0 === seconds ) { |
| 45 |
return '0 seconds'; |
| 46 |
} |
| 47 |
|
| 48 |
// Construct sentence, e.g., '1 week, 2 hours, 5 minutes, 10 seconds, ' |
| 49 |
var phrase = ''; |
| 50 |
for ( var name in units ) { |
| 51 |
if ( units.hasOwnProperty( name ) ) { |
| 52 |
var divisor = units[name]; |
| 53 |
var quot = Math.floor( seconds / divisor ); |
| 54 |
if ( quot ) { |
| 55 |
phrase += quot + ' ' + name; |
| 56 |
if ( 1 < Math.abs( quot ) ) { |
| 57 |
phrase += 's'; |
| 58 |
} |
| 59 |
phrase += ', '; |
| 60 |
seconds -= quot * divisor; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return phrase.substring( 0, phrase.length - 2 ); // trim off last ', ' |
| 66 |
} |
| 67 |
|
| 68 |
} )( jQuery ); |
| 69 |
|