[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
This commit is contained in:
Simon Genin (ges)
2022-08-01 12:12:11 +02:00
parent 46f9f0de87
commit 150f63c1b6
3 changed files with 36 additions and 6 deletions
@@ -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);
@@ -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 },
};
+14 -2
View File
@@ -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*/ `