From 150f63c1b69998ecc5f0cf702071b1dde88018f6 Mon Sep 17 00:00:00 2001 From: "Simon Genin (ges)" Date: Fri, 29 Jul 2022 11:41:49 +0000 Subject: [PATCH] [IMP] web, *: add extract props for view widgets To make cleaner code and keep a similar API than fields, the extractProps static method is added to the the widgets. extractProps allows making new props out of the attrs of the widget node. Part-of: odoo/odoo#96843 --- .../src/app_store_widget/app_store_widget.js | 17 +++++++++++++---- .../src/views/widgets/standard_widget_props.js | 9 +++++++++ addons/web/static/src/views/widgets/widget.js | 16 ++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 addons/web/static/src/views/widgets/standard_widget_props.js diff --git a/addons/hr_timesheet/static/src/app_store_widget/app_store_widget.js b/addons/hr_timesheet/static/src/app_store_widget/app_store_widget.js index 5355a228432..b5d3cad3eb0 100644 --- a/addons/hr_timesheet/static/src/app_store_widget/app_store_widget.js +++ b/addons/hr_timesheet/static/src/app_store_widget/app_store_widget.js @@ -5,6 +5,7 @@ import { _t } from "@web/core/l10n/translation"; import { Dialog } from "@web/core/dialog/dialog"; import { useService } from "@web/core/utils/hooks"; import { isAndroid, isIOS } from "@web/core/browser/feature_detection"; +import { standardWidgetProps } from "@web/views/widgets/standard_widget_props"; const { Component } = owl; @@ -26,13 +27,13 @@ const configs = { class AppStoreWidget extends Component { setup() { this.dialog = useService("dialog"); - this.config = configs[this.props.node.attrs.type]; + this.config = configs[this.props.type]; } openQRDialog() { if ( - (this.props.node.attrs.type === "apple" && isIOS()) || - (this.props.node.attrs.type === "google" && isAndroid()) + (this.props.type === "apple" && isIOS()) || + (this.props.type === "google" && isAndroid()) ) { this.env.services.action.doAction({ type: "ir.actions.act_url", @@ -57,5 +58,13 @@ class AppStoreQRDialog extends Component { } AppStoreQRDialog.components = { Dialog }; AppStoreQRDialog.template = "hr_timesheet.AppStoreQRDialog"; - +AppStoreQRDialog.props = { + ...standardWidgetProps, + type: { type: String }, +}; +AppStoreQRDialog.extractProps = ({ attrs }) => { + return { + type: attrs.type, + }; +}; registry.category("view_widgets").add("hr_timesheet.app_store_widget", AppStoreWidget); diff --git a/addons/web/static/src/views/widgets/standard_widget_props.js b/addons/web/static/src/views/widgets/standard_widget_props.js new file mode 100644 index 00000000000..e5145a3ed8c --- /dev/null +++ b/addons/web/static/src/views/widgets/standard_widget_props.js @@ -0,0 +1,9 @@ +/** @odoo-module **/ + +export const standardWidgetProps = { + readonly: { type: Boolean, optional: true }, + record: { type: Object }, + // TODO WOWL remove "node" once there are no more legacy widgets. + // DO NOT USE THIS PROP IN NEW WIDGETS. USE extractProps() INSTEAD. + node: { type: Object }, +}; diff --git a/addons/web/static/src/views/widgets/widget.js b/addons/web/static/src/views/widgets/widget.js index 58c1b3a0792..c239d3369fa 100644 --- a/addons/web/static/src/views/widgets/widget.js +++ b/addons/web/static/src/views/widgets/widget.js @@ -1,5 +1,6 @@ /* @odoo-module */ +import { evaluateExpr } from "@web/core/py_js/py"; import { registry } from "@web/core/registry"; import { decodeObjectForTemplate } from "@web/views/view_compiler"; @@ -31,9 +32,20 @@ export class Widget extends Component { }; } get widgetProps() { - const { record, node: rawNode, options, readonly } = this.props; + const { record, node: rawNode, readonly } = this.props; const node = rawNode ? decodeObjectForTemplate(rawNode) : {}; - return { record, node, options: options || {}, readonly }; + let propsFromAttrs = {}; + if (node.attrs) { + const extractProps = this.Widget.extractProps || (() => ({})); + propsFromAttrs = extractProps({ + attrs: { + ...node.attrs, + options: evaluateExpr(node.attrs.options || "{}"), + }, + }); + } + // TODO WOWL remove "node" once there are no more legacy widgets. + return { readonly, node, ...propsFromAttrs, record }; } } Widget.template = xml/*xml*/ `