diff --git a/src/js/captions.js b/src/js/captions.js index 04da46510..7d8752c46 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -179,7 +179,7 @@ const captions = { // When passive, don't override user preferences if (!passive) { this.captions.active = active; - this.storage.set({ captions: active }); + this.storage.set({ captions: this.captions }); } // Force language if the call isn't passive and there is no matching language to toggle to diff --git a/src/js/listeners.js b/src/js/listeners.js index cd4680839..828850f5c 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -479,7 +479,7 @@ class Listeners { controls.updateSetting.call(player, 'speed'); // Save to storage - player.storage.set({ speed: player.speed }); + player.storage.set({ speed: player.config.speed }); }); // Quality change diff --git a/src/js/plyr.js b/src/js/plyr.js index 00b95a5fa..78892c590 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -25,6 +25,7 @@ import { createElement, hasClass, removeElement, replaceElement, toggleClass, wr import { off, on, once, triggerEvent, unbindListeners } from './utils/events'; import is from './utils/is'; import loadSprite from './utils/load-sprite'; +import normalizeOptions from './utils/normalize-options'; import { clamp } from './utils/numbers'; import { cloneDeep, extend } from './utils/objects'; import { silencePromise } from './utils/promise'; @@ -37,7 +38,11 @@ import { parseUrl } from './utils/urls'; // Plyr instance class Plyr { - constructor(target, options) { + constructor(target, options = {}) { + const { storage } = defaults; + + this.storage = new Storage(storage.enabled, storage.key); + this.timers = {}; // State @@ -65,18 +70,16 @@ class Plyr { // Set config this.config = extend( {}, - defaults, Plyr.defaults, - options || {}, - (() => { - try { - return JSON.parse(this.media.getAttribute('data-plyr-config')); - } catch (e) { - return {}; - } - })(), + defaults, + this.storage.get() || {}, + options, + this.dataPlayerConfig ); + this.config.speed = normalizeOptions(this.config.speed, defaults.speed); + this.config.quality = normalizeOptions(this.config.quality, defaults.quality); + // Elements cache this.elements = { container: null, @@ -257,9 +260,6 @@ class Plyr { // Create listeners this.listeners = new Listeners(this); - // Setup local storage for user settings - this.storage = new Storage(this); - // Store reference this.media.plyr = this; @@ -403,6 +403,14 @@ class Plyr { return Boolean(this.media.ended); } + get dataPlayerConfig() { + try { + return JSON.parse(this.media.getAttribute('data-plyr-config')); + } catch (e) { + return {}; + } + } + /** * Toggle playback based on current status * @param {Boolean} input @@ -653,10 +661,6 @@ class Plyr { speed = input; } - if (!is.number(speed)) { - speed = this.storage.get('speed'); - } - if (!is.number(speed)) { speed = this.config.speed.selected; } diff --git a/src/js/storage.js b/src/js/storage.js index 27fdad9fa..584bf74c5 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -6,9 +6,9 @@ import is from './utils/is'; import { extend } from './utils/objects'; class Storage { - constructor(player) { - this.enabled = player.config.storage.enabled; - this.key = player.config.storage.key; + constructor(enabled, key) { + this.enabled = enabled; + this.key = key; } // Check for actual support (see if we can use it) diff --git a/src/js/ui.js b/src/js/ui.js index 32db6ae78..da2a362c7 100644 --- a/src/js/ui.js +++ b/src/js/ui.js @@ -61,20 +61,12 @@ const ui = { captions.setup.call(this); } - // Reset volume - this.volume = null; - - // Reset mute state - this.muted = null; - - // Reset loop state - this.loop = null; - - // Reset quality setting - this.quality = null; - - // Reset speed - this.speed = null; + // set volume, mute, loop, quality, speed + this.volume = this.config.volume; + this.muted = this.config.muted; + this.loop = this.config.loop; + this.quality = this.config.quality; + this.speed = this.config.speed; // Reset volume display controls.updateVolume.call(this); diff --git a/src/js/utils/normalize-options.js b/src/js/utils/normalize-options.js new file mode 100644 index 000000000..d6b7ce046 --- /dev/null +++ b/src/js/utils/normalize-options.js @@ -0,0 +1,18 @@ +export default function(options, defaults) { + if (!options) { + return defaults; + } + + const normalizedOptions = {}; + + Object.keys(defaults) + .forEach(key => { + if (Object.prototype.hasOwnProperty.call(options, key)) { + normalizedOptions[key] = options[key]; + } else { + normalizedOptions[key] = defaults[key]; + } + }); + + return normalizedOptions; +} diff --git a/src/js/utils/objects.js b/src/js/utils/objects.js index 225bb4590..75253da3c 100644 --- a/src/js/utils/objects.js +++ b/src/js/utils/objects.js @@ -28,10 +28,7 @@ export function extend(target = {}, ...sources) { Object.keys(source).forEach(key => { if (is.object(source[key])) { - if (!Object.keys(target).includes(key)) { - Object.assign(target, { [key]: {} }); - } - + Object.assign(target, { [key]: {} }); extend(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] });