aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/storage.js
diff options
context:
space:
mode:
authorSam Potts <me@sampotts.me>2017-11-04 14:25:28 +1100
committerSam Potts <me@sampotts.me>2017-11-04 14:25:28 +1100
commit1cc2930dc0b81183bc47442f5ad9b5d8df94cc5f (patch)
tree349313769a5e3d786a51b45b0a5c849dc7e3211d /src/js/storage.js
parent3d50936b47fdd691816843de962d5699c3c8f596 (diff)
downloadplyr-1cc2930dc0b81183bc47442f5ad9b5d8df94cc5f.tar.lz
plyr-1cc2930dc0b81183bc47442f5ad9b5d8df94cc5f.tar.xz
plyr-1cc2930dc0b81183bc47442f5ad9b5d8df94cc5f.zip
ES6-ified
Diffstat (limited to 'src/js/storage.js')
-rw-r--r--src/js/storage.js56
1 files changed, 56 insertions, 0 deletions
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 };