Scripting
The JavaScript object model for this example contains three objects: MenuButton, PopupMenu and MenuItemAgent,
each of which is described below.
MenuButton
- JavaScript file: MenuButton.js
- The MenuButton object serves as the delegate for an HTML
button
element that acts as a menu button.
- It maintains a reference to its PopupMenu object.
Related objects
- PopupMenu: The MenuButton object instantiates and initializes a PopupMenu object,
which is the delegate for the
ul
menu
element that it controls.
Keyboard Event Listeners & Behavior
Event | Key | Behavior | Prevent Default |
keydown |
Space / Return |
Open menu; set focus to first item |
yes |
keydown |
Down Arrow |
Open menu; set focus to first item |
yes |
keydown |
Up Arrow |
Open menu; set focus to last item |
yes |
Mouse Event Listeners & Behavior
Event | Behavior | Prevent Default |
mouseover |
Open menu |
no |
mouseout |
Close menu |
no |
click |
Open menu; set focus to first item |
no |
PopupMenu
- JavaScript file: PopupMenu.js
- The PopupMenu object serves as the delegate for an HTML
ul
element that acts as a popup menu.
- It checks that each child
li
element of the ul
has role menuitem
.
- Utilizing an instance of MenuItemAgent, it configures each
li
menuitem
element and then
saves a reference to each element in its menuitems
array.
- It saves the state of the menu in its properties
hasFocus
and hasHover
.
Related objects
- MenuButton: The PopupMenu is initialized with a reference to its MenuButton controller object, which is
the delegate for the
button
element that controls the menu.
- MenuItemAgent: The PopupMenu instantiates and initializes a MenuItemAgent object, and then uses it to
configure each of its
menuitem
li
elements.
Mouse Event Listeners & Behavior
Event | Behavior | Prevent Default |
mouseover |
Save hover state (affects whether menu is closed by other means) |
no |
mouseout |
Conditionally close menu |
no |
Note: Whether the PopupMenu's mouseout
event handler closes the menu depends on (a) the focus state of its menuitem elements and (b) the hover state of its controller element.
MenuItemAgent
- JavaScript file: MenuItemAgent.js
- The MenuItemAgent object serves as an event aggregator for all of the HTML
li
elements that act as menu items.
- It implements
menuitem
behavior by adding the necessary event listeners to an li
element.
- Most of the keyboard event handling in this example is done by the MenuItemAgent object.
- In response to those events, the MenuItemAgent object calls the methods of its related PopuMenu object.
Related objects
- PopupMenu: Each MenuItem object is instantiated with a reference to its PopupMenu object, which is the
delegate for the
ul
menu
element that contains the li
menuitem
element.
Focus Event Listeners & Behavior
Event | Behavior | Prevent Default |
focus |
Save focus state in menu object |
no |
blur |
Save focus state in menu object |
no |
Keyboard Event Listeners & Behavior
Event | Key | Behavior | Prevent Default |
keydown |
Space / Return |
Generate click event (see mouse click behavior) |
yes |
keydown |
Escape |
Set focus to controller element; close menu |
yes |
keydown |
Tab |
Set focus to controller element; close menu; Note: Because the handler does not
'Prevent Default', the browser also handles the keydown tab event. |
no |
keydown |
Down Arrow |
Set focus to next item (wraps) |
yes |
keydown |
Up Arrow |
Set focus to previous item (wraps) |
yes |
keydown |
Home / Page Up |
Set focus to first item |
yes |
keydown |
End / Page Down |
Set focus to last item |
yes |
keypress |
Any Printable Character |
Set focus to item with matching first character |
no |
Mouse Event Listeners & Behavior
Event | Behavior | Prevent Default |
click |
Set focus to menu's controller element; close menu |
no |