import {PlainObject} from "@peryl/utils/event"; import {iObjectServiceDefaultConfig, iObjectServicePickConfig, iObjectServicePickMultipleConfig, iObjectServicePickSingleConfig} from "./object.service.utils"; import {$dialog} from "../$dialog"; import {AutoTable} from "../AutoTable"; import {PlcCheck} from "../Table/standard/PlcCheck/PlcCheck"; import {getComponentCls, reactive} from "@peryl/react-compose"; import {deepcopy} from "@peryl/utils/deepcopy"; import {convertRowToSelected, convertSelectedToRow} from "./object.conver"; import {createEffects} from "@peryl/utils/createEffects"; import {defer} from "@peryl/utils/defer"; import {$message} from "../$message"; import {delay} from "@peryl/utils/delay"; import {getTableRowHeight} from "../Table/table/utils/table.utils"; import {iDialogServiceOption} from "../useDialog/dialog.service.utils"; import {TableOptionSpace} from "../AutoTable/utils/TableOption.space"; import i18n from "../i18n"; import {ClientZoom} from "../../utils/ClientZoom"; export function createObjectService(defaultConfig: iObjectServiceDefaultConfig) { const publicDialogOption: iDialogServiceOption = { headAlign: 'left', title: i18n.$it('base.selectData').d('选择数据'), confirmButton: true, cancelButton: true, noContentPadding: true, maxHeight: ' ', maxWidth: ' ', width: '50vw' }; function processShowRows(option: TableOptionSpace.iTableOption) { if (option.useConfig.showRows == null) { const { clientHeight } = ClientZoom.getClientSize(); const availableHeight = clientHeight * 0.8 - 48 - 44 - 40 - 40 - 28; const { bodyRowHeight } = getTableRowHeight(option.config.size, option.config); const showRows = Math.floor(availableHeight / bodyRowHeight) - 1; option.config.showRows = showRows; option.pagination.state.size = option.config.showRows; option.pagination.state.pageSizeOptions.indexOf(showRows) === -1 && (option.pagination.state.pageSizeOptions.unshift(showRows)); option.fill.state.showRows = showRows; } } function processPermit(option: TableOptionSpace.iTableOption) { /*clear data*/ option.config.loadOnStart = false; option.state.list = []; option.hooks.onPageDataChange.exec(null); /*reset showRow*/ processShowRows(option); /*clear filters*/ option.filterState.clearAll(false); /*默认不可编辑*/ if (!option.config.permit) {option.config.permit = {};} if (!option.config.permit.show) {option.config.permit.show = {};} if (option.config.permit.show.insert == null) {option.config.permit.show.insert = false;} if (option.config.permit.show.update == null) {option.config.permit.show.update = false;} if (option.config.permit.show.delete == null) {option.config.permit.show.delete = false;} } /** * 单选 * @author 韦胜健 * @date 2023.1.24 20:32 */ function single(serviceConfig: iObjectServicePickSingleConfig): Promise { const dfd = defer(); const { effects } = createEffects(); const config = { ...defaultConfig, ...serviceConfig }; processPermit(config.option); const state = reactive({ selected: !config.selected ? undefined : deepcopy(convertRowToSelected(config.selected, config.map)) }); const handler = { handleConfirm: async () => { const { selected } = state; if (!selected) { $message(i18n.$it('base.selectPieceOfData').d('请选择一条数据')); return false; } const flag = config.beforeConfirm?.(selected); if (flag === false) { return false; } const row = convertSelectedToRow(selected, config.map); dfd.resolve(row); close(); return; }, }; !serviceConfig.readonly && effects.push( config.option.hooks.onRenderColumns.use({ processor: ({ list, render }) => {list.push({ render, key: 'object-check', seq: 0 });}, render: () => ( ) }) ); const close = $dialog({ ...publicDialogOption, ...serviceConfig.dialogProps, ...serviceConfig.readonly ? { confirmButton: false, cancelButton: false } : {}, title: serviceConfig.option.config.title || publicDialogOption.title, externalClass: [serviceConfig.dialogProps?.externalClass, getComponentCls('object-dialog')].filter(Boolean).join(' '), onClose: () => {effects.clear();}, onOpen: () => config.option.methods.pageMethods.reload(), handleConfirm: handler.handleConfirm, handleCancel: () => config.beforeCancel?.(), render: () => { return (
); } }); !serviceConfig.readonly && (!config.option.permission.permit.value.show.update || config.option.permission.permit.value.disabled.update) && effects.push( config.option.hooks.onDblclickRow.use(async () => { await delay(); handler.handleConfirm(); }) ); return dfd.promise; } /** * 多选 * @author 韦胜健 * @date 2023.1.24 20:33 */ function multiple(serviceConfig: iObjectServicePickMultipleConfig): Promise { const dfd = defer(); const { effects } = createEffects(); const config = { ...defaultConfig, ...serviceConfig }; processPermit(config.option); const state = reactive({ selected: (!config.selected ? undefined : deepcopy(convertRowToSelected(config.selected, config.map))) as undefined | PlainObject[] }); !serviceConfig.readonly && effects.push( config.option.hooks.onRenderColumns.use({ processor: ({ list, render }) => {list.push({ render, key: 'object-check', seq: 0 });}, render: () => ( ) }) ); $dialog({ headAlign: 'left', ...publicDialogOption, ...serviceConfig.dialogProps, ...serviceConfig.readonly ? { confirmButton: false, cancelButton: false } : {}, title: serviceConfig.option.config.title || publicDialogOption.title, externalClass: [serviceConfig.dialogProps?.externalClass, getComponentCls('object-dialog')].filter(Boolean).join(' '), onClose: () => {effects.clear();}, onOpen: () => config.option.methods.pageMethods.reload(), handleConfirm: async () => { const { selected } = state; if (!selected || selected.length === 0) { $message(i18n.$it('base.selectAtLeastOnePieceOfData').d('请至少选择一条数据')); return false; } const flag = config.beforeConfirm?.(selected); if (flag === false) { return false; } const rows = convertSelectedToRow(selected, config.map); dfd.resolve(rows); return; }, handleCancel: () => config.beforeCancel?.(), render: () => { return (
); } }); return dfd.promise; } const service: iObjectServicePicker = (serviceConfig: iObjectServicePickConfig): any => serviceConfig.multiple ? multiple(serviceConfig) : single(serviceConfig); return service; } type iObjectServicePicker = ((config: iObjectServicePickSingleConfig & { clear?: false }) => Promise) & ((config: iObjectServicePickSingleConfig & { clear: true }) => Promise) & ((config: iObjectServicePickMultipleConfig & { clear?: false }) => Promise) & ((config: iObjectServicePickMultipleConfig & { clear: true }) => Promise) export type iObjectService = ReturnType