1 // Author: Yanko Shterev
2 // Thanks https://github.com/yshterev/videojs-settings-menu
4 import { toTitleCase } from './utils'
5 import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
7 const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
8 const component: VideoJSComponentInterface = videojsUntyped.getComponent('Component')
10 class SettingsMenuItem extends MenuItem {
12 constructor (player: videojs.Player, options, entry: string, menuButton: VideoJSComponentInterface) {
13 super(player, options)
15 this.settingsButton = menuButton
16 this.dialog = this.settingsButton.dialog
17 this.mainMenu = this.settingsButton.menu
18 this.panel = this.dialog.getChild('settingsPanel')
19 this.panelChild = this.panel.getChild('settingsPanelChild')
20 this.panelChildEl = this.panelChild.el_
24 // keep state of what menu type is loading next
25 this.menuToLoad = 'mainmenu'
27 const subMenuName = toTitleCase(entry)
28 const SubMenuComponent = videojsUntyped.getComponent(subMenuName)
30 if (!SubMenuComponent) {
31 throw new Error(`Component ${subMenuName} does not exist`)
33 this.subMenu = new SubMenuComponent(this.player(), options, menuButton, this)
44 this.submenuClickHandler = this.onSubmenuClick.bind(this)
45 this.transitionEndHandler = this.onTransitionEnd.bind(this)
48 onSubmenuClick (event) {
51 if (event.type === 'tap') {
54 target = event.currentTarget
57 if (target.classList.contains('vjs-back-button')) {
62 // To update the sub menu value on click, setTimeout is needed because
63 // updating the value is not instant
64 setTimeout(() => this.update(event), 0)
68 * Create the component's DOM element
74 const el = videojsUntyped.dom.createEl('li', {
75 className: 'vjs-menu-item'
78 this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
79 className: 'vjs-settings-sub-menu-title'
82 el.appendChild(this.settingsSubMenuTitleEl_)
84 this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
85 className: 'vjs-settings-sub-menu-value'
88 el.appendChild(this.settingsSubMenuValueEl_)
90 this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
91 className: 'vjs-settings-sub-menu'
98 * Handle click on menu item
100 * @method handleClick
103 this.menuToLoad = 'submenu'
104 // Remove open class to ensure only the open submenu gets this class
105 videojsUntyped.dom.removeClass(this.el_, 'open')
109 this.mainMenu.el_.style.opacity = '0'
110 // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
111 if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
112 videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')
114 // animation not played without timeout
116 this.settingsSubMenuEl_.style.opacity = '1'
117 this.settingsSubMenuEl_.style.marginRight = '0px'
120 this.settingsButton.setDialogSize(this.size)
122 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
129 * @method createBackButton
131 createBackButton () {
132 const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
133 button.name_ = 'BackButton'
134 button.addClass('vjs-back-button')
135 button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_)
139 * Add/remove prefixed event listener for CSS Transition
141 * @method PrefixedEvent
143 PrefixedEvent (element, type, callback, action = 'addEvent') {
144 let prefix = ['webkit', 'moz', 'MS', 'o', '']
146 for (let p = 0; p < prefix.length; p++) {
148 type = type.toLowerCase()
151 if (action === 'addEvent') {
152 element.addEventListener(prefix[p] + type, callback, false)
153 } else if (action === 'removeEvent') {
154 element.removeEventListener(prefix[p] + type, callback, false)
159 onTransitionEnd (event) {
160 if (event.propertyName !== 'margin-right') {
164 if (this.menuToLoad === 'mainmenu') {
166 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
168 // reset opacity to 0
169 this.settingsSubMenuEl_.style.opacity = '0'
174 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
175 this.settingsSubMenuEl_.style.opacity = '0'
180 this.menuToLoad = 'mainmenu'
182 this.mainMenu.el_.style.opacity = '0'
184 // back button will always take you to main menu, so set dialog sizes
185 this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])
187 // animation not triggered without timeout (some async stuff ?!?)
189 // animate margin and opacity before hiding the submenu
190 // this triggers CSS Transition event
192 this.mainMenu.el_.style.opacity = '1'
197 const saveUpdateLabel = this.subMenu.updateLabel
198 this.subMenu.updateLabel = () => {
201 saveUpdateLabel.call(this.subMenu)
204 this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_)
205 this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
206 this.panelChildEl.appendChild(this.settingsSubMenuEl_)
209 this.createBackButton()
211 this.bindClickEvents()
213 // prefixed event listeners for CSS TransitionEnd
215 this.settingsSubMenuEl_,
217 this.transitionEndHandler,
222 update (event?: Event) {
224 let subMenu = this.subMenu.name()
226 if (event && event.type === 'tap') {
227 target = event.target
229 target = event.currentTarget
232 // Playback rate menu button doesn't get a vjs-selected class
233 // or sets options_['selected'] on the selected playback rate.
234 // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
235 if (subMenu === 'PlaybackRateMenuButton') {
236 setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
238 // Loop trough the submenu items to find the selected child
239 for (let subMenuItem of this.subMenu.menu.children_) {
240 if (!(subMenuItem instanceof component)) {
244 if (subMenuItem.hasClass('vjs-selected')) {
245 // Prefer to use the function
246 if (typeof subMenuItem.getLabel === 'function') {
247 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel()
251 this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
256 if (target && !target.classList.contains('vjs-back-button')) {
257 this.settingsButton.hideDialog()
262 for (let item of this.subMenu.menu.children()) {
263 if (!(item instanceof component)) {
266 item.on(['tap', 'click'], this.submenuClickHandler)
270 // save size of submenus on first init
271 // if number of submenu items change dynamically more logic will be needed
273 this.dialog.removeClass('vjs-hidden')
274 this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
276 this.dialog.addClass('vjs-hidden')
277 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
281 let [width] = this.size
283 this.settingsSubMenuEl_.style.marginRight = `-${width}px`
290 // after removing settings item this.el_ === null
295 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
296 videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
297 videojsUntyped.dom.removeClass(this.el_, 'open')
303 SettingsMenuItem.prototype.contentElType = 'button'
304 videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)
306 export { SettingsMenuItem }