PeerTube/client/src/assets/player/shared/settings/resolution-menu-button.ts

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-04-21 11:02:28 +02:00
import videojs from 'video.js'
import { ResolutionMenuItem } from './resolution-menu-item'
2020-01-28 17:29:50 +01:00
const Menu = videojs.getComponent('Menu')
const MenuButton = videojs.getComponent('MenuButton')
class ResolutionMenuButton extends MenuButton {
2020-01-28 17:29:50 +01:00
labelEl_: HTMLElement
2020-04-17 11:20:12 +02:00
constructor (player: videojs.Player, options?: videojs.MenuButtonOptions) {
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())
player.peertubeResolutions().on('resolutionRemoved', () => this.cleanupQualities())
2021-09-06 16:08:59 +02:00
// For parent
player.peertubeResolutions().on('resolutionChanged', () => {
setTimeout(() => this.trigger('labelUpdated'))
})
}
createEl () {
const el = super.createEl()
2020-01-28 17:29:50 +01:00
this.labelEl_ = videojs.dom.createEl('div', {
className: 'vjs-resolution-value'
2020-01-28 17:29:50 +01:00
}) as HTMLElement
el.appendChild(this.labelEl_)
return el
}
updateARIAAttributes () {
this.el().setAttribute('aria-label', 'Quality')
}
createMenu () {
return new Menu(this.player_)
}
buildCSSClass () {
return super.buildCSSClass() + ' vjs-resolution-button'
}
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()) {
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
this.menu.addChild(new ResolutionMenuItem(
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
})
)
}
2019-01-24 10:16:30 +01:00
for (const m of this.menu.children()) {
this.addClickListener(m)
}
this.trigger('menuChanged')
}
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-06-06 14:23:40 +02:00
2020-01-28 17:29:50 +01:00
videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)