From 08081fc742a8258b45854d5ff50526060b3a9595 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Mon, 25 Mar 2024 15:16:48 +0100 Subject: [PATCH] [FIX] web: buttons with special attr close, close x2many dialogs Before this commit, if a button had the attribute special="close" on the footer of an x2many dialog, the dialog was not closed. This occurs because the view button hook searches the dialog close functions put in the environment by the dialog component, and in this case is another component that initialize the hook, and the function is not found. Now, the view button hook will use the dialog close function put in the environment by the dialog service. closes odoo/odoo#159121 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/core/dialog/dialog.js | 2 +- .../src/views/view_button/view_button_hook.js | 3 +- .../views/fields/one2many_field_tests.js | 41 +++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/addons/web/static/src/core/dialog/dialog.js b/addons/web/static/src/core/dialog/dialog.js index f73505b0f96..22cfad88a2f 100644 --- a/addons/web/static/src/core/dialog/dialog.js +++ b/addons/web/static/src/core/dialog/dialog.js @@ -54,7 +54,7 @@ export class Dialog extends Component { { bypassEditableProtection: true } ); this.id = `dialog_${this.data.id}`; - useChildSubEnv({ inDialog: true, dialogId: this.id, closeDialog: () => this.data.close() }); + useChildSubEnv({ inDialog: true, dialogId: this.id }); this.position = useState({ left: 0, top: 0 }); useDialogDraggable({ enable: () => !this.env.isSmall, diff --git a/addons/web/static/src/views/view_button/view_button_hook.js b/addons/web/static/src/views/view_button/view_button_hook.js index fa068f62ab4..1b3c6b191ce 100644 --- a/addons/web/static/src/views/view_button/view_button_hook.js +++ b/addons/web/static/src/views/view_button/view_button_hook.js @@ -58,7 +58,8 @@ export function useViewButtons(model, ref, options = {}) { if (!_continue) { return; } - const closeDialog = (clickParams.close || clickParams.special) && env.closeDialog; + const closeDialog = + (clickParams.close || clickParams.special) && env.dialogData?.close; const params = getResParams(); const resId = params.resId; const resIds = params.resIds || model.resIds; diff --git a/addons/web/static/tests/views/fields/one2many_field_tests.js b/addons/web/static/tests/views/fields/one2many_field_tests.js index 119f3ff9cea..189742a9aa0 100644 --- a/addons/web/static/tests/views/fields/one2many_field_tests.js +++ b/addons/web/static/tests/views/fields/one2many_field_tests.js @@ -500,6 +500,39 @@ QUnit.module("Fields", (hooks) => { await click(target, ".o_field_many2one input"); }); + QUnit.test( + 'O2M with buttons with attr "special" in dialog close the dialog', + async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + + + + + + + + + `, + }); + + await addRow(target); + assert.containsOnce(target, ".o_dialog"); + + assert.strictEqual(document.querySelector(".modal .btn").innerText, "Cancel"); + + await click(target, ".modal .btn"); + assert.containsNone(target, ".o_dialog"); + } + ); + QUnit.test("O2M modal buttons are disabled on click", async function (assert) { // Records in an o2m can have a m2o pointing to themselves. // In that case, a domain evaluation on that field followed by name_search @@ -14629,16 +14662,16 @@ QUnit.module("Fields", (hooks) => { if (args.method === "onchange") { if (onchangeCount === 1) { // partner turtles onchange for the new x2m record - assert.strictEqual(args.model, 'partner'); + assert.strictEqual(args.model, "partner"); assert.deepEqual(Object.keys(args.args[1]), ["turtles"]); assert.strictEqual(args.args[1].turtles[0][0], 0); - assert.deepEqual(args.args[2], ['turtles']); + assert.deepEqual(args.args[2], ["turtles"]); } else if (onchangeCount === 2) { // x2m record removed, empty list of commands expected - assert.strictEqual(args.model, 'partner'); + assert.strictEqual(args.model, "partner"); assert.deepEqual(Object.keys(args.args[1]), ["turtles"]); assert.deepEqual(args.args[1].turtles, []); - assert.deepEqual(args.args[2], ['turtles']); + assert.deepEqual(args.args[2], ["turtles"]); } onchangeCount++; }