1 // Author: Yanko Shterev
2 // Thanks https://github.com/yshterev/videojs-settings-menu
4 // FIXME: something weird with our path definition in tsconfig and typings
6 import * as videojs from 'video.js'
8 import { toTitleCase } from '../utils'
9 import { VideoJSComponentInterface, videojsUntyped } from '../peertube-videojs-typings'
11 const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
12 const component: VideoJSComponentInterface = videojsUntyped.getComponent('Component')
14 class SettingsMenuItem extends MenuItem {
16 constructor (player: videojs.Player, options: any, entry: string, menuButton: VideoJSComponentInterface) {
17 super(player, options)
19 this.settingsButton = menuButton
20 this.dialog = this.settingsButton.dialog
21 this.mainMenu = this.settingsButton.menu
22 this.panel = this.dialog.getChild('settingsPanel')
23 this.panelChild = this.panel.getChild('settingsPanelChild')
24 this.panelChildEl = this.panelChild.el_
28 // keep state of what menu type is loading next
29 this.menuToLoad = 'mainmenu'
31 const subMenuName = toTitleCase(entry)
32 const SubMenuComponent = videojsUntyped.getComponent(subMenuName)
34 if (!SubMenuComponent) {
35 throw new Error(`Component ${subMenuName} does not exist`)
37 this.subMenu = new SubMenuComponent(this.player(), options, menuButton, this)
38 const subMenuClass = this.subMenu.buildCSSClass().split(' ')[0]
39 this.settingsSubMenuEl_.className += ' ' + subMenuClass
44 // Voodoo magic for IOS
46 // Player was destroyed
47 if (!this.player_) return
51 // Update on rate change
52 player.on('ratechange', this.submenuClickHandler)
54 if (subMenuName === 'CaptionsButton') {
55 // Hack to regenerate captions on HTTP fallback
56 player.on('captionsChanged', () => {
58 this.settingsSubMenuEl_.innerHTML = ''
59 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
61 this.bindClickEvents()
73 this.submenuClickHandler = this.onSubmenuClick.bind(this)
74 this.transitionEndHandler = this.onTransitionEnd.bind(this)
77 onSubmenuClick (event: any) {
80 if (event.type === 'tap') {
83 target = event.currentTarget
86 if (target && target.classList.contains('vjs-back-button')) {
91 // To update the sub menu value on click, setTimeout is needed because
92 // updating the value is not instant
93 setTimeout(() => this.update(event), 0)
97 * Create the component's DOM element
103 const el = videojsUntyped.dom.createEl('li', {
104 className: 'vjs-menu-item'
107 this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
108 className: 'vjs-settings-sub-menu-title'
111 el.appendChild(this.settingsSubMenuTitleEl_)
113 this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
114 className: 'vjs-settings-sub-menu-value'
117 el.appendChild(this.settingsSubMenuValueEl_)
119 this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
120 className: 'vjs-settings-sub-menu'
127 * Handle click on menu item
129 * @method handleClick
132 this.menuToLoad = 'submenu'
133 // Remove open class to ensure only the open submenu gets this class
134 videojsUntyped.dom.removeClass(this.el_, 'open')
138 this.mainMenu.el_.style.opacity = '0'
139 // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
140 if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
141 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
143 // animation not played without timeout
145 this.settingsSubMenuEl_.style.opacity = '1'
146 this.settingsSubMenuEl_.style.marginRight = '0px'
149 this.settingsButton.setDialogSize(this.size)
151 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
158 * @method createBackButton
160 createBackButton () {
161 const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
162 button.name_ = 'BackButton'
163 button.addClass('vjs-back-button')
164 button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_)
168 * Add/remove prefixed event listener for CSS Transition
170 * @method PrefixedEvent
172 PrefixedEvent (element: any, type: any, callback: any, action = 'addEvent') {
173 const prefix = ['webkit', 'moz', 'MS', 'o', '']
175 for (let p = 0; p < prefix.length; p++) {
177 type = type.toLowerCase()
180 if (action === 'addEvent') {
181 element.addEventListener(prefix[p] + type, callback, false)
182 } else if (action === 'removeEvent') {
183 element.removeEventListener(prefix[p] + type, callback, false)
188 onTransitionEnd (event: any) {
189 if (event.propertyName !== 'margin-right') {
193 if (this.menuToLoad === 'mainmenu') {
195 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
197 // reset opacity to 0
198 this.settingsSubMenuEl_.style.opacity = '0'
203 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
204 this.settingsSubMenuEl_.style.opacity = '0'
209 this.menuToLoad = 'mainmenu'
211 this.mainMenu.el_.style.opacity = '0'
213 // back button will always take you to main menu, so set dialog sizes
214 this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])
216 // animation not triggered without timeout (some async stuff ?!?)
218 // animate margin and opacity before hiding the submenu
219 // this triggers CSS Transition event
221 this.mainMenu.el_.style.opacity = '1'
226 this.subMenu.on('updateLabel', () => {
229 this.subMenu.on('menuChanged', () => {
230 this.bindClickEvents()
235 this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_)
236 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
237 this.panelChildEl.appendChild(this.settingsSubMenuEl_)
240 this.createBackButton()
242 this.bindClickEvents()
244 // prefixed event listeners for CSS TransitionEnd
246 this.settingsSubMenuEl_,
248 this.transitionEndHandler,
253 update (event?: any) {
254 let target: HTMLElement = null
255 const subMenu = this.subMenu.name()
257 if (event && event.type === 'tap') {
258 target = event.target
260 target = event.currentTarget
263 // Playback rate menu button doesn't get a vjs-selected class
264 // or sets options_['selected'] on the selected playback rate.
265 // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
266 if (subMenu === 'PlaybackRateMenuButton') {
267 setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
269 // Loop trough the submenu items to find the selected child
270 for (const subMenuItem of this.subMenu.menu.children_) {
271 if (!(subMenuItem instanceof component)) {
275 if (subMenuItem.hasClass('vjs-selected')) {
276 // Prefer to use the function
277 if (typeof subMenuItem.getLabel === 'function') {
278 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel()
282 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
287 if (target && !target.classList.contains('vjs-back-button')) {
288 this.settingsButton.hideDialog()
293 for (const item of this.subMenu.menu.children()) {
294 if (!(item instanceof component)) {
297 item.on(['tap', 'click'], this.submenuClickHandler)
301 // save size of submenus on first init
302 // if number of submenu items change dynamically more logic will be needed
304 this.dialog.removeClass('vjs-hidden')
305 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
306 this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
308 this.dialog.addClass('vjs-hidden')
309 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
313 const [ width ] = this.size
315 this.settingsSubMenuEl_.style.marginRight = `-${width}px`
322 // after removing settings item this.el_ === null
327 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
328 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
329 videojsUntyped.dom.removeClass(this.el_, 'open')
335 SettingsMenuItem.prototype.contentElType = 'button'
336 videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)
338 export { SettingsMenuItem }