diff options
| author | Julien Mougenot <jum@odoo.com> | 2020-04-22 08:18:42 +0000 |
|---|---|---|
| committer | Julien Mougenot <jum@odoo.com> | 2020-04-22 15:11:02 +0000 |
| commit | dd9627120834961a546df3017fd7f2595e4762aa (patch) | |
| tree | 49d76b854f97442686d775e13d69eca0938a6f1e /addons/web/static | |
| parent | 97bc3717adb26bc459a61456228e8fae5f63fc2a (diff) | |
[FIX] web: handle empty notebooks in form views
Before this commit, when re-activating a previously active notebook tab, the form
renderer would assume that a tab was actually active. However this does not happen
if no tab is visible or defined and it results in a crash instead.
Now, if no active tab is found, a default active tab (0 = first) is set. This only
helps to prevent the crash and does not affect the current behaviour.
closes odoo/odoo#50006
X-original-commit: 73e0c0a1395ebd0e2b4ac7f857c863a0358c5e17
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Julien Mougenot (jum) <jum@odoo.com>
Diffstat (limited to 'addons/web/static')
| -rw-r--r-- | addons/web/static/src/js/views/form/form_renderer.js | 7 | ||||
| -rw-r--r-- | addons/web/static/tests/views/form_tests.js | 71 |
2 files changed, 76 insertions, 2 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 08819f79f57..1b672bdee19 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -213,9 +213,9 @@ var FormRenderer = BasicRenderer.extend({ for (const notebook of this.el.querySelectorAll(':scope div.o_notebook')) { const name = notebook.dataset.name; const navs = notebook.querySelectorAll(':scope .o_notebook_headers .nav-item > .nav-link'); - state[name] = [...navs].findIndex( + state[name] = Math.max([...navs].findIndex( nav => nav.classList.contains('active') - ); + ), 0); } return state; }, @@ -247,6 +247,9 @@ var FormRenderer = BasicRenderer.extend({ // We can't base the amount on the 'navs' length since some overrides // are adding pageless nav items. const validTabsAmount = pages.length; + if (!validTabsAmount) { + continue; // No page defined on the notebook. + } let activeIndex = state[name]; if (navs[activeIndex].classList.contains('o_invisible_modifier')) { activeIndex = [...navs].findIndex( diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index e9d1903c14d..def58359e2f 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -712,6 +712,77 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('empty notebook', async function (assert) { + assert.expect(2); + + const form = await createView({ + arch: ` + <form string="Partners"> + <sheet> + <notebook/> + </sheet> + </form>`, + data: this.data, + model: 'partner', + res_id: 1, + View: FormView, + }); + + // Does not change when switching state + await testUtils.form.clickEdit(form); + + assert.containsNone(form, ':scope .o_notebook .nav'); + + // Does not change when coming back to initial state + await testUtils.form.clickSave(form); + + assert.containsNone(form, ':scope .o_notebook .nav'); + + form.destroy(); + }); + + QUnit.test('no visible page', async function (assert) { + assert.expect(4); + + const form = await createView({ + arch: ` + <form string="Partners"> + <sheet> + <notebook> + <page string="Foo" invisible="1"> + <field name="foo"/> + </page> + <page string="Bar" invisible="1"> + <field name="bar"/> + </page> + </notebook> + </sheet> + </form>`, + data: this.data, + model: 'partner', + res_id: 1, + View: FormView, + }); + + // Does not change when switching state + await testUtils.form.clickEdit(form); + + for (const nav of form.el.querySelectorAll(':scope .o_notebook .nav')) { + assert.containsNone(nav, '.nav-link.active'); + assert.containsN(nav, '.nav-item.o_invisible_modifier', 2); + } + + // Does not change when coming back to initial state + await testUtils.form.clickSave(form); + + for (const nav of form.el.querySelectorAll(':scope .o_notebook .nav')) { + assert.containsNone(nav, '.nav-link.active'); + assert.containsN(nav, '.nav-item.o_invisible_modifier', 2); + } + + form.destroy(); + }); + QUnit.test('invisible attrs on first notebook page', async function (assert) { assert.expect(6); |
