aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/html5.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
committerSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
commitcc3c0b54484e6f5a7b4dba8a36a44f345e462f26 (patch)
tree5fe2838546d9f981b21572dee88ee7a1c3195477 /src/js/html5.js
parent4811e3333f1417bc9e14f6cc38afc789e9051c4c (diff)
parent3c9c1b4cdcd0eb9076c3f0bafbabb057ee140c42 (diff)
downloadplyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.lz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.xz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.zip
Merge branch 'develop'
Diffstat (limited to 'src/js/html5.js')
-rw-r--r--src/js/html5.js84
1 files changed, 23 insertions, 61 deletions
diff --git a/src/js/html5.js b/src/js/html5.js
index 63596cfc..6aa96f4c 100644
--- a/src/js/html5.js
+++ b/src/js/html5.js
@@ -3,40 +3,28 @@
// ==========================================================================
import support from './support';
-import utils from './utils';
+import { removeElement } from './utils/elements';
+import { triggerEvent } from './utils/events';
const html5 = {
getSources() {
if (!this.isHTML5) {
- return null;
+ return [];
}
- return this.media.querySelectorAll('source');
+ const sources = Array.from(this.media.querySelectorAll('source'));
+
+ // Filter out unsupported sources
+ return sources.filter(source => support.mime.call(this, source.getAttribute('type')));
},
// Get quality levels
getQualityOptions() {
- if (!this.isHTML5) {
- return null;
- }
-
- // Get sources
- const sources = html5.getSources.call(this);
-
- if (utils.is.empty(sources)) {
- return null;
- }
-
- // Get <source> with size attribute
- const sizes = Array.from(sources).filter(source => !utils.is.empty(source.getAttribute('size')));
-
- // If none, bail
- if (utils.is.empty(sizes)) {
- return null;
- }
-
- // Reduce to unique list
- return utils.dedupe(sizes.map(source => Number(source.getAttribute('size'))));
+ // Get sizes from <source> elements
+ return html5.getSources
+ .call(this)
+ .map(source => Number(source.getAttribute('size')))
+ .filter(Boolean);
},
extend() {
@@ -51,60 +39,34 @@ const html5 = {
get() {
// Get sources
const sources = html5.getSources.call(player);
+ const [source] = sources.filter(source => source.getAttribute('src') === player.source);
- if (utils.is.empty(sources)) {
- return null;
- }
-
- const matches = Array.from(sources).filter(source => source.getAttribute('src') === player.source);
-
- if (utils.is.empty(matches)) {
- return null;
- }
-
- return Number(matches[0].getAttribute('size'));
+ // Return size, if match is found
+ return source && Number(source.getAttribute('size'));
},
set(input) {
// Get sources
const sources = html5.getSources.call(player);
- if (utils.is.empty(sources)) {
- return;
- }
+ // Get first match for requested size
+ const source = sources.find(source => Number(source.getAttribute('size')) === input);
- // Get matches for requested size
- const matches = Array.from(sources).filter(source => Number(source.getAttribute('size')) === input);
-
- // No matches for requested size
- if (utils.is.empty(matches)) {
+ // No matching source found
+ if (!source) {
return;
}
- // Get supported sources
- const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type')));
-
- // No supported sources
- if (utils.is.empty(supported)) {
- return;
- }
-
- // Trigger change event
- utils.dispatchEvent.call(player, player.media, 'qualityrequested', false, {
- quality: input,
- });
-
// Get current state
const { currentTime, playing } = player;
// Set new source
- player.media.src = supported[0].getAttribute('src');
+ player.media.src = source.getAttribute('src');
// Restore time
const onLoadedMetaData = () => {
player.currentTime = currentTime;
- player.off('loadedmetadata', onLoadedMetaData);
};
- player.on('loadedmetadata', onLoadedMetaData);
+ player.once('loadedmetadata', onLoadedMetaData);
// Load new source
player.media.load();
@@ -115,7 +77,7 @@ const html5 = {
}
// Trigger change event
- utils.dispatchEvent.call(player, player.media, 'qualitychange', false, {
+ triggerEvent.call(player, player.media, 'qualitychange', false, {
quality: input,
});
},
@@ -130,7 +92,7 @@ const html5 = {
}
// Remove child sources
- utils.removeElement(html5.getSources());
+ removeElement(html5.getSources.call(this));
// Set blank video src attribute
// This is to prevent a MEDIA_ERR_SRC_NOT_SUPPORTED error