From 392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 13 Jun 2018 00:02:55 +1000 Subject: Utils broken down into seperate files and exports --- src/js/source.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'src/js/source.js') diff --git a/src/js/source.js b/src/js/source.js index e9a2938e..d4a66963 100644 --- a/src/js/source.js +++ b/src/js/source.js @@ -2,23 +2,24 @@ // Plyr source update // ========================================================================== +import { providers } from './config/types'; import html5 from './html5'; import media from './media'; import support from './support'; -import { providers } from './types'; import ui from './ui'; -import utils from './utils'; +import { createElement, insertElement, removeElement } from './utils/elements'; +import is from './utils/is'; const source = { // Add elements to HTML5 media (source, tracks, etc) insertElements(type, attributes) { - if (utils.is.string(attributes)) { - utils.insertElement(type, this.media, { + if (is.string(attributes)) { + insertElement(type, this.media, { src: attributes, }); - } else if (utils.is.array(attributes)) { + } else if (is.array(attributes)) { attributes.forEach(attribute => { - utils.insertElement(type, this.media, attribute); + insertElement(type, this.media, attribute); }); } }, @@ -26,7 +27,7 @@ const source = { // Update source // Sources are not checked for support so be careful change(input) { - if (!utils.is.object(input) || !('sources' in input) || !input.sources.length) { + if (!is.object(input) || !('sources' in input) || !input.sources.length) { this.debug.warn('Invalid source format'); return; } @@ -42,17 +43,17 @@ const source = { this.options.quality = []; // Remove elements - utils.removeElement(this.media); + removeElement(this.media); this.media = null; // Reset class name - if (utils.is.element(this.elements.container)) { + if (is.element(this.elements.container)) { this.elements.container.removeAttribute('class'); } // Set the type and provider this.type = input.type; - this.provider = !utils.is.empty(input.sources[0].provider) ? input.sources[0].provider : providers.html5; + this.provider = !is.empty(input.sources[0].provider) ? input.sources[0].provider : providers.html5; // Check for support this.supported = support.check(this.type, this.provider, this.config.playsinline); @@ -60,16 +61,16 @@ const source = { // Create new markup switch (`${this.provider}:${this.type}`) { case 'html5:video': - this.media = utils.createElement('video'); + this.media = createElement('video'); break; case 'html5:audio': - this.media = utils.createElement('audio'); + this.media = createElement('audio'); break; case 'youtube:video': case 'vimeo:video': - this.media = utils.createElement('div', { + this.media = createElement('div', { src: input.sources[0].src, }); break; @@ -82,7 +83,7 @@ const source = { this.elements.container.appendChild(this.media); // Autoplay the new source? - if (utils.is.boolean(input.autoplay)) { + if (is.boolean(input.autoplay)) { this.config.autoplay = input.autoplay; } @@ -94,7 +95,7 @@ const source = { if (this.config.autoplay) { this.media.setAttribute('autoplay', ''); } - if (!utils.is.empty(input.poster)) { + if (!is.empty(input.poster)) { this.poster = input.poster; } if (this.config.loop.active) { -- cgit v1.2.3 From 2aa967aba93cb485ea2c4d10615cee505a354d15 Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Fri, 15 Jun 2018 12:33:30 +0200 Subject: Replace switch in source.js with destructuring --- src/js/source.js | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) (limited to 'src/js/source.js') diff --git a/src/js/source.js b/src/js/source.js index d4a66963..c62db15a 100644 --- a/src/js/source.js +++ b/src/js/source.js @@ -8,6 +8,7 @@ import media from './media'; import support from './support'; import ui from './ui'; import { createElement, insertElement, removeElement } from './utils/elements'; +import { getDeep } from './utils/objects'; import is from './utils/is'; const source = { @@ -27,7 +28,7 @@ const source = { // Update source // Sources are not checked for support so be careful change(input) { - if (!is.object(input) || !('sources' in input) || !input.sources.length) { + if (!getDeep(input, 'sources.length')) { this.debug.warn('Invalid source format'); return; } @@ -52,32 +53,19 @@ const source = { } // Set the type and provider - this.type = input.type; - this.provider = !is.empty(input.sources[0].provider) ? input.sources[0].provider : providers.html5; - - // Check for support - this.supported = support.check(this.type, this.provider, this.config.playsinline); - - // Create new markup - switch (`${this.provider}:${this.type}`) { - case 'html5:video': - this.media = createElement('video'); - break; - - case 'html5:audio': - this.media = createElement('audio'); - break; - - case 'youtube:video': - case 'vimeo:video': - this.media = createElement('div', { - src: input.sources[0].src, - }); - break; - - default: - break; - } + const { sources, type } = input; + const [{ provider = providers.html5, src }] = sources; + const tagName = provider === 'html5' ? type : 'div'; + const attributes = provider === 'html5' ? {} : { src }; + + Object.assign(this, { + provider, + type, + // Check for support + supported: support.check(type, provider, this.config.playsinline), + // Create new element + media: createElement(tagName, attributes), + }); // Inject the new element this.elements.container.appendChild(this.media); @@ -114,7 +102,7 @@ const source = { // Set new sources for html5 if (this.isHTML5) { - source.insertElements.call(this, 'source', input.sources); + source.insertElements.call(this, 'source', sources); } // Set video title -- cgit v1.2.3 From d4abb4b1438cb316aacae480e7b7e9b055a60b24 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 17 Jun 2018 01:04:55 +1000 Subject: 120 line width, package upgrade --- src/js/source.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/js/source.js') diff --git a/src/js/source.js b/src/js/source.js index c62db15a..8c9fdf44 100644 --- a/src/js/source.js +++ b/src/js/source.js @@ -8,8 +8,8 @@ import media from './media'; import support from './support'; import ui from './ui'; import { createElement, insertElement, removeElement } from './utils/elements'; -import { getDeep } from './utils/objects'; import is from './utils/is'; +import { getDeep } from './utils/objects'; const source = { // Add elements to HTML5 media (source, tracks, etc) -- cgit v1.2.3