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