From a478a9e906058e5c4ab2cc0c4f1f6b4ed71fe1d4 Mon Sep 17 00:00:00 2001 From: "Hubert Van de Walle (huvw)" Date: Wed, 16 Mar 2022 15:45:34 +0000 Subject: [PATCH] [FIX] web: boolean_toggle widget ignores readonly attribute MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The boolean_toggle should only be disabled when there is a readonly modifier, not when the view is in readonly mode. Cause of the issue The input was disabled but the `_onClick` was registered on the widget root node. Solution - Listen for clicks on the input element - Override the render method to set the disabled prop according to the readonly modifier. opw-2739468 closes odoo/odoo#91601 X-original-commit: 29a2402153dc95414dad8c833487e7dd18c8a222 Signed-off-by: Michaƫl Mattiello Signed-off-by: Hubert Van De Walle --- .../src/legacy/js/fields/basic_fields.js | 11 +++- .../tests/legacy/fields/basic_fields_tests.js | 57 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/legacy/js/fields/basic_fields.js b/addons/web/static/src/legacy/js/fields/basic_fields.js index 9842479c6ca..2efd6738fef 100644 --- a/addons/web/static/src/legacy/js/fields/basic_fields.js +++ b/addons/web/static/src/legacy/js/fields/basic_fields.js @@ -3007,6 +3007,9 @@ var BooleanToggle = FieldBoolean.extend({ * Adds the icon fa-check-circle if value is true else adds icon * fa-times-circle * + * The boolean_toggle should only be disabled when there is a readonly modifier + * not when the view is in readonly mode + * * @override */ async _render() { @@ -3018,6 +3021,8 @@ var BooleanToggle = FieldBoolean.extend({ const i = document.createElement("i"); i.setAttribute('class', `fa ${classToApply}`); this.el.querySelector('label').appendChild(i); + const isReadonly = this.record.evalModifiers(this.attrs.modifiers).readonly || false; + this.$input.prop('disabled', isReadonly); }, //-------------------------------------------------------------------------- @@ -3032,8 +3037,10 @@ var BooleanToggle = FieldBoolean.extend({ */ _onClick: async function (event) { event.stopPropagation(); - await this._setValue(!this.value); - this._render(); + if (!this.$input.prop('disabled')) { + await this._setValue(!this.value); + this._render(); + } }, }); diff --git a/addons/web/static/tests/legacy/fields/basic_fields_tests.js b/addons/web/static/tests/legacy/fields/basic_fields_tests.js index e31b5e428f8..3ff7545ce4c 100644 --- a/addons/web/static/tests/legacy/fields/basic_fields_tests.js +++ b/addons/web/static/tests/legacy/fields/basic_fields_tests.js @@ -421,6 +421,63 @@ QUnit.module('basic_fields', { form.destroy(); }); + QUnit.test('boolean toggle widget is not disabled in readonly mode', async function (assert) { + assert.expect(3); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
', + res_id: 5, + }); + + assert.containsOnce(form, ".custom-checkbox.o_boolean_toggle", "Boolean toggle widget applied to boolean field"); + assert.notOk(form.$('.o_boolean_toggle input')[0].checked); + await testUtils.dom.click(form.$('.o_boolean_toggle')); + assert.ok(form.$('.o_boolean_toggle input')[0].checked); + form.destroy(); + }); + + QUnit.test('boolean toggle widget is disabled with a readonly attribute', async function (assert) { + assert.expect(3); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
', + res_id: 5, + }); + + assert.containsOnce(form, ".custom-checkbox.o_boolean_toggle", "Boolean toggle widget applied to boolean field"); + await testUtils.dom.click(form.$buttons.find('.o_form_button_edit')); + assert.notOk(form.$('.o_boolean_toggle input')[0].checked); + await testUtils.dom.click(form.$('.o_boolean_toggle')); + assert.notOk(form.$('.o_boolean_toggle input')[0].checked); + form.destroy(); + }); + + QUnit.test('boolean toggle widget is enabled in edit mode', async function (assert) { + assert.expect(3); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
', + res_id: 5, + }); + + assert.containsOnce(form, ".custom-checkbox.o_boolean_toggle", "Boolean toggle widget applied to boolean field"); + await testUtils.dom.click(form.$buttons.find('.o_form_button_edit')); + + assert.notOk(form.$('.o_boolean_toggle input')[0].checked); + await testUtils.dom.click(form.$('.o_boolean_toggle')); + assert.ok(form.$('.o_boolean_toggle input')[0].checked); + form.destroy(); + }); + QUnit.module('FieldToggleButton'); QUnit.test('use toggle_button in list view', async function (assert) {