aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/storage.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/storage.js')
-rw-r--r--src/js/storage.js104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/js/storage.js b/src/js/storage.js
index 27fdad9f..85b5e2b5 100644
--- a/src/js/storage.js
+++ b/src/js/storage.js
@@ -6,72 +6,72 @@ 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(player) {
+ this.enabled = player.config.storage.enabled;
+ this.key = player.config.storage.key;
+ }
+
+ // Check for actual support (see if we can use it)
+ static get supported() {
+ try {
+ 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
+ window.localStorage.setItem(test, test);
+ window.localStorage.removeItem(test);
+
+ return true;
+ } catch (e) {
+ return false;
}
+ }
- // Check for actual support (see if we can use it)
- static get supported() {
- try {
- 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
- window.localStorage.setItem(test, test);
- window.localStorage.removeItem(test);
-
- return true;
- } catch (e) {
- return false;
- }
+ get(key) {
+ if (!Storage.supported || !this.enabled) {
+ return null;
}
- get(key) {
- if (!Storage.supported || !this.enabled) {
- return null;
- }
+ const store = window.localStorage.getItem(this.key);
- const store = window.localStorage.getItem(this.key);
+ if (is.empty(store)) {
+ return null;
+ }
- if (is.empty(store)) {
- return null;
- }
+ const json = JSON.parse(store);
- const json = JSON.parse(store);
+ return is.string(key) && key.length ? json[key] : json;
+ }
- return 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;
}
- 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 (!is.object(object)) {
- return;
- }
+ // Can only store objectst
+ if (!is.object(object)) {
+ return;
+ }
- // Get current storage
- let storage = this.get();
+ // Get current storage
+ let storage = this.get();
- // Default to empty object
- if (is.empty(storage)) {
- storage = {};
- }
+ // Default to empty object
+ if (is.empty(storage)) {
+ storage = {};
+ }
- // Update the working copy of the values
- extend(storage, object);
+ // Update the working copy of the values
+ extend(storage, object);
- // Update storage
- window.localStorage.setItem(this.key, JSON.stringify(storage));
- }
+ // Update storage
+ window.localStorage.setItem(this.key, JSON.stringify(storage));
+ }
}
export default Storage;