Files
gzth/awesome_template_widget/static/src/template_widget.js
T
2024-10-31 20:35:38 +08:00

115 lines
2.9 KiB
JavaScript

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { _t } from "@web/core/l10n/translation";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { Component, xml, useEffect, useRef } from "@odoo/owl";
import { useService } from "@web/core/utils/hooks";
import { evaluateExpr } from "@web/core/py_js/py";
import { session } from "@web/session";
export class TemplateWidget extends Component {
get isReadonly() {
return true;
}
setup() {
super.setup();
this.action = useService("action");
this.rootRef = useRef("root");
this.uid = session.uid;
this.company_id = session.company_id;
useEffect(() => {
// listen to the button click event
this.rootRef.el.addEventListener("click", this._onButtonClick.bind(this));
return () => {
this.rootRef.el.removeEventListener("click", this._onButtonClick.bind(this));
}
}, () => [this.rootRef.el]);
}
_onButtonClick(ev) {
// check tag name
if (ev.target.tagName != 'BUTTON') {
return;
}
ev.preventDefault();
ev.stopPropagation();
let name = ev.target.attributes.name && ev.target.attributes.name.value
if (!name) {
return;
}
let type = ev.target.attributes.type.value
if (!type) {
return;
}
let context = ev.target.attributes.context && ev.target.attributes.context.value || '{}'
context = evaluateExpr(context, this.record.data)
// trigger up the button click message
this.action.doActionButton({
name: name,
type: type,
context: context,
resId: this.record.resId,
resModel: this.record.resModel,
onClose: async () => {
await this.record.model.root.load();
this.record.model.notify();
}
});
}
get data() {
return this.props.record.data;
}
get record() {
return this.props.record;
}
}
// props:
TemplateWidget.props = {
...standardFieldProps,
template: {
type: String,
optional: true,
}
};
TemplateWidget.displayName = _t("TemplateWidget");
TemplateWidget.template = xml`
<div t-ref="root">
<t t-call="{{this.props.template}}" />
</div>`
export const templateWidget = {
component: TemplateWidget,
displayName: _t("Template"),
supportedTypes: ["char", "Text"],
supportedOptions: [
{
label: _t("Template"),
name: "template",
type: "Char",
help: _t("The field template"),
},
],
extractProps: ({ attrs, options }) => ({
template: options.template,
})
};
registry.category("fields").add("template_widget", templateWidget);