]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/videojs-components/settings-menu-button.ts
Fix control bar inactive timeout
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / videojs-components / settings-menu-button.ts
CommitLineData
c6352f2c
C
1// Author: Yanko Shterev
2// Thanks https://github.com/yshterev/videojs-settings-menu
3
c199c427
C
4// FIXME: something weird with our path definition in tsconfig and typings
5// @ts-ignore
6import * as videojs from 'video.js'
7
c6352f2c 8import { SettingsMenuItem } from './settings-menu-item'
2adfc7ea
C
9import { VideoJSComponentInterface, videojsUntyped } from '../peertube-videojs-typings'
10import { toTitleCase } from '../utils'
c6352f2c
C
11
12const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button')
13const Menu: VideoJSComponentInterface = videojsUntyped.getComponent('Menu')
14const Component: VideoJSComponentInterface = videojsUntyped.getComponent('Component')
15
16class SettingsButton extends Button {
16b55259
C
17 playerComponent = videojs.Player
18 dialog: any
19 dialogEl: any
20 menu: any
21 panel: any
22 panelChild: any
23
24 addSettingsItemHandler: Function
25 disposeSettingsItemHandler: Function
26 playerClickHandler: Function
27 userInactiveHandler: Function
28
c199c427 29 constructor (player: videojs.Player, options: any) {
c6352f2c
C
30 super(player, options)
31
32 this.playerComponent = player
33 this.dialog = this.playerComponent.addChild('settingsDialog')
34 this.dialogEl = this.dialog.el_
35 this.menu = null
36 this.panel = this.dialog.addChild('settingsPanel')
37 this.panelChild = this.panel.addChild('settingsPanelChild')
38
39 this.addClass('vjs-settings')
40 this.el_.setAttribute('aria-label', 'Settings Button')
41
42 // Event handlers
43 this.addSettingsItemHandler = this.onAddSettingsItem.bind(this)
44 this.disposeSettingsItemHandler = this.onDisposeSettingsItem.bind(this)
45 this.playerClickHandler = this.onPlayerClick.bind(this)
46 this.userInactiveHandler = this.onUserInactive.bind(this)
47
48 this.buildMenu()
49 this.bindEvents()
50
b891f9bc 51 // Prepare the dialog
c6352f2c
C
52 this.player().one('play', () => this.hideDialog())
53 }
54
55 onPlayerClick (event: MouseEvent) {
56 const element = event.target as HTMLElement
57 if (element.classList.contains('vjs-settings') || element.parentElement.classList.contains('vjs-settings')) {
58 return
59 }
60
61 if (!this.dialog.hasClass('vjs-hidden')) {
62 this.hideDialog()
63 }
64 }
65
c199c427 66 onDisposeSettingsItem (event: any, name: string) {
c6352f2c 67 if (name === undefined) {
c4710631 68 const children = this.menu.children()
c6352f2c
C
69
70 while (children.length > 0) {
71 children[0].dispose()
72 this.menu.removeChild(children[0])
73 }
74
75 this.addClass('vjs-hidden')
76 } else {
c4710631 77 const item = this.menu.getChild(name)
c6352f2c
C
78
79 if (item) {
80 item.dispose()
81 this.menu.removeChild(item)
82 }
83 }
84
85 this.hideDialog()
86
87 if (this.options_.entries.length === 0) {
88 this.addClass('vjs-hidden')
89 }
90 }
91
c199c427 92 onAddSettingsItem (event: any, data: any) {
c6352f2c
C
93 const [ entry, options ] = data
94
95 this.addMenuItem(entry, options)
96 this.removeClass('vjs-hidden')
97 }
98
99 onUserInactive () {
100 if (!this.dialog.hasClass('vjs-hidden')) {
101 this.hideDialog()
102 }
103 }
104
105 bindEvents () {
106 this.playerComponent.on('click', this.playerClickHandler)
107 this.playerComponent.on('addsettingsitem', this.addSettingsItemHandler)
108 this.playerComponent.on('disposesettingsitem', this.disposeSettingsItemHandler)
109 this.playerComponent.on('userinactive', this.userInactiveHandler)
110 }
111
112 buildCSSClass () {
113 return `vjs-icon-settings ${super.buildCSSClass()}`
114 }
115
116 handleClick () {
117 if (this.dialog.hasClass('vjs-hidden')) {
118 this.showDialog()
119 } else {
120 this.hideDialog()
121 }
122 }
123
124 showDialog () {
d1f21ebb
C
125 this.player_.peertube().onMenuOpen()
126
c6352f2c
C
127 this.menu.el_.style.opacity = '1'
128 this.dialog.show()
129
130 this.setDialogSize(this.getComponentSize(this.menu))
131 }
132
133 hideDialog () {
d1f21ebb
C
134 this.player_.peertube().onMenuClosed()
135
c6352f2c
C
136 this.dialog.hide()
137 this.setDialogSize(this.getComponentSize(this.menu))
138 this.menu.el_.style.opacity = '1'
139 this.resetChildren()
140 }
141
244b4ae3 142 getComponentSize (element: any) {
c6352f2c
C
143 let width: number = null
144 let height: number = null
145
146 // Could be component or just DOM element
147 if (element instanceof Component) {
148 width = element.el_.offsetWidth
149 height = element.el_.offsetHeight
150
151 // keep width/height as properties for direct use
152 element.width = width
153 element.height = height
154 } else {
155 width = element.offsetWidth
156 height = element.offsetHeight
157 }
158
159 return [ width, height ]
160 }
161
162 setDialogSize ([ width, height ]: number[]) {
163 if (typeof height !== 'number') {
164 return
165 }
166
c4710631
C
167 const offset = this.options_.setup.maxHeightOffset
168 const maxHeight = this.playerComponent.el_.offsetHeight - offset
c6352f2c
C
169
170 if (height > maxHeight) {
171 height = maxHeight
172 width += 17
173 this.panel.el_.style.maxHeight = `${height}px`
174 } else if (this.panel.el_.style.maxHeight !== '') {
175 this.panel.el_.style.maxHeight = ''
176 }
177
178 this.dialogEl.style.width = `${width}px`
179 this.dialogEl.style.height = `${height}px`
180 }
181
182 buildMenu () {
183 this.menu = new Menu(this.player())
184 this.menu.addClass('vjs-main-menu')
c4710631 185 const entries = this.options_.entries
c6352f2c
C
186
187 if (entries.length === 0) {
188 this.addClass('vjs-hidden')
189 this.panelChild.addChild(this.menu)
190 return
191 }
192
c4710631 193 for (const entry of entries) {
c6352f2c
C
194 this.addMenuItem(entry, this.options_)
195 }
196
197 this.panelChild.addChild(this.menu)
198 }
199
244b4ae3 200 addMenuItem (entry: any, options: any) {
951ef829 201 const openSubMenu = function (this: any) {
c6352f2c
C
202 if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
203 videojsUntyped.dom.removeClass(this.el_, 'open')
204 } else {
205 videojsUntyped.dom.addClass(this.el_, 'open')
206 }
207 }
208
209 options.name = toTitleCase(entry)
c4710631 210 const settingsMenuItem = new SettingsMenuItem(this.player(), options, entry, this as any)
c6352f2c
C
211
212 this.menu.addChild(settingsMenuItem)
213
214 // Hide children to avoid sub menus stacking on top of each other
215 // or having multiple menus open
216 settingsMenuItem.on('click', videojs.bind(this, this.hideChildren))
217
218 // Whether to add or remove selected class on the settings sub menu element
219 settingsMenuItem.on('click', openSubMenu)
220 }
221
222 resetChildren () {
c4710631 223 for (const menuChild of this.menu.children()) {
c6352f2c
C
224 menuChild.reset()
225 }
226 }
227
228 /**
229 * Hide all the sub menus
230 */
231 hideChildren () {
c4710631 232 for (const menuChild of this.menu.children()) {
c6352f2c
C
233 menuChild.hideSubMenu()
234 }
235 }
236
237}
238
239class SettingsPanel extends Component {
c199c427 240 constructor (player: videojs.Player, options: any) {
c6352f2c
C
241 super(player, options)
242 }
243
244 createEl () {
245 return super.createEl('div', {
246 className: 'vjs-settings-panel',
247 innerHTML: '',
248 tabIndex: -1
249 })
250 }
251}
252
253class SettingsPanelChild extends Component {
c199c427 254 constructor (player: videojs.Player, options: any) {
c6352f2c
C
255 super(player, options)
256 }
257
258 createEl () {
259 return super.createEl('div', {
260 className: 'vjs-settings-panel-child',
261 innerHTML: '',
262 tabIndex: -1
263 })
264 }
265}
266
267class SettingsDialog extends Component {
c199c427 268 constructor (player: videojs.Player, options: any) {
c6352f2c
C
269 super(player, options)
270 this.hide()
271 }
272
273 /**
274 * Create the component's DOM element
275 *
276 * @return {Element}
277 * @method createEl
278 */
279 createEl () {
280 const uniqueId = this.id_
281 const dialogLabelId = 'TTsettingsDialogLabel-' + uniqueId
282 const dialogDescriptionId = 'TTsettingsDialogDescription-' + uniqueId
283
284 return super.createEl('div', {
285 className: 'vjs-settings-dialog vjs-modal-overlay',
286 innerHTML: '',
287 tabIndex: -1
288 }, {
289 'role': 'dialog',
290 'aria-labelledby': dialogLabelId,
291 'aria-describedby': dialogDescriptionId
292 })
293 }
294
295}
296
e945b184 297SettingsButton.prototype.controlText_ = 'Settings'
c6352f2c
C
298
299Component.registerComponent('SettingsButton', SettingsButton)
300Component.registerComponent('SettingsDialog', SettingsDialog)
301Component.registerComponent('SettingsPanel', SettingsPanel)
302Component.registerComponent('SettingsPanelChild', SettingsPanelChild)
303
304export { SettingsButton, SettingsDialog, SettingsPanel, SettingsPanelChild }