Skip to content

Commit 349e404

Browse files
SimonGeninmafo-odoo
authored andcommitted
[FIX] web: Form stat button stays correctly disabled
This commit is partial cherry-pick of commit 5f5e699 that was added in 15.1. Hence this is a backport fix. A stat button set as disabled was used to just show some statistics without an action on click. However, on tasks executed by the Form, all the buttons would be temporarly set as disabled then set back to enabled. This would lose the original state of the button and make them all clickable. The way it worked before v14.5 was most likely because the action manager would not throw an error on an action with those "invalid" arguments. Also, a button without a type and name will now be set the disabled attribute automatically. opw-2868647 closes odoo#93269 Signed-off-by: Simon Genin (ges@odoo) <[email protected]>
1 parent b685938 commit 349e404

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

addons/web/static/src/legacy/js/views/form/form_renderer.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ var FormRenderer = BasicRenderer.extend({
5353
// display them (e.g. in Studio, in "show invisible" mode). This flag
5454
// allows to disable this optimization.
5555
this.renderInvisible = false;
56+
// Keeps track of buttons that are disabled momentarily and need to be renabled.
57+
// Needed to compare with buttons that have to stay disabled all the time.
58+
this.manuallyDisabledButtons = new Set();
5659
},
5760
/**
5861
* @override
@@ -197,16 +200,24 @@ var FormRenderer = BasicRenderer.extend({
197200
*
198201
*/
199202
disableButtons: function () {
200-
this.$('.o_statusbar_buttons button, .oe_button_box button')
201-
.attr('disabled', true);
203+
const allButtons = this.$el[0].querySelectorAll('.o_statusbar_buttons button, .oe_button_box button');
204+
for (const button of allButtons) {
205+
if (!button.getAttribute("disabled")) {
206+
this.manuallyDisabledButtons.add(button)
207+
button.setAttribute("disabled", true)
208+
}
209+
}
202210
},
203211
/**
204212
* Enable statusbar buttons and stat buttons so they can be clicked again
205213
*
206214
*/
207215
enableButtons: function () {
208-
this.$('.o_statusbar_buttons button, .oe_button_box button')
209-
.removeAttr('disabled');
216+
const allButtons = this.$el[0].querySelectorAll('.o_statusbar_buttons button, .oe_button_box button');
217+
this.manuallyDisabledButtons.forEach((button) => {
218+
button.removeAttribute("disabled");
219+
});
220+
this.manuallyDisabledButtons.clear();
210221
},
211222
/**
212223
* Put the focus on the last activated widget.
@@ -1124,6 +1135,7 @@ var FormRenderer = BasicRenderer.extend({
11241135
return Promise.all(defs).then(() => this.__renderView()).then(function () {
11251136
self._postProcessLabels();
11261137
self._updateView($form.contents());
1138+
self.manuallyDisabledButtons.clear();
11271139
if (self.state.res_id in self.alertFields) {
11281140
self.displayTranslationAlert();
11291141
}

addons/web/static/tests/legacy/views/form_tests.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,47 @@ QUnit.module('Views', {
12691269
form.destroy();
12701270
});
12711271

1272+
QUnit.test('disabled stat buttons stays disabled', async function (assert) {
1273+
assert.expect(4);
1274+
1275+
var form = await createView({
1276+
View: FormView,
1277+
model: 'partner',
1278+
data: this.data,
1279+
arch:'<form string="Partners">' +
1280+
'<sheet>' +
1281+
'<div name="button_box" class="oe_button_box">' +
1282+
'<button class="oe_stat_button" disabled="disabled">' +
1283+
'<field name="int_field"/>' +
1284+
'</button>' +
1285+
'<button class="oe_stat_button" type="action" name="some_action">' +
1286+
'<field name="bar"/>' +
1287+
'</button>' +
1288+
'</div>' +
1289+
'<group>' +
1290+
'<button type="action" name="action_to_perform">Run an action</button>' +
1291+
'</group>' +
1292+
'</sheet>' +
1293+
'</form>',
1294+
res_id: 2,
1295+
});
1296+
1297+
var count = 0;
1298+
await testUtils.mock.intercept(form, "execute_action", function (event) {
1299+
if (event.data.action_data.name == "action_to_perform") {
1300+
assert.containsN(form, 'button.oe_stat_button[disabled]', 2, "While performing the action, both buttons should be disabled.");
1301+
event.data.on_success();
1302+
}
1303+
});
1304+
1305+
assert.containsN(form, 'button.oe_stat_button', 2);
1306+
assert.containsN(form, 'button.oe_stat_button[disabled]', 1);
1307+
await testUtils.dom.click('button[name=action_to_perform]');
1308+
assert.containsN(form, 'button.oe_stat_button[disabled]', 1, "After performing the action, only one button should be disabled.");
1309+
1310+
form.destroy();
1311+
});
1312+
12721313
QUnit.test('label uses the string attribute', async function (assert) {
12731314
assert.expect(1);
12741315

@@ -2951,7 +2992,7 @@ QUnit.module('Views', {
29512992
arch:'<form string="Partners">' +
29522993
'<sheet>' +
29532994
'<div name="button_box">' +
2954-
'<button class="oe_stat_button">' +
2995+
'<button class="oe_stat_button" name="some_action" type="action">' +
29552996
'<field name="bar"/>' +
29562997
'</button>' +
29572998
'</div>' +
@@ -7384,7 +7425,7 @@ QUnit.module('Views', {
73847425
'</header>' +
73857426
'<sheet>' +
73867427
'<div name="button_box" class="oe_button_box">' +
7387-
'<button class="oe_stat_button">' +
7428+
'<button class="oe_stat_button" name="some_action" type="action">' +
73887429
'<field name="bar"/>' +
73897430
'</button>' +
73907431
'</div>' +
@@ -7448,7 +7489,7 @@ QUnit.module('Views', {
74487489
'</header>' +
74497490
'<sheet>' +
74507491
'<div name="button_box" class="oe_button_box">' +
7451-
'<button class="oe_stat_button">' +
7492+
'<button class="oe_stat_button" name="some_action" type="action">' +
74527493
'<field name="bar"/>' +
74537494
'</button>' +
74547495
'</div>' +
@@ -7561,7 +7602,7 @@ QUnit.module('Views', {
75617602
'partner,false,form': '<form>' +
75627603
'<sheet>' +
75637604
'<div name="button_box" class="oe_button_box">' +
7564-
'<button class="oe_stat_button">' +
7605+
'<button class="oe_stat_button" name="some_action" type="action">' +
75657606
'<field name="bar"/>' +
75667607
'</button>' +
75677608
'</div>' +

0 commit comments

Comments
 (0)