import React, { useEffect, useState, useMemo } from "react"; import { useParams, useLocation, useNavigate } from "react-router-dom"; 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, Splitter } from "antd"; 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 { DtmplEditCard, DtmplViewCard, useLocale } from "../index"; import "./index.less"; export interface Props extends RouterCompProps { layoutRootPath?: string; tableType?: "common" | "report"; rowOperatable?: boolean; defShowType?: ActTableViewModel; } const Index = (props: Props) => { const { layoutRootPath, tableType, rowOperatable, basePath = "", defShowType } = props; const [pageInfo, setPageInfo] = useState<{ pageNo: number, pageSize: number }>(); const [ltmplConfig, setLtmplConfig] = useState(); const [splitterSize, setSplitterSize] = useState("50%"); const [pageLayout, setPageLayout] = useState("none"); const [selectedCode, setSelectedCode] = useState(); const [showEdit, setShowEdit] = useState(true); const [ractionId, setRactionId] = useState(); const [showDraw, setShowDraw] = useState(false); const [showView, setShowView] = useState(false); const [showWindowType, setShowWindowType] = useState("default"); const [toCustomPage, setToCustomPage] = useState(false); const [viewSourceId, setViewSourceId] = useState(); const [editSourceId, setEditSourceId] = useState(); const [userPageConfig, setUserPageConfig] = useState<{ [key: string]: any }>(); const [userPageConfigLoading, setUserPageConfigLoading] = useState(true); const [pageSizeOptions, setPageSizeOptions] = useState([]); // 分页条数 const [themeConfig, setThemeConfig] = useState(); // 主题配置信息 const [drawerSize, setDrawerSize] = useState("92vw"); const [isCopy, setIsCopy] = useState(false); // 是否复制 const { sourceId } = useParams(); const { search } = useLocation(); const { translate } = useLocale(); const navigate = useNavigate(); const computedLayoutPath = layoutRootPath !== undefined ? `/${layoutRootPath}` : ""; const { mainCode, menuId, serverKey } = useMemo(() => { const temParams = new URLSearchParams(search); const mainCode = temParams.get("mainCode"); const menuId = temParams.get("menuId"); const serverKey = temParams.get("serverKey") || null; return { mainCode, menuId, serverKey } }, [search]) const getLtmplConfig = async () => { const ltmplConfigRes: LtmplConfigRes = await HCDataSource.requestLtmplConfig(serverKey, sourceId); const ltmplConfig = ltmplConfigRes?.ltmplConfig; setLtmplConfig(ltmplConfig); setSplitterSize(`${ltmplConfig?.rightPercent || 50}%`); setPageLayout(ltmplConfig?.pageLayout || "none"); }; // 获取用户页面配置 const getUserPageConfig = async () => { const config = await Units.getUserPageConfig(sourceId); setUserPageConfig(config); setUserPageConfigLoading(false); }; // 获取全局主题配置 const getThemeConfig = async () => { const theme: ThemeConfig = await Units.getThemeConfig(); setThemeConfig(theme); } // 设置用户页面配置 const handleSetUserPageConfig = (config: { [key: string]: any }) => { const temConfig = Units.setUserPageConfig(sourceId, config); setUserPageConfig(temConfig); }; // 重置本地缓存页面配置 const resetUserPageConfig = () => { Units.resetUserPageConfig(sourceId); setUserPageConfig(undefined); } useEffect(() => { if (sourceId) { getLtmplConfig(); getUserPageConfig(); getThemeConfig(); } }, [sourceId]); const doSearch = (params_) => { let params = { ...Units.transQueryStrToParams(search, ltmplConfig), ...params_, }; const newfliter = Units.transQueryParamsToStr({ ...params }); const pathMark = tableType === "report" ? "splitview-report-table" : "splitview-act-table"; navigate(`${basePath}${computedLayoutPath}/${sourceId}/${pathMark}?${newfliter}`, { replace: true }); }; const onChangePage = (pageNo: number, pageSize: number, pageSizes?: number[]) => { const temPageSizeOptions = pageSizes ? pageSizes : pageSizeOptions; let params = Units.transQueryStrToParams(search, null); params["pageNo"] = pageNo; params["pageSize"] = pageSize; doSearch(params); const temPagesize = temPageSizeOptions.indexOf(pageSize); if (temPagesize == -1) { return; } setPageSizeOptions(temPageSizeOptions); setPageInfo({ pageNo, pageSize }); handleSetUserPageConfig({ pageSize: temPagesize }); }; // 更新分页数数组 const onUpdatePageSizeOptions = (val: number[]) => { setPageSizeOptions(val); } const onShowView = (showViewParam: ShowViewParam) => { doClose(); let showType: PageType = !ltmplConfig.buttonViewConfig ? "default" : ltmplConfig.buttonViewConfig.showType; let dtmplSourceId = showViewParam.dtmplSourceId ? showViewParam.dtmplSourceId : sourceId; let path = `${basePath}${computedLayoutPath}/${dtmplSourceId}/detail-view?code=${showViewParam.code}&menuId=${menuId}`; if (showViewParam.toCustomPage && ltmplConfig.customViewPagePath) { path = `${basePath}${computedLayoutPath}${ltmplConfig.customViewPagePath}?menuId=${menuId}&sourceId=${sourceId}`; } if (showType === "routing") { navigate(path); } else if (showType === "blank") { window.open("#" + path, '_blank'); } else { setShowView(true); setSelectedCode(showViewParam.code); setViewSourceId(dtmplSourceId); setToCustomPage(showViewParam.toCustomPage); setShowWindowType(pageLayout === "none" ? "drawer" : showType); } }; const onChangeSearch = (params_) => { let params = { ...params_ }; if (mainCode) { params["mainCode"] = mainCode; } params["pageNo"] = 1; if (pageInfo?.pageSize) { params["pageSize"] = pageInfo?.pageSize; } params["menuId"] = menuId; params["sourceId"] = sourceId; doSearch(params); }; const doEdit = (param: DoEditParam) => { doClose(); let showType: PageType = !ltmplConfig.buttonEditConfig ? "default" : ltmplConfig.buttonEditConfig.showType; let dtmplSourceId = param.dtmplSourceId ? param.dtmplSourceId : sourceId; let path = `${basePath}${computedLayoutPath}/${dtmplSourceId}/detail-edit?code=${param.code}&menuId=${menuId}&mode=${param.mode}`; if (param.toCustomPage && ltmplConfig.customEditPagePath) { path = `${basePath}${computedLayoutPath}/page/${dtmplSourceId}${ltmplConfig.customEditPagePath}?code=${param.code}&menuId=${menuId}`; } if (mainCode) { path = path + `&mainCode=${mainCode}`; } if (showType === "routing") { navigate(path); } else if (showType === "blank") { window.open("#" + path, '_blank'); } else { setShowEdit(true); setShowWindowType(pageLayout === "none" ? "drawer" : showType); setEditSourceId(dtmplSourceId); setSelectedCode(param.code); setIsCopy(param.mode === "copy"); } }; const doCreate = (addTmplId: string, toCustomPage: boolean = false) => { doClose(); let showType: PageType = !ltmplConfig.buttonAddConfig ? "default" : ltmplConfig.buttonAddConfig.showType; let path = `${basePath}${computedLayoutPath}/${sourceId}/detail-edit?menuId=${menuId}&addTmplId=${addTmplId ? addTmplId : ""}`; if (toCustomPage && ltmplConfig.customEditPagePath) { path = `${basePath}${computedLayoutPath}/page/${sourceId}${ltmplConfig.customEditPagePath}?menuId=${menuId}`; } if (mainCode) { path = path + `&mainCode=${mainCode}`; } if (showType === "routing") { navigate(path); } else if (showType === "blank") { window.open("#" + path, '_blank'); } else { let editSourceId = addTmplId ? addTmplId : sourceId; setTimeout(() => { setShowEdit(true); setShowWindowType(pageLayout === "none" ? "drawer" : showType); setEditSourceId(editSourceId); }, 100); } }; const doRAction = (ractionId: string, mainCode: string) => { setSelectedCode(mainCode); setRactionId(ractionId); setShowDraw(true); }; const renderActTable = () => { let criteriaData = Units.transQueryStrToParams(search, ltmplConfig); const cachePageSize = userPageConfig?.pageSize; // 获取缓存分页数配置,其配置的是分页数组下标 let pageSize = cachePageSize ? pageSizeOptions[cachePageSize] : pageInfo?.pageSize; if (!ltmplConfig || userPageConfigLoading) { return <>; } return pageLayout !== "none" && doCreate(editSourceId)} defShowType={defShowType} userPageConfig={userPageConfig} setUserPageConfig={handleSetUserPageConfig} resetUserPageConfig={resetUserPageConfig} allowUserConfig={themeConfig?.allowUserConfig} onUpdatePageSizeOptions={onUpdatePageSizeOptions} />; }; const doSubmited = (code?: string, isContinueSave?: boolean) => { onChangeSearch({ serial_number: Units.getReactKey() }); if (isContinueSave) { setSelectedCode(code); } else { doClose(); } } const doClose = () => { setShowDraw(false); setShowView(false); setShowEdit(false); setShowWindowType(undefined); setSelectedCode(undefined); setToCustomPage(false); }; // 关联查询close const doCloseShowDraw = () => { if (showWindowType === "default") { doCreate(editSourceId); } else { doClose(); } } const windowSize = useMemo(() => { const { buttonViewConfig, buttonEditConfig } = ltmplConfig || {}; let width = buttonViewConfig?.windowWidth width = width && width > 0 ? width : 1280; let height: number | string = buttonViewConfig?.windowHeight; height = height && height > 0 ? height : '80vh'; let editWidth = buttonEditConfig?.windowWidth; editWidth = editWidth && editWidth > 0 ? editWidth : 1280; let editHeight: number | string = buttonEditConfig?.windowHeight; editHeight = editHeight && editHeight > 0 ? editHeight : '80vh'; return { width, height, editWidth, editHeight }; }, [ltmplConfig]); return
{["leftRightSplit", "upDownSplit"].includes(pageLayout) ? setSplitterSize(val[0])}> {renderActTable()} {showEdit && showWindowType === "default" ? {translate(selectedCode ? "${编辑}" : "${添加}")}} mainCode={mainCode} sourceId={sourceId} code={selectedCode} isReset={false} onOk={() => doSearch({ serial_number: new Date().getTime() })} isCopy={isCopy} serverKey={serverKey} /> : null} {showView && selectedCode && showWindowType === "default" ? : null} : renderActTable()} {showDraw ? ( setDrawerSize(newSize)) }} size={drawerSize} styles={{ body: { minWidth: "720px" } }} extra={ } > ) : null} {showView && showWindowType === "drawer" && selectedCode ? ( toCustomPage && ltmplConfig?.customViewPagePath ? : ) : null} {showView && showWindowType === "modal" && selectedCode ? ( toCustomPage && ltmplConfig?.customViewPagePath ? : ) : null} {showEdit && showWindowType !== "default" && ltmplConfig ? ( ltmplConfig?.customEditPagePath ? ( ) : ( ) ) : null}
; }; export default withRouter(Index);