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
48 // Update on rate change
49 player.on('ratechange', this.submenuClickHandler)
51 if (subMenuName === 'CaptionsButton') {
52 // Hack to regenerate captions on HTTP fallback
53 player.on('captionsChanged', () => {
55 this.settingsSubMenuEl_.innerHTML = ''
56 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
58 this.bindClickEvents()
70 this.submenuClickHandler = this.onSubmenuClick.bind(this)
71 this.transitionEndHandler = this.onTransitionEnd.bind(this)
74 onSubmenuClick (event: any) {
77 if (event.type === 'tap') {
80 target = event.currentTarget
83 if (target && target.classList.contains('vjs-back-button')) {
88 // To update the sub menu value on click, setTimeout is needed because
89 // updating the value is not instant
90 setTimeout(() => this.update(event), 0)
94 * Create the component's DOM element
100 const el = videojsUntyped.dom.createEl('li', {
101 className: 'vjs-menu-item'
104 this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
105 className: 'vjs-settings-sub-menu-title'
108 el.appendChild(this.settingsSubMenuTitleEl_)
110 this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
111 className: 'vjs-settings-sub-menu-value'
114 el.appendChild(this.settingsSubMenuValueEl_)
116 this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
117 className: 'vjs-settings-sub-menu'
124 * Handle click on menu item
126 * @method handleClick
129 this.menuToLoad = 'submenu'
130 // Remove open class to ensure only the open submenu gets this class
131 videojsUntyped.dom.removeClass(this.el_, 'open')
135 this.mainMenu.el_.style.opacity = '0'
136 // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
137 if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
138 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
140 // animation not played without timeout
142 this.settingsSubMenuEl_.style.opacity = '1'
143 this.settingsSubMenuEl_.style.marginRight = '0px'
146 this.settingsButton.setDialogSize(this.size)
148 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
155 * @method createBackButton
157 createBackButton () {
158 const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
159 button.name_ = 'BackButton'
160 button.addClass('vjs-back-button')
161 button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_)
165 * Add/remove prefixed event listener for CSS Transition
167 * @method PrefixedEvent
169 PrefixedEvent (element: any, type: any, callback: any, action = 'addEvent') {
170 const prefix = ['webkit', 'moz', 'MS', 'o', '']
172 for (let p = 0; p < prefix.length; p++) {
174 type = type.toLowerCase()
177 if (action === 'addEvent') {
178 element.addEventListener(prefix[p] + type, callback, false)
179 } else if (action === 'removeEvent') {
180 element.removeEventListener(prefix[p] + type, callback, false)
185 onTransitionEnd (event: any) {
186 if (event.propertyName !== 'margin-right') {
190 if (this.menuToLoad === 'mainmenu') {
192 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
194 // reset opacity to 0
195 this.settingsSubMenuEl_.style.opacity = '0'
200 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
201 this.settingsSubMenuEl_.style.opacity = '0'
206 this.menuToLoad = 'mainmenu'
208 this.mainMenu.el_.style.opacity = '0'
210 // back button will always take you to main menu, so set dialog sizes
211 this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])
213 // animation not triggered without timeout (some async stuff ?!?)
215 // animate margin and opacity before hiding the submenu
216 // this triggers CSS Transition event
218 this.mainMenu.el_.style.opacity = '1'
223 this.subMenu.on('updateLabel', () => {
226 this.subMenu.on('menuChanged', () => {
227 this.bindClickEvents()
232 this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_)
233 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
234 this.panelChildEl.appendChild(this.settingsSubMenuEl_)
237 this.createBackButton()
239 this.bindClickEvents()
241 // prefixed event listeners for CSS TransitionEnd
243 this.settingsSubMenuEl_,
245 this.transitionEndHandler,
250 update (event?: any) {
251 let target: HTMLElement = null
252 const subMenu = this.subMenu.name()
254 if (event && event.type === 'tap') {
255 target = event.target
257 target = event.currentTarget
260 // Playback rate menu button doesn't get a vjs-selected class
261 // or sets options_['selected'] on the selected playback rate.
262 // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
263 if (subMenu === 'PlaybackRateMenuButton') {
264 setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
266 // Loop trough the submenu items to find the selected child
267 for (const subMenuItem of this.subMenu.menu.children_) {
268 if (!(subMenuItem instanceof component)) {
272 if (subMenuItem.hasClass('vjs-selected')) {
273 // Prefer to use the function
274 if (typeof subMenuItem.getLabel === 'function') {
275 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel()
279 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
284 if (target && !target.classList.contains('vjs-back-button')) {
285 this.settingsButton.hideDialog()
290 for (const item of this.subMenu.menu.children()) {
291 if (!(item instanceof component)) {
294 item.on(['tap', 'click'], this.submenuClickHandler)
298 // save size of submenus on first init
299 // if number of submenu items change dynamically more logic will be needed
301 this.dialog.removeClass('vjs-hidden')
302 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
303 this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
305 this.dialog.addClass('vjs-hidden')
306 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
310 const [ width ] = this.size
312 this.settingsSubMenuEl_.style.marginRight = `-${width}px`
319 // after removing settings item this.el_ === null
324 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
325 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
326 videojsUntyped.dom.removeClass(this.el_, 'open')
332 SettingsMenuItem.prototype.contentElType = 'button'
333 videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)
335 export { SettingsMenuItem }