1 import videojs from 'video.js'
3 const MenuItem = videojs.getComponent('MenuItem')
5 export interface ResolutionMenuItemOptions extends videojs.MenuItemOptions {
9 class ResolutionMenuItem extends MenuItem {
10 private readonly resolutionId: number
11 private readonly label: string
13 private autoResolutionEnabled: boolean
14 private autoResolutionChosen: string
16 constructor (player: videojs.Player, options?: ResolutionMenuItemOptions) {
17 options.selectable = true
19 super(player, options)
21 this.autoResolutionEnabled = true
22 this.autoResolutionChosen = ''
24 this.resolutionId = options.id
25 this.label = options.label
27 player.peertubeResolutions().on('resolutionChanged', () => this.updateSelection())
29 // We only want to disable the "Auto" item
30 if (this.resolutionId === -1) {
31 player.peertubeResolutions().on('autoResolutionEnabledChanged', () => this.updateAutoResolution())
35 handleClick (event: any) {
36 // Auto button disabled?
37 if (this.autoResolutionEnabled === false && this.resolutionId === -1) return
39 super.handleClick(event)
41 this.player().peertubeResolutions().select({ id: this.resolutionId, byEngine: false })
45 const selectedResolution = this.player().peertubeResolutions().getSelected()
47 if (this.resolutionId === -1) {
48 this.autoResolutionChosen = this.player().peertubeResolutions().getAutoResolutionChosen()?.label
51 this.selected(this.resolutionId === selectedResolution.id)
54 updateAutoResolution () {
55 const enabled = this.player().peertubeResolutions().isAutoResolutionEnabeld()
57 // Check if the auto resolution is enabled or not
58 if (enabled === false) {
59 this.addClass('disabled')
61 this.removeClass('disabled')
64 this.autoResolutionEnabled = enabled
68 if (this.resolutionId === -1) {
69 return this.label + ' <small>' + this.autoResolutionChosen + '</small>'
75 videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
77 export { ResolutionMenuItem }