-
Notifications
You must be signed in to change notification settings - Fork 837
/
core.js
374 lines (318 loc) · 11.2 KB
/
core.js
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import { isNumber, Fullscreen, DomRecycler } from '../../base/utils'
import Styler from '../../base/styler'
import Events from '../../base/events'
import UIObject from '../../base/ui_object'
import UICorePlugin from '../../base/ui_core_plugin'
import Browser from '../../components/browser'
import ContainerFactory from '../../components/container_factory'
import Mediator from '../../components/mediator'
import PlayerInfo from '../../components/player_info'
import PlayerError from '../../components/error'
import ErrorMixin from '../../base/error_mixin'
import $ from 'clappr-zepto'
import './public/style.scss'
import fontStyle from './public/fonts.css'
let style
/**
* The Core is responsible to manage Containers, the mediator, MediaControl
* and the player state.
* @class Core
* @constructor
* @extends UIObject
* @module components
*/
export default class Core extends UIObject {
get events() {
return {
'webkitfullscreenchange': 'handleFullscreenChange',
'mousemove': 'onMouseMove',
'mouseleave': 'onMouseLeave'
}
}
get attributes() {
return {
'data-player': '',
tabindex: 9999
}
}
/**
* checks if the core is ready.
* @property isReady
* @type {Boolean} `true` if the core is ready, otherwise `false`
*/
get isReady() {
return !!this.ready
}
/**
* The internationalization plugin.
* @property i18n
* @type {Strings}
*/
get i18n() {
return this.getPlugin('strings') || { t: (key) => key }
}
/**
* @deprecated
* This property currently exists for retrocompatibility reasons.
* If you want to access the media control instance, use the method getPlugin('media_control').
*/
get mediaControl() {
return this.getPlugin('media_control') || this.dummyMediaControl
}
get dummyMediaControl() {
if (this._dummyMediaControl) return this._dummyMediaControl
this._dummyMediaControl = new UICorePlugin(this)
return this._dummyMediaControl
}
/**
* gets the active container reference.
* @property activeContainer
* @type {Object}
*/
get activeContainer() {
return this._activeContainer
}
/**
* sets the active container reference and trigger a event with the new reference.
* @property activeContainer
* @type {Object}
*/
set activeContainer(container) {
this._activeContainer = container
this.trigger(Events.CORE_ACTIVE_CONTAINER_CHANGED, this._activeContainer)
}
/**
* gets the active playback reference.
* @property activePlayback
* @type {Object}
*/
get activePlayback() {
return this.activeContainer && this.activeContainer.playback
}
constructor(options) {
super(options)
this.playerError = new PlayerError(options, this)
this.configureDomRecycler()
this.playerInfo = PlayerInfo.getInstance(options.playerId)
this.firstResize = true
this.plugins = []
this.containers = []
//FIXME fullscreen api sucks
this._boundFullscreenHandler = () => this.handleFullscreenChange()
$(document).bind('fullscreenchange', this._boundFullscreenHandler)
$(document).bind('MSFullscreenChange', this._boundFullscreenHandler)
$(document).bind('mozfullscreenchange', this._boundFullscreenHandler)
Browser.isMobile && $(window).bind('resize', (o) => { this.handleWindowResize(o) })
}
configureDomRecycler() {
let recycleVideo = this.options && this.options.playback && this.options.playback.recycleVideo
DomRecycler.configure({ recycleVideo })
}
createContainers(options) {
this.defer = $.Deferred()
this.defer.promise(this)
this.containerFactory = new ContainerFactory(options, options.loader, this.i18n, this.playerError)
this.prepareContainers()
}
prepareContainers() {
this.containerFactory.createContainers()
.then((containers) => this.setupContainers(containers))
.then((containers) => this.resolveOnContainersReady(containers))
}
updateSize() {
this.isFullscreen() ? this.setFullscreen() : this.setPlayerSize()
}
setFullscreen() {
if (!Browser.isiOS) {
this.$el.addClass('fullscreen')
this.$el.removeAttr('style')
this.playerInfo.previousSize = { width: this.options.width, height: this.options.height }
this.playerInfo.currentSize = { width: $(window).width(), height: $(window).height() }
}
}
setPlayerSize() {
this.$el.removeClass('fullscreen')
this.playerInfo.currentSize = this.playerInfo.previousSize
this.playerInfo.previousSize = { width: $(window).width(), height: $(window).height() }
this.resize(this.playerInfo.currentSize)
}
resize(options) {
if (!isNumber(options.height) && !isNumber(options.width)) {
this.el.style.height = `${options.height}`
this.el.style.width = `${options.width}`
} else {
this.el.style.height = `${options.height}px`
this.el.style.width = `${options.width}px`
}
this.playerInfo.previousSize = { width: this.options.width, height: this.options.height }
this.options.width = options.width
this.options.height = options.height
this.playerInfo.currentSize = options
this.triggerResize(this.playerInfo.currentSize)
}
enableResizeObserver() {
const checkSizeCallback = () => {
this.triggerResize({ width: this.el.clientWidth, height: this.el.clientHeight })
}
this.resizeObserverInterval = setInterval(checkSizeCallback, 500)
}
triggerResize(newSize) {
const thereWasChange = this.firstResize || this.oldHeight !== newSize.height || this.oldWidth !== newSize.width
if (thereWasChange) {
this.oldHeight = newSize.height
this.oldWidth = newSize.width
this.playerInfo.computedSize = newSize
this.firstResize = false
Mediator.trigger(`${this.options.playerId}:${Events.PLAYER_RESIZE}`, newSize)
this.trigger(Events.CORE_RESIZE, newSize)
}
}
disableResizeObserver() {
this.resizeObserverInterval && clearInterval(this.resizeObserverInterval)
}
resolveOnContainersReady(containers) {
$.when.apply($, containers).done(() => {
this.defer.resolve(this)
this.ready = true
this.trigger(Events.CORE_READY)
})
}
addPlugin(plugin) {
this.plugins.push(plugin)
}
hasPlugin(name) {
return !!this.getPlugin(name)
}
getPlugin(name) {
return this.plugins.filter(plugin => plugin.name === name)[0]
}
load(sources, mimeType) {
this.options.mimeType = mimeType
sources = sources && sources.constructor === Array ? sources : [sources]
this.options.sources = sources
this.containers.forEach((container) => container.destroy())
this.containerFactory.options = $.extend(this.options, { sources })
this.prepareContainers()
}
destroy() {
this.disableResizeObserver()
this.containers.forEach((container) => container.destroy())
this.plugins.forEach((plugin) => plugin.destroy())
this.$el.remove()
$(document).unbind('fullscreenchange', this._boundFullscreenHandler)
$(document).unbind('MSFullscreenChange', this._boundFullscreenHandler)
$(document).unbind('mozfullscreenchange', this._boundFullscreenHandler)
this.stopListening()
}
handleFullscreenChange() {
this.trigger(Events.CORE_FULLSCREEN, this.isFullscreen())
this.updateSize()
}
handleWindowResize(event) {
const orientation = (window.innerWidth > window.innerHeight) ? 'landscape' : 'portrait'
if (this._screenOrientation === orientation) return
this._screenOrientation = orientation
this.triggerResize({ width: this.el.clientWidth, height: this.el.clientHeight })
this.trigger(Events.CORE_SCREEN_ORIENTATION_CHANGED, {
event: event,
orientation: this._screenOrientation
})
}
removeContainer(container) {
this.stopListening(container)
this.containers = this.containers.filter((c) => c !== container)
}
setupContainer(container) {
this.listenTo(container, Events.CONTAINER_DESTROYED, this.removeContainer)
this.containers.push(container)
}
setupContainers(containers) {
containers.forEach(this.setupContainer.bind(this))
this.trigger(Events.CORE_CONTAINERS_CREATED)
this.renderContainers()
this.activeContainer = containers[0]
this.render()
this.appendToParent()
return this.containers
}
renderContainers() {
this.containers.forEach((container) => this.el.appendChild(container.render().el))
}
createContainer(source, options) {
const container = this.containerFactory.createContainer(source, options)
this.setupContainer(container)
this.el.appendChild(container.render().el)
return container
}
/**
* @deprecated
* This method currently exists for retrocompatibility reasons.
* If you want the current container reference, use the activeContainer getter.
*/
getCurrentContainer() {
return this.activeContainer
}
/**
* @deprecated
* This method currently exists for retrocompatibility reasons.
* If you want the current playback reference, use the activePlayback getter.
*/
getCurrentPlayback() {
return this.activePlayback
}
getPlaybackType() {
return this.activeContainer && this.activeContainer.getPlaybackType()
}
isFullscreen() {
return Fullscreen.getFullscreenElement() === (Browser.isiOS ? this.activeContainer.el : this.el)
}
toggleFullscreen() {
if (this.isFullscreen()) {
Fullscreen.cancelFullscreen()
!Browser.isiOS && this.$el.removeClass('fullscreen nocursor')
} else {
Fullscreen.requestFullscreen(Browser.isiOS ? this.activeContainer.el : this.el)
!Browser.isiOS && this.$el.addClass('fullscreen')
}
}
onMouseMove(event) {
this.trigger(Events.CORE_MOUSE_MOVE, event)
}
onMouseLeave(event) {
this.trigger(Events.CORE_MOUSE_LEAVE, event)
}
/**
* enables to configure the container after its creation
* @method configure
* @param {Object} options all the options to change in form of a javascript object
*/
configure(options) {
this._options = $.extend(this._options, options)
this.configureDomRecycler()
const sources = options.source || options.sources
sources && this.load(sources, options.mimeType || this.options.mimeType)
this.trigger(Events.CORE_OPTIONS_CHANGE, options) // Trigger with newly provided options
this.containers.forEach((container) => container.configure(this.options))
}
appendToParent() {
let hasCoreParent = this.$el.parent() && this.$el.parent().length
!hasCoreParent && this.$el.appendTo(this.options.parentElement)
}
render() {
if (!style)
style = Styler.getStyleFor(fontStyle, { baseUrl: this.options.baseUrl })
$('head').append(style)
this.options.width = this.options.width || this.$el.width()
this.options.height = this.options.height || this.$el.height()
const size = { width: this.options.width, height: this.options.height }
this.playerInfo.previousSize = this.playerInfo.currentSize = this.playerInfo.computedSize = size
this.updateSize()
this.previousSize = { width: this.$el.width(), height: this.$el.height() }
this.enableResizeObserver()
return this
}
}
Object.assign(Core.prototype, ErrorMixin)