1 import videojs from 'video.js'
2 import { ResolutionMenuItem } from './resolution-menu-item'
4 const Menu = videojs.getComponent('Menu')
5 const MenuButton = videojs.getComponent('MenuButton')
6 class ResolutionMenuButton extends MenuButton {
9 constructor (player: videojs.Player, options?: videojs.MenuButtonOptions) {
10 super(player, options)
12 this.controlText('Quality')
14 player.peertubeResolutions().on('resolutionsAdded', () => this.buildQualities())
15 player.peertubeResolutions().on('resolutionRemoved', () => this.cleanupQualities())
18 player.peertubeResolutions().on('resolutionChanged', () => {
19 setTimeout(() => this.trigger('labelUpdated'))
24 const el = super.createEl()
26 this.labelEl_ = videojs.dom.createEl('div', {
27 className: 'vjs-resolution-value'
30 el.appendChild(this.labelEl_)
35 updateARIAAttributes () {
36 this.el().setAttribute('aria-label', 'Quality')
40 return new Menu(this.player_)
44 return super.buildCSSClass() + ' vjs-resolution-button'
47 buildWrapperCSSClass () {
48 return 'vjs-resolution-control ' + super.buildWrapperCSSClass()
51 private addClickListener (component: any) {
52 component.on('click', () => {
53 const children = this.menu.children()
55 for (const child of children) {
56 if (component !== child) {
57 (child as videojs.MenuItem).selected(false)
63 private buildQualities () {
64 for (const d of this.player().peertubeResolutions().getResolutions()) {
65 const label = d.label === '0p'
66 ? this.player().localize('Audio-only')
69 this.menu.addChild(new ResolutionMenuItem(
80 for (const m of this.menu.children()) {
81 this.addClickListener(m)
84 this.trigger('menuChanged')
87 private cleanupQualities () {
88 const resolutions = this.player().peertubeResolutions().getResolutions()
90 this.menu.children().forEach((children: ResolutionMenuItem) => {
91 if (children.resolutionId === undefined) {
95 if (resolutions.find(r => r.id === children.resolutionId)) {
99 this.menu.removeChild(children)
102 this.trigger('menuChanged')
106 videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)