1 import videojs, { VideoJsPlayer } from 'video.js'
2 import { AutoResolutionUpdateData, ResolutionUpdateData } from '../peertube-videojs-typings'
4 const MenuItem = videojs.getComponent('MenuItem')
6 export interface ResolutionMenuItemOptions extends videojs.MenuItemOptions {
7 labels?: { [id: number]: string }
12 class ResolutionMenuItem extends MenuItem {
13 private readonly resolutionId: number
14 private readonly label: string
15 // Only used for the automatic item
16 private readonly labels: { [id: number]: string }
17 private readonly callback: Function
19 private autoResolutionPossible: boolean
20 private currentResolutionLabel: string
22 constructor (player: VideoJsPlayer, options?: ResolutionMenuItemOptions) {
23 options.selectable = true
25 super(player, options)
27 this.autoResolutionPossible = true
28 this.currentResolutionLabel = ''
30 this.resolutionId = options.id
31 this.label = options.label
32 this.labels = options.labels
33 this.callback = options.callback
35 player.peertube().on('resolutionChange', (_: any, data: ResolutionUpdateData) => this.updateSelection(data))
37 // We only want to disable the "Auto" item
38 if (this.resolutionId === -1) {
39 player.peertube().on('autoResolutionChange', (_: any, data: AutoResolutionUpdateData) => this.updateAutoResolution(data))
43 handleClick (event: any) {
44 // Auto button disabled?
45 if (this.autoResolutionPossible === false && this.resolutionId === -1) return
47 super.handleClick(event)
49 this.callback(this.resolutionId, 'video')
52 updateSelection (data: ResolutionUpdateData) {
53 if (this.resolutionId === -1) {
54 this.currentResolutionLabel = this.labels[data.id]
57 // Automatic resolution only
58 if (data.auto === true) {
59 this.selected(this.resolutionId === -1)
63 this.selected(this.resolutionId === data.id)
66 updateAutoResolution (data: AutoResolutionUpdateData) {
67 // Check if the auto resolution is enabled or not
68 if (data.possible === false) {
69 this.addClass('disabled')
71 this.removeClass('disabled')
74 this.autoResolutionPossible = data.possible
78 if (this.resolutionId === -1) {
79 return this.label + ' <small>' + this.currentResolutionLabel + '</small>'
85 videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
87 export { ResolutionMenuItem }