Simplify JavaScript code

* Reuse $() closure.
* Query jQuery selector only once.

Change-Id: Ic302427fe7d23808b3e6718fd23a6b5d492bfa88
isekai
Fomafix 6 years ago
parent bde37bd025
commit 7f87813bc3

@ -1,32 +1,31 @@
/**
* Focus on search box when 'Tab' key is pressed once
*/
$( function () {
$( '#searchInput' ).attr( 'tabindex', $( document ).lastTabIndex() + 1 );
} );
/**
* Desktop menu click-toggling
*
* We're not even checking if it's desktop because the classes in play have no effect
* on mobile regardless... this may break things at some point, though.
*/
$( function () { $( function () {
// sidebar-chunk only applies to desktop-small, but the toggles are hidden at // sidebar-chunk only applies to desktop-small, but the toggles are hidden at
// other resolutions regardless and the css overrides any visible effects. // other resolutions regardless and the css overrides any visible effects.
var dropdowns = '#personal, #p-variants-desktop, .sidebar-chunk'; var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );
/**
* Focus on search box when 'Tab' key is pressed once
*/
$( '#searchInput' ).attr( 'tabindex', $( document ).lastTabIndex() + 1 );
/**
* Desktop menu click-toggling
*
* We're not even checking if it's desktop because the classes in play have no effect
* on mobile regardless... this may break things at some point, though.
*/
/** /**
* Close all dropdowns * Close all dropdowns
*/ */
function closeOpen() { function closeOpen() {
$( dropdowns ).removeClass( 'dropdown-active' ); $dropdowns.removeClass( 'dropdown-active' );
} }
/** /**
* Click behaviour * Click behaviour
*/ */
$( dropdowns ).on( 'click', function ( e ) { $dropdowns.on( 'click', function ( e ) {
var wasOpen = false; var wasOpen = false;
// Check if it's already open so we don't open it again // Check if it's already open so we don't open it again
if ( $( this ).hasClass( 'dropdown-active' ) ) { if ( $( this ).hasClass( 'dropdown-active' ) ) {
@ -39,7 +38,7 @@ $( function () {
} }
} ); } );
$( document ).on( 'click', function ( e ) { $( document ).on( 'click', function ( e ) {
if ( $( e.target ).closest( dropdowns ).length > 0 ) { if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
// Clicked inside an open menu; don't close anything // Clicked inside an open menu; don't close anything
} else { } else {
closeOpen(); closeOpen();

Loading…
Cancel
Save