|
| 1 | +import { Component, useEffect, useState } from '@odoo/owl'; |
| 2 | + |
| 3 | +import { registry } from '@web/core/registry'; |
| 4 | +import { useBus } from '@web/core/utils/hooks'; |
| 5 | +import { standardFieldProps } from "@web/views/fields/standard_field_props"; |
| 6 | +import { TextField } from '@web/views/fields/text/text_field'; |
| 7 | +import { X2ManyField, x2ManyField } from "@web/views/fields/x2many/x2many_field"; |
| 8 | +import { CheckBox } from "@web/core/checkbox/checkbox"; |
| 9 | + |
| 10 | +import { diff_match_patch } from "@runbot/libs/diff_match_patch/diff_match_patch"; |
| 11 | +import { DiffDisplay } from './diff_display'; |
| 12 | + |
| 13 | + |
| 14 | +export class ErrorContentOne2ManyList extends X2ManyField { |
| 15 | + static template = 'runbot.ErrorContentOne2ManyList'; |
| 16 | + static components = {...X2ManyField.components, CheckBox}; |
| 17 | + |
| 18 | + setup() { |
| 19 | + super.setup(...arguments); |
| 20 | + this.creates = []; |
| 21 | + this.state = useState({ |
| 22 | + useDiff: localStorage.getItem('runbot.error_content_diff_mode') === 'true', |
| 23 | + }); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + localStorage.setItem('runbot.error_content_diff_mode', this.state.useDiff); |
| 27 | + this.env.bus.trigger( |
| 28 | + 'RUNBOT.TOGGLE-DIFF-MODE', { |
| 29 | + mode: this.state.useDiff, |
| 30 | + }, |
| 31 | + ); |
| 32 | + }, () => [this.state.useDiff]); |
| 33 | + } |
| 34 | + |
| 35 | + get displayControlPanelButtons() { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + onToggle(state) { |
| 40 | + this.state.useDiff = state; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export const errorContentOne2ManyList = { |
| 45 | + ...x2ManyField, |
| 46 | + component: ErrorContentOne2ManyList, |
| 47 | +}; |
| 48 | + |
| 49 | +export class FieldErrorContentContent extends Component { |
| 50 | + static template = 'runbot.FieldErrorContentContent'; |
| 51 | + static components = { TextField, DiffDisplay }; |
| 52 | + static props = {...standardFieldProps}; |
| 53 | + |
| 54 | + setup() { |
| 55 | + // We assume that the content is readonly here |
| 56 | + this.otherRecords = this.props.record.model.root.data.error_content_ids.records; |
| 57 | + this.state = useState({ |
| 58 | + useDiff: localStorage.getItem('runbot.error_content_diff_mode') === 'true', |
| 59 | + }); |
| 60 | + |
| 61 | + useBus( |
| 62 | + this.env.bus, 'RUNBOT.TOGGLE-DIFF-MODE', |
| 63 | + ({detail: {mode}}) => this.state.useDiff = mode, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + get isParent() { |
| 68 | + return this.otherRecords[0] === this.props.record; |
| 69 | + } |
| 70 | + |
| 71 | + get parent() { |
| 72 | + return this.otherRecords[0]; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export const fieldErrorContentContent = { |
| 77 | + component: FieldErrorContentContent, |
| 78 | + displayName: "Error Content diffable (list)", |
| 79 | + supportedTypes: ["html", "text", "char"], |
| 80 | +}; |
| 81 | + |
| 82 | + |
| 83 | +registry.category('fields').add('error_content_list', errorContentOne2ManyList); |
| 84 | +registry.category('fields').add('list.error_content_content', fieldErrorContentContent); |
0 commit comments