IE & Edge fixes, Storage & Console classes

This commit is contained in:
Sam Potts
2017-12-08 10:05:38 +00:00
parent de54929bb7
commit c8990bd379
33 changed files with 1051 additions and 615 deletions

View File

@ -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);
// 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;
}
}
get(key) {
const store = window.localStorage.getItem(this.key);
if (!Storage.supported || utils.is.empty(store)) {
return null;
}
const json = JSON.parse(store);
return utils.is.string(key) && key.length ? json[key] : json;
}
set(object) {
// Bail if we don't have localStorage support or it's disabled
if (!Storage.supported || !this.enabled) {
return;
}
// Can only store objectst
if (!utils.is.object(object)) {
return;
}
// Get current storage
const storage = this.get();
// Update the working copy of the values
utils.extend(storage, object);
// Update storage
window.localStorage.setItem(this.key, JSON.stringify(storage));
}
}
// 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;
}
// 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(storage, object);
// Update storage
window.localStorage.setItem(this.config.storage.key, JSON.stringify(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, get };
export default Storage;