diff --git a/addons/web/static/src/model/relational_model/record.js b/addons/web/static/src/model/relational_model/record.js index 4bed78e5ce1..c7eff2f4520 100644 --- a/addons/web/static/src/model/relational_model/record.js +++ b/addons/web/static/src/model/relational_model/record.js @@ -439,6 +439,7 @@ export class Record extends DataPoint { this._invalidFields.clear(); this._closeInvalidFieldsNotification(); this._closeInvalidFieldsNotification = () => {}; + this._restoreActiveFields(); } _formatServerValue(fieldType, value) { @@ -769,6 +770,20 @@ export class Record extends DataPoint { } } + _restoreActiveFields() { + if (!this._activeFieldsToRestore) { + return; + } + this.model._updateConfig( + this.config, + { + activeFields: { ...this._activeFieldsToRestore }, + }, + { noReload: true } + ); + this._activeFieldsToRestore = undefined; + } + async _save({ noReload, onError } = {}) { // before saving, abandon new invalid, untouched records in x2manys for (const fieldName in this.activeFields) { diff --git a/addons/web/static/src/model/relational_model/static_list.js b/addons/web/static/src/model/relational_model/static_list.js index eb54a3bd14e..30788de8ae5 100644 --- a/addons/web/static/src/model/relational_model/static_list.js +++ b/addons/web/static/src/model/relational_model/static_list.js @@ -213,10 +213,18 @@ export class StaticList extends DataPoint { } if (record) { + record._activeFieldsToRestore = { ...this.config.activeFields }; + const config = { + ...record.config, + ...params, + activeFields, + }; + // case 1: the record already exists if (this._extendedRecords.has(record.id)) { // case 1.1: the record has already been extended // -> simply switch it to edit and store a savepoint + this.model._updateConfig(record.config, config, { noReload: true }); record._switchMode("edit"); record._addSavePoint(); return record; @@ -231,11 +239,6 @@ export class StaticList extends DataPoint { // These operations must be done in that specific order to ensure that the model is // mutated only once (in a tick), and that datapoints have the correct config to // handle field values they receive. - const config = { - ...record.config, - ...params, - activeFields, - }; let data = {}; if (!record.isNew) { const evalContext = Object.assign({}, record.evalContext, config.context); @@ -275,8 +278,8 @@ export class StaticList extends DataPoint { withoutParent: params.withoutParent, manuallyAdded: true, }); + record._activeFieldsToRestore = { ...this.config.activeFields }; } - // mark the record as being extended, to go through case 1.1 next time this._extendedRecords.add(record.id); @@ -379,6 +382,7 @@ export class StaticList extends DataPoint { if (this.orderBy.length) { await this._sort(); } + record._restoreActiveFields(); record._savePoint = undefined; }); } diff --git a/addons/web/static/src/model/relational_model/utils.js b/addons/web/static/src/model/relational_model/utils.js index 4ac786c1f45..0b5926f2e62 100644 --- a/addons/web/static/src/model/relational_model/utils.js +++ b/addons/web/static/src/model/relational_model/utils.js @@ -52,6 +52,24 @@ export function addFieldDependencies(activeFields, fields, fieldDependencies = [ } } +function completeActiveField(activeField, extra) { + if (extra.related) { + for (const fieldName in extra.related.activeFields) { + if (fieldName in activeField.related.activeFields) { + completeActiveField( + activeField.related.activeFields[fieldName], + extra.related.activeFields[fieldName] + ); + } else { + activeField.related.activeFields[fieldName] = { + ...extra.related.activeFields[fieldName], + }; + } + } + Object.assign(activeField.related.fields, extra.related.fields); + } +} + export function createPropertyActiveField(property) { const { type } = property; @@ -170,12 +188,13 @@ export function extractFieldsFromArchInfo({ fieldNodes, widgetNodes }, fields) { invisible: true, }; if (fieldName in activeField.related.activeFields) { - patchActiveFields( - formActiveField, - activeField.related.activeFields[fieldName] + completeActiveField( + activeField.related.activeFields[fieldName], + formActiveField ); + } else { + activeField.related.activeFields[fieldName] = formActiveField; } - activeField.related.activeFields[fieldName] = formActiveField; } Object.assign(activeField.related.fields, formArchInfo.fields); } diff --git a/addons/web/static/tests/views/fields/one2many_field_tests.js b/addons/web/static/tests/views/fields/one2many_field_tests.js index 7d7b4fde69e..082f161530c 100644 --- a/addons/web/static/tests/views/fields/one2many_field_tests.js +++ b/addons/web/static/tests/views/fields/one2many_field_tests.js @@ -287,6 +287,73 @@ QUnit.module("Fields", (hooks) => { } ); + QUnit.test("one2many in a list x2many editable use the right context", async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + + + + + + + + + `, + mockRPC(route, args) { + if (args.method === "name_create") { + assert.step(`name_create ${args.kwargs.context.my_context}`); + } + }, + resId: 1, + }); + + await addRow(target, ".o_field_x2many_list"); + await editInput(target, "[name='trululu'] input", "new partner"); + await selectDropdownItem(target, "trululu", 'Create "new partner"'); + + assert.verifySteps(["name_create list"]); + }); + + QUnit.test( + "one2many in a list x2many non-editable use the right context", + async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + + + + + + + + + `, + mockRPC(route, args) { + if (args.method === "name_create") { + assert.step(`name_create ${args.kwargs.context.my_context}`); + } + }, + resId: 1, + }); + + await addRow(target, ".o_field_x2many_list"); + await editInput(target, "[name='trululu'] input", "new partner"); + await selectDropdownItem(target, "trululu", 'Create "new partner"'); + + assert.verifySteps(["name_create form"]); + } + ); + QUnit.test("O2M field without relation_field", async function (assert) { delete serverData.models.partner.fields.p.relation_field; @@ -2064,8 +2131,6 @@ QUnit.module("Fields", (hooks) => { fields: { turtle_foo: {}, }, - limit: 40, - order: "", }, }, limit: 40, @@ -6738,8 +6803,6 @@ QUnit.module("Fields", (hooks) => { fields: { turtle_foo: {}, }, - limit: 40, - order: "", }, }, limit: 40,