[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).
This commit is contained in:
@@ -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;
|
||||
|
||||
});
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
});
|
||||
@@ -1,24 +1,17 @@
|
||||
odoo.define('web.LocalStorageService', function (require) {
|
||||
'use strict';
|
||||
|
||||
var AbstractService = require('web.AbstractService');
|
||||
/**
|
||||
* This module defines a service to access the localStorage object.
|
||||
*/
|
||||
|
||||
var AbstractStorageService = require('web.AbstractStorageService');
|
||||
var core = require('web.core');
|
||||
var localStorage = require('web.local_storage');
|
||||
|
||||
var LocalStorageService = AbstractService.extend({
|
||||
var LocalStorageService = AbstractStorageService.extend({
|
||||
name: 'local_storage',
|
||||
setItem: function(key, value) {
|
||||
localStorage.setItem(key,value);
|
||||
},
|
||||
getItem: function(key) {
|
||||
return localStorage.getItem(key);
|
||||
},
|
||||
removeItem: function(key) {
|
||||
localStorage.removeItem(key);
|
||||
},
|
||||
clear: function() {
|
||||
localStorage.clear();
|
||||
}
|
||||
storage: localStorage,
|
||||
});
|
||||
|
||||
core.serviceRegistry.add('local_storage', LocalStorageService);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
odoo.define('web.SessionStorageService', function (require) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* This module defines a service to access the sessionStorage object.
|
||||
*/
|
||||
|
||||
var AbstractStorageService = require('web.AbstractStorageService');
|
||||
var core = require('web.core');
|
||||
var sessionStorage = require('web.sessionStorage');
|
||||
|
||||
var SessionStorageService = AbstractStorageService.extend({
|
||||
name: 'session_storage',
|
||||
storage: sessionStorage,
|
||||
});
|
||||
|
||||
core.serviceRegistry.add('session_storage', SessionStorageService);
|
||||
|
||||
return SessionStorageService;
|
||||
|
||||
});
|
||||
@@ -69,6 +69,7 @@
|
||||
<script type="text/javascript" src="/web/static/src/js/libs/jquery.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/services/config.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/abstract_service.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/abstract_storage_service.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/class.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/collections.js"/>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/translation.js"></script>
|
||||
@@ -90,6 +91,9 @@
|
||||
<script type="text/javascript" src="/web/static/src/js/core/dialog.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/local_storage.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/services/local_storage_service.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/session_storage.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/services/session_storage_service.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/core/ram_storage.js"></script>
|
||||
<script type="text/javascript" src="/web/static/src/js/widgets/rainbow_man.js"></script>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user