import React from "react"; import { Card, Layout } from "antd"; import TmplTree from "./tmpl-tree"; import TreeUtils, { TtmplNodeConfig } from "./tree-utils"; import { TtmplConfig } from "../tmpl/interface"; import { DtmplEditCard, DtmplViewCard } from "../index"; import HcserviceV3 from "../tmpl/hcservice-v3"; import { LocaleContext } from "../locale/LocaleProvider"; const { Content, Sider } = Layout; interface ActTreeProps { ttmplConfig: TtmplConfig; top: number; serverKey?: string; } interface ActTreeState { ttmplNodeConfig?: TtmplNodeConfig; showEdit?: boolean; showView?: boolean; recordCode?: string; mainCode?: string; sourceId?: string; cardtitle?: string; addTmplId?: string; } export default class ActTree extends React.PureComponent< ActTreeProps, ActTreeState > { static defaultProps = { top: 66 }; state = { ttmplNodeConfig: undefined, recordCode: undefined, showEdit: false, showView: false, mainCode: undefined, sourceId: undefined, cardtitle: undefined, addTmplId: undefined, }; static contextType = LocaleContext; context: React.ContextType; refTmplTree: any = React.createRef(); async componentDidMount() { await this.loadData(); } loadData = async () => { const { ttmplConfig, serverKey } = this.props; let ttmplNodeConfig = await TreeUtils.initTtmplNodeConfig(ttmplConfig, serverKey); this.setState({ ttmplNodeConfig, showEdit: false, showView: false, }); }; async componentDidUpdate(prevProps) { let { ttmplConfig: prettmplConfig } = prevProps; let { ttmplConfig } = this.props; if (ttmplConfig && prettmplConfig && ttmplConfig.id == prettmplConfig.id) { return; } await this.loadData(); } onEditRoot = (title: string, code?: string) => { const { ttmplConfig } = this.props; this.showCard(title, ttmplConfig.rootGtmplId, null, code, true, false); }; showCard = ( cardtitle, sourceId, mainCode, recordCode, showEdit, showView, addTmplId?: string ) => { this.setState({ mainCode, cardtitle, recordCode, showEdit, showView, sourceId, addTmplId, }); }; onViewRoot = (title: string, code?: string) => { const { ttmplConfig } = this.props; this.showCard( title, ttmplConfig.rootGtmplId, null, code, false, true, null ); }; onEditSub = ( title: string, sourceId: string, mainCode: string, recordCode?: string ) => { this.showCard(title, sourceId, mainCode, recordCode, true, false, null); }; onViewSub = ( title: string, sourceId: string, mainCode: string, recordCode?: string ) => { this.showCard(title, sourceId, mainCode, recordCode, false, true, null); }; onDelete = async (sourceId: string, recordCode?: string) => { await HcserviceV3.deleteByCode(null, sourceId, [recordCode]); }; onAddSub = ( title: string, sourceId: string, mainCode: string, addTmplId: string ) => { this.showCard(title, sourceId, mainCode, null, true, false, addTmplId); }; render() { const { top, serverKey } = this.props; const { translate, siderCollapsed } = this.context; const { ttmplNodeConfig, showView, showEdit, recordCode, mainCode, sourceId, cardtitle, addTmplId, } = this.state; if (!ttmplNodeConfig) { return ; } return ( {showEdit ? ( {recordCode ? ( <> {translate("${编辑}")} {cardtitle ? cardtitle : ""} ) : ( <> {translate("${添加}")} {cardtitle ? cardtitle : ""} )}{" "} } mainCode={mainCode} sourceId={sourceId} code={recordCode} onOk={(recordCode) => { let a = this.refTmplTree.current?.reloadData(mainCode, recordCode); this.setState({ recordCode }); }} serverKey={serverKey} /> ) : null} {showView ? ( ) : null} ); } }