aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/settings/resolution-menu-button.ts
blob: 8bd5b4f03610220379bac16719e9f4e46410fff3 (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
import videojs from 'video.js'
import { ResolutionMenuItem } from './resolution-menu-item'

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

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

    this.controlText('Quality')

    player.peertubeResolutions().on('resolutionsAdded', () => this.buildQualities())

    // For parent
    player.peertubeResolutions().on('resolutionChanged', () => {
      setTimeout(() => this.trigger('labelUpdated'))
    })
  }

  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 () {
    for (const d of this.player().peertubeResolutions().getResolutions()) {
      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
        })
      )
    }

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

    this.trigger('menuChanged')
  }
}

videojs.registerComponent('ResolutionMenuButton', ResolutionMenuButton)