- 1 :
/**
- 2 :
* @file menu-item.js
- 3 :
*/
- 4 :
import ClickableComponent from '../clickable-component.js';
- 5 :
import Component from '../component.js';
- 6 :
import {assign} from '../utils/obj';
- 7 :
import {MenuKeys} from './menu-keys.js';
- 8 :
import keycode from 'keycode';
- 9 :
import {createEl} from '../utils/dom.js';
- 10 :
- 11 :
/**
- 12 :
* The component for a menu item. `<li>`
- 13 :
*
- 14 :
* @extends ClickableComponent
- 15 :
*/
- 16 :
class MenuItem extends ClickableComponent {
- 17 :
- 18 :
/**
- 19 :
* Creates an instance of the this class.
- 20 :
*
- 21 :
* @param {Player} player
- 22 :
* The `Player` that this class should be attached to.
- 23 :
*
- 24 :
* @param {Object} [options={}]
- 25 :
* The key/value store of player options.
- 26 :
*
- 27 :
*/
- 28 :
constructor(player, options) {
- 29 :
super(player, options);
- 30 :
- 31 :
this.selectable = options.selectable;
- 32 :
this.isSelected_ = options.selected || false;
- 33 :
this.multiSelectable = options.multiSelectable;
- 34 :
- 35 :
this.selected(this.isSelected_);
- 36 :
- 37 :
if (this.selectable) {
- 38 :
if (this.multiSelectable) {
- 39 :
this.el_.setAttribute('role', 'menuitemcheckbox');
- 40 :
} else {
- 41 :
this.el_.setAttribute('role', 'menuitemradio');
- 42 :
}
- 43 :
} else {
- 44 :
this.el_.setAttribute('role', 'menuitem');
- 45 :
}
- 46 :
}
- 47 :
- 48 :
/**
- 49 :
* Create the `MenuItem's DOM element
- 50 :
*
- 51 :
* @param {string} [type=li]
- 52 :
* Element's node type, not actually used, always set to `li`.
- 53 :
*
- 54 :
* @param {Object} [props={}]
- 55 :
* An object of properties that should be set on the element
- 56 :
*
- 57 :
* @param {Object} [attrs={}]
- 58 :
* An object of attributes that should be set on the element
- 59 :
*
- 60 :
* @return {Element}
- 61 :
* The element that gets created.
- 62 :
*/
- 63 :
createEl(type, props, attrs) {
- 64 :
// The control is textual, not just an icon
- 65 :
this.nonIconControl = true;
- 66 :
- 67 :
const el = super.createEl('li', assign({
- 68 :
className: 'vjs-menu-item',
- 69 :
tabIndex: -1
- 70 :
}, props), attrs);
- 71 :
- 72 :
// swap icon with menu item text.
- 73 :
el.replaceChild(createEl('span', {
- 74 :
className: 'vjs-menu-item-text',
- 75 :
textContent: this.localize(this.options_.label)
- 76 :
}), el.querySelector('.vjs-icon-placeholder'));
- 77 :
- 78 :
return el;
- 79 :
}
- 80 :
- 81 :
/**
- 82 :
* Ignore keys which are used by the menu, but pass any other ones up. See
- 83 :
* {@link ClickableComponent#handleKeyDown} for instances where this is called.
- 84 :
*
- 85 :
* @param {EventTarget~Event} event
- 86 :
* The `keydown` event that caused this function to be called.
- 87 :
*
- 88 :
* @listens keydown
- 89 :
*/
- 90 :
handleKeyDown(event) {
- 91 :
if (!MenuKeys.some((key) => keycode.isEventKey(event, key))) {
- 92 :
// Pass keydown handling up for unused keys
- 93 :
super.handleKeyDown(event);
- 94 :
}
- 95 :
}
- 96 :
- 97 :
/**
- 98 :
* Any click on a `MenuItem` puts it into the selected state.
- 99 :
* See {@link ClickableComponent#handleClick} for instances where this is called.
- 100 :
*
- 101 :
* @param {EventTarget~Event} event
- 102 :
* The `keydown`, `tap`, or `click` event that caused this function to be
- 103 :
* called.
- 104 :
*
- 105 :
* @listens tap
- 106 :
* @listens click
- 107 :
*/
- 108 :
handleClick(event) {
- 109 :
this.selected(true);
- 110 :
}
- 111 :
- 112 :
/**
- 113 :
* Set the state for this menu item as selected or not.
- 114 :
*
- 115 :
* @param {boolean} selected
- 116 :
* if the menu item is selected or not
- 117 :
*/
- 118 :
selected(selected) {
- 119 :
if (this.selectable) {
- 120 :
if (selected) {
- 121 :
this.addClass('vjs-selected');
- 122 :
this.el_.setAttribute('aria-checked', 'true');
- 123 :
// aria-checked isn't fully supported by browsers/screen readers,
- 124 :
// so indicate selected state to screen reader in the control text.
- 125 :
this.controlText(', selected');
- 126 :
this.isSelected_ = true;
- 127 :
} else {
- 128 :
this.removeClass('vjs-selected');
- 129 :
this.el_.setAttribute('aria-checked', 'false');
- 130 :
// Indicate un-selected state to screen reader
- 131 :
this.controlText('');
- 132 :
this.isSelected_ = false;
- 133 :
}
- 134 :
}
- 135 :
}
- 136 :
}
- 137 :
- 138 :
Component.registerComponent('MenuItem', MenuItem);
- 139 :
export default MenuItem;