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