/** @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, onWillUpdateProps } from "@odoo/owl"; export class JsonTemplateWidget extends Component { setup() { this.parseValue(); onWillUpdateProps(() => { this.parseValue(); }); } parseValue() { try { let name = this.props.name; let data = this.props.record.data; let value = data[name] || "{}"; this.jsonData = JSON.parse(value); console.log('JsonTemplateWidget - 解析的JSON数据:', this.jsonData); } catch (e) { console.error('JsonTemplateWidget - JSON解析错误:', e.message); this.jsonData = { error: e.message }; } } get templateName() { if (this.jsonData && this.jsonData.template) { return this.jsonData.template; } return null; } get hasValidTemplate() { return this.templateName !== null; } static template = xml`
`; static props = { ...standardFieldProps, }; get isReadonly() { return true; } } export const JsonTemplateField = { component: JsonTemplateWidget, displayName: _t("Json Template"), supportedTypes: ["char", "text"], isEmpty: () => false }; registry.category("fields").add("json_template", JsonTemplateField);