From 9e4abc10479bfccc67f42e875180f5aac1355eff Mon Sep 17 00:00:00 2001 From: David Date: Wed, 28 Dec 2022 13:33:59 +0100 Subject: [PATCH] [FIX] web_responsive: slashes in app names When the app name had a slash ("/") on it we couldn't properly find it by name. This alternative doesn't depend on such weak rule. We'll find out the root app element for our submenu from the data already available in the widget. TT40970 --- .../static/src/js/web_responsive.js | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/web_responsive/static/src/js/web_responsive.js b/web_responsive/static/src/js/web_responsive.js index d8da1795b..a53da695a 100644 --- a/web_responsive/static/src/js/web_responsive.js +++ b/web_responsive/static/src/js/web_responsive.js @@ -128,6 +128,8 @@ odoo.define("web_responsive", function (require) { for (const n in this._apps) { this._apps[n].web_icon_data = menuData.children[n].web_icon_data; } + // This is handy to be kept + this.menuData = menuData; // Store menu data in a format searchable by fuzzy.js this._searchableMenus = _.reduce(menuData.children, findNames, {}); // Search only after timeout, for fast typers @@ -226,6 +228,33 @@ odoo.define("web_responsive", function (require) { ); }, + /** + * Filters which menu tree object contains a given subelement + * + * @param {Array|Object} menu containing the tree to search for + * @param {Number} id The id to find + * @returns {Object} + */ + _isInMenuTree: function (menu, id) { + let tree = menu; + if (tree instanceof Object && tree.children) { + tree = tree.children; + } + for (const node of tree) { + if (node.id === id) { + return node; + } else if (node.children.length === 0) { + continue; + } else { + const root = this._isInMenuTree(node.children, id); + if (root !== null) { + return root; + } + } + } + return false; + }, + /** * Use chooses a search result, so we navigate to that menu * @@ -234,10 +263,8 @@ odoo.define("web_responsive", function (require) { _searchResultChosen: function (event) { event.preventDefault(); event.stopPropagation(); - const $result = $(event.currentTarget), - text = $result.text().trim(), - data = $result.data(), - suffix = ~text.indexOf("/") ? "/" : ""; + const $result = $(event.currentTarget); + const data = $result.data(); // Load the menu view this.trigger_up("menu_clicked", { action_id: data.actionId, @@ -245,11 +272,14 @@ odoo.define("web_responsive", function (require) { previous_menu_id: data.parentId, }); // Find app that owns the chosen menu - const app = _.find(this._apps, function (_app) { - return text.indexOf(_app.name + suffix) === 0; + const app = this.menuData.children.find((app_menu) => { + return this._isInMenuTree(app_menu, data.menuId); }); + if (!app) { + return; + } // Update navbar menus - core.bus.trigger("change_menu_section", app.menuID); + core.bus.trigger("change_menu_section", app.id); }, /**