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)
45 this.submenuClickHandler = this.onSubmenuClick.bind(this)
46 this.transitionEndHandler = this.onTransitionEnd.bind(this)
49 onSubmenuClick (event) {
52 if (event.type === 'tap') {
55 target = event.currentTarget
58 if (target.classList.contains('vjs-back-button')) {
63 // To update the sub menu value on click, setTimeout is needed because
64 // updating the value is not instant
65 setTimeout(() => this.update(event), 0)
69 * Create the component's DOM element
75 const el = videojsUntyped.dom.createEl('li', {
76 className: 'vjs-menu-item'
79 this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
80 className: 'vjs-settings-sub-menu-title'
83 el.appendChild(this.settingsSubMenuTitleEl_)
85 this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
86 className: 'vjs-settings-sub-menu-value'
89 el.appendChild(this.settingsSubMenuValueEl_)
91 this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
92 className: 'vjs-settings-sub-menu'
99 * Handle click on menu item
101 * @method handleClick
104 this.menuToLoad = 'submenu'
105 // Remove open class to ensure only the open submenu gets this class
106 videojsUntyped.dom.removeClass(this.el_, 'open')
110 this.mainMenu.el_.style.opacity = '0'
111 // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
112 if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
113 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
115 // animation not played without timeout
117 this.settingsSubMenuEl_.style.opacity = '1'
118 this.settingsSubMenuEl_.style.marginRight = '0px'
121 this.settingsButton.setDialogSize(this.size)
123 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
130 * @method createBackButton
132 createBackButton () {
133 const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
134 button.name_ = 'BackButton'
135 button.addClass('vjs-back-button')
136 button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_)
140 * Add/remove prefixed event listener for CSS Transition
142 * @method PrefixedEvent
144 PrefixedEvent (element, type, callback, action = 'addEvent') {
145 let prefix = ['webkit', 'moz', 'MS', 'o', '']
147 for (let p = 0; p < prefix.length; p++) {
149 type = type.toLowerCase()
152 if (action === 'addEvent') {
153 element.addEventListener(prefix[p] + type, callback, false)
154 } else if (action === 'removeEvent') {
155 element.removeEventListener(prefix[p] + type, callback, false)
160 onTransitionEnd (event) {
161 if (event.propertyName !== 'margin-right') {
165 if (this.menuToLoad === 'mainmenu') {
167 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
169 // reset opacity to 0
170 this.settingsSubMenuEl_.style.opacity = '0'
175 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
176 this.settingsSubMenuEl_.style.opacity = '0'
181 this.menuToLoad = 'mainmenu'
183 this.mainMenu.el_.style.opacity = '0'
185 // back button will always take you to main menu, so set dialog sizes
186 this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])
188 // animation not triggered without timeout (some async stuff ?!?)
190 // animate margin and opacity before hiding the submenu
191 // this triggers CSS Transition event
193 this.mainMenu.el_.style.opacity = '1'
198 const saveUpdateLabel = this.subMenu.updateLabel
199 this.subMenu.updateLabel = () => {
202 saveUpdateLabel.call(this.subMenu)
205 this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_)
206 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
207 this.panelChildEl.appendChild(this.settingsSubMenuEl_)
210 this.createBackButton()
212 this.bindClickEvents()
214 // prefixed event listeners for CSS TransitionEnd
216 this.settingsSubMenuEl_,
218 this.transitionEndHandler,
223 update (event?: Event) {
225 let subMenu = this.subMenu.name()
227 if (event && event.type === 'tap') {
228 target = event.target
230 target = event.currentTarget
233 // Playback rate menu button doesn't get a vjs-selected class
234 // or sets options_['selected'] on the selected playback rate.
235 // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
236 if (subMenu === 'PlaybackRateMenuButton') {
237 setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
239 // Loop trough the submenu items to find the selected child
240 for (let subMenuItem of this.subMenu.menu.children_) {
241 if (!(subMenuItem instanceof component)) {
245 if (subMenuItem.hasClass('vjs-selected')) {
246 // Prefer to use the function
247 if (typeof subMenuItem.getLabel === 'function') {
248 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel()
252 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
257 if (target && !target.classList.contains('vjs-back-button')) {
258 this.settingsButton.hideDialog()
263 for (let item of this.subMenu.menu.children()) {
264 if (!(item instanceof component)) {
267 item.on(['tap', 'click'], this.submenuClickHandler)
271 // save size of submenus on first init
272 // if number of submenu items change dynamically more logic will be needed
274 this.dialog.removeClass('vjs-hidden')
275 this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
277 this.dialog.addClass('vjs-hidden')
278 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
282 let [width] = this.size
284 this.settingsSubMenuEl_.style.marginRight = `-${width}px`
291 // after removing settings item this.el_ === null
296 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
297 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
298 videojsUntyped.dom.removeClass(this.el_, 'open')
304 SettingsMenuItem.prototype.contentElType = 'button'
305 videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)
307 export { SettingsMenuItem }