1
0
mirror of https://github.com/videojs/video.js.git synced 2025-02-02 11:34:50 +02:00

feat: Add videojs.getAllPlayers to get an array of players. (#4842)

This commit is contained in:
Pat O'Neill 2018-01-30 11:43:47 -05:00 committed by Gary Katsevman
parent bf3eb45a37
commit 6a0057716b
2 changed files with 40 additions and 31 deletions

View File

@ -285,6 +285,21 @@ videojs.getPlayer = (id) => {
} }
}; };
/**
* Returns an array of all current players.
*
* @return {Array}
* An array of all players. The array will be in the order that
* `Object.keys` provides, which could potentially vary between
* JavaScript engines.
*
*/
videojs.getAllPlayers = () =>
// Disposed players leave a key with a `null` value, so we need to make sure
// we filter those out.
Object.keys(Player.players).map(k => Player.players[k]).filter(Boolean);
/** /**
* Expose players object. * Expose players object.
* *

View File

@ -11,6 +11,7 @@ QUnit.module('video.js', {
}, },
afterEach() { afterEach() {
this.clock.restore(); this.clock.restore();
videojs.getAllPlayers().forEach(p => p.dispose());
} }
}); });
@ -48,9 +49,6 @@ QUnit.test('should return a video player instance', function(assert) {
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('should log if the supplied element is not included in the DOM', QUnit.test('should log if the supplied element is not included in the DOM',
@ -89,8 +87,6 @@ function(assert) {
assert.equal(warnLogs.length, 1, 'did not log another warning'); assert.equal(warnLogs.length, 1, 'did not log another warning');
log.warn = origWarnLog; log.warn = origWarnLog;
player.dispose();
player2.dispose();
}); });
QUnit.test('should log about already initalized players if options already passed', QUnit.test('should log about already initalized players if options already passed',
@ -127,8 +123,6 @@ function(assert) {
'logged the right message'); 'logged the right message');
log.warn = origWarnLog; log.warn = origWarnLog;
player.dispose();
}); });
QUnit.test('should return a video player instance from el html5 tech', function(assert) { QUnit.test('should return a video player instance from el html5 tech', function(assert) {
@ -155,9 +149,6 @@ QUnit.test('should return a video player instance from el html5 tech', function(
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('should return a video player instance from el techfaker', function(assert) { QUnit.test('should return a video player instance from el techfaker', function(assert) {
@ -183,9 +174,6 @@ QUnit.test('should return a video player instance from el techfaker', function(a
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('should add the value to the languages object', function(assert) { QUnit.test('should add the value to the languages object', function(assert) {
@ -295,8 +283,6 @@ QUnit.test('ingest player div if data-vjs-player attribute is present on video p
assert.equal(player.el(), playerDiv, 'we re-used the given div'); assert.equal(player.el(), playerDiv, 'we re-used the given div');
assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously'); assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously');
player.dispose();
}); });
QUnit.test('ingested player div should not create a new tag for movingMediaElementInDOM', function(assert) { QUnit.test('ingested player div should not create a new tag for movingMediaElementInDOM', function(assert) {
@ -330,7 +316,6 @@ QUnit.test('ingested player div should not create a new tag for movingMediaEleme
assert.equal(player.tech_.el(), vid, 'we re-used the video element'); assert.equal(player.tech_.el(), vid, 'we re-used the video element');
assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously'); assert.ok(player.hasClass('foo'), 'keeps any classes that were around previously');
player.dispose();
Html5.prototype.movingMediaElementInDOM = oldMoving; Html5.prototype.movingMediaElementInDOM = oldMoving;
Html5.isSupported = oldIS; Html5.isSupported = oldIS;
Html5.nativeSourceHandler.canPlayType = oldCPT; Html5.nativeSourceHandler.canPlayType = oldCPT;
@ -366,7 +351,6 @@ QUnit.test('should create a new tag for movingMediaElementInDOM', function(asser
assert.notEqual(player.el(), playerDiv, 'we used a new div'); assert.notEqual(player.el(), playerDiv, 'we used a new div');
assert.notEqual(player.tech_.el(), vid, 'we a new video element'); assert.notEqual(player.tech_.el(), vid, 'we a new video element');
player.dispose();
Html5.prototype.movingMediaElementInDOM = oldMoving; Html5.prototype.movingMediaElementInDOM = oldMoving;
Html5.isSupported = oldIS; Html5.isSupported = oldIS;
Html5.nativeSourceHandler.canPlayType = oldCPT; Html5.nativeSourceHandler.canPlayType = oldCPT;
@ -389,6 +373,28 @@ QUnit.test('getPlayer', function(assert) {
player.dispose(); player.dispose();
}); });
QUnit.test('getAllPlayers', function(assert) {
const fixture = document.getElementById('qunit-fixture');
fixture.innerHTML += '<video id="test_vid_id"></video>' +
'<video id="test_vid_id2"></video>';
let all = videojs.getAllPlayers();
assert.ok(Array.isArray(all), 'an array was returned');
assert.strictEqual(all.length, 0, 'the array was empty because no players have been created yet');
const player = videojs('test_vid_id');
const player2 = videojs('test_vid_id2');
all = videojs.getAllPlayers();
assert.ok(Array.isArray(all), 'an array was returned');
assert.strictEqual(all.length, 2, 'the array had two items');
assert.notStrictEqual(all.indexOf(player), -1, 'the first player was in the array');
assert.notStrictEqual(all.indexOf(player2), -1, 'the second player was in the array');
});
/* **************************************************** * /* **************************************************** *
* div embed tests copied from video emebed tests above * * div embed tests copied from video emebed tests above *
* **************************************************** */ * **************************************************** */
@ -398,8 +404,10 @@ QUnit.module('video.js video-js embed', {
}, },
afterEach() { afterEach() {
this.clock.restore(); this.clock.restore();
videojs.getAllPlayers().forEach(p => p.dispose());
} }
}); });
QUnit.test('should return a video player instance', function(assert) { QUnit.test('should return a video player instance', function(assert) {
const fixture = document.getElementById('qunit-fixture'); const fixture = document.getElementById('qunit-fixture');
@ -423,9 +431,6 @@ QUnit.test('should return a video player instance', function(assert) {
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('should log about already initalized players if options already passed', QUnit.test('should log about already initalized players if options already passed',
@ -462,8 +467,6 @@ function(assert) {
'logged the right message'); 'logged the right message');
log.warn = origWarnLog; log.warn = origWarnLog;
player.dispose();
}); });
QUnit.test('should return a video player instance from el html5 tech', function(assert) { QUnit.test('should return a video player instance from el html5 tech', function(assert) {
@ -490,9 +493,6 @@ QUnit.test('should return a video player instance from el html5 tech', function(
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('should return a video player instance from el techfaker', function(assert) { QUnit.test('should return a video player instance from el techfaker', function(assert) {
@ -518,9 +518,6 @@ QUnit.test('should return a video player instance from el techfaker', function(a
const player2 = videojs(tag2, { techOrder: ['techFaker'] }); const player2 = videojs(tag2, { techOrder: ['techFaker'] });
assert.ok(player2.id() === 'test_vid_id2', 'created player from element'); assert.ok(player2.id() === 'test_vid_id2', 'created player from element');
player.dispose();
player2.dispose();
}); });
QUnit.test('adds video-js class name with the video-js embed', function(assert) { QUnit.test('adds video-js class name with the video-js embed', function(assert) {
@ -536,7 +533,4 @@ QUnit.test('adds video-js class name with the video-js embed', function(assert)
assert.ok(player.hasClass('video-js'), 'video-js class was added to the first embed'); assert.ok(player.hasClass('video-js'), 'video-js class was added to the first embed');
assert.ok(player2.hasClass('video-js'), 'video-js class was preserved to the second embed'); assert.ok(player2.hasClass('video-js'), 'video-js class was preserved to the second embed');
player.dispose();
player2.dispose();
}); });