From 1cc2930dc0b81183bc47442f5ad9b5d8df94cc5f Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 4 Nov 2017 14:25:28 +1100 Subject: ES6-ified --- src/js/storage.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/js/storage.js (limited to 'src/js/storage.js') diff --git a/src/js/storage.js b/src/js/storage.js new file mode 100644 index 00000000..0d6031be --- /dev/null +++ b/src/js/storage.js @@ -0,0 +1,56 @@ +// ========================================================================== +// Plyr storage +// ========================================================================== + +import support from './support'; +import utils from './utils'; + +// Save a value back to local storage +function set(value) { + // Bail if we don't have localStorage support or it's disabled + if (!support.storage || !this.config.storage.enabled) { + return; + } + + // Update the working copy of the values + utils.extend(this.storage, value); + + // Update storage + window.localStorage.setItem(this.config.storage.key, JSON.stringify(this.storage)); +} + +// Setup localStorage +function setup() { + let value = null; + let storage = {}; + + // Bail if we don't have localStorage support or it's disabled + if (!support.storage || !this.config.storage.enabled) { + return storage; + } + + // Clean up old volume + // https://github.com/sampotts/plyr/issues/171 + window.localStorage.removeItem('plyr-volume'); + + // load value from the current key + value = window.localStorage.getItem(this.config.storage.key); + + if (!value) { + // Key wasn't set (or had been cleared), move along + } else if (/^\d+(\.\d+)?$/.test(value)) { + // If value is a number, it's probably volume from an older + // version of this. See: https://github.com/sampotts/plyr/pull/313 + // Update the key to be JSON + set({ + volume: parseFloat(value), + }); + } else { + // Assume it's JSON from this or a later version of plyr + storage = JSON.parse(value); + } + + return storage; +} + +export default { setup, set }; -- cgit v1.2.3 From 1c693df00b44961674a35243f5172716a3735b71 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 5 Nov 2017 11:45:02 +1100 Subject: src getter fix, local storage fix --- src/js/storage.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/js/storage.js') diff --git a/src/js/storage.js b/src/js/storage.js index 0d6031be..ff4222ad 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -5,18 +5,37 @@ import support from './support'; import utils from './utils'; +// Get contents of local storage +function get() { + const store = window.localStorage.getItem(this.config.storage.key); + + if (utils.is.empty(store)) { + return {}; + } + + return JSON.parse(store); +} + // Save a value back to local storage -function set(value) { +function set(object) { // Bail if we don't have localStorage support or it's disabled if (!support.storage || !this.config.storage.enabled) { return; } + // Can only store objectst + if (!utils.is.object(object)) { + return; + } + + // Get current storage + const storage = get.call(this); + // Update the working copy of the values - utils.extend(this.storage, value); + utils.extend(storage, object); // Update storage - window.localStorage.setItem(this.config.storage.key, JSON.stringify(this.storage)); + window.localStorage.setItem(this.config.storage.key, JSON.stringify(storage)); } // Setup localStorage @@ -53,4 +72,4 @@ function setup() { return storage; } -export default { setup, set }; +export default { setup, set, get }; -- cgit v1.2.3 From c8990bd379d97f4eb14cc35aaa90caebfb7db220 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Fri, 8 Dec 2017 10:05:38 +0000 Subject: IE & Edge fixes, Storage & Console classes --- src/js/storage.js | 99 +++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 54 deletions(-) (limited to 'src/js/storage.js') diff --git a/src/js/storage.js b/src/js/storage.js index ff4222ad..f876f107 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -2,74 +2,65 @@ // Plyr storage // ========================================================================== -import support from './support'; import utils from './utils'; -// Get contents of local storage -function get() { - const store = window.localStorage.getItem(this.config.storage.key); - - if (utils.is.empty(store)) { - return {}; +class Storage { + constructor(player) { + this.enabled = player.config.storage.enabled; + this.key = player.config.storage.key; } - return JSON.parse(store); -} - -// Save a value back to local storage -function set(object) { - // Bail if we don't have localStorage support or it's disabled - if (!support.storage || !this.config.storage.enabled) { - return; + // Check for actual support (see if we can use it) + static get supported() { + if (!('localStorage' in window)) { + return false; + } + + const test = '___test'; + + // Try to use it (it might be disabled, e.g. user is in private mode) + // see: https://github.com/sampotts/plyr/issues/131 + try { + window.localStorage.setItem(test, test); + window.localStorage.removeItem(test); + return true; + } catch (e) { + return false; + } } - // Can only store objectst - if (!utils.is.object(object)) { - return; - } + get(key) { + const store = window.localStorage.getItem(this.key); - // Get current storage - const storage = get.call(this); + if (!Storage.supported || utils.is.empty(store)) { + return null; + } - // Update the working copy of the values - utils.extend(storage, object); + const json = JSON.parse(store); - // Update storage - window.localStorage.setItem(this.config.storage.key, JSON.stringify(storage)); -} + return utils.is.string(key) && key.length ? json[key] : json; + } -// Setup localStorage -function setup() { - let value = null; - let storage = {}; + set(object) { + // Bail if we don't have localStorage support or it's disabled + if (!Storage.supported || !this.enabled) { + return; + } - // Bail if we don't have localStorage support or it's disabled - if (!support.storage || !this.config.storage.enabled) { - return storage; - } + // Can only store objectst + if (!utils.is.object(object)) { + return; + } - // Clean up old volume - // https://github.com/sampotts/plyr/issues/171 - window.localStorage.removeItem('plyr-volume'); + // Get current storage + const storage = this.get(); - // load value from the current key - value = window.localStorage.getItem(this.config.storage.key); + // Update the working copy of the values + utils.extend(storage, object); - if (!value) { - // Key wasn't set (or had been cleared), move along - } else if (/^\d+(\.\d+)?$/.test(value)) { - // If value is a number, it's probably volume from an older - // version of this. See: https://github.com/sampotts/plyr/pull/313 - // Update the key to be JSON - set({ - volume: parseFloat(value), - }); - } else { - // Assume it's JSON from this or a later version of plyr - storage = JSON.parse(value); + // Update storage + window.localStorage.setItem(this.key, JSON.stringify(storage)); } - - return storage; } -export default { setup, set, get }; +export default Storage; -- cgit v1.2.3 From dd190155c4aec1d1550517525554c53b10eceef3 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 7 Jan 2018 23:16:55 +1100 Subject: Empty storage fix --- src/js/storage.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/js/storage.js') diff --git a/src/js/storage.js b/src/js/storage.js index f876f107..5f663ab5 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -53,7 +53,12 @@ class Storage { } // Get current storage - const storage = this.get(); + let storage = this.get(); + + // Default to empty object + if (utils.is.empty(storage)) { + storage = {}; + } // Update the working copy of the values utils.extend(storage, object); -- cgit v1.2.3