]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/playlist/playlist-menu.ts
Handle unavailable videos in embed playlists
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / playlist / playlist-menu.ts
CommitLineData
4572c3d0
C
1import videojs from 'video.js'
2import { VideoPlaylistElement } from '@shared/models'
3import { PlaylistPluginOptions } from '../peertube-videojs-typings'
4import { PlaylistMenuItem } from './playlist-menu-item'
5
6const Component = videojs.getComponent('Component')
7
8class PlaylistMenu extends Component {
9 private menuItems: PlaylistMenuItem[]
10
11 constructor (player: videojs.Player, options?: PlaylistPluginOptions) {
12 super(player, options as any)
13
56674bb9
C
14 const self = this
15
16 function userInactiveHandler () {
17 self.close()
18 }
19
20 this.el().addEventListener('mouseenter', () => {
21 this.player().off('userinactive', userInactiveHandler)
22 })
23
24 this.el().addEventListener('mouseleave', () => {
25 this.player().one('userinactive', userInactiveHandler)
4572c3d0
C
26 })
27
28 this.player().on('click', event => {
29 let current = event.target as HTMLElement
30
31 do {
32 if (
33 current.classList.contains('vjs-playlist-menu') ||
34 current.classList.contains('vjs-playlist-button')
35 ) {
36 return
37 }
38
39 current = current.parentElement
40 } while (current)
41
42 this.close()
43 })
44 }
45
46 createEl () {
47 this.menuItems = []
48
49 const options = this.getOptions()
50
51 const menu = super.createEl('div', {
52 className: 'vjs-playlist-menu',
53 innerHTML: '',
54 tabIndex: -1
55 })
56
57 const header = super.createEl('div', {
58 className: 'header'
59 })
60
61 const headerLeft = super.createEl('div')
62
63 const leftTitle = super.createEl('div', {
64 innerHTML: options.playlist.displayName,
65 className: 'title'
66 })
67
68 const leftSubtitle = super.createEl('div', {
69 innerHTML: this.player().localize('By {1}', [ options.playlist.videoChannel.displayName ]),
70 className: 'channel'
71 })
72
73 headerLeft.appendChild(leftTitle)
74 headerLeft.appendChild(leftSubtitle)
75
76 const tick = super.createEl('div', {
77 className: 'cross'
78 })
79 tick.addEventListener('click', () => this.close())
80
81 header.appendChild(headerLeft)
82 header.appendChild(tick)
83
84 const list = super.createEl('ol')
85
86 for (const playlistElement of options.elements) {
87 const item = new PlaylistMenuItem(this.player(), {
88 element: playlistElement,
89 onClicked: () => this.onItemClicked(playlistElement)
90 })
91
92 list.appendChild(item.el())
93
94 this.menuItems.push(item)
95 }
96
97 menu.appendChild(header)
98 menu.appendChild(list)
99
100 return menu
101 }
102
103 update () {
104 const options = this.getOptions()
105
106 this.updateSelected(options.getCurrentPosition())
107 }
108
109 open () {
110 this.player().addClass('playlist-menu-displayed')
111 }
112
113 close () {
114 this.player().removeClass('playlist-menu-displayed')
115 }
116
117 updateSelected (newPosition: number) {
118 for (const item of this.menuItems) {
119 item.setSelected(item.getElement().position === newPosition)
120 }
121 }
122
123 private getOptions () {
124 return this.options_ as PlaylistPluginOptions
125 }
126
127 private onItemClicked (element: VideoPlaylistElement) {
128 this.getOptions().onItemClicked(element)
129 }
130}
131
132Component.registerComponent('PlaylistMenu', PlaylistMenu)
133
134export { PlaylistMenu }