aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/resolution-menu-button.ts
blob: a3c1108cac2b9debd6f4f236ca20c5d34ae65164 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// FIXME: something weird with our path definition in tsconfig and typings
// @ts-ignore
import { Player } from 'video.js'

import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
import { ResolutionMenuItem } from './resolution-menu-item'

const Menu: VideoJSComponentInterface = videojsUntyped.getComponent('Menu')
const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton')
class ResolutionMenuButton extends MenuButton {
  label: HTMLElement

  constructor (player: Player, options: any) {
    super(player, options)
    this.player = player

    player.peertube().on('videoFileUpdate', () => this.updateLabel())
    player.peertube().on('autoResolutionUpdate', () => this.updateLabel())
  }

  createEl () {
    const el = super.createEl()

    this.labelEl_ = videojsUntyped.dom.createEl('div', {
      className: 'vjs-resolution-value',
      innerHTML: this.buildLabelHTML()
    })

    el.appendChild(this.labelEl_)

    return el
  }

  updateARIAAttributes () {
    this.el().setAttribute('aria-label', 'Quality')
  }

  createMenu () {
    const menu = new Menu(this.player_)
    for (const videoFile of this.player_.peertube().videoFiles) {
      let label = videoFile.resolution.label
      if (videoFile.fps && videoFile.fps >= 50) {
        label += videoFile.fps
      }

      menu.addChild(new ResolutionMenuItem(
        this.player_,
        {
          id: videoFile.resolution.id,
          label,
          src: videoFile.magnetUri
        })
      )
    }

    menu.addChild(new ResolutionMenuItem(
      this.player_,
      {
        id: -1,
        label: this.player_.localize('Auto'),
        src: null
      }
    ))

    return menu
  }

  updateLabel () {
    if (!this.labelEl_) return

    this.labelEl_.innerHTML = this.buildLabelHTML()
  }

  buildCSSClass () {
    return super.buildCSSClass() + ' vjs-resolution-button'
  }

  buildWrapperCSSClass () {
    return 'vjs-resolution-control ' + super.buildWrapperCSSClass()
  }

  private buildLabelHTML () {
    return this.player_.peertube().getCurrentResolutionLabel()
  }
}
ResolutionMenuButton.prototype.controlText_ = 'Quality'

MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton)