1 // Author: Yanko Shterev
2 // Thanks https://github.com/yshterev/videojs-settings-menu
4 import * as videojs from 'video.js'
5 import { toTitleCase } from './utils'
6 import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
8 const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
9 const component: VideoJSComponentInterface = videojsUntyped.getComponent('Component')
11 class SettingsMenuItem extends MenuItem {
13 constructor (player: videojs.Player, options, entry: string, menuButton: VideoJSComponentInterface) {
14 super(player, options)
16 this.settingsButton = menuButton
17 this.dialog = this.settingsButton.dialog
18 this.mainMenu = this.settingsButton.menu
19 this.panel = this.dialog.getChild('settingsPanel')
20 this.panelChild = this.panel.getChild('settingsPanelChild')
21 this.panelChildEl = this.panelChild.el_
25 // keep state of what menu type is loading next
26 this.menuToLoad = 'mainmenu'
28 const subMenuName = toTitleCase(entry)
29 const SubMenuComponent = videojsUntyped.getComponent(subMenuName)
31 if (!SubMenuComponent) {
32 throw new Error(`Component ${subMenuName} does not exist`)
34 this.subMenu = new SubMenuComponent(this.player(), options, menuButton, this)
35 const subMenuClass = this.subMenu.buildCSSClass().split(' ')[0]
36 this.settingsSubMenuEl_.className += ' ' + subMenuClass
41 // Voodoo magic for IOS
50 this.submenuClickHandler = this.onSubmenuClick.bind(this)
51 this.transitionEndHandler = this.onTransitionEnd.bind(this)
54 onSubmenuClick (event) {
57 if (event.type === 'tap') {
60 target = event.currentTarget
63 if (target.classList.contains('vjs-back-button')) {
68 // To update the sub menu value on click, setTimeout is needed because
69 // updating the value is not instant
70 setTimeout(() => this.update(event), 0)
74 * Create the component's DOM element
80 const el = videojsUntyped.dom.createEl('li', {
81 className: 'vjs-menu-item'
84 this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
85 className: 'vjs-settings-sub-menu-title'
88 el.appendChild(this.settingsSubMenuTitleEl_)
90 this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
91 className: 'vjs-settings-sub-menu-value'
94 el.appendChild(this.settingsSubMenuValueEl_)
96 this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
97 className: 'vjs-settings-sub-menu'
104 * Handle click on menu item
106 * @method handleClick
109 this.menuToLoad = 'submenu'
110 // Remove open class to ensure only the open submenu gets this class
111 videojsUntyped.dom.removeClass(this.el_, 'open')
115 this.mainMenu.el_.style.opacity = '0'
116 // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
117 if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
118 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
120 // animation not played without timeout
122 this.settingsSubMenuEl_.style.opacity = '1'
123 this.settingsSubMenuEl_.style.marginRight = '0px'
126 this.settingsButton.setDialogSize(this.size)
128 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
135 * @method createBackButton
137 createBackButton () {
138 const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
139 button.name_ = 'BackButton'
140 button.addClass('vjs-back-button')
141 button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_)
145 * Add/remove prefixed event listener for CSS Transition
147 * @method PrefixedEvent
149 PrefixedEvent (element, type, callback, action = 'addEvent') {
150 let prefix = ['webkit', 'moz', 'MS', 'o', '']
152 for (let p = 0; p < prefix.length; p++) {
154 type = type.toLowerCase()
157 if (action === 'addEvent') {
158 element.addEventListener(prefix[p] + type, callback, false)
159 } else if (action === 'removeEvent') {
160 element.removeEventListener(prefix[p] + type, callback, false)
165 onTransitionEnd (event) {
166 if (event.propertyName !== 'margin-right') {
170 if (this.menuToLoad === 'mainmenu') {
172 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
174 // reset opacity to 0
175 this.settingsSubMenuEl_.style.opacity = '0'
180 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
181 this.settingsSubMenuEl_.style.opacity = '0'
186 this.menuToLoad = 'mainmenu'
188 this.mainMenu.el_.style.opacity = '0'
190 // back button will always take you to main menu, so set dialog sizes
191 this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])
193 // animation not triggered without timeout (some async stuff ?!?)
195 // animate margin and opacity before hiding the submenu
196 // this triggers CSS Transition event
198 this.mainMenu.el_.style.opacity = '1'
203 const saveUpdateLabel = this.subMenu.updateLabel
204 this.subMenu.updateLabel = () => {
207 saveUpdateLabel.call(this.subMenu)
210 this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_)
211 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
212 this.panelChildEl.appendChild(this.settingsSubMenuEl_)
215 this.createBackButton()
217 this.bindClickEvents()
219 // prefixed event listeners for CSS TransitionEnd
221 this.settingsSubMenuEl_,
223 this.transitionEndHandler,
228 update (event?: Event) {
230 let subMenu = this.subMenu.name()
232 if (event && event.type === 'tap') {
233 target = event.target
235 target = event.currentTarget
238 // Playback rate menu button doesn't get a vjs-selected class
239 // or sets options_['selected'] on the selected playback rate.
240 // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
241 if (subMenu === 'PlaybackRateMenuButton') {
242 setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
244 // Loop trough the submenu items to find the selected child
245 for (let subMenuItem of this.subMenu.menu.children_) {
246 if (!(subMenuItem instanceof component)) {
250 if (subMenuItem.hasClass('vjs-selected')) {
251 // Prefer to use the function
252 if (typeof subMenuItem.getLabel === 'function') {
253 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel()
257 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
262 if (target && !target.classList.contains('vjs-back-button')) {
263 this.settingsButton.hideDialog()
268 for (let item of this.subMenu.menu.children()) {
269 if (!(item instanceof component)) {
272 item.on(['tap', 'click'], this.submenuClickHandler)
276 // save size of submenus on first init
277 // if number of submenu items change dynamically more logic will be needed
279 this.dialog.removeClass('vjs-hidden')
280 this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
282 this.dialog.addClass('vjs-hidden')
283 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
287 let [width] = this.size
289 this.settingsSubMenuEl_.style.marginRight = `-${width}px`
296 // after removing settings item this.el_ === null
301 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
302 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
303 videojsUntyped.dom.removeClass(this.el_, 'open')
309 SettingsMenuItem.prototype.contentElType = 'button'
310 videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)
312 export { SettingsMenuItem }