From 3161f1f6dd1283ca9594da926f8395bf61c76a44 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Tue, 7 Mar 2023 10:15:25 +0000 Subject: [PATCH] [REF] web: standardize parseFieldNode and parseWidgetNode This commit standardizes both parsers, this commit also leaves all other (non-dynamic) attributes on the "attrs" object. This means that all already processed attributes of the nodes are removed from the attrs object. Part-of task-id 3179751 closes odoo/odoo#114581 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/views/fields/field.js | 46 ++++++++++++------- .../static/src/views/fields/field_tooltip.js | 2 +- addons/web/static/src/views/widgets/widget.js | 17 +++++-- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/addons/web/static/src/views/fields/field.js b/addons/web/static/src/views/fields/field.js index aa40ab76689..13850310e78 100644 --- a/addons/web/static/src/views/fields/field.js +++ b/addons/web/static/src/views/fields/field.js @@ -165,30 +165,44 @@ Field.parseFieldNode = function (node, models, modelName, viewType, jsClass) { const name = node.getAttribute("name"); const widget = node.getAttribute("widget"); const fields = models[modelName]; - const modifiers = JSON.parse(node.getAttribute("modifiers") || "{}"); const field = getFieldFromRegistry(fields[name].type, widget, viewType, jsClass); const fieldInfo = { name, viewType, widget, - modifiers, + modifiers: {}, field, - context: node.getAttribute("context") || "{}", - string: node.getAttribute("string") || fields[name].string, - help: node.getAttribute("help"), - onChange: archParseBoolean(node.getAttribute("on_change")), - forceSave: archParseBoolean(node.getAttribute("force_save")), - options: evaluateExpr(node.getAttribute("options") || "{}"), - alwaysInvisible: modifiers.invisible === true || modifiers.column_invisible === true, - decorations: {}, // populated below - attrs: {}, // populated below + context: "{}", + string: fields[name].string, + help: undefined, + onChange: false, + forceSave: false, + options: {}, + alwaysInvisible: false, + decorations: {}, + attrs: {}, + domain: undefined, }; - if (node.getAttribute("domain")) { - // TODO WOWl: remove with new model? - fieldInfo.domain = node.getAttribute("domain"); - } + for (const { name, value } of node.attributes) { - if (name.startsWith("decoration-")) { + if (["name", "widget"].includes(name)) { + // avoid adding name and widget to attrs + continue; + } + if (["context", "string", "help", "domain"].includes(name)) { + fieldInfo[name] = value; + } else if (name === "modifiers") { + fieldInfo.modifiers = JSON.parse(value); + fieldInfo.alwaysInvisible = + fieldInfo.modifiers.invisible === true || + fieldInfo.modifiers.column_invisible === true; + } else if (name === "on_change") { + fieldInfo.onChange = archParseBoolean(value); + } else if (name === "options") { + fieldInfo.options = evaluateExpr(value); + } else if (name === "force_save") { + fieldInfo.forceSave = archParseBoolean(value); + } else if (name.startsWith("decoration-")) { // prepare field decorations fieldInfo.decorations[name.replace("decoration-", "")] = value; } else if (!name.startsWith("t-att")) { diff --git a/addons/web/static/src/views/fields/field_tooltip.js b/addons/web/static/src/views/fields/field_tooltip.js index 9b92b79dec4..c19bb817f55 100644 --- a/addons/web/static/src/views/fields/field_tooltip.js +++ b/addons/web/static/src/views/fields/field_tooltip.js @@ -12,7 +12,7 @@ export function getTooltipInfo(params) { debug: Boolean(odoo.debug), field: { name: params.field.name, - help: params.fieldInfo.help !== null ? params.fieldInfo.help : params.field.help, + help: params.fieldInfo.help ?? params.field.help, type: params.field.type, widget: params.fieldInfo.widget, widgetDescription, diff --git a/addons/web/static/src/views/widgets/widget.js b/addons/web/static/src/views/widgets/widget.js index b809dcd1263..ff81d46ff6c 100644 --- a/addons/web/static/src/views/widgets/widget.js +++ b/addons/web/static/src/views/widgets/widget.js @@ -65,17 +65,24 @@ Widget.template = xml/*xml*/ ` Widget.parseWidgetNode = function (node) { const name = node.getAttribute("name"); const widget = viewWidgetRegistry.get(name); - const widgetInfo = { name, - modifiers: JSON.parse(node.getAttribute("modifiers") || "{}"), + modifiers: {}, widget, - options: evaluateExpr(node.getAttribute("options") || "{}"), - attrs: {}, // populated below + options: {}, + attrs: {}, }; for (const { name, value } of node.attributes) { - if (!name.startsWith("t-att")) { + if (["name", "widget"].includes(name)) { + // avoid adding name and widget to attrs + continue; + } + if (name === "modifiers") { + widgetInfo.modifiers = JSON.parse(value); + } else if (name === "options") { + widgetInfo.options = evaluateExpr(value); + } else if (!name.startsWith("t-att")) { // all other (non dynamic) attributes widgetInfo.attrs[name] = value; }