aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/resolution-menu-item.ts
blob: b54fd91efa6f982e6695e82d9f5d65b98479ebbf (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
// 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'

const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
class ResolutionMenuItem extends MenuItem {

  constructor (player: Player, options: any) {
    const currentResolutionId = player.peertube().getCurrentResolutionId()
    options.selectable = true
    options.selected = options.id === currentResolutionId

    super(player, options)

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

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

  handleClick (event: any) {
    if (this.id === -1 && this.player_.peertube().isAutoResolutionForbidden()) return

    super.handleClick(event)

    // Auto resolution
    if (this.id === -1) {
      this.player_.peertube().enableAutoResolution()
      return
    }

    this.player_.peertube().disableAutoResolution()
    this.player_.peertube().updateResolution(this.id)
  }

  updateSelection () {
    // Check if auto resolution is forbidden or not
    if (this.id === -1) {
      if (this.player_.peertube().isAutoResolutionForbidden()) {
        this.addClass('disabled')
      } else {
        this.removeClass('disabled')
      }
    }

    if (this.player_.peertube().isAutoResolutionOn()) {
      this.selected(this.id === -1)
      return
    }

    this.selected(this.player_.peertube().getCurrentResolutionId() === this.id)
  }

  getLabel () {
    if (this.id === -1) {
      return this.label + ' <small>' + this.player_.peertube().getCurrentResolutionLabel() + '</small>'
    }

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

export { ResolutionMenuItem }