import React from "react"; import { DoEditParam, LtmplConfig, LtmplConfigRes, PageType, RouterCompProps, ShowViewParam, ActTableViewModel, ThemeConfig } from "../tmpl/interface"; import Units from "../units"; import ActTable from "../table/act-table"; import HCDataSource from "../tmpl/hc-data-source"; import withRouter from "../routable/withroute"; import { Button, Drawer, Space } from "antd"; import { LocaleContext } from "../locale/LocaleProvider"; import LtmplTable from "../module/ltmpl-table"; import DtmplViewDrawer from "../module/dtmpl-view-drawer"; import DtmplCustomEditModalPage from "../custom-page/dtmpl-custom-edit-modal-page"; import CustomDetailModalPage from "../custom-page/custom-detail-modal-page"; import DtmplEditPage from "../module/dtmpl-edit-page"; import DtmplViewModal from "../module/dtmpl-view-modal"; import Edit2D from "../draw-canvas-edit"; // 编辑2D图 import View2D from "../draw-canvas-view"; // 查看2D图 const defShowTypePaths = { RealTimeCard: "real-time-card", calendar: "calendar", ganttChart: "gantt" }; export interface LtmplRouteProps extends RouterCompProps { layoutRootPath?: string; tableType?: "common" | "report"; rowOperatable?: boolean; defShowType?: ActTableViewModel; } export interface LtmplRouteState { ltmplConfigRes?: LtmplConfigRes; pageNo?: number; pageSize?: number; selectedCode?: string; showDraw?: boolean; ractionId?: string; showViewDrawer?: boolean; showViewModal?: boolean; showEdit?: boolean; showWindowType?: PageType; viewSourceId?: string; editSourceId?: string; toCustomPage?: boolean; userPageConfig?: { [key: string]: any }; userPageConfigLoading?: boolean; // 缓存配置请求状态 themeConfig?: ThemeConfig; pageSizeOptions?: number[]; // 分页条数 drawerSize?: number | string; // 抽屉组件 showEdit2D?: boolean; // 编辑2D图 showView2D?: boolean; // 查看2D图 isCopy?: boolean; // 是否复制 isAudit?: boolean; // 是否审计 } class LtmplRoute extends React.PureComponent { static contextType = LocaleContext; context: React.ContextType; static defaultProps = { basePath: "", tableType: "common", rowOperatable: true, }; state = { ltmplConfigRes: undefined, pageNo: undefined, pageSize: undefined, selectedCode: undefined, showDraw: false, ractionId: undefined, showViewDrawer: false, showViewModal: false, showEdit: false, showWindowType: "modal" as PageType, routeMode: 'multiple', viewSourceId: undefined, editSourceId: undefined, toCustomPage: false, userPageConfig: undefined, userPageConfigLoading: true, themeConfig: undefined, pageSizeOptions: [], drawerSize: "92vw", showEdit2D: false, showView2D: false, isCopy: false, isAudit: false }; async componentDidMount() { this.loadData(); this.getUserPageConfig(); this.getThemeConfig(); } Z get computedLayoutPath() { const { layoutRootPath } = this.props; if (layoutRootPath !== undefined) return `/${layoutRootPath}`; return ""; } // 获取用户页面配置 getUserPageConfig = async () => { const { params } = this.props; const { sourceId } = params; const config = await Units.getUserPageConfig(sourceId); this.setState({ userPageConfig: config, userPageConfigLoading: false }); }; getThemeConfig = async () => { const theme: ThemeConfig = await Units.getThemeConfig(); this.setState({ themeConfig: theme }); } // 设置用户页面配置 setUserPageConfig = (config: { [key: string]: any }) => { const { params } = this.props; const { sourceId } = params; const temConfig = Units.setUserPageConfig(sourceId, config); this.setState({ userPageConfig: temConfig }); }; // 重置本地缓存页面配置 resetUserPageConfig = () => { const { params } = this.props; const { sourceId } = params; Units.resetUserPageConfig(sourceId); this.setState({ userPageConfig: undefined }); } onRoute = (path) => { this.props.navigate(path); }; getMenuId = () => { const { searchParams } = this.props; let menuId = searchParams.get("menuId"); return menuId; }; getServerKey = () => { // 获取serverKey const { searchParams } = this.props; let serverKey = searchParams.get("serverKey"); return serverKey || null; }; doClose = () => { this.setState( { showDraw: false, showViewDrawer: false, showViewModal: false, showWindowType: undefined, showEdit: false, selectedCode: undefined, toCustomPage: false, isAudit: false } ) }; loadData = async () => { const { params } = this.props; const { sourceId } = params; const serverKey = this.getServerKey(); let ltmplConfigRes: LtmplConfigRes = await HCDataSource.requestLtmplConfig(serverKey, sourceId); this.setState({ ltmplConfigRes: ltmplConfigRes }); }; async componentDidUpdate(prevProps) { const { location, params } = this.props; const { sourceId } = params; let { sourceId: preSourceId } = prevProps.params; let { location: preLocation } = prevProps; if ( !sourceId || (!preSourceId && this.state.ltmplConfigRes) || (location.pathname == preLocation.pathname && location.search == preLocation.search) ) { return false; } await this.loadData(); } showView = (showViewParam: ShowViewParam) => { const { basePath } = this.props; const { sourceId } = this.props.params; const { ltmplConfigRes } = this.state; let ltmplConfig: LtmplConfig = ltmplConfigRes.ltmplConfig; let showType: string = !ltmplConfig.buttonViewConfig ? "drawer" : ltmplConfig.buttonViewConfig.showType; let dtmplSourceId = showViewParam.dtmplSourceId ? showViewParam.dtmplSourceId : sourceId; let path = `${basePath}${this.computedLayoutPath }/${dtmplSourceId}/detail-view?code=${showViewParam.code }&menuId=${this.getMenuId()}`; if ( showViewParam.toCustomPage && ltmplConfig.customViewPagePath ) { path = `${basePath}${this.computedLayoutPath}${ltmplConfig.customViewPagePath }?menuId=${this.getMenuId()}&sourceId=${sourceId}`; } if (showType == "routing") { this.onRoute(path); } else if (showType == "blank") { window.open("#" + path, '_blank'); } else { this.setState({ showViewModal: showType === "modal", showViewDrawer: ["drawer", "default"].includes(showType), selectedCode: showViewParam.code, viewSourceId: dtmplSourceId, toCustomPage: showViewParam.toCustomPage }) } }; doEdit = (param: DoEditParam) => { const { basePath } = this.props; const { sourceId } = this.props.params; const { ltmplConfigRes } = this.state; let mainCode = this.getMainCode(); let ltmplConfig: LtmplConfig = ltmplConfigRes.ltmplConfig; let showWindowType: PageType = !ltmplConfig.buttonEditConfig ? "drawer" : ltmplConfig.buttonEditConfig.showType; let dtmplSourceId = param.dtmplSourceId ? param.dtmplSourceId : sourceId; let path = `${basePath}${this.computedLayoutPath }/${dtmplSourceId}/detail-edit?code=${param.code }&menuId=${this.getMenuId()}&mode=${param.mode}`; if (param.toCustomPage && ltmplConfig.customEditPagePath) { path = `${basePath}${this.computedLayoutPath}/page/${dtmplSourceId}${ltmplConfig.customEditPagePath }?code=${param.code}&menuId=${this.getMenuId()}`; } if (mainCode) { path = path + `&mainCode=${mainCode}`; } if (showWindowType === "routing") { this.onRoute(path); } else if (showWindowType === "blank") { window.open("#" + path, '_blank'); } else { this.setState({ showEdit: true, showWindowType: showWindowType === "default" ? "drawer" : showWindowType, selectedCode: param.code, editSourceId: dtmplSourceId, isCopy: param.mode === "copy" }) } }; getMainCode = () => { const { searchParams } = this.props; let mainCode = undefined; // let criteriaData = Units.transQueryStrToParams(search); let mcode = searchParams.get("mainCode"); if (mcode) { // criteriaData = Units.transQueryStrToParams(search); mainCode = mcode; } return mainCode; }; doCreate = (addTmplId: string, toCustomPage: boolean = false) => { const { basePath } = this.props; const { sourceId } = this.props.params; const { ltmplConfigRes } = this.state; let ltmplConfig: LtmplConfig = ltmplConfigRes.ltmplConfig; let showWindowType: PageType = !ltmplConfig.buttonAddConfig ? "drawer" : ltmplConfig.buttonAddConfig.showType; let path = `${basePath}${this.computedLayoutPath }/${sourceId}/detail-edit?menuId=${this.getMenuId()}&addTmplId=${addTmplId ? addTmplId : "" }`; if (toCustomPage && ltmplConfig.customEditPagePath) { path = `${basePath}${this.computedLayoutPath}/page/${sourceId}${ltmplConfig.customEditPagePath }?menuId=${this.getMenuId()}`; } let mainCode = this.getMainCode(); if (mainCode) { path = path + `&mainCode=${mainCode}`; } if (showWindowType == "routing") { this.onRoute(path); } else if (showWindowType == "blank") { window.open("#" + path, '_blank'); } else { let editSourceId = addTmplId ? addTmplId : sourceId; this.setState({ showWindowType: showWindowType === "default" ? "drawer" : showWindowType, showEdit: true, editSourceId }) } }; doRAction = (ractionId: string, mainCode: string) => { if (false) { const { basePath } = this.props; let path = `${basePath}${this.computedLayoutPath }/${ractionId}/${this.pathMark()}?mainCode=${mainCode}&menuId=${this.getMenuId()}`; this.onRoute(path); } else { this.setState({ selectedCode: mainCode, showDraw: true, ractionId, }); } }; // 2D图编辑 doEdit2D = (val: string) => { this.setState({ showEdit2D: true, selectedCode: val }); } // 2D图查看 doView2D = (val: string) => { this.setState({ showView2D: true, selectedCode: val }); } // 审计查看 doAuditView = (val: string) => { const { sourceId } = this.props.params; const { ltmplConfigRes } = this.state; let ltmplConfig: LtmplConfig = ltmplConfigRes.ltmplConfig; let showType: string = !ltmplConfig.buttonViewConfig ? "drawer" : ltmplConfig.buttonViewConfig.showType; this.setState({ showViewModal: showType === "modal", showViewDrawer: ["drawer", "default"].includes(showType), selectedCode: val, viewSourceId: sourceId, isAudit: true }); } // 审计编辑 doAuditEdit = (val: string) => { const { sourceId } = this.props.params; const { ltmplConfigRes } = this.state; let ltmplConfig: LtmplConfig = ltmplConfigRes.ltmplConfig; let showWindowType: PageType = !ltmplConfig.buttonEditConfig ? "drawer" : ltmplConfig.buttonEditConfig.showType; this.setState({ showEdit: true, showWindowType: showWindowType === "default" ? "drawer" : showWindowType, selectedCode: val, editSourceId: sourceId, isAudit: true }); } pathMark = () => { const { tableType, defShowType } = this.props; if (defShowType) { return defShowTypePaths[defShowType]; } return tableType && tableType == "report" ? "report-table" : "act-table"; } doSearch = (params_) => { const { basePath } = this.props; const { sourceId } = this.props.params; let params = { ...Units.transQueryStrToParams( this.props.location.search, this.state.ltmplConfigRes?.ltmplConfig ), ...params_, }; const newfliter = Units.transQueryParamsToStr({ ...params }); this.props.navigate( `${basePath}${this.computedLayoutPath}/${sourceId}/${this.pathMark()}?${newfliter}`, { replace: true, } ); }; onChangeSearch = (params_) => { let params = { ...params_ }; //追加过滤条件,hiddenColIds,innerQueryKey const { searchParams } = this.props; let mcode = searchParams.get("mainCode"); if (mcode) { params["mainCode"] = mcode; } const { pageSize, pageNo } = this.state; params["pageNo"] = 1; if (pageSize) { params["pageSize"] = pageSize; } params["menuId"] = this.getMenuId(); params["sourceId"] = this.getMenuId(); this.doSearch(params); }; doSubmited = (code?: string, isContinueSave?: boolean) => { this.onChangeSearch({ serial_number: Units.getReactKey() }); if (isContinueSave) { this.setState({ selectedCode: code }); } else { this.doClose(); } } onChangePage = (pageNo: number, pageSize: number, pageSizeOptions?: number[]) => { const { location } = this.props; const temPageSizeOptions = pageSizeOptions?.length ? pageSizeOptions : this.state.pageSizeOptions; let search = location.search; let params = Units.transQueryStrToParams(search, null); params["pageNo"] = pageNo; params["pageSize"] = pageSize; this.doSearch(params); const temPagesize = temPageSizeOptions.indexOf(pageSize); if (temPagesize == -1) { return; } this.setState({ pageSizeOptions: temPageSizeOptions, pageSize }); this.setUserPageConfig({ pageSize: temPagesize }); }; // 更新分页数数组 onUpdatePageSizeOptions = (val: number[]) => { this.setState({ pageSizeOptions: val }); } render() { const { ltmplConfigRes, showDraw, showWindowType, ractionId, selectedCode, showViewDrawer, showViewModal, showEdit, editSourceId, viewSourceId, toCustomPage, userPageConfig, themeConfig, pageSizeOptions, userPageConfigLoading, drawerSize, showEdit2D, showView2D, isCopy, isAudit } = this.state; const { location, params, tableType, rowOperatable, defShowType } = this.props; const { sourceId } = params; const serverKey = this.getServerKey(); const { translate } = this.context; //let menuId = undefined; let mainCode = undefined; let search = location.search; let criteriaData = Units.transQueryStrToParams( search, ltmplConfigRes?.ltmplConfig ); const cachePageSize = userPageConfig?.["pageSize"]; // 获取缓存分页数配置,其配置的是分页数组下标 let pageSize = cachePageSize ? pageSizeOptions[cachePageSize] : criteriaData["pageSize"]; let pageNo = criteriaData["pageNo"]; if (search.indexOf("mainCode") > 0) { //criteriaData = Units.transQueryStrToParams(search); mainCode = criteriaData["mainCode"]; } let ltmplConfig = ltmplConfigRes ? ltmplConfigRes.ltmplConfig : null; let windowWidth = ltmplConfig?.buttonViewConfig?.windowWidth windowWidth = windowWidth && windowWidth > 0 ? windowWidth : 1280; let windowHeight = ltmplConfig?.buttonViewConfig?.windowHeight; windowHeight = windowHeight && windowHeight > 0 ? windowHeight : '80vh'; let windowEditWidth = ltmplConfig?.buttonEditConfig?.windowWidth; windowEditWidth = windowEditWidth && windowEditWidth > 0 ? windowEditWidth : 1280; let windowEditHeight = ltmplConfig?.buttonEditConfig?.windowHeight; windowEditHeight = windowEditHeight && windowEditHeight > 0 ? windowEditHeight : '80vh'; if (!ltmplConfig || userPageConfigLoading) { return <>; } else { return ( <> {showDraw ? ( this.setState({ drawerSize: newSize })) }} size={drawerSize} extra={ } styles={{ body: { minWidth: "720px" } }} > ) : null} {showViewDrawer && selectedCode ? ( toCustomPage && ltmplConfig.customViewPagePath ? : ) : null} {showViewModal && selectedCode ? ( toCustomPage && ltmplConfig.customViewPagePath ? : ) : null} {showEdit && ltmplConfig ? ( ltmplConfig.customEditPagePath ? ( { this.doSubmited(); }} onCancel={() => { this.doClose(); }} customPath={ltmplConfig.customEditPagePath} sourceId={sourceId} mainCode={mainCode} code={selectedCode} isCopy={isCopy} /> ) : ( ) ) : null} {showEdit2D && selectedCode ? this.setState({ showEdit2D: false, selectedCode: undefined })} open={showEdit2D} resizable={{ onResize: (newSize => this.setState({ drawerSize: newSize })) }} size={drawerSize} styles={{ body: { minWidth: "720px", padding: 0 } }} > : null } {showView2D && selectedCode ? this.setState({ showView2D: false, selectedCode: undefined })} open={showView2D} resizable={{ onResize: (newSize => this.setState({ drawerSize: newSize })) }} size={drawerSize} styles={{ body: { minWidth: "720px", padding: 0 } }} > : null } ); } } } export default withRouter(LtmplRoute);