]>
Commit | Line | Data |
---|---|---|
f5fcd9f7 | 1 | // Thanks to Yanko Shterev: https://github.com/yshterev/videojs-settings-menu |
2adfc7ea | 2 | import { toTitleCase } from '../utils' |
512decf3 | 3 | import videojs from 'video.js' |
f5fcd9f7 C |
4 | import { SettingsButton } from './settings-menu-button' |
5 | import { SettingsDialog } from './settings-dialog' | |
6 | import { SettingsPanel } from './settings-panel' | |
7 | import { SettingsPanelChild } from './settings-panel-child' | |
8 | ||
9 | const MenuItem = videojs.getComponent('MenuItem') | |
10 | const component = videojs.getComponent('Component') | |
11 | ||
12 | export interface SettingsMenuItemOptions extends videojs.MenuItemOptions { | |
13 | entry: string | |
14 | menuButton: SettingsButton | |
15 | } | |
c6352f2c C |
16 | |
17 | class SettingsMenuItem extends MenuItem { | |
f5fcd9f7 C |
18 | settingsButton: SettingsButton |
19 | dialog: SettingsDialog | |
20 | mainMenu: videojs.Menu | |
21 | panel: SettingsPanel | |
22 | panelChild: SettingsPanelChild | |
23 | panelChildEl: HTMLElement | |
24 | size: number[] | |
16b55259 | 25 | menuToLoad: string |
f5fcd9f7 | 26 | subMenu: SettingsButton |
16b55259 | 27 | |
f5fcd9f7 C |
28 | submenuClickHandler: typeof SettingsMenuItem.prototype.onSubmenuClick |
29 | transitionEndHandler: typeof SettingsMenuItem.prototype.onTransitionEnd | |
16b55259 | 30 | |
f5fcd9f7 C |
31 | settingsSubMenuTitleEl_: HTMLElement |
32 | settingsSubMenuValueEl_: HTMLElement | |
33 | settingsSubMenuEl_: HTMLElement | |
c6352f2c | 34 | |
7e37e111 | 35 | constructor (player: videojs.Player, options?: SettingsMenuItemOptions) { |
c6352f2c C |
36 | super(player, options) |
37 | ||
f5fcd9f7 | 38 | this.settingsButton = options.menuButton |
c6352f2c C |
39 | this.dialog = this.settingsButton.dialog |
40 | this.mainMenu = this.settingsButton.menu | |
41 | this.panel = this.dialog.getChild('settingsPanel') | |
42 | this.panelChild = this.panel.getChild('settingsPanelChild') | |
f5fcd9f7 | 43 | this.panelChildEl = this.panelChild.el() as HTMLElement |
c6352f2c C |
44 | |
45 | this.size = null | |
46 | ||
47 | // keep state of what menu type is loading next | |
48 | this.menuToLoad = 'mainmenu' | |
49 | ||
f5fcd9f7 C |
50 | const subMenuName = toTitleCase(options.entry) |
51 | const SubMenuComponent = videojs.getComponent(subMenuName) | |
c6352f2c C |
52 | |
53 | if (!SubMenuComponent) { | |
54 | throw new Error(`Component ${subMenuName} does not exist`) | |
55 | } | |
f5fcd9f7 C |
56 | |
57 | const newOptions = Object.assign({}, options, { entry: options.menuButton, menuButton: this }) | |
58 | ||
59 | this.subMenu = new SubMenuComponent(this.player(), newOptions) as any // FIXME: typings | |
60 | const subMenuClass = this.subMenu.buildCSSClass().split(' ')[ 0 ] | |
16f7022b | 61 | this.settingsSubMenuEl_.className += ' ' + subMenuClass |
c6352f2c C |
62 | |
63 | this.eventHandlers() | |
64 | ||
65 | player.ready(() => { | |
b335ccec C |
66 | // Voodoo magic for IOS |
67 | setTimeout(() => { | |
bfbd9128 C |
68 | // Player was destroyed |
69 | if (!this.player_) return | |
70 | ||
b335ccec | 71 | this.build() |
5363a766 C |
72 | |
73 | // Update on rate change | |
74 | player.on('ratechange', this.submenuClickHandler) | |
75 | ||
c32bf839 C |
76 | if (subMenuName === 'CaptionsButton') { |
77 | // Hack to regenerate captions on HTTP fallback | |
78 | player.on('captionsChanged', () => { | |
79 | setTimeout(() => { | |
80 | this.settingsSubMenuEl_.innerHTML = '' | |
f5fcd9f7 | 81 | this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el()) |
c32bf839 C |
82 | this.update() |
83 | this.bindClickEvents() | |
c32bf839 C |
84 | }, 0) |
85 | }) | |
86 | } | |
87 | ||
b335ccec C |
88 | this.reset() |
89 | }, 0) | |
c6352f2c C |
90 | }) |
91 | } | |
92 | ||
93 | eventHandlers () { | |
94 | this.submenuClickHandler = this.onSubmenuClick.bind(this) | |
95 | this.transitionEndHandler = this.onTransitionEnd.bind(this) | |
96 | } | |
97 | ||
244b4ae3 | 98 | onSubmenuClick (event: any) { |
c6352f2c C |
99 | let target = null |
100 | ||
101 | if (event.type === 'tap') { | |
102 | target = event.target | |
103 | } else { | |
104 | target = event.currentTarget | |
105 | } | |
106 | ||
5363a766 | 107 | if (target && target.classList.contains('vjs-back-button')) { |
c6352f2c C |
108 | this.loadMainMenu() |
109 | return | |
110 | } | |
111 | ||
112 | // To update the sub menu value on click, setTimeout is needed because | |
113 | // updating the value is not instant | |
114 | setTimeout(() => this.update(event), 0) | |
42aac9fc C |
115 | |
116 | // Seems like videojs adds a vjs-hidden class on the caption menu after a click | |
117 | // We don't need it | |
118 | this.subMenu.menu.removeClass('vjs-hidden') | |
c6352f2c C |
119 | } |
120 | ||
121 | /** | |
122 | * Create the component's DOM element | |
123 | * | |
124 | * @return {Element} | |
125 | * @method createEl | |
126 | */ | |
127 | createEl () { | |
f5fcd9f7 | 128 | const el = videojs.dom.createEl('li', { |
c6352f2c C |
129 | className: 'vjs-menu-item' |
130 | }) | |
131 | ||
f5fcd9f7 | 132 | this.settingsSubMenuTitleEl_ = videojs.dom.createEl('div', { |
c6352f2c | 133 | className: 'vjs-settings-sub-menu-title' |
f5fcd9f7 | 134 | }) as HTMLElement |
c6352f2c C |
135 | |
136 | el.appendChild(this.settingsSubMenuTitleEl_) | |
137 | ||
f5fcd9f7 | 138 | this.settingsSubMenuValueEl_ = videojs.dom.createEl('div', { |
c6352f2c | 139 | className: 'vjs-settings-sub-menu-value' |
f5fcd9f7 | 140 | }) as HTMLElement |
c6352f2c C |
141 | |
142 | el.appendChild(this.settingsSubMenuValueEl_) | |
143 | ||
f5fcd9f7 | 144 | this.settingsSubMenuEl_ = videojs.dom.createEl('div', { |
c6352f2c | 145 | className: 'vjs-settings-sub-menu' |
f5fcd9f7 | 146 | }) as HTMLElement |
c6352f2c | 147 | |
f5fcd9f7 | 148 | return el as HTMLLIElement |
c6352f2c C |
149 | } |
150 | ||
151 | /** | |
152 | * Handle click on menu item | |
153 | * | |
154 | * @method handleClick | |
155 | */ | |
f5fcd9f7 | 156 | handleClick (event: videojs.EventTarget.Event) { |
c6352f2c C |
157 | this.menuToLoad = 'submenu' |
158 | // Remove open class to ensure only the open submenu gets this class | |
f5fcd9f7 | 159 | videojs.dom.removeClass(this.el(), 'open') |
c6352f2c | 160 | |
f5fcd9f7 | 161 | super.handleClick(event); |
c6352f2c | 162 | |
f5fcd9f7 | 163 | (this.mainMenu.el() as HTMLElement).style.opacity = '0' |
c6352f2c | 164 | // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element |
f5fcd9f7 C |
165 | if (videojs.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) { |
166 | videojs.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
c6352f2c C |
167 | |
168 | // animation not played without timeout | |
169 | setTimeout(() => { | |
170 | this.settingsSubMenuEl_.style.opacity = '1' | |
171 | this.settingsSubMenuEl_.style.marginRight = '0px' | |
172 | }, 0) | |
173 | ||
174 | this.settingsButton.setDialogSize(this.size) | |
175 | } else { | |
f5fcd9f7 | 176 | videojs.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') |
c6352f2c C |
177 | } |
178 | } | |
179 | ||
180 | /** | |
181 | * Create back button | |
182 | * | |
183 | * @method createBackButton | |
184 | */ | |
185 | createBackButton () { | |
186 | const button = this.subMenu.menu.addChild('MenuItem', {}, 0) | |
f5fcd9f7 C |
187 | |
188 | button.addClass('vjs-back-button'); | |
189 | (button.el() as HTMLElement).innerHTML = this.player().localize(this.subMenu.controlText()) | |
c6352f2c C |
190 | } |
191 | ||
192 | /** | |
193 | * Add/remove prefixed event listener for CSS Transition | |
194 | * | |
195 | * @method PrefixedEvent | |
196 | */ | |
244b4ae3 | 197 | PrefixedEvent (element: any, type: any, callback: any, action = 'addEvent') { |
f5fcd9f7 | 198 | const prefix = [ 'webkit', 'moz', 'MS', 'o', '' ] |
c6352f2c C |
199 | |
200 | for (let p = 0; p < prefix.length; p++) { | |
f5fcd9f7 | 201 | if (!prefix[ p ]) { |
c6352f2c C |
202 | type = type.toLowerCase() |
203 | } | |
204 | ||
205 | if (action === 'addEvent') { | |
f5fcd9f7 | 206 | element.addEventListener(prefix[ p ] + type, callback, false) |
c6352f2c | 207 | } else if (action === 'removeEvent') { |
f5fcd9f7 | 208 | element.removeEventListener(prefix[ p ] + type, callback, false) |
c6352f2c C |
209 | } |
210 | } | |
211 | } | |
212 | ||
244b4ae3 | 213 | onTransitionEnd (event: any) { |
c6352f2c C |
214 | if (event.propertyName !== 'margin-right') { |
215 | return | |
216 | } | |
217 | ||
218 | if (this.menuToLoad === 'mainmenu') { | |
219 | // hide submenu | |
f5fcd9f7 | 220 | videojs.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') |
c6352f2c C |
221 | |
222 | // reset opacity to 0 | |
223 | this.settingsSubMenuEl_.style.opacity = '0' | |
224 | } | |
225 | } | |
226 | ||
227 | reset () { | |
f5fcd9f7 | 228 | videojs.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') |
c6352f2c C |
229 | this.settingsSubMenuEl_.style.opacity = '0' |
230 | this.setMargin() | |
231 | } | |
232 | ||
233 | loadMainMenu () { | |
f5fcd9f7 | 234 | const mainMenuEl = this.mainMenu.el() as HTMLElement |
c6352f2c C |
235 | this.menuToLoad = 'mainmenu' |
236 | this.mainMenu.show() | |
f5fcd9f7 | 237 | mainMenuEl.style.opacity = '0' |
c6352f2c C |
238 | |
239 | // back button will always take you to main menu, so set dialog sizes | |
f5fcd9f7 C |
240 | const mainMenuAny = this.mainMenu as any |
241 | this.settingsButton.setDialogSize([ mainMenuAny.width, mainMenuAny.height ]) | |
c6352f2c C |
242 | |
243 | // animation not triggered without timeout (some async stuff ?!?) | |
244 | setTimeout(() => { | |
245 | // animate margin and opacity before hiding the submenu | |
246 | // this triggers CSS Transition event | |
247 | this.setMargin() | |
f5fcd9f7 | 248 | mainMenuEl.style.opacity = '1' |
c6352f2c C |
249 | }, 0) |
250 | } | |
251 | ||
252 | build () { | |
2adfc7ea | 253 | this.subMenu.on('updateLabel', () => { |
c6352f2c | 254 | this.update() |
2adfc7ea | 255 | }) |
3b6f205c C |
256 | this.subMenu.on('menuChanged', () => { |
257 | this.bindClickEvents() | |
258 | this.setSize() | |
259 | this.update() | |
260 | }) | |
c6352f2c | 261 | |
f5fcd9f7 C |
262 | this.settingsSubMenuTitleEl_.innerHTML = this.player().localize(this.subMenu.controlText()) |
263 | this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el()) | |
c6352f2c C |
264 | this.panelChildEl.appendChild(this.settingsSubMenuEl_) |
265 | this.update() | |
266 | ||
267 | this.createBackButton() | |
3b6f205c | 268 | this.setSize() |
c6352f2c C |
269 | this.bindClickEvents() |
270 | ||
271 | // prefixed event listeners for CSS TransitionEnd | |
272 | this.PrefixedEvent( | |
273 | this.settingsSubMenuEl_, | |
274 | 'TransitionEnd', | |
275 | this.transitionEndHandler, | |
276 | 'addEvent' | |
277 | ) | |
278 | } | |
279 | ||
244b4ae3 | 280 | update (event?: any) { |
c199c427 | 281 | let target: HTMLElement = null |
c4710631 | 282 | const subMenu = this.subMenu.name() |
c6352f2c C |
283 | |
284 | if (event && event.type === 'tap') { | |
285 | target = event.target | |
286 | } else if (event) { | |
287 | target = event.currentTarget | |
288 | } | |
289 | ||
290 | // Playback rate menu button doesn't get a vjs-selected class | |
291 | // or sets options_['selected'] on the selected playback rate. | |
292 | // Thus we get the submenu value based on the labelEl of playbackRateMenuButton | |
293 | if (subMenu === 'PlaybackRateMenuButton') { | |
f5fcd9f7 C |
294 | const html = (this.subMenu as any).labelEl_.innerHTML |
295 | setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = html, 250) | |
c6352f2c C |
296 | } else { |
297 | // Loop trough the submenu items to find the selected child | |
c4710631 | 298 | for (const subMenuItem of this.subMenu.menu.children_) { |
c6352f2c C |
299 | if (!(subMenuItem instanceof component)) { |
300 | continue | |
301 | } | |
302 | ||
a8462c8e | 303 | if (subMenuItem.hasClass('vjs-selected')) { |
f5fcd9f7 C |
304 | const subMenuItemUntyped = subMenuItem as any |
305 | ||
a8462c8e | 306 | // Prefer to use the function |
f5fcd9f7 C |
307 | if (typeof subMenuItemUntyped.getLabel === 'function') { |
308 | this.settingsSubMenuValueEl_.innerHTML = subMenuItemUntyped.getLabel() | |
c6352f2c | 309 | break |
a8462c8e | 310 | } |
c6352f2c | 311 | |
f5fcd9f7 | 312 | this.settingsSubMenuValueEl_.innerHTML = subMenuItemUntyped.options_.label |
c6352f2c C |
313 | } |
314 | } | |
315 | } | |
316 | ||
317 | if (target && !target.classList.contains('vjs-back-button')) { | |
318 | this.settingsButton.hideDialog() | |
319 | } | |
320 | } | |
321 | ||
322 | bindClickEvents () { | |
c4710631 | 323 | for (const item of this.subMenu.menu.children()) { |
c6352f2c C |
324 | if (!(item instanceof component)) { |
325 | continue | |
326 | } | |
f5fcd9f7 | 327 | item.on([ 'tap', 'click' ], this.submenuClickHandler) |
c6352f2c C |
328 | } |
329 | } | |
330 | ||
331 | // save size of submenus on first init | |
332 | // if number of submenu items change dynamically more logic will be needed | |
3b6f205c | 333 | setSize () { |
c6352f2c | 334 | this.dialog.removeClass('vjs-hidden') |
f5fcd9f7 | 335 | videojs.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden') |
c6352f2c C |
336 | this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_) |
337 | this.setMargin() | |
338 | this.dialog.addClass('vjs-hidden') | |
f5fcd9f7 | 339 | videojs.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') |
c6352f2c C |
340 | } |
341 | ||
342 | setMargin () { | |
d275e754 C |
343 | if (!this.size) return |
344 | ||
c4710631 | 345 | const [ width ] = this.size |
c6352f2c C |
346 | |
347 | this.settingsSubMenuEl_.style.marginRight = `-${width}px` | |
348 | } | |
349 | ||
350 | /** | |
351 | * Hide the sub menu | |
352 | */ | |
353 | hideSubMenu () { | |
354 | // after removing settings item this.el_ === null | |
f5fcd9f7 | 355 | if (!this.el()) { |
c6352f2c C |
356 | return |
357 | } | |
358 | ||
f5fcd9f7 C |
359 | if (videojs.dom.hasClass(this.el(), 'open')) { |
360 | videojs.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
361 | videojs.dom.removeClass(this.el(), 'open') | |
c6352f2c C |
362 | } |
363 | } | |
364 | ||
365 | } | |
366 | ||
f5fcd9f7 C |
367 | (SettingsMenuItem as any).prototype.contentElType = 'button' |
368 | videojs.registerComponent('SettingsMenuItem', SettingsMenuItem) | |
c6352f2c C |
369 | |
370 | export { SettingsMenuItem } |