Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
4b8490d9
Commit
4b8490d9
authored
Aug 26, 2016
by
Jareer Ahsan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed to global keycodes, refactored for quality
parent
7b8b1f9d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
38 deletions
+33
-38
lms/static/js/my_courses_dropdown.js
+33
-38
No files found.
lms/static/js/my_courses_dropdown.js
View file @
4b8490d9
$
(
document
).
ready
(
function
(
)
{
(
function
(
require
)
{
'use strict'
;
require
([
'jquery'
,
'edx-ui-toolkit/js/utils/constants'
],
function
(
$
,
constants
)
{
// define variables for code legibility
var
dropdownMenuToggle
=
$
(
'.dropdown'
);
var
dropdownMenu
=
$
(
'.dropdown-menu'
);
var
menuItems
=
dropdownMenu
.
find
(
'a'
);
var
keyCodes
=
{
leftArrow
:
37
,
upArrow
:
38
,
rightArrow
:
39
,
downArrow
:
40
,
tab
:
9
,
escape
:
27
,
space
:
32
};
var
$dropdownMenuToggle
=
$
(
'.dropdown'
);
var
$dropdownMenu
=
$
(
'.dropdown-menu'
);
var
menuItems
=
$dropdownMenu
.
find
(
'a'
);
// bind menu toggle click for later use
dropdownMenuToggle
.
toggle
(
function
()
{
dropdownMenu
.
addClass
(
'expanded'
).
find
(
'a'
).
first
().
focus
();
dropdownMenuToggle
.
addClass
(
'active'
).
attr
(
'aria-expanded'
,
'true'
);
$dropdownMenuToggle
.
toggle
(
function
()
{
$dropdownMenu
.
addClass
(
'expanded'
).
find
(
'a'
).
first
()
.
focus
();
$dropdownMenuToggle
.
addClass
(
'active'
).
attr
(
'aria-expanded'
,
'true'
);
},
function
()
{
dropdownMenu
.
removeClass
(
'expanded'
);
dropdownMenuToggle
.
removeClass
(
'active'
).
attr
(
'aria-expanded'
,
'false'
).
focus
();
$
dropdownMenu
.
removeClass
(
'expanded'
);
$
dropdownMenuToggle
.
removeClass
(
'active'
).
attr
(
'aria-expanded'
,
'false'
).
focus
();
});
// 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
(
event
.
which
===
keyCodes
.
space
)
{
dropdownMenuToggle
.
click
();
if
(
event
.
which
===
constants
.
keyCodes
.
space
)
{
$
dropdownMenuToggle
.
click
();
event
.
preventDefault
();
}
});
// catch keypresses when inside dropdownMenu
// (we want to catch spacebar; escape; up arrow or shift+tab; and down arrow or tab)
dropdownMenu
.
on
(
'keydown'
,
function
(
event
)
{
catchKeyPress
(
$
(
this
),
event
);
});
function
catchKeyPress
(
object
,
event
)
{
// get currently focused item
var
focusedItem
=
jQuery
(
':focus'
);
...
...
@@ -55,20 +43,20 @@ $(document).ready(function() {
var
itemToFocusIndex
;
// if space key pressed
if
(
event
.
which
===
keyCodes
.
space
)
{
dropdownMenuToggle
.
click
();
if
(
event
.
which
===
constants
.
keyCodes
.
space
)
{
$
dropdownMenuToggle
.
click
();
event
.
preventDefault
();
}
// if escape key pressed
if
(
event
.
which
===
keyCodes
.
escape
)
{
dropdownMenuToggle
.
click
();
if
(
event
.
which
===
constants
.
keyCodes
.
esc
)
{
$
dropdownMenuToggle
.
click
();
event
.
preventDefault
();
}
// if up arrow key pressed or shift+tab else down key or tab is pressed
if
(
event
.
which
===
keyCodes
.
upArrow
||
event
.
which
===
keyCodes
.
leftArrow
||
(
event
.
which
===
keyCodes
.
tab
&&
event
.
shiftKey
))
{
if
(
event
.
which
===
constants
.
keyCodes
.
up
||
event
.
which
===
constants
.
keyCodes
.
left
||
(
event
.
which
===
constants
.
keyCodes
.
tab
&&
event
.
shiftKey
))
{
// if first item go to last
if
(
focusedItemIndex
===
0
)
{
menuItems
.
last
().
focus
();
...
...
@@ -77,8 +65,8 @@ $(document).ready(function() {
menuItems
.
get
(
itemToFocusIndex
).
focus
();
}
event
.
preventDefault
();
}
else
if
(
event
.
which
===
keyCodes
.
downArrow
||
event
.
which
===
keyCodes
.
rightArrow
||
event
.
which
===
keyCodes
.
tab
)
{
}
else
if
(
event
.
which
===
constants
.
keyCodes
.
down
||
event
.
which
===
constants
.
keyCodes
.
right
||
event
.
which
===
constants
.
keyCodes
.
tab
)
{
// if last item go to first
if
(
focusedItemIndex
===
numberOfMenuItems
-
1
)
{
menuItems
.
first
().
focus
();
...
...
@@ -89,4 +77,11 @@ $(document).ready(function() {
event
.
preventDefault
();
}
}
});
// catch keypresses when inside dropdownMenu
// (we want to catch spacebar; escape; up arrow or shift+tab; and down arrow or tab)
$dropdownMenu
.
on
(
'keydown'
,
function
(
event
)
{
catchKeyPress
(
$
(
this
),
event
);
});
});
}).
call
(
this
,
require
||
RequireJS
.
require
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment