aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/settings-menu-item.ts
blob: e979ae088a165562b5b891ec4b350f495e2de97e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Author: Yanko Shterev
// Thanks https://github.com/yshterev/videojs-settings-menu

import { toTitleCase } from './utils'
import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'

const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
const component: VideoJSComponentInterface = videojsUntyped.getComponent('Component')

class SettingsMenuItem extends MenuItem {

  constructor (player: videojs.Player, options, entry: string, menuButton: VideoJSComponentInterface) {
    super(player, options)

    this.settingsButton = menuButton
    this.dialog = this.settingsButton.dialog
    this.mainMenu = this.settingsButton.menu
    this.panel = this.dialog.getChild('settingsPanel')
    this.panelChild = this.panel.getChild('settingsPanelChild')
    this.panelChildEl = this.panelChild.el_

    this.size = null

    // keep state of what menu type is loading next
    this.menuToLoad = 'mainmenu'

    const subMenuName = toTitleCase(entry)
    const SubMenuComponent = videojsUntyped.getComponent(subMenuName)

    if (!SubMenuComponent) {
      throw new Error(`Component ${subMenuName} does not exist`)
    }
    this.subMenu = new SubMenuComponent(this.player(), options, menuButton, this)

    this.eventHandlers()

    player.ready(() => {
      this.build()
      this.reset()
    })
  }

  eventHandlers () {
    this.submenuClickHandler = this.onSubmenuClick.bind(this)
    this.transitionEndHandler = this.onTransitionEnd.bind(this)
  }

  onSubmenuClick (event) {
    let target = null

    if (event.type === 'tap') {
      target = event.target
    } else {
      target = event.currentTarget
    }

    if (target.classList.contains('vjs-back-button')) {
      this.loadMainMenu()
      return
    }

    // To update the sub menu value on click, setTimeout is needed because
    // updating the value is not instant
    setTimeout(() => this.update(event), 0)
  }

  /**
   * Create the component's DOM element
   *
   * @return {Element}
   * @method createEl
   */
  createEl () {
    const el = videojsUntyped.dom.createEl('li', {
      className: 'vjs-menu-item'
    })

    this.settingsSubMenuTitleEl_ = videojsUntyped.dom.createEl('div', {
      className: 'vjs-settings-sub-menu-title'
    })

    el.appendChild(this.settingsSubMenuTitleEl_)

    this.settingsSubMenuValueEl_ = videojsUntyped.dom.createEl('div', {
      className: 'vjs-settings-sub-menu-value'
    })

    el.appendChild(this.settingsSubMenuValueEl_)

    this.settingsSubMenuEl_ = videojsUntyped.dom.createEl('div', {
      className: 'vjs-settings-sub-menu'
    })

    return el
  }

  /**
   * Handle click on menu item
   *
   * @method handleClick
   */
  handleClick () {
    this.menuToLoad = 'submenu'
    // Remove open class to ensure only the open submenu gets this class
    videojsUntyped.dom.removeClass(this.el_, 'open')

    super.handleClick()

    this.mainMenu.el_.style.opacity = '0'
    // Whether to add or remove vjs-hidden class on the settingsSubMenuEl element
    if (videojsUntyped.dom.hasClass(this.settingsSubMenuEl_, 'vjs-hidden')) {
      videojsUntyped.dom.removeClass(this.settingsSubMenuEl_, 'vjs-hidden')

      // animation not played without timeout
      setTimeout(() => {
        this.settingsSubMenuEl_.style.opacity = '1'
        this.settingsSubMenuEl_.style.marginRight = '0px'
      }, 0)

      this.settingsButton.setDialogSize(this.size)
    } else {
      videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
    }
  }

  /**
   * Create back button
   *
   * @method createBackButton
   */
  createBackButton () {
    const button = this.subMenu.menu.addChild('MenuItem', {}, 0)
    button.name_ = 'BackButton'
    button.addClass('vjs-back-button')
    button.el_.innerHTML = this.subMenu.controlText_
  }

  /**
   * Add/remove prefixed event listener for CSS Transition
   *
   * @method PrefixedEvent
   */
  PrefixedEvent (element, type, callback, action = 'addEvent') {
    let prefix = ['webkit', 'moz', 'MS', 'o', '']

    for (let p = 0; p < prefix.length; p++) {
      if (!prefix[p]) {
        type = type.toLowerCase()
      }

      if (action === 'addEvent') {
        element.addEventListener(prefix[p] + type, callback, false)
      } else if (action === 'removeEvent') {
        element.removeEventListener(prefix[p] + type, callback, false)
      }
    }
  }

  onTransitionEnd (event) {
    if (event.propertyName !== 'margin-right') {
      return
    }

    if (this.menuToLoad === 'mainmenu') {
      // hide submenu
      videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')

      // reset opacity to 0
      this.settingsSubMenuEl_.style.opacity = '0'
    }
  }

  reset () {
    videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
    this.settingsSubMenuEl_.style.opacity = '0'
    this.setMargin()
  }

  loadMainMenu () {
    this.menuToLoad = 'mainmenu'
    this.mainMenu.show()
    this.mainMenu.el_.style.opacity = '0'

    // back button will always take you to main menu, so set dialog sizes
    this.settingsButton.setDialogSize([this.mainMenu.width, this.mainMenu.height])

    // animation not triggered without timeout (some async stuff ?!?)
    setTimeout(() => {
      // animate margin and opacity before hiding the submenu
      // this triggers CSS Transition event
      this.setMargin()
      this.mainMenu.el_.style.opacity = '1'
    }, 0)
  }

  build () {
    const saveUpdateLabel = this.subMenu.updateLabel
    this.subMenu.updateLabel = () => {
      this.update()

      saveUpdateLabel.call(this.subMenu)
    }

    this.settingsSubMenuTitleEl_.innerHTML = this.subMenu.controlText_
    this.settingsSubMenuEl_.appendChild(this.subMenu.menu.el_)
    this.panelChildEl.appendChild(this.settingsSubMenuEl_)
    this.update()

    this.createBackButton()
    this.getSize()
    this.bindClickEvents()

    // prefixed event listeners for CSS TransitionEnd
    this.PrefixedEvent(
      this.settingsSubMenuEl_,
      'TransitionEnd',
      this.transitionEndHandler,
      'addEvent'
    )
  }

  update (event?: Event) {
    let target = null
    let subMenu = this.subMenu.name()

    if (event && event.type === 'tap') {
      target = event.target
    } else if (event) {
      target = event.currentTarget
    }

    // Playback rate menu button doesn't get a vjs-selected class
    // or sets options_['selected'] on the selected playback rate.
    // Thus we get the submenu value based on the labelEl of playbackRateMenuButton
    if (subMenu === 'PlaybackRateMenuButton') {
      setTimeout(() => this.settingsSubMenuValueEl_.innerHTML = this.subMenu.labelEl_.innerHTML, 250)
    } else {
      // Loop trough the submenu items to find the selected child
      for (let subMenuItem of this.subMenu.menu.children_) {
        if (!(subMenuItem instanceof component)) {
          continue
        }

        switch (subMenu) {
          case 'SubtitlesButton':
          case 'CaptionsButton':
            // subtitlesButton entering default check twice and overwriting
            // selected label in main manu
            if (subMenuItem.hasClass('vjs-selected')) {
              this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
            }
            break

          default:
            // Set submenu value based on what item is selected
            if (subMenuItem.options_.selected || subMenuItem.hasClass('vjs-selected')) {
              this.settingsSubMenuValueEl_.innerHTML = subMenuItem.options_.label
            }
        }
      }
    }

    if (target && !target.classList.contains('vjs-back-button')) {
      this.settingsButton.hideDialog()
    }
  }

  bindClickEvents () {
    for (let item of this.subMenu.menu.children()) {
      if (!(item instanceof component)) {
        continue
      }
      item.on(['tap', 'click'], this.submenuClickHandler)
    }
  }

  // save size of submenus on first init
  // if number of submenu items change dynamically more logic will be needed
  getSize () {
    this.dialog.removeClass('vjs-hidden')
    this.size = this.settingsButton.getComponentSize(this.settingsSubMenuEl_)
    this.setMargin()
    this.dialog.addClass('vjs-hidden')
    videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
  }

  setMargin () {
    let [width] = this.size

    this.settingsSubMenuEl_.style.marginRight = `-${width}px`
  }

  /**
   * Hide the sub menu
   */
  hideSubMenu () {
    // after removing settings item this.el_ === null
    if (!this.el_) {
      return
    }

    if (videojsUntyped.dom.hasClass(this.el_, 'open')) {
      videojsUntyped.dom.addClass(this.settingsSubMenuEl_, 'vjs-hidden')
      videojsUntyped.dom.removeClass(this.el_, 'open')
    }
  }

}

SettingsMenuItem.prototype.contentElType = 'button'
videojsUntyped.registerComponent('SettingsMenuItem', SettingsMenuItem)

export { SettingsMenuItem }