From 16f23e8246db4da4eb34552f360974c2a15f553b Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Thu, 19 Sep 2019 06:19:05 +0000 Subject: [PATCH] [FIX] web: fix m2mtags widget to be able to use 'SAVE & NEW' PURPOSE Previously when the 'SAVE & NEW' button was pressed in the 'create and edit...' menu of the many2many_tags widget was pressed, the modal window would close and the button would act exactly like the 'SAVE & CLOSE' button. This was due to the fact that whenever a new record was created and added to the field, it would reset the many2one field it uses for display, which would destroy the edit modal. This commit fixes that by not notifying the underlying many2one field of any change until the modal is closed by the user. This has the disadvantage that tags display, if visible behind the modal 'shade', will not be updated until the modal is closed, but restores the functionality of the save&new button. It also adds a test for this behaviour task-2070454 closes odoo/odoo#37114 Signed-off-by: Aaron Bohy (aab) --- .../static/src/js/fields/relational_fields.js | 6 ++- .../field_many2many_tests.js | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 1646ef735a2..2a56c6c93e8 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2286,11 +2286,15 @@ var FieldMany2ManyTags = AbstractField.extend({ this.many2one._getSearchCreatePopupOptions = function (view, ids, context, dynamicFilters) { var options = _getSearchCreatePopupOptions.apply(this, arguments); var domain = this.record.getDomain({fieldName: this.name}); + var m2mRecords = []; return _.extend({}, options, { domain: domain.concat(["!", ["id", "in", self.value.res_ids]]), disable_multiple_selection: false, on_selected: function (records) { - self.many2one.reinitialize(records); + m2mRecords.push(...records); + }, + on_closed: function () { + self.many2one.reinitialize(m2mRecords); }, }); }; diff --git a/addons/web/static/tests/fields/relational_fields/field_many2many_tests.js b/addons/web/static/tests/fields/relational_fields/field_many2many_tests.js index db0fb3632d3..487cd8647b8 100644 --- a/addons/web/static/tests/fields/relational_fields/field_many2many_tests.js +++ b/addons/web/static/tests/fields/relational_fields/field_many2many_tests.js @@ -1106,6 +1106,47 @@ QUnit.module('fields', {}, function () { form.destroy(); }); + QUnit.test("many2many tags widget: save&new in edit mode doesn't close edit window", async function (assert) { + assert.expect(5); + for (var i = 1; i <= 10; i++) { + this.data.partner_type.records.push({ id: 100 + i, display_name: "Partner" + i}); + } + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '', + res_id: 1, + archs: { + 'partner_type,false,list': '', + 'partner_type,false,search': '', + 'partner_type,false,form': '
' + }, + }); + await testUtils.form.clickEdit(form); + + await testUtils.fields.many2one.clickOpenDropdown('timmy'); + await testUtils.fields.many2one.clickItem('timmy','Create and Edit'); + assert.containsOnce($(document), '.modal .o_form_view', "should have opened the modal"); + + // Create multiple records with save & new + await testUtils.fields.editInput($('.modal input:first'), 'Ralts'); + await testUtils.dom.click($('.modal .btn-primary:nth-child(2)')); + assert.containsOnce($(document), '.modal .o_form_view', "modal should still be open"); + assert.equal($('.modal input:first')[0].value, '', "input should be empty") + + // Create another record and click save & close + await testUtils.fields.editInput($('.modal input:first'), 'Pikachu'); + await testUtils.dom.click($('.modal .btn-primary:first')); + assert.containsNone($(document),'.modal .o_list_view', "should have closed the modal"); + assert.containsN(form, '.o_field_many2manytags[name="timmy"] .badge', 2, "many2many tag should now contain 2 records"); + + form.destroy(); + }); + QUnit.test('many2many list add *many* records, remove, re-add', async function (assert) { assert.expect(5);