From aed1ba484d0c48a59e166ef01e69967bd618a562 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Tue, 21 Feb 2023 14:52:21 +0000 Subject: [PATCH] [REF] web, * : simplify concrete fields API - remove type prop This commit, is part of a series of commits that aim to simplifie the concrete fields API. In this commit we will remove type prop from concrete fields. Now each field could directly use this.props.record.fields[this.props.name].type if they need to know the type of field. task-id 3179751 closes odoo/odoo#112792 Related: odoo/enterprise#37118 Signed-off-by: Aaron Bohy (aab) --- .../src/views/fields/assign_user_command_hook.js | 15 ++++++++------- .../static/src/views/fields/badge/badge_field.js | 2 +- .../badge_selection/badge_selection_field.js | 8 +++++--- .../fields/copy_clipboard/copy_clipboard_field.js | 7 +++++-- addons/web/static/src/views/fields/field.js | 2 +- .../static/src/views/fields/radio/radio_field.js | 4 ++-- .../src/views/fields/reference/reference_field.js | 7 +++++-- .../fields/remaining_days/remaining_days_field.js | 2 +- .../src/views/fields/selection/selection_field.js | 8 +++++--- .../src/views/fields/standard_field_props.js | 1 - .../src/views/fields/stat_info/stat_info_field.js | 2 +- .../src/views/fields/statusbar/statusbar_field.js | 4 ++-- 12 files changed, 36 insertions(+), 26 deletions(-) diff --git a/addons/mail/static/src/views/fields/assign_user_command_hook.js b/addons/mail/static/src/views/fields/assign_user_command_hook.js index 07592037215..7a4d29a0860 100644 --- a/addons/mail/static/src/views/fields/assign_user_command_hook.js +++ b/addons/mail/static/src/views/fields/assign_user_command_hook.js @@ -15,6 +15,7 @@ export function useAssignUserCommand() { const env = useEnv(); const orm = useService("orm"); const user = useService("user"); + const type = component.props.record.fields[component.props.name].type; if ( component.props.relation !== "res.users" || component.props.record.activeFields[component.props.name].viewType !== "form" @@ -23,18 +24,18 @@ export function useAssignUserCommand() { } const getCurrentIds = () => { - if (component.props.type === "many2one" && component.props.value) { + if (type === "many2one" && component.props.value) { return [component.props.value[0]]; - } else if (component.props.type === "many2many") { + } else if (type === "many2many") { return component.props.value.currentIds; } return []; }; const add = async (record) => { - if (component.props.type === "many2one") { + if (type === "many2one") { component.props.record.update({ [component.props.name]: record }); - } else if (component.props.type === "many2many") { + } else if (type === "many2many") { component.props.record.update({ [component.props.name]: { operation: "REPLACE_WITH", @@ -45,9 +46,9 @@ export function useAssignUserCommand() { }; const remove = async (record) => { - if (component.props.type === "many2one") { + if (type === "many2one") { component.props.record.update({ [component.props.name]: [] }); - } else if (component.props.type === "many2many") { + } else if (type === "many2many") { component.props.record.update({ [component.props.name]: { operation: "REPLACE_WITH", @@ -61,7 +62,7 @@ export function useAssignUserCommand() { const value = options.searchValue.trim(); let domain = component.props.record.getFieldDomain(component.props.name); const context = component.props.record.getFieldContext(component.props.name); - if (component.props.type === "many2many") { + if (type === "many2many") { const selectedUserIds = getCurrentIds(); if (selectedUserIds.length) { domain = Domain.and([domain, [["id", "not in", selectedUserIds]]]); diff --git a/addons/web/static/src/views/fields/badge/badge_field.js b/addons/web/static/src/views/fields/badge/badge_field.js index 4c569bdd3ec..473a52a0f35 100644 --- a/addons/web/static/src/views/fields/badge/badge_field.js +++ b/addons/web/static/src/views/fields/badge/badge_field.js @@ -14,7 +14,7 @@ export class BadgeField extends Component { }; get formattedValue() { - const formatter = formatters.get(this.props.type); + const formatter = formatters.get(this.props.record.fields[this.props.name].type); return formatter(this.props.value, { selection: this.props.record.fields[this.props.name].selection, }); diff --git a/addons/web/static/src/views/fields/badge_selection/badge_selection_field.js b/addons/web/static/src/views/fields/badge_selection/badge_selection_field.js index aa73459914d..a14bb587ab9 100644 --- a/addons/web/static/src/views/fields/badge_selection/badge_selection_field.js +++ b/addons/web/static/src/views/fields/badge_selection/badge_selection_field.js @@ -27,7 +27,7 @@ export class BadgeSelectionField extends Component { } get string() { - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "many2one": return this.props.value ? this.props.value[1] : ""; case "selection": @@ -40,7 +40,9 @@ export class BadgeSelectionField extends Component { } get value() { const rawValue = this.props.value; - return this.props.type === "many2one" && rawValue ? rawValue[0] : rawValue; + return this.props.record.fields[this.props.name].type === "many2one" && rawValue + ? rawValue[0] + : rawValue; } stringify(value) { @@ -51,7 +53,7 @@ export class BadgeSelectionField extends Component { * @param {string | number | false} value */ onChange(value) { - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "many2one": if (value === false) { this.props.record.update({ [this.props.name]: false }); diff --git a/addons/web/static/src/views/fields/copy_clipboard/copy_clipboard_field.js b/addons/web/static/src/views/fields/copy_clipboard/copy_clipboard_field.js index 1b885e43a9d..a8f1aa5583c 100644 --- a/addons/web/static/src/views/fields/copy_clipboard/copy_clipboard_field.js +++ b/addons/web/static/src/views/fields/copy_clipboard/copy_clipboard_field.js @@ -28,11 +28,14 @@ class CopyClipboardField extends Component { } get copyButtonClassName() { - return `o_btn_${this.props.type}_copy btn-sm`; + return `o_btn_${this.type}_copy btn-sm`; } get fieldProps() { return omit(this.props, "string", "disabledExpr"); } + get type() { + return this.props.record.fields[this.props.name].type; + } get disabled() { const context = this.props.record.evalContext; const evaluated = this.props.disabledExpr ? evaluateExpr(this.props.disabledExpr) : false; @@ -48,7 +51,7 @@ export class CopyClipboardButtonField extends CopyClipboardField { static components = { CopyButton }; get copyButtonClassName() { - return `o_btn_${this.props.type}_copy rounded-2`; + return `o_btn_${this.type}_copy rounded-2`; } } diff --git a/addons/web/static/src/views/fields/field.js b/addons/web/static/src/views/fields/field.js index a558882620e..efe8cd69250 100644 --- a/addons/web/static/src/views/fields/field.js +++ b/addons/web/static/src/views/fields/field.js @@ -157,6 +157,7 @@ export class Field extends Component { delete props.showTooltip; delete props.fieldInfo; delete props.attrs; + delete props.type; return { ...fieldInfo.props, @@ -165,7 +166,6 @@ export class Field extends Component { readonly: !record.isInEdition || readonlyFromModifiers || false, ...propsFromAttrs, ...props, - type: field.type, }; } diff --git a/addons/web/static/src/views/fields/radio/radio_field.js b/addons/web/static/src/views/fields/radio/radio_field.js index e4055d3567d..ac42fb9a6db 100644 --- a/addons/web/static/src/views/fields/radio/radio_field.js +++ b/addons/web/static/src/views/fields/radio/radio_field.js @@ -42,7 +42,7 @@ export class RadioField extends Component { return this.props.record.activeFields[this.props.name].string; } get value() { - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "selection": return this.props.value; case "many2one": @@ -56,7 +56,7 @@ export class RadioField extends Component { * @param {any} value */ onChange(value) { - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "selection": this.props.record.update({ [this.props.name]: value[0] }); break; diff --git a/addons/web/static/src/views/fields/reference/reference_field.js b/addons/web/static/src/views/fields/reference/reference_field.js index edffa378310..f88c073d7a7 100644 --- a/addons/web/static/src/views/fields/reference/reference_field.js +++ b/addons/web/static/src/views/fields/reference/reference_field.js @@ -43,7 +43,7 @@ export class ReferenceField extends Component { return p.record.preloadedData[p.name]; } getValue(p) { - if (p.type === "char") { + if (p.record.fields[p.name].type === "char") { const pdata = this.getPreloadedData(p); if (!pdata) { return null; @@ -69,7 +69,10 @@ export class ReferenceField extends Component { return p; } get selection() { - if (this.props.type !== "char" && !this.props.hideModelSelector) { + if ( + this.props.record.fields[this.props.name].type !== "char" && + !this.props.hideModelSelector + ) { return this.props.record.fields[this.props.name].selection; } return []; diff --git a/addons/web/static/src/views/fields/remaining_days/remaining_days_field.js b/addons/web/static/src/views/fields/remaining_days/remaining_days_field.js index 6449ae401da..3cbd1712771 100644 --- a/addons/web/static/src/views/fields/remaining_days/remaining_days_field.js +++ b/addons/web/static/src/views/fields/remaining_days/remaining_days_field.js @@ -16,7 +16,7 @@ export class RemainingDaysField extends Component { }; get hasTime() { - return this.props.type === "datetime"; + return this.props.record.fields[this.props.name].type === "datetime"; } get pickerComponent() { diff --git a/addons/web/static/src/views/fields/selection/selection_field.js b/addons/web/static/src/views/fields/selection/selection_field.js index 02785ca7f3f..32ba1266b5c 100644 --- a/addons/web/static/src/views/fields/selection/selection_field.js +++ b/addons/web/static/src/views/fields/selection/selection_field.js @@ -26,7 +26,7 @@ export class SelectionField extends Component { } } get string() { - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "many2one": return this.props.value ? this.props.value[1] : ""; case "selection": @@ -39,7 +39,9 @@ export class SelectionField extends Component { } get value() { const rawValue = this.props.value; - return this.props.type === "many2one" && rawValue ? rawValue[0] : rawValue; + return this.props.record.fields[this.props.name].type === "many2one" && rawValue + ? rawValue[0] + : rawValue; } get isRequired() { return this.props.record.isRequired(this.props.name); @@ -54,7 +56,7 @@ export class SelectionField extends Component { */ onChange(ev) { const value = JSON.parse(ev.target.value); - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "many2one": if (value === false) { this.props.record.update({ [this.props.name]: false }); diff --git a/addons/web/static/src/views/fields/standard_field_props.js b/addons/web/static/src/views/fields/standard_field_props.js index d227b56a8e8..589ccc805ef 100644 --- a/addons/web/static/src/views/fields/standard_field_props.js +++ b/addons/web/static/src/views/fields/standard_field_props.js @@ -5,7 +5,6 @@ export const standardFieldProps = { name: { type: String, optional: true }, readonly: { type: Boolean, optional: true }, record: { type: Object }, - type: { type: String, optional: true }, value: true, decorations: { type: Object, optional: true }, setDirty: { type: Function, optional: true }, diff --git a/addons/web/static/src/views/fields/stat_info/stat_info_field.js b/addons/web/static/src/views/fields/stat_info/stat_info_field.js index 8ce27a91e22..c5237ede11a 100644 --- a/addons/web/static/src/views/fields/stat_info/stat_info_field.js +++ b/addons/web/static/src/views/fields/stat_info/stat_info_field.js @@ -18,7 +18,7 @@ export class StatInfoField extends Component { }; get formattedValue() { - const formatter = formatters.get(this.props.type); + const formatter = formatters.get(this.props.record.fields[this.props.name].type); return formatter(this.props.value || 0, { digits: this.props.digits }); } get label() { diff --git a/addons/web/static/src/views/fields/statusbar/statusbar_field.js b/addons/web/static/src/views/fields/statusbar/statusbar_field.js index ae477bb9942..0422274dd4c 100644 --- a/addons/web/static/src/views/fields/statusbar/statusbar_field.js +++ b/addons/web/static/src/views/fields/statusbar/statusbar_field.js @@ -168,7 +168,7 @@ export class StatusBarField extends Component { computeItems(grouped = true) { let items = null; - if (this.props.type === "many2one") { + if (this.props.record.fields[this.props.name].type === "many2one") { items = this.getVisibleMany2Ones(); } else { items = this.getVisibleSelection(); @@ -196,7 +196,7 @@ export class StatusBarField extends Component { this.props.record.model.root instanceof this.props.record.constructor && this.props.record.model.root; const isInEdition = rootRecord ? rootRecord.isInEdition : this.props.record.isInEdition; - switch (this.props.type) { + switch (this.props.record.fields[this.props.name].type) { case "many2one": this.props.record.update({ [this.props.name]: [item.id, item.name] }); // We save only if we're on view mode readonly and no readonly field modifier