refactor keyboard listener conditions

This commit is contained in:
Hakim El Hattab 2019-01-22 15:43:31 +01:00
parent fef15ea9d1
commit 4c3f778e6e

View file

@ -4909,6 +4909,9 @@
return true; return true;
} }
// Shorthand
var keyCode = event.keyCode;
// Remember if auto-sliding was paused so we can toggle it // Remember if auto-sliding was paused so we can toggle it
var autoSlideWasPaused = autoSlidePaused; var autoSlideWasPaused = autoSlidePaused;
@ -4921,8 +4924,8 @@
// Whitelist specific modified + keycode combinations // Whitelist specific modified + keycode combinations
var prevSlideShortcut = event.shiftKey && event.keyCode === 32; var prevSlideShortcut = event.shiftKey && event.keyCode === 32;
var firstSlideShortcut = ( event.metaKey || event.ctrlKey ) && event.keyCode === 37; var firstSlideShortcut = ( event.metaKey || event.ctrlKey ) && keyCode === 37;
var lastSlideShortcut = ( event.metaKey || event.ctrlKey ) && event.keyCode === 39; var lastSlideShortcut = ( event.metaKey || event.ctrlKey ) && keyCode === 39;
// Prevent all other events when a modifier is pressed // Prevent all other events when a modifier is pressed
var unusedModifier = !prevSlideShortcut && !firstSlideShortcut && !lastSlideShortcut && var unusedModifier = !prevSlideShortcut && !firstSlideShortcut && !lastSlideShortcut &&
@ -4945,7 +4948,7 @@
} }
} }
if( isPaused() && resumeKeyCodes.indexOf( event.keyCode ) === -1 ) { if( isPaused() && resumeKeyCodes.indexOf( keyCode ) === -1 ) {
return false; return false;
} }
@ -4957,7 +4960,7 @@
for( key in config.keyboard ) { for( key in config.keyboard ) {
// Check if this binding matches the pressed key // Check if this binding matches the pressed key
if( parseInt( key, 10 ) === event.keyCode ) { if( parseInt( key, 10 ) === keyCode ) {
var value = config.keyboard[ key ]; var value = config.keyboard[ key ];
@ -4984,7 +4987,7 @@
for( key in registeredKeyBindings ) { for( key in registeredKeyBindings ) {
// Check if this binding matches the pressed key // Check if this binding matches the pressed key
if( parseInt( key, 10 ) === event.keyCode ) { if( parseInt( key, 10 ) === keyCode ) {
var action = registeredKeyBindings[ key ].callback; var action = registeredKeyBindings[ key ].callback;
@ -5008,34 +5011,91 @@
// Assume true and try to prove false // Assume true and try to prove false
triggered = true; triggered = true;
switch( event.keyCode ) { // P, PAGE UP
// p, page up if( keyCode === 80 || keyCode === 33 ) {
case 80: case 33: navigatePrev(); break; navigatePrev();
// n, page down }
case 78: case 34: navigateNext(); break; // N, PAGE DOWN
// h, left else if( keyCode === 78 || keyCode === 34 ) {
case 72: case 37: firstSlideShortcut ? slide( 0 ) : isOverview() || !config.simpleNavigation ? navigateLeft() : navigatePrev(); break; navigateNext();
// l, right }
case 76: case 39: lastSlideShortcut ? slide( Number.MAX_VALUE ) : isOverview() || !config.simpleNavigation ? navigateRight() : navigateNext(); break; // H, LEFT
// k, up else if( keyCode === 72 || keyCode === 37 ) {
case 75: case 38: isOverview() || !config.simpleNavigation ? navigateUp() : navigatePrev(); break; if( firstSlideShortcut ) {
// j, down slide( 0 );
case 74: case 40: isOverview() || !config.simpleNavigation ? navigateDown() : navigateNext(); break; }
// home else if( !isOverview() && config.simpleNavigation ) {
case 36: slide( 0 ); break; navigatePrev();
// end }
case 35: slide( Number.MAX_VALUE ); break; else {
// space navigateLeft();
case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break; }
// return }
case 13: isOverview() ? deactivateOverview() : triggered = false; break; // L, RIGHT
// two-spot, semicolon, b, v, period, Logitech presenter tools "black screen" button else if( keyCode === 76 || keyCode === 39 ) {
case 58: case 59: case 66: case 86: case 190: case 191: togglePause(); break; if( lastSlideShortcut ) {
// f slide( Number.MAX_VALUE );
case 70: enterFullscreen(); break; }
// a else if( !isOverview() && config.simpleNavigation ) {
case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break; navigateNext();
default: }
else {
navigateRight();
}
}
// K, UP
else if( keyCode === 75 || keyCode === 38 ) {
if( !isOverview() && config.simpleNavigation ) {
navigatePrev();
}
else {
navigateUp()
}
}
// J, DOWN
else if( keyCode === 74 || keyCode === 40 ) {
if( !isOverview() && config.simpleNavigation ) {
navigateNext()
}
else {
navigateDown()
}
}
// HOME
else if( keyCode === 36 ) {
slide( 0 );
}
// END
else if( keyCode === 35 ) {
slide( Number.MAX_VALUE );
}
// SPACE
else if( keyCode === 32 ) {
if( isOverview() ) {
deactivateOverview();
}
if( event.shiftKey ) {
navigatePrev();
}
else {
navigateNext();
}
}
// TWO-SPOT, SEMICOLON, B, V, PERIOD, LOGITECH PRESENTER TOOLS "BLACK SCREEN" BUTTON
else if( keyCode === 58 || keyCode === 59 || keyCode === 66 || keyCode === 86 || keyCode === 190 || keyCode === 191 ) {
togglePause();
}
// F
else if( keyCode === 70 ) {
enterFullscreen();
}
// A
else if( keyCode === 65 ) {
if ( config.autoSlideStoppable ) {
toggleAutoSlide( autoSlideWasPaused );
}
}
else {
triggered = false; triggered = false;
} }
@ -5047,7 +5107,7 @@
event.preventDefault && event.preventDefault(); event.preventDefault && event.preventDefault();
} }
// ESC or O key // ESC or O key
else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) { else if ( ( keyCode === 27 || keyCode === 79 ) && features.transforms3d ) {
if( dom.overlay ) { if( dom.overlay ) {
closeOverlay(); closeOverlay();
} }