2020-04-21 11:02:28 +02:00
|
|
|
import videojs from 'video.js'
|
2018-03-30 17:40:00 +02:00
|
|
|
import { ResolutionMenuItem } from './resolution-menu-item'
|
|
|
|
|
2020-01-28 17:29:50 +01:00
|
|
|
const Menu = videojs.getComponent('Menu')
|
|
|
|
const MenuButton = videojs.getComponent('MenuButton')
|
2018-03-30 17:40:00 +02:00
|
|
|
class ResolutionMenuButton extends MenuButton {
|
2020-01-28 17:29:50 +01:00
|
|
|
labelEl_: HTMLElement
|
2018-03-30 17:40:00 +02:00
|
|
|
|
2020-04-17 11:20:12 +02:00
|
|
|
constructor (player: videojs.Player, options?: videojs.MenuButtonOptions) {
|
2018-03-30 17:40:00 +02:00
|
|
|
super(player, options)
|
|
|
|
|
2020-01-28 17:29:50 +01:00
|
|
|
this.controlText('Quality')
|
|
|
|
|
2021-09-06 16:08:59 +02:00
|
|
|
player.peertubeResolutions().on('resolutionsAdded', () => this.buildQualities())
|
2022-09-28 11:52:23 +02:00
|
|
|
player.peertubeResolutions().on('resolutionRemoved', () => this.cleanupQualities())
|
2019-01-23 15:36:45 +01:00
|
|
|
|
2021-09-06 16:08:59 +02:00
|
|
|
// For parent
|
|
|
|
player.peertubeResolutions().on('resolutionChanged', () => {
|
|
|
|
setTimeout(() => this.trigger('labelUpdated'))
|
|
|
|
})
|
2018-03-30 17:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createEl () {
|
|
|
|
const el = super.createEl()
|
|
|
|
|
2020-01-28 17:29:50 +01:00
|
|
|
this.labelEl_ = videojs.dom.createEl('div', {
|
2019-01-23 15:36:45 +01:00
|
|
|
className: 'vjs-resolution-value'
|
2020-01-28 17:29:50 +01:00
|
|
|
}) as HTMLElement
|
2018-03-30 17:40:00 +02:00
|
|
|
|
|
|
|
el.appendChild(this.labelEl_)
|
|
|
|
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
|
|
|
updateARIAAttributes () {
|
|
|
|
this.el().setAttribute('aria-label', 'Quality')
|
|
|
|
}
|
|
|
|
|
|
|
|
createMenu () {
|
2019-01-23 15:36:45 +01:00
|
|
|
return new Menu(this.player_)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildCSSClass () {
|
|
|
|
return super.buildCSSClass() + ' vjs-resolution-button'
|
|
|
|
}
|
2018-06-29 16:41:29 +02:00
|
|
|
|
2019-01-23 15:36:45 +01:00
|
|
|
buildWrapperCSSClass () {
|
|
|
|
return 'vjs-resolution-control ' + super.buildWrapperCSSClass()
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:16:30 +01:00
|
|
|
private addClickListener (component: any) {
|
|
|
|
component.on('click', () => {
|
2019-04-02 18:30:26 +02:00
|
|
|
const children = this.menu.children()
|
2019-01-24 10:16:30 +01:00
|
|
|
|
|
|
|
for (const child of children) {
|
|
|
|
if (component !== child) {
|
2020-01-28 17:29:50 +01:00
|
|
|
(child as videojs.MenuItem).selected(false)
|
2019-01-24 10:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:08:59 +02:00
|
|
|
private buildQualities () {
|
|
|
|
for (const d of this.player().peertubeResolutions().getResolutions()) {
|
2020-01-29 14:22:32 +01:00
|
|
|
const label = d.label === '0p'
|
2020-01-28 17:29:50 +01:00
|
|
|
? this.player().localize('Audio-only')
|
2019-11-22 11:43:17 +01:00
|
|
|
: d.label
|
|
|
|
|
2019-01-23 15:36:45 +01:00
|
|
|
this.menu.addChild(new ResolutionMenuItem(
|
2018-03-30 17:40:00 +02:00
|
|
|
this.player_,
|
|
|
|
{
|
2022-06-13 14:05:46 +02:00
|
|
|
id: d.id + '',
|
|
|
|
resolutionId: d.id,
|
2019-11-22 11:43:17 +01:00
|
|
|
label,
|
2021-09-06 16:08:59 +02:00
|
|
|
selected: d.selected
|
2018-03-30 17:40:00 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:16:30 +01:00
|
|
|
for (const m of this.menu.children()) {
|
|
|
|
this.addClickListener(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.trigger('menuChanged')
|
2018-04-05 16:15:51 +02:00
|
|
|
}
|
2022-09-28 11:52:23 +02:00
|
|
|
|
|
|
|
private cleanupQualities () {
|
|
|
|
const resolutions = this.player().peertubeResolutions().getResolutions()
|
|
|
|
|
|
|
|
this.menu.children().forEach((children: ResolutionMenuItem) => {
|
|
|
|
if (children.resolutionId === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolutions.find(r => r.id === children.resolutionId)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.menu.removeChild(children)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.trigger('menuChanged')
|
|
|
|
}
|
2018-03-30 17:40:00 +02:00
|
|
|
}
|
2018-06-06 14:23:40 +02:00
|
|
|
|
2020-01-28 17:29:50 +01:00
|
|
|
videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)
|