Commit 118afcd3 by Jareer Ahsan Committed by Jareer Ahsan

ECOM-4945 Fixed Shift-Tab on menu

TNL-5437 Reverted to no RequireJS
parent e6b5cc9e
(function(require) { $(document).ready(function() {
'use strict'; 'use strict';
require([
'jquery',
'edx-ui-toolkit/js/utils/constants'
], function($, constants) {
// define variables for code legibility // define variables for code legibility
var $dropdownMenuToggle = $('.dropdown'); var $dropdownMenuToggle = $('.dropdown');
var $dropdownMenu = $('.dropdown-menu'); var $dropdownMenu = $('.dropdown-menu');
var menuItems = $dropdownMenu.find('.dropdown-menuitem'); var menuItems = $dropdownMenu.find('.dropdown-menuitem');
var keyCodes = {
leftArrow: 37,
upArrow: 38,
rightArrow: 39,
downArrow: 40,
tab: 9,
escape: 27,
space: 32
};
// bind menu toggle click for later use // bind menu toggle click for later use
$dropdownMenuToggle.toggle(function() { $dropdownMenuToggle.toggle(function() {
$dropdownMenu.addClass('expanded').find('.dropdown-menuitem').first() $dropdownMenu.addClass('expanded').find('.dropdown-menuitem').first()
...@@ -20,10 +27,10 @@ ...@@ -20,10 +27,10 @@
$dropdownMenuToggle.removeClass('active').attr('aria-expanded', 'false').focus(); $dropdownMenuToggle.removeClass('active').attr('aria-expanded', 'false').focus();
}); });
// catch keypresses when focused on dropdownMenuToggle (we only care about spacebar keypresses here) // catch keypresses when focused on $dropdownMenuToggle (we only care about spacebar keypresses here)
$dropdownMenuToggle.on('keydown', function(event) { $dropdownMenuToggle.on('keydown', function(event) {
// if space key pressed // if space key pressed
if (event.which === constants.keyCodes.space) { if (event.which === keyCodes.space) {
$dropdownMenuToggle.click(); $dropdownMenuToggle.click();
event.preventDefault(); event.preventDefault();
} }
...@@ -43,20 +50,20 @@ ...@@ -43,20 +50,20 @@
var itemToFocusIndex; var itemToFocusIndex;
// if space key pressed // if space key pressed
if (event.which === constants.keyCodes.space) { if (event.which === keyCodes.space) {
$dropdownMenuToggle.click(); $dropdownMenuToggle.click();
event.preventDefault(); event.preventDefault();
} }
// if escape key pressed // if escape key pressed
if (event.which === constants.keyCodes.esc) { if (event.which === keyCodes.escape) {
$dropdownMenuToggle.click(); $dropdownMenuToggle.click();
event.preventDefault(); event.preventDefault();
} }
// if up arrow key pressed or shift+tab else down key or tab is pressed // if up arrow key pressed or shift+tab else down key or tab is pressed
if (event.which === constants.keyCodes.up || event.which === constants.keyCodes.left || if (event.which === keyCodes.upArrow || event.which === keyCodes.leftArrow ||
(event.which === constants.keyCodes.tab && event.shiftKey)) { (event.which === keyCodes.tab && event.shiftKey)) {
// if first item go to last // if first item go to last
if (focusedItemIndex === 0) { if (focusedItemIndex === 0) {
menuItems.last().focus(); menuItems.last().focus();
...@@ -65,8 +72,8 @@ ...@@ -65,8 +72,8 @@
menuItems.get(itemToFocusIndex).focus(); menuItems.get(itemToFocusIndex).focus();
} }
event.preventDefault(); event.preventDefault();
} else if (event.which === constants.keyCodes.down || event.which === constants.keyCodes.right || } else if (event.which === keyCodes.downArrow || event.which === keyCodes.rightArrow ||
event.which === constants.keyCodes.tab) { event.which === keyCodes.tab) {
// if last item go to first // if last item go to first
if (focusedItemIndex === numberOfMenuItems - 1) { if (focusedItemIndex === numberOfMenuItems - 1) {
menuItems.first().focus(); menuItems.first().focus();
...@@ -78,10 +85,9 @@ ...@@ -78,10 +85,9 @@
} }
} }
// catch keypresses when inside dropdownMenu // catch keypresses when inside $dropdownMenu
// (we want to catch spacebar; escape; up arrow or shift+tab; and down arrow or tab) // (we want to catch spacebar; escape; up arrow or shift+tab; and down arrow or tab)
$dropdownMenu.on('keydown', function(event) { $dropdownMenu.on('keydown', function(event) {
catchKeyPress($(this), event); catchKeyPress($(this), event);
}); });
}); });
}).call(this, require || RequireJS.require);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment