[REF] web, *: remove legacy storage

*: web_editor, website, website_sale, website_slides

This commit removes the legacy storage (localStorage and sessionStorage)
and replaces it with the storage coming from browser.

Part of task-id 3439226

Part-of: odoo/odoo#133132
This commit is contained in:
Pierre Rousseau
2023-08-25 10:59:54 +02:00
parent fd53a6bac6
commit 609a7b234b
11 changed files with 11 additions and 212 deletions
@@ -111,14 +111,11 @@
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',
-3
View File
@@ -162,14 +162,11 @@
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',
-6
View File
@@ -111,14 +111,11 @@ This module provides the core of the Odoo Web Client.
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',
@@ -274,14 +271,11 @@ This module provides the core of the Odoo Web Client.
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',
@@ -1,51 +0,0 @@
/** @odoo-module **/
import RamStorage from "@web/legacy/js/core/ram_storage";
import mixins from "@web/legacy/js/core/mixins";
// use a fake localStorage in RAM if the native localStorage is unavailable
// (e.g. private browsing in Safari)
var storage;
var localStorage = window.localStorage;
try {
var uid = new Date();
localStorage.setItem(uid, uid);
localStorage.removeItem(uid);
/*
* We create an intermediate object in order to triggered the storage on
* this object. the localStorage. This simplifies testing and usage as
* starages are commutable in services without change. Also, objects
* that use storage do not have to know that events go through window,
* it's not up to them to handle these cases.
*/
storage = (function () {
var storage = Object.create(Object.assign({
getItem: localStorage.getItem.bind(localStorage),
setItem: localStorage.setItem.bind(localStorage),
removeItem: localStorage.removeItem.bind(localStorage),
clear: localStorage.clear.bind(localStorage),
},
mixins.EventDispatcherMixin
));
storage.init();
$(window).on('storage', function (e) {
var key = e.originalEvent.key;
var newValue = e.originalEvent.newValue;
try {
JSON.parse(newValue);
storage.trigger('storage', {
key: key,
newValue: newValue,
});
} catch {}
});
return storage;
})();
} catch {
console.warn('Fail to load localStorage');
storage = new RamStorage();
}
export default storage;
@@ -1,79 +0,0 @@
/** @odoo-module **/
/**
* 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).
*/
import Class from "@web/legacy/js/core/class";
import mixins from "@web/legacy/js/core/mixins";
var RamStorage = Class.extend(mixins.EventDispatcherMixin, {
/**
* @constructor
*/
init: function () {
mixins.EventDispatcherMixin.init.call(this);
if (!this.storage) {
this.clear();
}
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Removes all data from the storage
*/
clear: function () {
this.storage = Object.create(null);
this.length = 0;
},
/**
* Returns the value associated with a given key in the storage
*
* @param {string} key
* @returns {string}
*/
getItem: function (key) {
return this.storage[key];
},
/**
* @param {integer} index
* @return {string}
*/
key: function (index) {
return Object.keys(this.storage)[index];
},
/**
* Removes the given key from the storage
*
* @param {string} key
*/
removeItem: function (key) {
if (key in this.storage) {
this.length--;
}
delete this.storage[key];
this.trigger('storage', {key: key, newValue: null});
},
/**
* 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) {
if (!(key in this.storage)) {
this.length++;
}
this.storage[key] = value;
this.trigger('storage', {key: key, newValue: value});
},
});
export default RamStorage;
@@ -1,53 +0,0 @@
/** @odoo-module **/
import RamStorage from "@web/legacy/js/core/ram_storage";
import mixins from "@web/legacy/js/core/mixins";
// use a fake sessionStorage in RAM if the native sessionStorage is unavailable
// (e.g. private browsing in Safari)
var storage;
var sessionStorage = window.sessionStorage;
try {
var uid = new Date();
sessionStorage.setItem(uid, uid);
sessionStorage.removeItem(uid);
/*
* We create an intermediate object in order to triggered the storage on
* this object. the sessionStorage. This simplifies testing and usage as
* starages are commutable in services without change. Also, objects
* that use storage do not have to know that events go through window,
* it's not up to them to handle these cases.
*/
storage = (function () {
var storage = Object.create(Object.assign({
getItem: sessionStorage.getItem.bind(sessionStorage),
setItem: sessionStorage.setItem.bind(sessionStorage),
removeItem: sessionStorage.removeItem.bind(sessionStorage),
clear: sessionStorage.clear.bind(sessionStorage),
},
mixins.EventDispatcherMixin
));
storage.init();
$(window).on('storage', function (e) {
var key = e.originalEvent.key;
var newValue = e.originalEvent.newValue;
try {
JSON.parse(newValue);
if (sessionStorage.getItem(key) === newValue) {
storage.trigger('storage', {
key: key,
newValue: newValue,
});
}
} catch {}
});
return storage;
})();
} catch {
console.warn('Fail to load sessionStorage');
storage = new RamStorage();
}
export default storage;
-3
View File
@@ -101,14 +101,11 @@ Odoo Web Editor widget.
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',
@@ -5,7 +5,7 @@ import { Mutex } from "@web/core/utils/concurrency";
import { _t } from "@web/core/l10n/translation";
import dom from "@web/legacy/js/core/dom";
import Widget from "@web/legacy/js/core/widget";
import localStorage from "@web/legacy/js/core/local_storage";
import { browser } from "@web/core/browser/browser";
import { debounce } from "@web/core/utils/timing";
import { sortBy } from "@web/core/utils/arrays";
import { pick } from "@web/core/utils/objects";
@@ -329,10 +329,10 @@ var ViewEditor = Widget.extend({
}
}
function storeEditorWidth() {
localStorage.setItem('ace_editor_width', this.$el.width());
browser.localStorage.setItem('ace_editor_width', this.$el.width());
}
function readEditorWidth() {
var width = localStorage.getItem('ace_editor_width');
var width = browser.localStorage.getItem('ace_editor_width');
return parseInt(width || 720, 10);
}
function startResizing(e) {
@@ -4,10 +4,9 @@ import { _t } from "@web/core/l10n/translation";
import { useService } from '@web/core/utils/hooks';
import { WebsiteEditorComponent } from '../editor/editor';
import { WebsiteDialog } from '../dialog/dialog';
import localStorage from '@web/legacy/js/core/local_storage';
import { browser } from "@web/core/browser/browser";
const { useEffect, useRef, Component, xml } = owl;
const localStorageNoDialogKey = 'website_translator_nodialog';
export class AttributeTranslateDialog extends Component {
@@ -86,7 +85,7 @@ export class TranslatorInfoDialog extends Component {
}
onStrongOkClick() {
localStorage.setItem(localStorageNoDialogKey, true);
browser.localStorage.setItem(localStorageNoDialogKey, true);
}
}
TranslatorInfoDialog.components = { WebsiteDialog };
@@ -250,7 +249,7 @@ export class WebsiteTranslator extends WebsiteEditorComponent {
ev.preventDefault();
});
if (!localStorage.getItem(localStorageNoDialogKey)) {
if (!browser.localStorage.getItem(localStorageNoDialogKey)) {
this.dialogService.add(TranslatorInfoDialog);
}
@@ -1,6 +1,6 @@
/** @odoo-module **/
import localStorage from "@web/legacy/js/core/local_storage";
import { browser } from "@web/core/browser/browser";
import { registry } from "@web/core/registry";
import tourUtils from "@website_sale/js/tours/tour_utils";
@@ -18,7 +18,7 @@ registry.category("web_tour.tours").add('shop_cart_recovery', {
trigger: 'td.td-product_name:contains("Acoustic Bloc Screens")',
run: function () {
var orderId = $('.my_cart_quantity').data('order-id');
localStorage.setItem(orderIdKey, orderId);
browser.localStorage.setItem(orderIdKey, orderId);
window.location.href = "/web/session/logout?redirect=/web/login";
},
},
@@ -26,7 +26,7 @@ registry.category("web_tour.tours").add('shop_cart_recovery', {
content: "login as admin and go to the SO (backend)",
trigger: '.oe_login_form',
run: function () {
var orderId = localStorage.getItem(orderIdKey);
var orderId = browser.localStorage.getItem(orderIdKey);
var url = "/web#action=sale.action_orders&view_type=form&id=" + orderId;
var $loginForm = $('.oe_login_form');
$loginForm.find('input[name="login"]').val("admin");
@@ -52,7 +52,7 @@ registry.category("web_tour.tours").add('shop_cart_recovery', {
trigger: '.o-mail-Message-body a:containsExact("Resume order")',
run: function () {
var link = $('.o-mail-Message-body a:containsExact("Resume order")').attr('href');
localStorage.setItem(recoveryLinkKey, link);
browser.localStorage.setItem(recoveryLinkKey, link);
window.location.href = "/web/session/logout?redirect=/";
}
},
@@ -60,6 +60,7 @@ registry.category("web_tour.tours").add('shop_cart_recovery', {
content: "go to the recovery link",
trigger: 'a[href="/web/login"]',
run: function () {
const localStorage = browser.localStorage;
window.location.href = localStorage.getItem(recoveryLinkKey);
},
},
-3
View File
@@ -204,14 +204,11 @@ Featuring
'web/static/src/legacy/js/core/dialog.js',
'web/static/src/legacy/xml/dialog.xml',
'web/static/src/legacy/js/core/dom.js',
'web/static/src/legacy/js/core/local_storage.js',
'web/static/src/legacy/js/core/mixins.js',
'web/static/src/legacy/js/core/ram_storage.js',
'web/static/src/legacy/js/core/registry.js',
'web/static/src/legacy/js/core/rpc.js',
'web/static/src/legacy/js/core/service_mixins.js',
'web/static/src/legacy/js/core/session.js',
'web/static/src/legacy/js/core/session_storage.js',
'web/static/src/legacy/js/core/time.js',
'web/static/src/legacy/js/core/translation.js',
'web/static/src/legacy/js/core/utils.js',