/* * @Author: xiaodongyu * @Date 2019-11-29 23:27:11 * @Last Modified by: bowang * @Last Modified time: 2023-03-27 17:19:34 */ import {upload} from 'qiniu-js'; import type {Def, FormCtx, TableOptions} from '@yqg/type'; import Web from '../resource/web'; import {boolOf} from '../util/enum'; import DefType from './def-type'; export type CommonObject = Record; export const merge = (...args: T[]): T => Object.assign({}, ...args); export const required = (def: Def): Def => merge(def, {required: true}); export const primaryStaticProps: Def = {staticProps: {class: 'yqg-text-primary'}}; export const successStaticProps: Def = {staticProps: {class: 'yqg-text-success'}}; export const warningStaticProps: Def = {staticProps: {class: 'yqg-text-warning'}}; export const sizeLongProps: Def = {props: {style: {maxWidth: '100%', width: '500px'}}}; export const fixedLeft = (def: Def, width = 20): Def => merge(def, {column: {fixed: 'left', width}}); export const fixedRight = (def: Def, width = 20): Def => merge(def, {column: {fixed: 'right', width}}); // ant-form-item-control-wrapper占全行 export const wholeRow = (def: Def): Def => merge(def, {itemProps: {wrapperCol: {span: 24}}}); export const getPre = (prefix: string): (suffix: string) => string => suffix => `${prefix}${suffix}`; export const clearFields = (fields: Def['field'][]): (formCtx: FormCtx) => void => ({form}) => form.setFieldsValue(fields.reduce((acc, field) => ({ ...acc, [field]: undefined }), {})); type Checker = (value: any) => boolean; type ReturnStaticProps = Record<'class', Record> export const getSdTextStaticProps = (checker: Checker = boolOf, success = true): (defValueCtx: CommonObject) => ReturnStaticProps => ({value}) => { const valid = checker(value); return { class: { 'yqg-text-success': success && valid, 'yqg-text-danger': !valid } }; }; type ColunmStyleConfig = { minWidth?: number, maxWidth?: number, wrap?: boolean }; type ReturnColumnStyle = { style: CommonObject }; // yqg-simple-table的column默认不换行,此方法用来给长文本设置宽度和换行,放在staticProps里,slot不可用 export const genColumnWrapStyle = ({minWidth, maxWidth, wrap = false}: ColunmStyleConfig): ReturnColumnStyle => ({ style: { display: 'inline-block', minWidth: minWidth ? `${minWidth}px` : 'inherit', maxWidth: maxWidth ? `${maxWidth}px` : 'inherit', whiteSpace: wrap ? 'normal' : null, wordBreak: wrap ? 'break-all' : null } }); export const staticComp = (def: Def): Def => merge(def, {component: 'def-value'}); export const dateTimeDef: Def = {type: 'date', props: {showTime: true}, filter: 'dateTime'}; export const blockItem: Def = {itemProps: {class: 'yqg-form-item-block'}}; export const hiddenItem: Def = {itemProps: {class: 'yqg-form-item-hidden'}}; export const rangeItem: Def = {label: ' ', itemProps: {class: 'yqg-form-item-range'}}; export const op: Def = {field: 'op', label: 'common.op'}; export const id: Def = {field: 'id', label: 'ID'}; export const time: Def = {field: 'time', label: 'common.time', filter: 'dateTime'}; export const timeCreate: Def = {field: 'timeCreate', label: 'common.timeCreate', filter: 'dateTime'}; export const timeUpdate: Def = {field: 'timeUpdate', label: 'common.timeUpdate', filter: 'dateTime'}; export const timeCreated: Def = merge(timeCreate, {field: 'timeCreated'}); export const timeUpdated: Def = merge(timeUpdate, {field: 'timeUpdated'}); export const remark: Def = {field: 'remark', type: 'textarea', label: 'common.remark'}; export const numberCommasFixed2: Def = {filter: 'numberCommasFixed2'}; export type TableTitleItem = Record<'value'|'label'|'cellFilter', string> & CommonObject; export type ExtraDefMap = Record export const generateColumnDefs = (tableTitle: TableTitleItem[], extraDefMap: ExtraDefMap): Def[] => tableTitle.map(({ value: field, label, cellFilter: filter }) => ({ field, label, filter, ...(DefType[filter] ? {type: filter} : {}) as Pick, ...(extraDefMap && extraDefMap[field]) })); export type CommonTableOptions = { tableTitle: T, rowKey: string, op: boolean, extraDefMap: ExtraDefMap, extraCols: Def[] } & CommonObject; export const getCommonOptions: (options: CommonTableOptions) => TableOptions = ({ tableTitle, rowKey = 'id', op: flag = true, extraDefMap = {}, extraCols = [], ...rest }) => ({ rowKey, colDefs: [ ...generateColumnDefs(tableTitle, extraDefMap), ...(flag ? [op] : []), ...extraCols ], ...rest }); const customRequest = async (options: CommonObject): Promise => { const {file, onError, onProgress, onSuccess} = options; const {data: {body: token}} = await Web.getToken(); const observable = upload(file, null, token); observable.subscribe({ next: onProgress, error: onError, complete: onSuccess }); }; export const qiniuFileDef: Def = { type: 'file', props: {customRequest, listType: 'text', valueField: 'url', urlField: 'url'} }; export const qiniuImageDef: Def = { type: 'file', props: {customRequest, listType: 'picture', valueField: 'url', urlField: 'url'} };