Version 1
[yaffs-website] / node_modules / video.js / es5 / menu / menu.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _component = require('../component.js');
6
7 var _component2 = _interopRequireDefault(_component);
8
9 var _dom = require('../utils/dom.js');
10
11 var Dom = _interopRequireWildcard(_dom);
12
13 var _fn = require('../utils/fn.js');
14
15 var Fn = _interopRequireWildcard(_fn);
16
17 var _events = require('../utils/events.js');
18
19 var Events = _interopRequireWildcard(_events);
20
21 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
22
23 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
24
25 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26
27 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
28
29 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
30                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 * @file menu.js
31                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 */
32
33
34 /**
35  * The Menu component is used to build popup menus, including subtitle and
36  * captions selection menus.
37  *
38  * @extends Component
39  */
40 var Menu = function (_Component) {
41   _inherits(Menu, _Component);
42
43   /**
44    * Create an instance of this class.
45    *
46    * @param {Player} player
47    *        the player that this component should attach to
48    *
49    * @param {Object} [options]
50    *        Object of option names and values
51    *
52    */
53   function Menu(player, options) {
54     _classCallCheck(this, Menu);
55
56     var _this = _possibleConstructorReturn(this, _Component.call(this, player, options));
57
58     _this.focusedChild_ = -1;
59
60     _this.on('keydown', _this.handleKeyPress);
61     return _this;
62   }
63
64   /**
65    * Add a {@link MenuItem} to the menu.
66    *
67    * @param {Object|string} component
68    *        The name or instance of the `MenuItem` to add.
69    *
70    */
71
72
73   Menu.prototype.addItem = function addItem(component) {
74     this.addChild(component);
75     component.on('click', Fn.bind(this, function (event) {
76       this.unlockShowing();
77       // TODO: Need to set keyboard focus back to the menuButton
78     }));
79   };
80
81   /**
82    * Create the `Menu`s DOM element.
83    *
84    * @return {Element}
85    *         the element that was created
86    */
87
88
89   Menu.prototype.createEl = function createEl() {
90     var contentElType = this.options_.contentElType || 'ul';
91
92     this.contentEl_ = Dom.createEl(contentElType, {
93       className: 'vjs-menu-content'
94     });
95
96     this.contentEl_.setAttribute('role', 'menu');
97
98     var el = _Component.prototype.createEl.call(this, 'div', {
99       append: this.contentEl_,
100       className: 'vjs-menu'
101     });
102
103     el.setAttribute('role', 'presentation');
104     el.appendChild(this.contentEl_);
105
106     // Prevent clicks from bubbling up. Needed for Menu Buttons,
107     // where a click on the parent is significant
108     Events.on(el, 'click', function (event) {
109       event.preventDefault();
110       event.stopImmediatePropagation();
111     });
112
113     return el;
114   };
115
116   /**
117    * Handle a `keydown` event on this menu. This listener is added in the constructor.
118    *
119    * @param {EventTarget~Event} event
120    *        A `keydown` event that happened on the menu.
121    *
122    * @listens keydown
123    */
124
125
126   Menu.prototype.handleKeyPress = function handleKeyPress(event) {
127     // Left and Down Arrows
128     if (event.which === 37 || event.which === 40) {
129       event.preventDefault();
130       this.stepForward();
131
132       // Up and Right Arrows
133     } else if (event.which === 38 || event.which === 39) {
134       event.preventDefault();
135       this.stepBack();
136     }
137   };
138
139   /**
140    * Move to next (lower) menu item for keyboard users.
141    */
142
143
144   Menu.prototype.stepForward = function stepForward() {
145     var stepChild = 0;
146
147     if (this.focusedChild_ !== undefined) {
148       stepChild = this.focusedChild_ + 1;
149     }
150     this.focus(stepChild);
151   };
152
153   /**
154    * Move to previous (higher) menu item for keyboard users.
155    */
156
157
158   Menu.prototype.stepBack = function stepBack() {
159     var stepChild = 0;
160
161     if (this.focusedChild_ !== undefined) {
162       stepChild = this.focusedChild_ - 1;
163     }
164     this.focus(stepChild);
165   };
166
167   /**
168    * Set focus on a {@link MenuItem} in the `Menu`.
169    *
170    * @param {Object|string} [item=0]
171    *        Index of child item set focus on.
172    */
173
174
175   Menu.prototype.focus = function focus() {
176     var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
177
178     var children = this.children().slice();
179     var haveTitle = children.length && children[0].className && /vjs-menu-title/.test(children[0].className);
180
181     if (haveTitle) {
182       children.shift();
183     }
184
185     if (children.length > 0) {
186       if (item < 0) {
187         item = 0;
188       } else if (item >= children.length) {
189         item = children.length - 1;
190       }
191
192       this.focusedChild_ = item;
193
194       children[item].el_.focus();
195     }
196   };
197
198   return Menu;
199 }(_component2['default']);
200
201 _component2['default'].registerComponent('Menu', Menu);
202 exports['default'] = Menu;