PeerTube/client/src/assets/player/videojs-components/resolution-menu-item.ts

88 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-01-28 10:29:50 -06:00
import videojs, { VideoJsPlayer } from 'video.js'
import { AutoResolutionUpdateData, ResolutionUpdateData } from '../peertube-videojs-typings'
2020-01-28 10:29:50 -06:00
const MenuItem = videojs.getComponent('MenuItem')
export interface ResolutionMenuItemOptions extends videojs.MenuItemOptions {
labels?: { [id: number]: string }
id: number
callback: Function
}
class ResolutionMenuItem extends MenuItem {
2020-01-28 10:29:50 -06:00
private readonly resolutionId: number
private readonly label: string
// Only used for the automatic item
private readonly labels: { [id: number]: string }
private readonly callback: Function
private autoResolutionPossible: boolean
private currentResolutionLabel: string
2020-01-28 10:29:50 -06:00
constructor (player: VideoJsPlayer, options?: ResolutionMenuItemOptions) {
options.selectable = true
super(player, options)
this.autoResolutionPossible = true
this.currentResolutionLabel = ''
2020-01-28 10:29:50 -06:00
this.resolutionId = options.id
this.label = options.label
this.labels = options.labels
this.callback = options.callback
2019-01-24 03:16:30 -06:00
player.peertube().on('resolutionChange', (_: any, data: ResolutionUpdateData) => this.updateSelection(data))
2019-01-24 03:16:30 -06:00
// We only want to disable the "Auto" item
2020-01-28 10:29:50 -06:00
if (this.resolutionId === -1) {
2019-01-24 03:16:30 -06:00
player.peertube().on('autoResolutionChange', (_: any, data: AutoResolutionUpdateData) => this.updateAutoResolution(data))
}
}
handleClick (event: any) {
// Auto button disabled?
2020-01-28 10:29:50 -06:00
if (this.autoResolutionPossible === false && this.resolutionId === -1) return
super.handleClick(event)
2020-01-28 10:29:50 -06:00
this.callback(this.resolutionId, 'video')
}
updateSelection (data: ResolutionUpdateData) {
2020-01-28 10:29:50 -06:00
if (this.resolutionId === -1) {
2019-01-24 03:16:30 -06:00
this.currentResolutionLabel = this.labels[data.id]
}
// Automatic resolution only
if (data.auto === true) {
2020-01-28 10:29:50 -06:00
this.selected(this.resolutionId === -1)
return
}
2020-01-28 10:29:50 -06:00
this.selected(this.resolutionId === data.id)
}
updateAutoResolution (data: AutoResolutionUpdateData) {
// Check if the auto resolution is enabled or not
if (data.possible === false) {
this.addClass('disabled')
} else {
this.removeClass('disabled')
}
this.autoResolutionPossible = data.possible
}
getLabel () {
2020-01-28 10:29:50 -06:00
if (this.resolutionId === -1) {
return this.label + ' <small>' + this.currentResolutionLabel + '</small>'
}
return this.label
}
}
2020-01-28 10:29:50 -06:00
videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
export { ResolutionMenuItem }