From d060853b7ac2530bcb4e2bae68142f83fda3f326 Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Tue, 7 Mar 2023 10:39:12 +0000 Subject: [PATCH] [REF] web: remove dirtyTranslatableFields from model The logic of dirtyTranslatableFields is only needed in one place in the form view. We will therefore remove this function from the model. We'll take the opportunity to replace dirtyFields with isFieldDirty because all uses of dirtyFields want to check with the name of a field if it is dirty or not. Part of Task: 3179751 closes odoo/odoo#114560 Related: odoo/enterprise#37866 Signed-off-by: Aaron Bohy (aab) --- .../hr/static/src/views/profile_form_view.js | 3 +-- .../src/views/basic_relational_model.js | 27 ++++--------------- .../static/src/views/form/form_controller.js | 8 ++++-- .../web/static/src/views/relational_model.js | 11 +++----- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/addons/hr/static/src/views/profile_form_view.js b/addons/hr/static/src/views/profile_form_view.js index 8919f66d14e..e2b16a4f778 100644 --- a/addons/hr/static/src/views/profile_form_view.js +++ b/addons/hr/static/src/views/profile_form_view.js @@ -12,8 +12,7 @@ export class EmployeeProfileController extends formView.Controller { } onWillSaveRecord(record) { - const dirtyFields = record.dirtyFields.map((f) => f.name); - this.mustReload = dirtyFields.includes("lang"); + this.mustReload = record.isFieldDirty("lang"); } onRecordSaved(record) { diff --git a/addons/web/static/src/views/basic_relational_model.js b/addons/web/static/src/views/basic_relational_model.js index 21325d94030..cea3472433a 100644 --- a/addons/web/static/src/views/basic_relational_model.js +++ b/addons/web/static/src/views/basic_relational_model.js @@ -14,7 +14,6 @@ import { mapViews, mapActiveFieldsToFieldsInfo, } from "@web/views/legacy_utils"; -import { localization } from "@web/core/l10n/localization"; import BasicModel from "web.BasicModel"; import Context from "web.Context"; @@ -150,27 +149,6 @@ export class Record extends DataPoint { return this.model.__bm__.isDirty(this.__bm_handle__); } - get dirtyFields() { - const changes = this.model.__bm__.localData[this.__bm_handle__]._changes; - if (!changes) { - return []; - } - return Object.keys(changes).map((change) => this.activeFields[change]); - } - - get translatableFields() { - if (!localization.multiLang) { - return []; - } - return Object.values(this.fields) - .filter((f) => f.translate) - .map((f) => this.activeFields[f.name]); - } - - get dirtyTranslatableFields() { - return this.translatableFields.filter((f) => this.dirtyFields.includes(f)); - } - get isInEdition() { return this.mode === "edit"; } @@ -290,6 +268,11 @@ export class Record extends DataPoint { return true; } + isFieldDirty(fieldName) { + const changes = this.model.__bm__.localData[this.__bm_handle__]._changes; + return changes && changes[fieldName]; + } + /** * FIXME: memoize this at some point? * @param {string} fieldName diff --git a/addons/web/static/src/views/form/form_controller.js b/addons/web/static/src/views/form/form_controller.js index 7104eaaaa55..854935e8616 100644 --- a/addons/web/static/src/views/form/form_controller.js +++ b/addons/web/static/src/views/form/form_controller.js @@ -4,6 +4,7 @@ import { hasTouch } from "@web/core/browser/feature_detection"; import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; import { makeContext } from "@web/core/context"; import { useDebugCategory } from "@web/core/debug/debug_context"; +import { localization } from "@web/core/l10n/localization"; import { registry } from "@web/core/registry"; import { SIZES } from "@web/core/ui/ui_service"; import { useBus, useService } from "@web/core/utils/hooks"; @@ -442,11 +443,14 @@ export class FormController extends Component { // Before we save, we gather dirty translate fields data. It needs to be done before the // save as nothing will be dirty after. It is why there is a compute part and a show part. - if (record.dirtyTranslatableFields.length) { + const dirtyTranslatableFields = localization.multiLang + ? Object.values(record.fields).filter((f) => f.translate && record.isFieldDirty(f.name)) + : []; + if (dirtyTranslatableFields.length) { const { resId } = record; this.fieldsToTranslate[resId] = new Set([ ...toRaw(this.fieldsToTranslate[resId] || []), - ...record.dirtyTranslatableFields, + ...dirtyTranslatableFields, ]); } if (this.props.saveRecord) { diff --git a/addons/web/static/src/views/relational_model.js b/addons/web/static/src/views/relational_model.js index 6dbf9899130..57084df2882 100644 --- a/addons/web/static/src/views/relational_model.js +++ b/addons/web/static/src/views/relational_model.js @@ -523,13 +523,6 @@ export class Record extends DataPoint { return false; } - get dirtyFields() { - if (!this.isDirty) { - return []; - } - return this._changes.map((change) => this.activeFields[change]); - } - get isInEdition() { return this.mode === "edit"; } @@ -770,6 +763,10 @@ export class Record extends DataPoint { ); } + isFieldDirty(fieldName) { + return this.isDirty && !!this._changes[fieldName]; + } + isInvalid(fieldName) { return this._invalidFields.has(fieldName); }