]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/videojs-components/resolution-menu-button.ts
Try to fix mock server ports
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / videojs-components / resolution-menu-button.ts
1 import videojs from 'video.js'
2
3 import { LoadedQualityData } from '../peertube-videojs-typings'
4 import { ResolutionMenuItem } from './resolution-menu-item'
5
6 const Menu = videojs.getComponent('Menu')
7 const MenuButton = videojs.getComponent('MenuButton')
8 class ResolutionMenuButton extends MenuButton {
9 labelEl_: HTMLElement
10
11 constructor (player: videojs.Player, options?: videojs.MenuButtonOptions) {
12 super(player, options)
13
14 this.controlText('Quality')
15
16 player.tech(true).on('loadedqualitydata', (e: any, data: any) => this.buildQualities(data))
17
18 player.peertube().on('resolutionChange', () => setTimeout(() => this.trigger('updateLabel'), 0))
19 }
20
21 createEl () {
22 const el = super.createEl()
23
24 this.labelEl_ = videojs.dom.createEl('div', {
25 className: 'vjs-resolution-value'
26 }) as HTMLElement
27
28 el.appendChild(this.labelEl_)
29
30 return el
31 }
32
33 updateARIAAttributes () {
34 this.el().setAttribute('aria-label', 'Quality')
35 }
36
37 createMenu () {
38 return new Menu(this.player_)
39 }
40
41 buildCSSClass () {
42 return super.buildCSSClass() + ' vjs-resolution-button'
43 }
44
45 buildWrapperCSSClass () {
46 return 'vjs-resolution-control ' + super.buildWrapperCSSClass()
47 }
48
49 private addClickListener (component: any) {
50 component.on('click', () => {
51 const children = this.menu.children()
52
53 for (const child of children) {
54 if (component !== child) {
55 (child as videojs.MenuItem).selected(false)
56 }
57 }
58 })
59 }
60
61 private buildQualities (data: LoadedQualityData) {
62 // The automatic resolution item will need other labels
63 const labels: { [ id: number ]: string } = {}
64
65 data.qualityData.video.sort((a, b) => {
66 if (a.id > b.id) return -1
67 if (a.id === b.id) return 0
68 return 1
69 })
70
71 for (const d of data.qualityData.video) {
72 // Skip auto resolution, we'll add it ourselves
73 if (d.id === -1) continue
74
75 const label = d.label === '0p'
76 ? this.player().localize('Audio-only')
77 : d.label
78
79 this.menu.addChild(new ResolutionMenuItem(
80 this.player_,
81 {
82 id: d.id,
83 label,
84 selected: d.selected,
85 callback: data.qualitySwitchCallback
86 })
87 )
88
89 labels[d.id] = d.label
90 }
91
92 this.menu.addChild(new ResolutionMenuItem(
93 this.player_,
94 {
95 id: -1,
96 label: this.player_.localize('Auto'),
97 labels,
98 callback: data.qualitySwitchCallback,
99 selected: true // By default, in auto mode
100 }
101 ))
102
103 for (const m of this.menu.children()) {
104 this.addClickListener(m)
105 }
106
107 this.trigger('menuChanged')
108 }
109 }
110
111 videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)