From f3e71d01eecfa186c5fcdc40fa6d4c2805207a76 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 8 Dec 2021 10:03:07 +0000 Subject: [PATCH] [REF] web: rework env.config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The motivation of this commit comes from breadcrumbs. Breadcrumbs work as follows: for each controller in the stack of the ActionService, there is an entry in the breadcrumbs. Except for the last entry (which corresponds to the currently displayed controller), the value to display is computed by the ActionService and stored in this.env.config.breadcrumbs. For the last entry, we use the displayName set in this.env.config. When a view wants to update its displayName (e.g. the form view when we switch to another record), it updates the displayName in the config, which thus correctly updates the breadcrumbs. However, this doesn't change the internal values in the ActionService, so if another controller is stacked over the current one, the penultimate entry is wrong. To update the internal state of the ActionService, the event 'controller-title-updated' must be triggered as well, which is cumbersome. Note that we don't really face the issue yet because among already converted views, none of them need to update its displayName in the breadcrumbs. This commit aims at uniformizing the way the (n-1) first entries and the last one behave, by making the "breadcrumbs" key encode all breadcrumbs entries (the n-1 first ones, and the last, current one). We also replace the "displayName" key by "getDisplayName" such that it uses a single source of truth, located in the ActionService, and we provide a function "setDisplayName" to update it. Finally, this commit introduces the notion of "default config" which ensures that standalone views, or views in tests, have a valid config with expected keys. closes odoo/odoo#81031 Related: odoo/enterprise#22799 Signed-off-by: Géry Debongnie --- .../static/src/add_to_board/add_to_board.js | 4 +- .../web/static/src/legacy/action_adapters.js | 4 +- addons/web/static/src/legacy/backend_utils.js | 2 +- addons/web/static/src/legacy/legacy_views.js | 2 +- addons/web/static/src/legacy/xml/base.xml | 6 ++- .../search/control_panel/control_panel.xml | 25 +++++---- .../favorite_menu/custom_favorite_item.js | 4 +- addons/web/static/src/views/view.js | 48 ++++++++++++++++- .../src/webclient/actions/action_service.js | 53 ++++++++++--------- .../web/static/tests/search/control_panel.js | 15 +++--- .../search/custom_favorite_item_tests.js | 4 +- .../tests/search/favorite_menu_tests.js | 4 +- addons/web/static/tests/search/helpers.js | 8 ++- .../static/tests/views/graph_view_tests.js | 30 +++++++---- addons/web/static/tests/views/helpers.js | 7 ++- addons/web/static/tests/views/layout_tests.js | 8 ++- .../webclient/actions/client_action_tests.js | 6 +-- 17 files changed, 149 insertions(+), 81 deletions(-) diff --git a/addons/board/static/src/add_to_board/add_to_board.js b/addons/board/static/src/add_to_board/add_to_board.js index 75f0767339d..3157ffd2070 100644 --- a/addons/board/static/src/add_to_board/add_to_board.js +++ b/addons/board/static/src/add_to_board/add_to_board.js @@ -24,7 +24,7 @@ export class AddToBoard extends Component { setup() { this.notification = useService("notification"); this.rpc = useService("rpc"); - this.state = useState({ name: this.env.config.displayName }); + this.state = useState({ name: this.env.config.getDisplayName() }); useAutofocus(); } @@ -58,7 +58,7 @@ export class AddToBoard extends Component { type: "warning", } ); - this.state.name = this.env.config.displayName; + this.state.name = this.env.config.getDisplayName(); } else { this.notification.add(this.env._t("Could not add filter to dashboard"), { type: "danger", diff --git a/addons/web/static/src/legacy/action_adapters.js b/addons/web/static/src/legacy/action_adapters.js index b3626805703..0003435f22d 100644 --- a/addons/web/static/src/legacy/action_adapters.js +++ b/addons/web/static/src/legacy/action_adapters.js @@ -44,7 +44,7 @@ class ActionAdapter extends ComponentAdapter { }); originalUpdateControlPanel = this.__widget.updateControlPanel.bind(this.__widget); this.__widget.updateControlPanel = (newProps) => { - this.trigger("controller-title-updated", this.__widget.getTitle()); + this.wowlEnv.config.setDisplayName(this.__widget.getTitle()); return originalUpdateControlPanel(newProps); }; core.bus.trigger("DOM_updated"); @@ -77,7 +77,7 @@ class ActionAdapter extends ComponentAdapter { if (this.widget) { const actionTitle = this.widget.getTitle(); if (actionTitle) { - this.trigger("controller-title-updated", actionTitle); + this.wowlEnv.config.setDisplayName(actionTitle); } } this.router.pushState(query); diff --git a/addons/web/static/src/legacy/backend_utils.js b/addons/web/static/src/legacy/backend_utils.js index 3acdc02e097..16a37d3adc6 100644 --- a/addons/web/static/src/legacy/backend_utils.js +++ b/addons/web/static/src/legacy/backend_utils.js @@ -264,7 +264,7 @@ export function breadcrumbsToLegacy(breadcrumbs) { if (!breadcrumbs) { return; } - return breadcrumbs.slice().map((bc) => { + return breadcrumbs.slice(0, -1).map((bc) => { return { title: bc.name, controllerID: bc.jsId }; }); } diff --git a/addons/web/static/src/legacy/legacy_views.js b/addons/web/static/src/legacy/legacy_views.js index 02f04fdb50d..6c256de4f85 100644 --- a/addons/web/static/src/legacy/legacy_views.js +++ b/addons/web/static/src/legacy/legacy_views.js @@ -57,7 +57,7 @@ function registerView(name, LegacyView) { context: Object.assign({}, this.user.context, this.props.action.context), }); - const { actionFlags, breadcrumbs = [] } = this.env.config; + const { actionFlags, breadcrumbs } = this.env.config; this.viewParams = Object.assign({}, actionFlags, { action, // legacy views automatically add the last part of the breadcrumbs diff --git a/addons/web/static/src/legacy/xml/base.xml b/addons/web/static/src/legacy/xml/base.xml index eb34cb1563b..a4fa913585a 100644 --- a/addons/web/static/src/legacy/xml/base.xml +++ b/addons/web/static/src/legacy/xml/base.xml @@ -15,8 +15,10 @@ t-on-click.prevent="trigger('breadcrumb-clicked', { controllerID: bc.controllerID })" title="Previous menu" > - - Unnamed + + + Unnamed +