From 2e6ec660227ce8591a40d9a85c39303f4e9220c1 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 3 Jul 2020 12:05:29 +0000 Subject: [PATCH] [FIX] web: editable list: correctly edit in readonly Some widgets allow the edition in readonly (e.g. boolean_toggle, priority...). In this case, the changed value is saved directly. Before this commit, this didn't work in (main) list views: the widget was updated with the new value, but it wasn't saved in db, so the change was lost at reload. This was due to the list controller not being correctly aware of its mode ('edit' or 'readonly'), on which the basic controller relies to determine whether a change must be saved directly or not. With this commit, a list is always in 'readonly', except when a row is being edited, in which case it is in 'edit'. Issue reported on task 2288963 closes odoo/odoo#54075 X-original-commit: 64e55cb42163643386740fbd23d710f64033b2ae Signed-off-by: Julien Mougenot (jum) Signed-off-by: Aaron Bohy (aab) --- .../src/js/views/list/list_controller.js | 5 +- .../web/static/src/js/views/list/list_view.js | 2 +- addons/web/static/tests/views/list_tests.js | 46 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/js/views/list/list_controller.js b/addons/web/static/src/js/views/list/list_controller.js index b786673ec7c..42da94f7166 100644 --- a/addons/web/static/src/js/views/list/list_controller.js +++ b/addons/web/static/src/js/views/list/list_controller.js @@ -61,10 +61,6 @@ var ListController = BasicController.extend({ session.user_has_group('base.group_allow_export').then(has_group => { this.isExportEnable = has_group; }); - Object.defineProperty(this, 'mode', { - get: () => this.renderer.isEditable() ? 'edit' : 'readonly', - set: () => {}, - }); }, //-------------------------------------------------------------------------- @@ -519,6 +515,7 @@ var ListController = BasicController.extend({ */ _setMode: function (mode, recordID) { if ((recordID || this.handle) !== this.handle) { + this.mode = mode; this.updateButtons(mode); return this.renderer.setRowMode(recordID, mode); } else { diff --git a/addons/web/static/src/js/views/list/list_view.js b/addons/web/static/src/js/views/list/list_view.js index 5845375fd70..abe05173bc8 100644 --- a/addons/web/static/src/js/views/list/list_view.js +++ b/addons/web/static/src/js/views/list/list_view.js @@ -60,7 +60,7 @@ var ListView = BasicView.extend({ this.controllerParams.editable = editable; this.controllerParams.hasActionMenus = params.hasActionMenus; this.controllerParams.toolbarActions = viewInfo.toolbar; - this.controllerParams.mode = editable ? 'edit' : 'readonly'; + this.controllerParams.mode = 'readonly'; this.controllerParams.selectedRecords = selectedRecords; this.rendererParams.arch = this.arch; diff --git a/addons/web/static/tests/views/list_tests.js b/addons/web/static/tests/views/list_tests.js index 703218a071a..1e2357b1eb3 100644 --- a/addons/web/static/tests/views/list_tests.js +++ b/addons/web/static/tests/views/list_tests.js @@ -1229,6 +1229,52 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('edit field in editable field without editing the row', async function (assert) { + // some widgets are editable in readonly (e.g. priority, boolean_toggle...) and they + // thus don't require the row to be switched in edition to be edited + assert.expect(13); + + const list = await createView({ + View: ListView, + model: 'foo', + data: this.data, + arch: ` + + + + `, + mockRPC(route, args) { + if (args.method === 'write') { + assert.step('write: ' + args.args[1].bar); + } + return this._super(...arguments); + }, + }); + + // toggle the boolean value of the first row without editing the row + assert.ok(list.$('.o_data_row:first .o_boolean_toggle input')[0].checked); + assert.containsNone(list, '.o_selected_row'); + await testUtils.dom.click(list.$('.o_data_row:first .o_boolean_toggle')); + assert.notOk(list.$('.o_data_row:first .o_boolean_toggle input')[0].checked); + assert.containsNone(list, '.o_selected_row'); + assert.verifySteps(['write: false']); + + // toggle the boolean value after switching the row in edition + assert.containsNone(list, '.o_selected_row'); + await testUtils.dom.click(list.$('.o_data_row .o_data_cell:first')); + assert.containsOnce(list, '.o_selected_row'); + await testUtils.dom.click(list.$('.o_selected_row .o_boolean_toggle')); + assert.containsOnce(list, '.o_selected_row'); + assert.verifySteps([]); + + // save + await testUtils.dom.click(list.$('.o_list_button_save')); + assert.containsNone(list, '.o_selected_row'); + assert.verifySteps(['write: true']); + + list.destroy(); + }); + QUnit.test('basic operations for editable list renderer', async function (assert) { assert.expect(2);