From ce69db55f80777b95015c23dc4b3fa1a6db5daad Mon Sep 17 00:00:00 2001 From: Mohammed Shekha Date: Tue, 7 Apr 2020 12:32:43 +0000 Subject: [PATCH] [IMP] web: set focus to first visible page when active page invisible Before this commit: if there are modifiers based on field which hides one page and show another and if active page get invisible then instead of first page second page gets focus, consider following example, `
` Consider the initial boolean value to be true; initially the first page will be visible. But when "bar" is unchecked then page1 will be hidden and page3 will get the focus as page2 is still not visible: it will be after the computaion/application of all modifiers, which is currently done after the rendering of a tab page. Now: the first visible tab is focused on rendering (same as before) but also after all modifiers have been updated. closes odoo/odoo#56209 X-original-commit: 10365572aa3629e9b19d2b29726b351275de9f52 Signed-off-by: Lucas Perais (lpe) Co-authored-by: Ipsita Borisagar Co-authored-by: Julien Mougenot --- .../static/src/js/views/form/form_renderer.js | 51 ++++++++++++++----- addons/web/static/tests/views/form_tests.js | 51 +++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) diff --git a/addons/web/static/src/js/views/form/form_renderer.js b/addons/web/static/src/js/views/form/form_renderer.js index 2b91c5820ee..97e3cbd8cb0 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -303,6 +303,28 @@ var FormRenderer = BasicRenderer.extend({ //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- + + /** + * Activates the first visible tab from a given list of tab objects. The + * first tab having an "autofocus" attribute set will be focused in + * priority. + * + * @private + * @param {Object[]} tabs + */ + _activateFirstVisibleTab(tabs) { + const visibleTabs = tabs.filter( + (tab) => !tab.$header.hasClass("o_invisible_modifier") + ); + const autofocusTab = visibleTabs.findIndex( + (tab) => tab.node.attrs.autofocus === "autofocus" + ); + const tabToFocus = visibleTabs[Math.max(0, autofocusTab)]; + if (tabToFocus) { + tabToFocus.$header.find('.nav-link').addClass('active'); + tabToFocus.$page.addClass('active'); + } + }, /** * @override */ @@ -932,7 +954,6 @@ var FormRenderer = BasicRenderer.extend({ var self = this; var $headers = $('