From 609a7b234b317e19d76c0f5b80f107a0b50bbefd Mon Sep 17 00:00:00 2001 From: Pierre Rousseau Date: Thu, 24 Aug 2023 07:01:09 +0000 Subject: [PATCH] [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 --- addons/mrp_subcontracting/__manifest__.py | 3 - addons/project/__manifest__.py | 3 - addons/web/__manifest__.py | 6 -- .../src/legacy/js/core/local_storage.js | 51 ------------ .../static/src/legacy/js/core/ram_storage.js | 79 ------------------- .../src/legacy/js/core/session_storage.js | 53 ------------- addons/web_editor/__manifest__.py | 3 - addons/web_editor/static/src/js/common/ace.js | 6 +- .../src/components/translator/translator.js | 7 +- .../tours/website_sale_shop_cart_recovery.js | 9 ++- addons/website_slides/__manifest__.py | 3 - 11 files changed, 11 insertions(+), 212 deletions(-) delete mode 100644 addons/web/static/src/legacy/js/core/local_storage.js delete mode 100644 addons/web/static/src/legacy/js/core/ram_storage.js delete mode 100644 addons/web/static/src/legacy/js/core/session_storage.js diff --git a/addons/mrp_subcontracting/__manifest__.py b/addons/mrp_subcontracting/__manifest__.py index 1aa5d3e3b06..54f653af6e5 100644 --- a/addons/mrp_subcontracting/__manifest__.py +++ b/addons/mrp_subcontracting/__manifest__.py @@ -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', diff --git a/addons/project/__manifest__.py b/addons/project/__manifest__.py index b494b27374c..e5d4a5fb49b 100644 --- a/addons/project/__manifest__.py +++ b/addons/project/__manifest__.py @@ -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', diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py index c5157099c45..23c2bb26b7b 100644 --- a/addons/web/__manifest__.py +++ b/addons/web/__manifest__.py @@ -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', diff --git a/addons/web/static/src/legacy/js/core/local_storage.js b/addons/web/static/src/legacy/js/core/local_storage.js deleted file mode 100644 index 37151812f36..00000000000 --- a/addons/web/static/src/legacy/js/core/local_storage.js +++ /dev/null @@ -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; diff --git a/addons/web/static/src/legacy/js/core/ram_storage.js b/addons/web/static/src/legacy/js/core/ram_storage.js deleted file mode 100644 index 02e1f2ad18a..00000000000 --- a/addons/web/static/src/legacy/js/core/ram_storage.js +++ /dev/null @@ -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; diff --git a/addons/web/static/src/legacy/js/core/session_storage.js b/addons/web/static/src/legacy/js/core/session_storage.js deleted file mode 100644 index 27d0c0e3a59..00000000000 --- a/addons/web/static/src/legacy/js/core/session_storage.js +++ /dev/null @@ -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; diff --git a/addons/web_editor/__manifest__.py b/addons/web_editor/__manifest__.py index 5e0ada6f6e9..ade253344d7 100644 --- a/addons/web_editor/__manifest__.py +++ b/addons/web_editor/__manifest__.py @@ -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', diff --git a/addons/web_editor/static/src/js/common/ace.js b/addons/web_editor/static/src/js/common/ace.js index aced2833db2..14431f21d89 100644 --- a/addons/web_editor/static/src/js/common/ace.js +++ b/addons/web_editor/static/src/js/common/ace.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) { diff --git a/addons/website/static/src/components/translator/translator.js b/addons/website/static/src/components/translator/translator.js index 01fb85cac34..7de5db4088b 100644 --- a/addons/website/static/src/components/translator/translator.js +++ b/addons/website/static/src/components/translator/translator.js @@ -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); } diff --git a/addons/website_sale/static/tests/tours/website_sale_shop_cart_recovery.js b/addons/website_sale/static/tests/tours/website_sale_shop_cart_recovery.js index 8a08276d590..8aecc6d6b65 100644 --- a/addons/website_sale/static/tests/tours/website_sale_shop_cart_recovery.js +++ b/addons/website_sale/static/tests/tours/website_sale_shop_cart_recovery.js @@ -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); }, }, diff --git a/addons/website_slides/__manifest__.py b/addons/website_slides/__manifest__.py index 95062847425..457f422f6d6 100644 --- a/addons/website_slides/__manifest__.py +++ b/addons/website_slides/__manifest__.py @@ -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',