@@ -36,8 +36,8 @@ const isNumber = n => typeof n === 'number' && n === n;
36
36
/**
37
37
* Whether a value is a string with no whitespace.
38
38
*
39
- * @param {String } s
40
- * @return {Boolean }
39
+ * @param {string } s
40
+ * @return {boolean }
41
41
*/
42
42
const hasNoWhitespace = s => typeof s === 'string' && ( / ^ \S + $ / ) . test ( s ) ;
43
43
@@ -116,6 +116,7 @@ class Overlay extends Component {
116
116
117
117
/**
118
118
* Logs debug errors
119
+ *
119
120
* @param {...[type] } args [description]
120
121
* @return {[type] } [description]
121
122
*/
@@ -160,11 +161,11 @@ class Overlay extends Component {
160
161
/**
161
162
* Determine whether or not the overlay should hide.
162
163
*
163
- * @param {Number } time
164
+ * @param {number } time
164
165
* The current time reported by the player.
165
- * @param {String } type
166
+ * @param {string } type
166
167
* An event type.
167
- * @return {Boolean }
168
+ * @return {boolean }
168
169
*/
169
170
shouldHide_ ( time , type ) {
170
171
const end = this . options_ . end ;
@@ -195,11 +196,11 @@ class Overlay extends Component {
195
196
/**
196
197
* Determine whether or not the overlay should show.
197
198
*
198
- * @param {Number } time
199
+ * @param {number } time
199
200
* The current time reported by the player.
200
- * @param {String } type
201
+ * @param {string } type
201
202
* An event type.
202
- * @return {Boolean }
203
+ * @return {boolean }
203
204
*/
204
205
shouldShow_ ( time , type ) {
205
206
const start = this . options_ . start ;
@@ -301,6 +302,7 @@ videojs.registerComponent('Overlay', Overlay);
301
302
* @param {Object } [options={}]
302
303
*/
303
304
const plugin = function ( options ) {
305
+ const player = this ;
304
306
const settings = videojs . mergeOptions ( defaults , options ) ;
305
307
306
308
// De-initialize the plugin if it already has an array of overlays.
@@ -320,37 +322,98 @@ const plugin = function(options) {
320
322
// because it doesn't make sense to pass it to each Overlay component.
321
323
delete settings . overlays ;
322
324
323
- this . overlays_ = overlays . map ( o => {
324
- const mergeOptions = videojs . mergeOptions ( settings , o ) ;
325
- const attachToControlBar = typeof mergeOptions . attachToControlBar === 'string' || mergeOptions . attachToControlBar === true ;
325
+ const mapOverlays = ( items ) => {
326
+ return items . map ( o => {
327
+ const mergeOptions = videojs . mergeOptions ( settings , o ) ;
328
+ const attachToControlBar = typeof mergeOptions . attachToControlBar === 'string' || mergeOptions . attachToControlBar === true ;
326
329
327
- if ( ! this . controls ( ) || ! this . controlBar ) {
328
- return this . addChild ( 'overlay' , mergeOptions ) ;
329
- }
330
+ if ( ! this . controls ( ) || ! this . controlBar ) {
331
+ return this . addChild ( 'overlay' , mergeOptions ) ;
332
+ }
330
333
331
- if ( attachToControlBar && mergeOptions . align . indexOf ( 'bottom' ) !== - 1 ) {
332
- let referenceChild = this . controlBar . children ( ) [ 0 ] ;
334
+ if ( attachToControlBar && mergeOptions . align . indexOf ( 'bottom' ) !== - 1 ) {
335
+ let referenceChild = this . controlBar . children ( ) [ 0 ] ;
333
336
334
- if ( this . controlBar . getChild ( mergeOptions . attachToControlBar ) !== undefined ) {
335
- referenceChild = this . controlBar . getChild ( mergeOptions . attachToControlBar ) ;
336
- }
337
+ if ( this . controlBar . getChild ( mergeOptions . attachToControlBar ) !== undefined ) {
338
+ referenceChild = this . controlBar . getChild ( mergeOptions . attachToControlBar ) ;
339
+ }
337
340
338
- if ( referenceChild ) {
339
- const referenceChildIndex = this . controlBar . children ( ) . indexOf ( referenceChild ) ;
340
- const controlBarChild = this . controlBar . addChild ( 'overlay' , mergeOptions , referenceChildIndex ) ;
341
+ if ( referenceChild ) {
342
+ const referenceChildIndex = this . controlBar . children ( ) . indexOf ( referenceChild ) ;
343
+ const controlBarChild = this . controlBar . addChild ( 'overlay' , mergeOptions , referenceChildIndex ) ;
341
344
342
- return controlBarChild ;
345
+ return controlBarChild ;
346
+ }
343
347
}
348
+
349
+ const playerChild = this . addChild ( 'overlay' , mergeOptions ) ;
350
+
351
+ this . el ( ) . insertBefore (
352
+ playerChild . el ( ) ,
353
+ this . controlBar . el ( )
354
+ ) ;
355
+
356
+ return playerChild ;
357
+ } ) ;
358
+ } ;
359
+
360
+ this . overlays_ = mapOverlays ( overlays ) ;
361
+
362
+ /**
363
+ * Adds one or more items to the existing list of overlays.
364
+ *
365
+ * @param {Object|Array } item
366
+ * An item (or an array of items) to be added as overlay/s
367
+ *
368
+ * @return {Array[Overlay] }
369
+ * The array of overlay objects that were added
370
+ */
371
+ function add ( item ) {
372
+ if ( ! Array . isArray ( item ) ) {
373
+ item = [ item ] ;
344
374
}
345
375
346
- const playerChild = this . addChild ( 'overlay' , mergeOptions ) ;
376
+ const addedOverlays = mapOverlays ( item ) ;
377
+
378
+ player . overlays_ = player . overlays_ . concat ( addedOverlays ) ;
379
+
380
+ return addedOverlays ;
381
+ }
382
+
383
+ /**
384
+ *
385
+ * @param {Overlay } item
386
+ * An item to be removed from the array of overlays
387
+ *
388
+ * @throws {Error }
389
+ * Item to remove must be present in the array of overlays
390
+ *
391
+ */
392
+ function remove ( item ) {
393
+ const index = player . overlays_ . indexOf ( item ) ;
394
+
395
+ if ( index !== - 1 ) {
396
+ item . el ( ) . parentNode . removeChild ( item . el ( ) ) ;
397
+ player . overlays_ . splice ( index , 1 ) ;
398
+ } else {
399
+ player . log . warn ( 'overlay does not exist and cannot be removed' ) ;
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Gets the array of overlays used for the current video
405
+ *
406
+ * @return The array of overlay objects currently used by the plugin
407
+ */
408
+ function get ( ) {
409
+ return player . overlays_ ;
410
+ }
347
411
348
- this . el ( ) . insertBefore (
349
- playerChild . el ( ) ,
350
- this . controlBar . el ( )
351
- ) ;
352
- return playerChild ;
353
- } ) ;
412
+ return {
413
+ add,
414
+ remove,
415
+ get
416
+ } ;
354
417
} ;
355
418
356
419
plugin . VERSION = VERSION ;
0 commit comments