resolution-menu-item.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import videojs, { VideoJsPlayer } from 'video.js'
  2. import { AutoResolutionUpdateData, ResolutionUpdateData } from '../peertube-videojs-typings'
  3. const MenuItem = videojs.getComponent('MenuItem')
  4. export interface ResolutionMenuItemOptions extends videojs.MenuItemOptions {
  5. labels?: { [id: number]: string }
  6. id: number
  7. callback: Function
  8. }
  9. class ResolutionMenuItem extends MenuItem {
  10. private readonly resolutionId: number
  11. private readonly label: string
  12. // Only used for the automatic item
  13. private readonly labels: { [id: number]: string }
  14. private readonly callback: Function
  15. private autoResolutionPossible: boolean
  16. private currentResolutionLabel: string
  17. constructor (player: VideoJsPlayer, options?: ResolutionMenuItemOptions) {
  18. options.selectable = true
  19. super(player, options)
  20. this.autoResolutionPossible = true
  21. this.currentResolutionLabel = ''
  22. this.resolutionId = options.id
  23. this.label = options.label
  24. this.labels = options.labels
  25. this.callback = options.callback
  26. player.peertube().on('resolutionChange', (_: any, data: ResolutionUpdateData) => this.updateSelection(data))
  27. // We only want to disable the "Auto" item
  28. if (this.resolutionId === -1) {
  29. player.peertube().on('autoResolutionChange', (_: any, data: AutoResolutionUpdateData) => this.updateAutoResolution(data))
  30. }
  31. }
  32. handleClick (event: any) {
  33. // Auto button disabled?
  34. if (this.autoResolutionPossible === false && this.resolutionId === -1) return
  35. super.handleClick(event)
  36. this.callback(this.resolutionId, 'video')
  37. }
  38. updateSelection (data: ResolutionUpdateData) {
  39. if (this.resolutionId === -1) {
  40. this.currentResolutionLabel = this.labels[data.id]
  41. }
  42. // Automatic resolution only
  43. if (data.auto === true) {
  44. this.selected(this.resolutionId === -1)
  45. return
  46. }
  47. this.selected(this.resolutionId === data.id)
  48. }
  49. updateAutoResolution (data: AutoResolutionUpdateData) {
  50. // Check if the auto resolution is enabled or not
  51. if (data.possible === false) {
  52. this.addClass('disabled')
  53. } else {
  54. this.removeClass('disabled')
  55. }
  56. this.autoResolutionPossible = data.possible
  57. }
  58. getLabel () {
  59. if (this.resolutionId === -1) {
  60. return this.label + ' <small>' + this.currentResolutionLabel + '</small>'
  61. }
  62. return this.label
  63. }
  64. }
  65. videojs.registerComponent('ResolutionMenuItem', ResolutionMenuItem)
  66. export { ResolutionMenuItem }