-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
Copy pathplugin.js
447 lines (396 loc) · 11.7 KB
/
plugin.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* @file plugin.js
*/
import evented from './mixins/evented';
import stateful from './mixins/stateful';
import * as Events from './utils/events';
import * as Fn from './utils/fn';
import Player from './player';
/**
* The base plugin name.
*
* @private
* @constant
* @type {string}
*/
const BASE_PLUGIN_NAME = 'plugin';
/**
* The key on which a player's active plugins cache is stored.
*
* @private
* @constant
* @type {string}
*/
const PLUGIN_CACHE_KEY = 'activePlugins_';
/**
* Stores registered plugins in a private space.
*
* @private
* @type {Object}
*/
const pluginStorage = {};
/**
* Reports whether or not a plugin has been registered.
*
* @private
* @param {string} name
* The name of a plugin.
*
* @returns {boolean}
* Whether or not the plugin has been registered.
*/
const pluginExists = (name) => pluginStorage.hasOwnProperty(name);
/**
* Get a single registered plugin by name.
*
* @private
* @param {string} name
* The name of a plugin.
*
* @returns {Function|undefined}
* The plugin (or undefined).
*/
const getPlugin = (name) => pluginExists(name) ? pluginStorage[name] : undefined;
/**
* Marks a plugin as "active" on a player.
*
* Also, ensures that the player has an object for tracking active plugins.
*
* @private
* @param {Player} player
* A Video.js player instance.
*
* @param {string} name
* The name of a plugin.
*/
const markPluginAsActive = (player, name) => {
player[PLUGIN_CACHE_KEY] = player[PLUGIN_CACHE_KEY] || {};
player[PLUGIN_CACHE_KEY][name] = true;
};
/**
* Takes a basic plugin function and returns a wrapper function which marks
* on the player that the plugin has been activated.
*
* @private
* @param {string} name
* The name of the plugin.
*
* @param {Function} plugin
* The basic plugin.
*
* @returns {Function}
* A wrapper function for the given plugin.
*/
const createBasicPlugin = (name, plugin) => function() {
const instance = plugin.apply(this, arguments);
markPluginAsActive(this, name);
// We trigger the "pluginsetup" event on the player regardless, but we want
// the hash to be consistent with the hash provided for advanced plugins.
// The only potentially counter-intuitive thing here is the `instance` is the
// value returned by the `plugin` function.
this.trigger('pluginsetup', {name, plugin, instance});
return instance;
};
/**
* Takes a plugin sub-class and returns a factory function for generating
* instances of it.
*
* This factory function will replace itself with an instance of the requested
* sub-class of Plugin.
*
* @private
* @param {string} name
* The name of the plugin.
*
* @param {Plugin} PluginSubClass
* The advanced plugin.
*
* @returns {Function}
*/
const createPluginFactory = (name, PluginSubClass) => {
// Add a `name` property to the plugin prototype so that each plugin can
// refer to itself by name.
PluginSubClass.prototype.name = name;
return function(...args) {
const instance = new PluginSubClass(...[this, ...args]);
// The plugin is replaced by a function that returns the current instance.
this[name] = () => instance;
return instance;
};
};
/**
* Parent class for all advanced plugins.
*
* @mixes module:evented~EventedMixin
* @mixes module:stateful~StatefulMixin
* @fires Player#pluginsetup
* @listens Player#dispose
* @throws {Error}
* If attempting to instantiate the base {@link Plugin} class
* directly instead of via a sub-class.
*/
class Plugin {
/**
* Creates an instance of this class.
*
* Sub-classes should call `super` to ensure plugins are properly initialized.
*
* @param {Player} player
* A Video.js player instance.
*/
constructor(player) {
this.player = player;
if (this.constructor === Plugin) {
throw new Error('Plugin must be sub-classed; not directly instantiated.');
}
// Make this object evented, but remove the added `trigger` method so we
// use the prototype version instead.
evented(this);
delete this.trigger;
stateful(this, this.constructor.defaultState);
markPluginAsActive(player, this.name);
// Auto-bind the dispose method so we can use it as a listener and unbind
// it later easily.
this.dispose = Fn.bind(this, this.dispose);
// If the player is disposed, dispose the plugin.
player.on('dispose', this.dispose);
player.trigger('pluginsetup', this.getEventHash());
}
/**
* Each event triggered by plugins includes a hash of additional data with
* conventional properties.
*
* This returns that object or mutates an existing hash.
*
* @param {Object} [hash={}]
* An object to be used as event an event hash.
*
* @returns {Plugin~PluginEventHash}
* An event hash object with provided properties mixed-in.
*/
getEventHash(hash = {}) {
hash.name = this.name;
hash.plugin = this.constructor;
hash.instance = this;
return hash;
}
/**
* Triggers an event on the plugin object and overrides
* {@link module:evented~EventedMixin.trigger|EventedMixin.trigger}.
*
* @param {string|Object} event
* An event type or an object with a type property.
*
* @param {Object} [hash={}]
* Additional data hash to merge with a
* {@link Plugin~PluginEventHash|PluginEventHash}.
*
* @returns {boolean}
* Whether or not default was prevented.
*/
trigger(event, hash = {}) {
return Events.trigger(this.eventBusEl_, event, this.getEventHash(hash));
}
/**
* Handles "statechanged" events on the plugin. No-op by default, override by
* subclassing.
*
* @abstract
* @param {Event} e
* An event object provided by a "statechanged" event.
*
* @param {Object} e.changes
* An object describing changes that occurred with the "statechanged"
* event.
*/
handleStateChanged(e) {}
/**
* Disposes a plugin.
*
* Subclasses can override this if they want, but for the sake of safety,
* it's probably best to subscribe the "dispose" event.
*
* @fires Plugin#dispose
*/
dispose() {
const {name, player} = this;
/**
* Signals that a advanced plugin is about to be disposed.
*
* @event Plugin#dispose
* @type {EventTarget~Event}
*/
this.trigger('dispose');
this.off();
player.off('dispose', this.dispose);
// Eliminate any possible sources of leaking memory by clearing up
// references between the player and the plugin instance and nulling out
// the plugin's state and replacing methods with a function that throws.
player[PLUGIN_CACHE_KEY][name] = false;
this.player = this.state = null;
// Finally, replace the plugin name on the player with a new factory
// function, so that the plugin is ready to be set up again.
player[name] = createPluginFactory(name, pluginStorage[name]);
}
/**
* Determines if a plugin is a basic plugin (i.e. not a sub-class of `Plugin`).
*
* @param {string|Function} plugin
* If a string, matches the name of a plugin. If a function, will be
* tested directly.
*
* @returns {boolean}
* Whether or not a plugin is a basic plugin.
*/
static isBasic(plugin) {
const p = (typeof plugin === 'string') ? getPlugin(plugin) : plugin;
return typeof p === 'function' && !Plugin.prototype.isPrototypeOf(p.prototype);
}
/**
* Register a Video.js plugin.
*
* @param {string} name
* The name of the plugin to be registered. Must be a string and
* must not match an existing plugin or a method on the `Player`
* prototype.
*
* @param {Function} plugin
* A sub-class of `Plugin` or a function for basic plugins.
*
* @returns {Function}
* For advanced plugins, a factory function for that plugin. For
* basic plugins, a wrapper function that initializes the plugin.
*/
static registerPlugin(name, plugin) {
if (typeof name !== 'string') {
throw new Error(`Illegal plugin name, "${name}", must be a string, was ${typeof name}.`);
}
if (pluginExists(name) || Player.prototype.hasOwnProperty(name)) {
throw new Error(`Illegal plugin name, "${name}", already exists.`);
}
if (typeof plugin !== 'function') {
throw new Error(`Illegal plugin for "${name}", must be a function, was ${typeof plugin}.`);
}
pluginStorage[name] = plugin;
// Add a player prototype method for all sub-classed plugins (but not for
// the base Plugin class).
if (name !== BASE_PLUGIN_NAME) {
if (Plugin.isBasic(plugin)) {
Player.prototype[name] = createBasicPlugin(name, plugin);
Object.keys(plugin).forEach(function(prop) {
Player.prototype[name][prop] = plugin[prop];
});
} else {
Player.prototype[name] = createPluginFactory(name, plugin);
}
}
return plugin;
}
/**
* De-register a Video.js plugin.
*
* @param {string} name
* The name of the plugin to be deregistered.
*/
static deregisterPlugin(name) {
if (name === BASE_PLUGIN_NAME) {
throw new Error('Cannot de-register base plugin.');
}
if (pluginExists(name)) {
delete pluginStorage[name];
delete Player.prototype[name];
}
}
/**
* Gets an object containing multiple Video.js plugins.
*
* @param {Array} [names]
* If provided, should be an array of plugin names. Defaults to _all_
* plugin names.
*
* @returns {Object|undefined}
* An object containing plugin(s) associated with their name(s) or
* `undefined` if no matching plugins exist).
*/
static getPlugins(names = Object.keys(pluginStorage)) {
let result;
names.forEach(name => {
const plugin = getPlugin(name);
if (plugin) {
result = result || {};
result[name] = plugin;
}
});
return result;
}
/**
* Gets a plugin's version, if available
*
* @param {string} name
* The name of a plugin.
*
* @returns {string}
* The plugin's version or an empty string.
*/
static getPluginVersion(name) {
const plugin = getPlugin(name);
return plugin && plugin.VERSION || '';
}
}
/**
* Gets a plugin by name if it exists.
*
* @static
* @method getPlugin
* @memberOf Plugin
* @param {string} name
* The name of a plugin.
*
* @returns {Function|undefined}
* The plugin (or `undefined`).
*/
Plugin.getPlugin = getPlugin;
/**
* The name of the base plugin class as it is registered.
*
* @type {string}
*/
Plugin.BASE_PLUGIN_NAME = BASE_PLUGIN_NAME;
Plugin.registerPlugin(BASE_PLUGIN_NAME, Plugin);
/**
* Documented in player.js
*
* @ignore
*/
Player.prototype.usingPlugin = function(name) {
return !!this[PLUGIN_CACHE_KEY] && this[PLUGIN_CACHE_KEY][name] === true;
};
/**
* Documented in player.js
*
* @ignore
*/
Player.prototype.hasPlugin = function(name) {
return !!pluginExists(name);
};
export default Plugin;
/**
* Signals that a plugin has just been set up on a player.
*
* @event Player#pluginsetup
* @type {Plugin~PluginEventHash}
*/
/**
* @typedef {Object} Plugin~PluginEventHash
*
* @property {string} instance
* For basic plugins, the return value of the plugin function. For
* advanced plugins, the plugin instance on which the event is fired.
*
* @property {string} name
* The name of the plugin.
*
* @property {string} plugin
* For basic plugins, the plugin function. For advanced plugins, the
* plugin class/constructor.
*/