aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/videojs-components/resolution-menu-item.ts
blob: 6047f52f7878623cee01a0b69f8794dd925e5fc6 (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
import videojs from 'video.js'

const MenuItem = videojs.getComponent('MenuItem')

export interface ResolutionMenuItemOptions extends videojs.MenuItemOptions {
  id: number
}

class ResolutionMenuItem extends MenuItem {
  private readonly resolutionId: number
  private readonly label: string

  private autoResolutionEnabled: boolean
  private autoResolutionChosen: string

  constructor (player: videojs.Player, options?: ResolutionMenuItemOptions) {
    options.selectable = true

    super(player, options)

    this.autoResolutionEnabled = true
    this.autoResolutionChosen = ''

    this.resolutionId = options.id
    this.label = options.label

    player.peertubeResolutions().on('resolutionChanged', () => this.updateSelection())

    // We only want to disable the "Auto" item
    if (this.resolutionId === -1) {
      player.peertubeResolutions().on('autoResolutionEnabledChanged', () => this.updateAutoResolution())
    }
  }

  handleClick (event: any) {
    // Auto button disabled?
    if (this.autoResolutionEnabled === false && this.resolutionId === -1) return

    super.handleClick(event)

    this.player().peertubeResolutions().select({ id: this.resolutionId, byEngine: false })
  }

  updateSelection () {
    const selectedResolution = this.player().peertubeResolutions().getSelected()

    if (this.resolutionId === -1) {
      this.autoResolutionChosen = this.player().peertubeResolutions().getAutoResolutionChosen()?.label
    }

    this.selected(this.resolutionId === selectedResolution.id)
  }

  updateAutoResolution () {
    const enabled = this.player().peertubeResolutions().isAutoResolutionEnabeld()

    // Check if the auto resolution is enabled or not
    if (enabled === false) {
      this.addClass('disabled')
    } else {
      this.removeClass('disabled')
    }

    this.autoResolutionEnabled = enabled
  }

  getLabel () {
    if (this.resolutionId === -1) {
      return this.label + ' <small>' + this.autoResolutionChosen + '</small>'
    }

    return this.label
  }
}
videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem)

export { ResolutionMenuItem }