[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) <aab@odoo.com>
This commit is contained in:
Samuel Degueldre
2019-09-19 08:48:41 +00:00
parent fa6dcc7332
commit 16f23e8246
2 changed files with 46 additions and 1 deletions
@@ -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);
},
});
};
@@ -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: '<form string="Partners">' +
'<field name="display_name"/>' +
'<field name="timmy" widget="many2many_tags"/>' +
'</form>',
res_id: 1,
archs: {
'partner_type,false,list': '<tree><field name="display_name"/></tree>',
'partner_type,false,search': '<search><field name="display_name"/></search>',
'partner_type,false,form': '<form><field name="display_name"/></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);