From a0b06afcf3852c2f97eacbddada253c928f37428 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 12 Mar 2018 11:24:41 +0100 Subject: [PATCH] [FIX] web: 'toggle_button' widget in edit mode The 'toggle_button' widget is one of special field widgets that can trigger changes in readonly mode (in this case, toggle the boolean value when clicked). It was working fine in form views in 'readonly' mode, as in this case, the new value is saved directly, and the form view is re-rendered (a new widget is created with the new value). However, in 'edit' mode, the value isn't saved directly (the user has to click on 'Save' to save the changes), the widget is reset with its new value, and the reset wasn't correctly handled in this particular widget. Fixes #23400 --- .../web/static/src/js/fields/basic_fields.js | 5 +- addons/web/static/src/xml/base.xml | 2 +- .../static/tests/fields/basic_fields_tests.js | 76 ++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 26e7d86fabc..8a42f91161c 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1993,8 +1993,9 @@ var FieldToggleBoolean = AbstractField.extend({ * @private */ _render: function () { - var className = this.value ? 'o_toggle_button_success' : 'text-muted'; - this.$('i').addClass('fa fa-circle ' + className); + this.$('i') + .toggleClass('o_toggle_button_success', !!this.value) + .toggleClass('text-muted', !this.value); var title = this.value ? this.attrs.options.active : this.attrs.options.inactive; this.$el.attr('title', title); }, diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index 87a9ca2c989..29030241204 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -1272,7 +1272,7 @@ diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js index fdc8c530af8..4958e2ba42a 100644 --- a/addons/web/static/tests/fields/basic_fields_tests.js +++ b/addons/web/static/tests/fields/basic_fields_tests.js @@ -386,7 +386,6 @@ QUnit.module('basic_fields', { '' + '', - res_id: 2, }); assert.strictEqual(list.$('button i.fa.fa-circle.o_toggle_button_success').length, 4, @@ -410,6 +409,81 @@ QUnit.module('basic_fields', { list.destroy(); }); + QUnit.test('toggle_button in form view (edit mode)', function (assert) { + assert.expect(6); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + mockRPC: function (route, args) { + if (args.method === 'write') { + assert.step('write'); + } + return this._super.apply(this, arguments); + }, + res_id: 2, + viewOptions: { + mode: 'edit', + }, + }); + + assert.strictEqual(form.$('.o_field_widget[name=bar] i.o_toggle_button_success:not(.text-muted)').length, + 1, "should be green"); + + // click on the button to toggle the value + form.$('.o_field_widget[name=bar]').click(); + + assert.strictEqual(form.$('.o_field_widget[name=bar] i.text-muted:not(.o_toggle_button_success)').length, + 1, "should be gray"); + assert.verifySteps([]); + + // save + form.$buttons.find('.o_form_button_save').click(); + + assert.strictEqual(form.$('.o_field_widget[name=bar] i.text-muted:not(.o_toggle_button_success)').length, + 1, "should still be gray"); + assert.verifySteps(['write']); + + form.destroy(); + }); + + QUnit.test('toggle_button in form view (readonly mode)', function (assert) { + assert.expect(4); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + mockRPC: function (route, args) { + if (args.method === 'write') { + assert.step('write'); + } + return this._super.apply(this, arguments); + }, + res_id: 2, + }); + + assert.strictEqual(form.$('.o_field_widget[name=bar] i.o_toggle_button_success:not(.text-muted)').length, + 1, "should be green"); + + // click on the button to toggle the value + form.$('.o_field_widget[name=bar]').click(); + + assert.strictEqual(form.$('.o_field_widget[name=bar] i.text-muted:not(.o_toggle_button_success)').length, + 1, "should be gray"); + assert.verifySteps(['write']); + + form.destroy(); + }); QUnit.module('FieldFloat');