aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/videojs-components/resolution-menu-button.ts
blob: 0fa6272e7bcf8ac2183efa6e32f252760f8c9bb7 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import videojs, { VideoJsPlayer } from 'video.js'

import { LoadedQualityData } from '../peertube-videojs-typings'
import { ResolutionMenuItem } from './resolution-menu-item'

const Menu = videojs.getComponent('Menu')
const MenuButton = videojs.getComponent('MenuButton')
class ResolutionMenuButton extends MenuButton {
  labelEl_: HTMLElement

  constructor (player: VideoJsPlayer, options?: videojs.MenuButtonOptions) {
    super(player, options)

    this.controlText('Quality')

    player.tech(true).on('loadedqualitydata', (e: any, data: any) => this.buildQualities(data))

    player.peertube().on('resolutionChange', () => setTimeout(() => this.trigger('updateLabel'), 0))
  }

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

    this.labelEl_ = videojs.dom.createEl('div', {
      className: 'vjs-resolution-value'
    }) 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()
  }

  private addClickListener (component: any) {
    component.on('click', () => {
      const children = this.menu.children()

      for (const child of children) {
        if (component !== child) {
          (child as videojs.MenuItem).selected(false)
        }
      }
    })
  }

  private buildQualities (data: LoadedQualityData) {
    // The automatic resolution item will need other labels
    const labels: { [ id: number ]: string } = {}

    data.qualityData.video.sort((a, b) => {
      if (a.id > b.id) return -1
      if (a.id === b.id) return 0
      return 1
    })

    for (const d of data.qualityData.video) {
      // Skip auto resolution, we'll add it ourselves
      if (d.id === -1) continue

      const label = d.label === '0p'
        ? this.player().localize('Audio-only')
        : d.label

      this.menu.addChild(new ResolutionMenuItem(
        this.player_,
        {
          id: d.id,
          label,
          selected: d.selected,
          callback: data.qualitySwitchCallback
        })
      )

      labels[d.id] = d.label
    }

    this.menu.addChild(new ResolutionMenuItem(
      this.player_,
      {
        id: -1,
        label: this.player_.localize('Auto'),
        labels,
        callback: data.qualitySwitchCallback,
        selected: true // By default, in auto mode
      }
    ))

    for (const m of this.menu.children()) {
      this.addClickListener(m)
    }

    this.trigger('menuChanged')
  }
}

videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)