summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2018-03-19 13:40:10 +0100
committerAaron Bohy <aab@odoo.com>2018-03-23 09:02:03 +0100
commitee0605f405a84439bc71fde3f6bdc2da427ec774 (patch)
tree34f16cbe39912f5eac3794a092af11f619bfb6bf /addons/web/static/src/js/core
parent2a53338649eba63d21560a7ce3da6b003447c628 (diff)
[IMP] web: add sessionStorage service
This rev. adds the 'sessionStorage' service, allowing to write and read stuff in/from the sessionStorage (with a compatibility layer to handle the case where the sessionStorage can't be used, e.g. private browsing in Safari, like it is already done for the localStorage). This new service is necessary for the ActionManager to store the current action, so that it can be fully restored on F5 (this comes with the next commit).
Diffstat (limited to 'addons/web/static/src/js/core')
-rw-r--r--addons/web/static/src/js/core/abstract_storage_service.js56
-rw-r--r--addons/web/static/src/js/core/local_storage.js21
-rw-r--r--addons/web/static/src/js/core/ram_storage.js61
-rw-r--r--addons/web/static/src/js/core/session_storage.js19
4 files changed, 139 insertions, 18 deletions
diff --git a/addons/web/static/src/js/core/abstract_storage_service.js b/addons/web/static/src/js/core/abstract_storage_service.js
new file mode 100644
index 00000000000..6a96036f5f5
--- /dev/null
+++ b/addons/web/static/src/js/core/abstract_storage_service.js
@@ -0,0 +1,56 @@
+odoo.define('web.AbstractStorageService', function (require) {
+'use strict';
+
+/**
+ * This module defines an abstraction for services that write into Storage
+ * objects (e.g. localStorage or sessionStorage).
+ */
+
+var AbstractService = require('web.AbstractService');
+
+var AbstractStorageService = AbstractService.extend({
+ // the 'storage' attribute must be set by actual StorageServices extending
+ // this abstraction
+ storage: null,
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Removes all data from the storage
+ */
+ clear: function() {
+ this.storage.clear();
+ },
+ /**
+ * Returns the value associated with a given key in the storage
+ *
+ * @param {string} key
+ * @returns {string}
+ */
+ getItem: function(key) {
+ return this.storage.getItem(key);
+ },
+ /**
+ * Removes the given key from the storage
+ *
+ * @param {string} key
+ */
+ removeItem: function(key) {
+ this.storage.removeItem(key);
+ },
+ /**
+ * Sets the value of a given key in the storage
+ *
+ * @param {string} key
+ * @param {string} value
+ */
+ setItem: function(key, value) {
+ this.storage.setItem(key,value);
+ },
+});
+
+return AbstractStorageService;
+
+});
diff --git a/addons/web/static/src/js/core/local_storage.js b/addons/web/static/src/js/core/local_storage.js
index 6cf82e87752..da8e6714cb3 100644
--- a/addons/web/static/src/js/core/local_storage.js
+++ b/addons/web/static/src/js/core/local_storage.js
@@ -1,31 +1,16 @@
odoo.define('web.local_storage', function (require) {
'use strict';
-var Class = require('web.Class');
+var RamStorage = require('web.RamStorage');
-// Compatibility with private browsing in Safari
-// Use dict in case of local storage is disabled
+// use a fake localStorage in RAM if the native localStorage is unavailable
+// (e.g. private browsing in Safari)
var localStorage = window.localStorage;
try {
var uid = new Date();
localStorage.setItem(uid, uid);
localStorage.removeItem(uid);
} catch (exception) {
- var storage = {};
- var RamStorage = Class.extend({
- setItem: function(key, value) {
- storage[key] = value;
- },
- getItem: function(key) {
- return storage[key];
- },
- removeItem: function(key) {
- delete storage[key];
- },
- clear: function() {
- storage = {};
- },
- });
localStorage = new RamStorage();
}
diff --git a/addons/web/static/src/js/core/ram_storage.js b/addons/web/static/src/js/core/ram_storage.js
new file mode 100644
index 00000000000..cea66a10dfa
--- /dev/null
+++ b/addons/web/static/src/js/core/ram_storage.js
@@ -0,0 +1,61 @@
+odoo.define('web.RamStorage', function (require) {
+'use strict';
+
+/**
+ * This module defines an alternative of the Storage objects (localStorage,
+ * sessionStorage), stored in RAM. It is used when those native Storage objects
+ * are unavailable (e.g. in private browsing on Safari).
+ */
+
+var Class = require('web.Class');
+
+var RamStorage = Class.extend({
+ /**
+ * @constructor
+ */
+ init: function () {
+ this.storage = Object.create(null);
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Removes all data from the storage
+ */
+ clear: function () {
+ this.init();
+ },
+ /**
+ * Returns the value associated with a given key in the storage
+ *
+ * @param {string} key
+ * @returns {string}
+ */
+ getItem: function (key) {
+ return this.storage[key];
+ },
+ /**
+ * Removes the given key from the storage
+ *
+ * @param {string} key
+ */
+ removeItem: function (key) {
+ delete this.storage[key];
+ },
+ /**
+ * Adds a given key-value pair to the storage, or update the value of the
+ * given key if it already exists
+ *
+ * @param {string} key
+ * @param {string} value
+ */
+ setItem: function (key, value) {
+ this.storage[key] = value;
+ },
+});
+
+return RamStorage;
+
+});
diff --git a/addons/web/static/src/js/core/session_storage.js b/addons/web/static/src/js/core/session_storage.js
new file mode 100644
index 00000000000..f648cee084a
--- /dev/null
+++ b/addons/web/static/src/js/core/session_storage.js
@@ -0,0 +1,19 @@
+odoo.define('web.sessionStorage', function (require) {
+'use strict';
+
+var RamStorage = require('web.RamStorage');
+
+// use a fake sessionStorage in RAM if the native sessionStorage is unavailable
+// (e.g. private browsing in Safari)
+var sessionStorage = window.sessionStorage;
+try {
+ var uid = new Date();
+ sessionStorage.setItem(uid, uid);
+ sessionStorage.removeItem(uid);
+} catch (exception) {
+ sessionStorage = new RamStorage();
+}
+
+return sessionStorage;
+
+});