]>
Commit | Line | Data |
---|---|---|
1 | // Author: Yanko Shterev | |
2 | // Thanks https://github.com/yshterev/videojs-settings-menu | |
3 | ||
4 | // FIXME: something weird with our path definition in tsconfig and typings | |
5 | // @ts-ignore | |
6 | import * as videojs from 'video.js' | |
7 | ||
8 | import { toTitleCase } from '../utils' | |
9 | import { VideoJSComponentInterface, videojsUntyped } from '../peertube-videojs-typings' | |
10 | ||
11 | const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem') | |
12 | const component: VideoJSComponentInterface = videojsUntyped.getComponent('Component') | |
13 | ||
14 | class SettingsMenuItem extends MenuItem { | |
15 | settingsButton: any | |
16 | dialog: any | |
17 | mainMenu: any | |
18 | panel: any | |
19 | panelChild: any | |
20 | panelChildEl: any | |
21 | size: any | |
22 | menuToLoad: string | |
23 | subMenu: any | |
24 | ||
25 | submenuClickHandler: Function | |
26 | transitionEndHandler: Function | |
27 | ||
28 | settingsSubMenuTitleEl_: any | |
29 | settingsSubMenuValueEl_: any | |
30 | settingsSubMenuEl_: any | |
31 | ||
32 | constructor (player: videojs.Player, options: any, entry: string, menuButton: VideoJSComponentInterface) { | |
33 | super(player, options) | |
34 | ||
35 | this.settingsButton = menuButton | |
36 | this.dialog = this.settingsButton.dialog | |
37 | this.mainMenu = this.settingsButton.menu | |
38 | this.panel = this.dialog.getChild('settingsPanel') | |
39 | this.panelChild = this.panel.getChild('settingsPanelChild') | |
40 | this.panelChildEl = this.panelChild.el_ | |
41 | ||
42 | this.size = null | |
43 | ||
44 | // keep state of what menu type is loading next | |
45 | this.menuToLoad = 'mainmenu' | |
46 | ||
47 | const subMenuName = toTitleCase(entry) | |
48 | const SubMenuComponent = videojsUntyped.getComponent(subMenuName) | |
49 | ||
50 | if (!SubMenuComponent) { | |
51 | throw new Error(`Component ${subMenuName} does not exist`) | |
52 | } | |
53 | this.subMenu = new SubMenuComponent(this.player(), options, menuButton, this) | |
54 | const subMenuClass = this.subMenu.buildCSSClass().split(' ')[0] | |
55 | this.settingsSubMenuEl_.className += ' ' + subMenuClass | |
56 | ||
57 | this.eventHandlers() | |
58 | ||
59 | player.ready(() => { | |
60 | // Voodoo magic for IOS | |
61 | setTimeout(() => { | |
62 | // Player was destroyed | |
63 | if (!this.player_) return | |
64 | ||
65 | this.build() | |
66 | ||
67 | // Update on rate change | |
68 | player.on('ratechange', this.submenuClickHandler) | |
69 | ||
70 | if (subMenuName === 'CaptionsButton') { | |
71 | // Hack to regenerate captions on HTTP fallback | |
72 | player.on('captionsChanged', () => { | |
73 | setTimeout(() => { | |
74 | this.settingsSubMenuEl_.innerHTML = '' | |
75 | this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_) | |
76 | this.update() | |
77 | this.bindClickEvents() | |
78 | ||
79 | }, 0) | |
80 | }) | |
81 | } | |
82 | ||
83 | this.reset() | |
84 | }, 0) | |
85 | }) | |
86 | } | |
87 | ||
88 | eventHandlers () { | |
89 | this.submenuClickHandler = this.onSubmenuClick.bind(this) | |
90 | this.transitionEndHandler = this.onTransitionEnd.bind(this) | |
91 | } | |
92 | ||
93 | onSubmenuClick (event: any) { | |
94 | let target = null | |
95 | ||
96 | if (event.type === 'tap') { | |
97 | target = event.target | |
98 | } else { | |
99 | target = event.currentTarget | |
100 | } | |
101 | ||
102 | if (target && target.classList.contains('vjs-back-button')) { | |
103 | this.loadMainMenu() | |
104 | return | |
105 | } | |
106 | ||
107 | // To update the sub menu value on click, setTimeout is needed because | |
108 | // updating the value is not instant | |
109 | setTimeout(() => this.update(event), 0) | |
110 | } | |
111 | ||
112 | /** | |
113 | * Create the component's DOM element | |
114 | * | |
115 | * @return {Element} | |
116 | * @method createEl | |
117 | */ | |
118 | createEl () { | |
119 | const el = videojsUntyped.dom.createEl('li', { | |
120 | className: 'vjs-menu-item' | |
121 | }) | |
122 | ||
123 | this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', { | |
124 | className: 'vjs-settings-sub-menu-title' | |
125 | }) | |
126 | ||
127 | el.appendChild(this.settingsSubMenuTitleEl_) | |
128 | ||
129 | this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', { | |
130 | className: 'vjs-settings-sub-menu-value' | |
131 | }) | |
132 | ||
133 | el.appendChild(this.settingsSubMenuValueEl_) | |
134 | ||
135 | this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', { | |
136 | className: 'vjs-settings-sub-menu' | |
137 | }) | |
138 | ||
139 | return el | |
140 | } | |
141 | ||
142 | /** | |
143 | * Handle click on menu item | |
144 | * | |
145 | * @method handleClick | |
146 | */ | |
147 | handleClick () { | |
148 | this.menuToLoad = 'submenu' | |
149 | // Remove open class to ensure only the open submenu gets this class | |
150 | videojsUntyped.dom.removeClass(this.el_, 'open') | |
151 | ||
152 | super.handleClick() | |
153 | ||
154 | this.mainMenu.el_.style.opacity = '0' | |
155 | // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element | |
156 | if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) { | |
157 | videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
158 | ||
159 | // animation not played without timeout | |
160 | setTimeout(() => { | |
161 | this.settingsSubMenuEl_.style.opacity = '1' | |
162 | this.settingsSubMenuEl_.style.marginRight = '0px' | |
163 | }, 0) | |
164 | ||
165 | this.settingsButton.setDialogSize(this.size) | |
166 | } else { | |
167 | videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * Create back button | |
173 | * | |
174 | * @method createBackButton | |
175 | */ | |
176 | createBackButton () { | |
177 | const button = this.subMenu.menu.addChild('MenuItem', {}, 0) | |
178 | button.name_ = 'BackButton' | |
179 | button.addClass('vjs-back-button') | |
180 | button.el_.innerHTML = this.player_.localize(this.subMenu.controlText_) | |
181 | } | |
182 | ||
183 | /** | |
184 | * Add/remove prefixed event listener for CSS Transition | |
185 | * | |
186 | * @method PrefixedEvent | |
187 | */ | |
188 | PrefixedEvent (element: any, type: any, callback: any, action = 'addEvent') { | |
189 | const prefix = ['webkit', 'moz', 'MS', 'o', ''] | |
190 | ||
191 | for (let p = 0; p < prefix.length; p++) { | |
192 | if (!prefix[p]) { | |
193 | type = type.toLowerCase() | |
194 | } | |
195 | ||
196 | if (action === 'addEvent') { | |
197 | element.addEventListener(prefix[p] + type, callback, false) | |
198 | } else if (action === 'removeEvent') { | |
199 | element.removeEventListener(prefix[p] + type, callback, false) | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 | onTransitionEnd (event: any) { | |
205 | if (event.propertyName !== 'margin-right') { | |
206 | return | |
207 | } | |
208 | ||
209 | if (this.menuToLoad === 'mainmenu') { | |
210 | // hide submenu | |
211 | videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
212 | ||
213 | // reset opacity to 0 | |
214 | this.settingsSubMenuEl_.style.opacity = '0' | |
215 | } | |
216 | } | |
217 | ||
218 | reset () { | |
219 | videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
220 | this.settingsSubMenuEl_.style.opacity = '0' | |
221 | this.setMargin() | |
222 | } | |
223 | ||
224 | loadMainMenu () { | |
225 | this.menuToLoad = 'mainmenu' | |
226 | this.mainMenu.show() | |
227 | this.mainMenu.el_.style.opacity = '0' | |
228 | ||
229 | // back button will always take you to main menu, so set dialog sizes | |
230 | this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height]) | |
231 | ||
232 | // animation not triggered without timeout (some async stuff ?!?) | |
233 | setTimeout(() => { | |
234 | // animate margin and opacity before hiding the submenu | |
235 | // this triggers CSS Transition event | |
236 | this.setMargin() | |
237 | this.mainMenu.el_.style.opacity = '1' | |
238 | }, 0) | |
239 | } | |
240 | ||
241 | build () { | |
242 | this.subMenu.on('updateLabel', () => { | |
243 | this.update() | |
244 | }) | |
245 | this.subMenu.on('menuChanged', () => { | |
246 | this.bindClickEvents() | |
247 | this.setSize() | |
248 | this.update() | |
249 | }) | |
250 | ||
251 | this.settingsSubMenuTitleEl_.innerHTML = this.player_.localize(this.subMenu.controlText_) | |
252 | this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_) | |
253 | this.panelChildEl.appendChild(this.settingsSubMenuEl_) | |
254 | this.update() | |
255 | ||
256 | this.createBackButton() | |
257 | this.setSize() | |
258 | this.bindClickEvents() | |
259 | ||
260 | // prefixed event listeners for CSS TransitionEnd | |
261 | this.PrefixedEvent( | |
262 | this.settingsSubMenuEl_, | |
263 | 'TransitionEnd', | |
264 | this.transitionEndHandler, | |
265 | 'addEvent' | |
266 | ) | |
267 | } | |
268 | ||
269 | update (event?: any) { | |
270 | let target: HTMLElement = null | |
271 | const subMenu = this.subMenu.name() | |
272 | ||
273 | if (event && event.type === 'tap') { | |
274 | target = event.target | |
275 | } else if (event) { | |
276 | target = event.currentTarget | |
277 | } | |
278 | ||
279 | // Playback rate menu button doesn't get a vjs-selected class | |
280 | // or sets options_['selected'] on the selected playback rate. | |
281 | // Thus we get the submenu value based on the labelEl of playbackRateMenuButton | |
282 | if (subMenu === 'PlaybackRateMenuButton') { | |
283 | setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250) | |
284 | } else { | |
285 | // Loop trough the submenu items to find the selected child | |
286 | for (const subMenuItem of this.subMenu.menu.children_) { | |
287 | if (!(subMenuItem instanceof component)) { | |
288 | continue | |
289 | } | |
290 | ||
291 | if (subMenuItem.hasClass('vjs-selected')) { | |
292 | // Prefer to use the function | |
293 | if (typeof subMenuItem.getLabel === 'function') { | |
294 | this.settingsSubMenuValueEl_.innerHTML = subMenuItem.getLabel() | |
295 | break | |
296 | } | |
297 | ||
298 | this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | if (target && !target.classList.contains('vjs-back-button')) { | |
304 | this.settingsButton.hideDialog() | |
305 | } | |
306 | } | |
307 | ||
308 | bindClickEvents () { | |
309 | for (const item of this.subMenu.menu.children()) { | |
310 | if (!(item instanceof component)) { | |
311 | continue | |
312 | } | |
313 | item.on(['tap', 'click'], this.submenuClickHandler) | |
314 | } | |
315 | } | |
316 | ||
317 | // save size of submenus on first init | |
318 | // if number of submenu items change dynamically more logic will be needed | |
319 | setSize () { | |
320 | this.dialog.removeClass('vjs-hidden') | |
321 | videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
322 | this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_) | |
323 | this.setMargin() | |
324 | this.dialog.addClass('vjs-hidden') | |
325 | videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
326 | } | |
327 | ||
328 | setMargin () { | |
329 | const [ width ] = this.size | |
330 | ||
331 | this.settingsSubMenuEl_.style.marginRight = `-${width}px` | |
332 | } | |
333 | ||
334 | /** | |
335 | * Hide the sub menu | |
336 | */ | |
337 | hideSubMenu () { | |
338 | // after removing settings item this.el_ === null | |
339 | if (!this.el_) { | |
340 | return | |
341 | } | |
342 | ||
343 | if (videojsUntyped.dom.hasClass(this.el_, 'open')) { | |
344 | videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden') | |
345 | videojsUntyped.dom.removeClass(this.el_, 'open') | |
346 | } | |
347 | } | |
348 | ||
349 | } | |
350 | ||
351 | SettingsMenuItem.prototype.contentElType = 'button' | |
352 | videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem) | |
353 | ||
354 | export { SettingsMenuItem } |