From 145540921f2629ae188c84dee17c8d95dccdde16 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 13 Feb 2023 11:58:32 +0000 Subject: [PATCH] [REF] web,mail: handle "color_field" option in M2MTagsField MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, there was a hack done in Field.js to handle a special case of the Many2ManyTagsField which sometimes require an additional "color" field to be fetched. This field is given in option on the many2many field, in the arch. It wasn't possible for the Many2ManyTags field to add that field on its "FieldToFetch", because this object was static (it didn't depend on the arch). This commit allows it to do so as FieldsToFetch can now be a function that receives the fieldInfo (extracted from the arch) in arguments. So with this commit, fieldsToFetch can be either an array of fields (instead of an object as before), or a function returning an array of fields. Part of task 3179751 closes odoo/odoo#112518 Related: odoo/enterprise#37020 Signed-off-by: Georis François (fge) --- .../many2many_tags_email.js | 6 +++--- addons/web/static/src/views/fields/field.js | 17 ++++++++--------- .../many2many_binary/many2many_binary_field.js | 8 ++++---- .../many2many_tags/many2many_tags_field.js | 8 ++++++-- addons/web/static/src/views/legacy_utils.js | 18 +++++++----------- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/addons/mail/static/src/views/fields/many2many_tags_email/many2many_tags_email.js b/addons/mail/static/src/views/fields/many2many_tags_email/many2many_tags_email.js index f2b8fd05734..236ef9b3282 100644 --- a/addons/mail/static/src/views/fields/many2many_tags_email/many2many_tags_email.js +++ b/addons/mail/static/src/views/fields/many2many_tags_email/many2many_tags_email.js @@ -87,9 +87,9 @@ FieldMany2ManyTagsEmail.components = { TagsList: FieldMany2ManyTagsEmailTagsList, }; -FieldMany2ManyTagsEmail.fieldsToFetch = Object.assign({}, Many2ManyTagsField.fieldsToFetch, { - email: { name: "email", type: "char" }, -}); +FieldMany2ManyTagsEmail.fieldsToFetch = (fieldInfo) => { + return [...Many2ManyTagsField.fieldsToFetch(fieldInfo), { name: "email", type: "char" }]; +}; FieldMany2ManyTagsEmail.additionalClasses = ["o_field_many2many_tags"]; diff --git a/addons/web/static/src/views/fields/field.js b/addons/web/static/src/views/fields/field.js index 77f24f93bdc..3d51d513319 100644 --- a/addons/web/static/src/views/fields/field.js +++ b/addons/web/static/src/views/fields/field.js @@ -279,17 +279,16 @@ Field.parseFieldNode = function (node, models, modelName, viewType, jsClass) { viewMode = viewMode.replace("tree", "list"); } fieldInfo.viewMode = viewMode; - - const fieldsToFetch = { ...fieldInfo.FieldComponent.fieldsToFetch }; // should become an array? - // special case for color field - // GES: this is not nice, we will look for something better. - const colorField = fieldInfo.options.color_field; - if (colorField) { - fieldsToFetch[colorField] = { name: colorField, type: "integer", active: true }; - } - fieldInfo.fieldsToFetch = fieldsToFetch; fieldInfo.relation = field.relation; // not really necessary fieldInfo.views = views; + + let fieldsToFetch = fieldInfo.FieldComponent.fieldsToFetch; + if (fieldsToFetch) { + if (fieldsToFetch instanceof Function) { + fieldsToFetch = fieldsToFetch(fieldInfo); + } + fieldInfo.fieldsToFetch = Object.fromEntries(fieldsToFetch.map((f) => [f.name, f])); + } } return fieldInfo; diff --git a/addons/web/static/src/views/fields/many2many_binary/many2many_binary_field.js b/addons/web/static/src/views/fields/many2many_binary/many2many_binary_field.js index 0397ee08d18..e7f26fcf5b0 100644 --- a/addons/web/static/src/views/fields/many2many_binary/many2many_binary_field.js +++ b/addons/web/static/src/views/fields/many2many_binary/many2many_binary_field.js @@ -56,10 +56,10 @@ Many2ManyBinaryField.props = { uploadText: { type: String, optional: true }, }; Many2ManyBinaryField.supportedTypes = ["many2many"]; -Many2ManyBinaryField.fieldsToFetch = { - name: { type: "char" }, - mimetype: { type: "char" }, -}; +Many2ManyBinaryField.fieldsToFetch = [ + { name: "name", type: "char" }, + { name: "mimetype", type: "char" }, +]; Many2ManyBinaryField.isEmpty = () => false; Many2ManyBinaryField.extractProps = ({ attrs, field }) => { diff --git a/addons/web/static/src/views/fields/many2many_tags/many2many_tags_field.js b/addons/web/static/src/views/fields/many2many_tags/many2many_tags_field.js index c57acb133e1..5b02b501bd6 100644 --- a/addons/web/static/src/views/fields/many2many_tags/many2many_tags_field.js +++ b/addons/web/static/src/views/fields/many2many_tags/many2many_tags_field.js @@ -245,8 +245,12 @@ Many2ManyTagsField.defaultProps = { Many2ManyTagsField.displayName = _lt("Tags"); Many2ManyTagsField.supportedTypes = ["many2many"]; -Many2ManyTagsField.fieldsToFetch = { - display_name: { name: "display_name", type: "char" }, +Many2ManyTagsField.fieldsToFetch = (fieldInfo) => { + const fieldsToFetch = [{ name: "display_name", type: "char" }]; + if (fieldInfo.options.color_field) { + fieldsToFetch.push({ name: fieldInfo.options.color_field, type: "integer" }); + } + return fieldsToFetch; }; Many2ManyTagsField.isSet = (value) => value.count > 0; diff --git a/addons/web/static/src/views/legacy_utils.js b/addons/web/static/src/views/legacy_utils.js index a98189c308e..3eb8bfa48e1 100644 --- a/addons/web/static/src/views/legacy_utils.js +++ b/addons/web/static/src/views/legacy_utils.js @@ -115,10 +115,15 @@ export function mapActiveFieldsToFieldsInfo(activeFields, fields, viewType, env) } if (!fieldInfo.__no_fetch && FieldComponent && FieldComponent.fieldsToFetch) { - fieldInfo.relatedFields = { ...FieldComponent.fieldsToFetch }; + let fieldsToFetch = FieldComponent.fieldsToFetch; + if (fieldsToFetch instanceof Function) { + fieldsToFetch = fieldsToFetch(fieldInfo.__WOWL_FIELD_DESCR__); + } + fieldsToFetch = Object.fromEntries(fieldsToFetch.map((f) => [f.name, f])); + fieldInfo.relatedFields = { ...fieldsToFetch }; fieldInfo.viewType = "default"; const defaultView = {}; - for (const fieldName of Object.keys(FieldComponent.fieldsToFetch)) { + for (const fieldName of Object.keys(fieldsToFetch)) { defaultView[fieldName] = {}; if (fieldDescr.fieldsToFetch[fieldName]) { defaultView[fieldName].__WOWL_FIELD_DESCR__ = @@ -126,15 +131,6 @@ export function mapActiveFieldsToFieldsInfo(activeFields, fields, viewType, env) } } fieldInfo.fieldsInfo = { default: defaultView }; - const colorField = fieldInfo.options && fieldInfo.options.color_field; - if (colorField) { - fieldInfo.relatedFields[colorField] = { type: "integer" }; - fieldInfo.fieldsInfo.default[colorField] = {}; - if (fieldDescr.fieldsToFetch[colorField]) { - fieldInfo.fieldsInfo.default[colorField].__WOWL_FIELD_DESCR__ = - fieldDescr.fieldsToFetch[colorField]; - } - } } if (fieldDescr.views && fieldDescr.views[fieldDescr.viewMode]) { fieldInfo.limit = fieldDescr.views[fieldDescr.viewMode].limit || 40;