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); }, /**