]>
Commit | Line | Data |
---|---|---|
f5fcd9f7 | 1 | // Thanks to Yanko Shterev: https://github.com/yshterev/videojs-settings-menu |
c6352f2c | 2 | import { SettingsMenuItem } from './settings-menu-item' |
2adfc7ea | 3 | import { toTitleCase } from '../utils' |
512decf3 | 4 | import videojs from 'video.js' |
f5fcd9f7 C |
5 | |
6 | import { SettingsDialog } from './settings-dialog' | |
7 | import { SettingsPanel } from './settings-panel' | |
8 | import { SettingsPanelChild } from './settings-panel-child' | |
c6352f2c | 9 | |
f5fcd9f7 C |
10 | const Button = videojs.getComponent('Button') |
11 | const Menu = videojs.getComponent('Menu') | |
12 | const Component = videojs.getComponent('Component') | |
13 | ||
14 | export interface SettingsButtonOptions extends videojs.ComponentOptions { | |
15 | entries: any[] | |
16 | setup?: { | |
17 | maxHeightOffset: number | |
18 | } | |
19 | } | |
c6352f2c C |
20 | |
21 | class SettingsButton extends Button { | |
f5fcd9f7 C |
22 | dialog: SettingsDialog |
23 | dialogEl: HTMLElement | |
24 | menu: videojs.Menu | |
25 | panel: SettingsPanel | |
26 | panelChild: SettingsPanelChild | |
27 | ||
28 | addSettingsItemHandler: typeof SettingsButton.prototype.onAddSettingsItem | |
29 | disposeSettingsItemHandler: typeof SettingsButton.prototype.onDisposeSettingsItem | |
75d74959 | 30 | documentClickHandler: typeof SettingsButton.prototype.onDocumentClick |
f5fcd9f7 C |
31 | userInactiveHandler: typeof SettingsButton.prototype.onUserInactive |
32 | ||
33 | private settingsButtonOptions: SettingsButtonOptions | |
34 | ||
7e37e111 | 35 | constructor (player: videojs.Player, options?: SettingsButtonOptions) { |
c6352f2c C |
36 | super(player, options) |
37 | ||
f5fcd9f7 C |
38 | this.settingsButtonOptions = options |
39 | ||
40 | this.controlText('Settings') | |
41 | ||
42 | this.dialog = this.player().addChild('settingsDialog') | |
43 | this.dialogEl = this.dialog.el() as HTMLElement | |
c6352f2c C |
44 | this.menu = null |
45 | this.panel = this.dialog.addChild('settingsPanel') | |
46 | this.panelChild = this.panel.addChild('settingsPanelChild') | |
47 | ||
48 | this.addClass('vjs-settings') | |
f5fcd9f7 | 49 | this.el().setAttribute('aria-label', 'Settings Button') |
c6352f2c C |
50 | |
51 | // Event handlers | |
52 | this.addSettingsItemHandler = this.onAddSettingsItem.bind(this) | |
53 | this.disposeSettingsItemHandler = this.onDisposeSettingsItem.bind(this) | |
75d74959 | 54 | this.documentClickHandler = this.onDocumentClick.bind(this) |
c6352f2c C |
55 | this.userInactiveHandler = this.onUserInactive.bind(this) |
56 | ||
57 | this.buildMenu() | |
58 | this.bindEvents() | |
59 | ||
b891f9bc | 60 | // Prepare the dialog |
c6352f2c C |
61 | this.player().one('play', () => this.hideDialog()) |
62 | } | |
63 | ||
75d74959 | 64 | onDocumentClick (event: MouseEvent) { |
c6352f2c | 65 | const element = event.target as HTMLElement |
75d74959 C |
66 | |
67 | if ( | |
68 | element && element.classList && | |
69 | (element.classList.contains('vjs-settings') || element.parentElement.classList.contains('vjs-settings')) | |
70 | ) { | |
c6352f2c C |
71 | return |
72 | } | |
73 | ||
74 | if (!this.dialog.hasClass('vjs-hidden')) { | |
75 | this.hideDialog() | |
76 | } | |
77 | } | |
78 | ||
c199c427 | 79 | onDisposeSettingsItem (event: any, name: string) { |
c6352f2c | 80 | if (name === undefined) { |
c4710631 | 81 | const children = this.menu.children() |
c6352f2c C |
82 | |
83 | while (children.length > 0) { | |
84 | children[0].dispose() | |
85 | this.menu.removeChild(children[0]) | |
86 | } | |
87 | ||
88 | this.addClass('vjs-hidden') | |
89 | } else { | |
c4710631 | 90 | const item = this.menu.getChild(name) |
c6352f2c C |
91 | |
92 | if (item) { | |
93 | item.dispose() | |
94 | this.menu.removeChild(item) | |
95 | } | |
96 | } | |
97 | ||
98 | this.hideDialog() | |
99 | ||
f5fcd9f7 | 100 | if (this.settingsButtonOptions.entries.length === 0) { |
c6352f2c C |
101 | this.addClass('vjs-hidden') |
102 | } | |
103 | } | |
104 | ||
36d9a79f C |
105 | dispose () { |
106 | document.removeEventListener('click', this.documentClickHandler) | |
107 | ||
108 | if (this.isInIframe()) { | |
109 | window.removeEventListener('blur', this.documentClickHandler) | |
110 | } | |
111 | } | |
112 | ||
c199c427 | 113 | onAddSettingsItem (event: any, data: any) { |
c6352f2c C |
114 | const [ entry, options ] = data |
115 | ||
116 | this.addMenuItem(entry, options) | |
117 | this.removeClass('vjs-hidden') | |
118 | } | |
119 | ||
120 | onUserInactive () { | |
121 | if (!this.dialog.hasClass('vjs-hidden')) { | |
122 | this.hideDialog() | |
123 | } | |
124 | } | |
125 | ||
126 | bindEvents () { | |
75d74959 C |
127 | document.addEventListener('click', this.documentClickHandler) |
128 | if (this.isInIframe()) { | |
129 | window.addEventListener('blur', this.documentClickHandler) | |
130 | } | |
131 | ||
f5fcd9f7 C |
132 | this.player().on('addsettingsitem', this.addSettingsItemHandler) |
133 | this.player().on('disposesettingsitem', this.disposeSettingsItemHandler) | |
134 | this.player().on('userinactive', this.userInactiveHandler) | |
c6352f2c C |
135 | } |
136 | ||
137 | buildCSSClass () { | |
138 | return `vjs-icon-settings ${super.buildCSSClass()}` | |
139 | } | |
140 | ||
141 | handleClick () { | |
142 | if (this.dialog.hasClass('vjs-hidden')) { | |
143 | this.showDialog() | |
144 | } else { | |
145 | this.hideDialog() | |
146 | } | |
147 | } | |
148 | ||
149 | showDialog () { | |
f5fcd9f7 | 150 | this.player().peertube().onMenuOpen(); |
d1f21ebb | 151 | |
f5fcd9f7 | 152 | (this.menu.el() as HTMLElement).style.opacity = '1' |
c6352f2c C |
153 | this.dialog.show() |
154 | ||
155 | this.setDialogSize(this.getComponentSize(this.menu)) | |
156 | } | |
157 | ||
158 | hideDialog () { | |
d1f21ebb C |
159 | this.player_.peertube().onMenuClosed() |
160 | ||
c6352f2c | 161 | this.dialog.hide() |
f5fcd9f7 C |
162 | this.setDialogSize(this.getComponentSize(this.menu)); |
163 | (this.menu.el() as HTMLElement).style.opacity = '1' | |
c6352f2c C |
164 | this.resetChildren() |
165 | } | |
166 | ||
f5fcd9f7 | 167 | getComponentSize (element: videojs.Component | HTMLElement) { |
c6352f2c C |
168 | let width: number = null |
169 | let height: number = null | |
170 | ||
171 | // Could be component or just DOM element | |
172 | if (element instanceof Component) { | |
f5fcd9f7 C |
173 | const el = element.el() as HTMLElement |
174 | ||
175 | width = el.offsetWidth | |
176 | height = el.offsetHeight; | |
c6352f2c | 177 | |
f5fcd9f7 C |
178 | (element as any).width = width; |
179 | (element as any).height = height | |
c6352f2c C |
180 | } else { |
181 | width = element.offsetWidth | |
182 | height = element.offsetHeight | |
183 | } | |
184 | ||
185 | return [ width, height ] | |
186 | } | |
187 | ||
188 | setDialogSize ([ width, height ]: number[]) { | |
189 | if (typeof height !== 'number') { | |
190 | return | |
191 | } | |
192 | ||
f5fcd9f7 C |
193 | const offset = this.settingsButtonOptions.setup.maxHeightOffset |
194 | const maxHeight = (this.player().el() as HTMLElement).offsetHeight - offset // FIXME: typings | |
195 | ||
196 | const panelEl = this.panel.el() as HTMLElement | |
c6352f2c C |
197 | |
198 | if (height > maxHeight) { | |
199 | height = maxHeight | |
200 | width += 17 | |
f5fcd9f7 C |
201 | panelEl.style.maxHeight = `${height}px` |
202 | } else if (panelEl.style.maxHeight !== '') { | |
203 | panelEl.style.maxHeight = '' | |
c6352f2c C |
204 | } |
205 | ||
206 | this.dialogEl.style.width = `${width}px` | |
207 | this.dialogEl.style.height = `${height}px` | |
208 | } | |
209 | ||
210 | buildMenu () { | |
211 | this.menu = new Menu(this.player()) | |
212 | this.menu.addClass('vjs-main-menu') | |
f5fcd9f7 | 213 | const entries = this.settingsButtonOptions.entries |
c6352f2c C |
214 | |
215 | if (entries.length === 0) { | |
216 | this.addClass('vjs-hidden') | |
217 | this.panelChild.addChild(this.menu) | |
218 | return | |
219 | } | |
220 | ||
c4710631 | 221 | for (const entry of entries) { |
f5fcd9f7 | 222 | this.addMenuItem(entry, this.settingsButtonOptions) |
c6352f2c C |
223 | } |
224 | ||
225 | this.panelChild.addChild(this.menu) | |
226 | } | |
227 | ||
244b4ae3 | 228 | addMenuItem (entry: any, options: any) { |
951ef829 | 229 | const openSubMenu = function (this: any) { |
f5fcd9f7 C |
230 | if (videojs.dom.hasClass(this.el_, 'open')) { |
231 | videojs.dom.removeClass(this.el_, 'open') | |
c6352f2c | 232 | } else { |
f5fcd9f7 | 233 | videojs.dom.addClass(this.el_, 'open') |
c6352f2c C |
234 | } |
235 | } | |
236 | ||
237 | options.name = toTitleCase(entry) | |
f5fcd9f7 C |
238 | |
239 | const newOptions = Object.assign({}, options, { entry, menuButton: this }) | |
240 | const settingsMenuItem = new SettingsMenuItem(this.player(), newOptions) | |
c6352f2c C |
241 | |
242 | this.menu.addChild(settingsMenuItem) | |
243 | ||
244 | // Hide children to avoid sub menus stacking on top of each other | |
245 | // or having multiple menus open | |
246 | settingsMenuItem.on('click', videojs.bind(this, this.hideChildren)) | |
247 | ||
248 | // Whether to add or remove selected class on the settings sub menu element | |
249 | settingsMenuItem.on('click', openSubMenu) | |
250 | } | |
251 | ||
252 | resetChildren () { | |
c4710631 | 253 | for (const menuChild of this.menu.children()) { |
f5fcd9f7 | 254 | (menuChild as SettingsMenuItem).reset() |
c6352f2c C |
255 | } |
256 | } | |
257 | ||
258 | /** | |
259 | * Hide all the sub menus | |
260 | */ | |
261 | hideChildren () { | |
c4710631 | 262 | for (const menuChild of this.menu.children()) { |
f5fcd9f7 | 263 | (menuChild as SettingsMenuItem).hideSubMenu() |
c6352f2c C |
264 | } |
265 | } | |
266 | ||
75d74959 C |
267 | isInIframe () { |
268 | return window.self !== window.top | |
269 | } | |
270 | ||
c6352f2c C |
271 | } |
272 | ||
c6352f2c | 273 | Component.registerComponent('SettingsButton', SettingsButton) |
c6352f2c | 274 | |
f5fcd9f7 | 275 | export { SettingsButton } |