diff options
author | Sam Potts <sam@potts.es> | 2017-12-08 10:05:38 +0000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2017-12-08 10:05:38 +0000 |
commit | c8990bd379d97f4eb14cc35aaa90caebfb7db220 (patch) | |
tree | b6305ac793b3f012007feacb87f23b4918dfd48b /src/js/storage.js | |
parent | de54929bb7bfb38e5720637846d1e7f5552cdc86 (diff) | |
download | plyr-c8990bd379d97f4eb14cc35aaa90caebfb7db220.tar.lz plyr-c8990bd379d97f4eb14cc35aaa90caebfb7db220.tar.xz plyr-c8990bd379d97f4eb14cc35aaa90caebfb7db220.zip |
IE & Edge fixes, Storage & Console classes
Diffstat (limited to 'src/js/storage.js')
-rw-r--r-- | src/js/storage.js | 99 |
1 files changed, 45 insertions, 54 deletions
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; |