UNPKG

vue-admin-core

Version:
634 lines (629 loc) 21.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue$1 = require('vue'); var vue = require('@formily/vue'); var reactiveVue = require('@formily/reactive-vue'); var shared = require('@formily/shared'); var elementPlus = require('element-plus'); var index$3 = require('../../space/src/index.js'); var index = require('../../array-base/src/index.js'); var index$2 = require('../../__builtins__/configs/index.js'); require('../../__builtins__/shared/index.js'); require('../../preview-text/index.js'); var reactive = require('@formily/reactive'); var eachTree = require('xe-utils/eachTree.js'); var lodashEs = require('lodash-es'); var defaultProps = require('element-plus/es/components/table/src/table/defaults'); var utils = require('../../__builtins__/shared/utils.js'); var resolveComponent = require('../../__builtins__/shared/resolve-component.js'); var index$1 = require('../../preview-text/src/index.js'); const arrayTableProps = { ...defaultProps, loading: { type: Boolean }, table: { type: Object }, pagination: { type: [Boolean, Object], default: false } }; const RecursionField = vue.RecursionField; const isColumnComponent = (schema) => { var _a; if (typeof schema["x-component"] !== "string") return false; return ((_a = schema["x-component"]) == null ? void 0 : _a.indexOf("Column")) > -1; }; const isOperationsComponent = (schema) => { var _a; if (typeof schema["x-component"] !== "string") return false; return ((_a = schema["x-component"]) == null ? void 0 : _a.indexOf("Operation")) > -1; }; const isAdditionComponent = (schema) => { var _a; if (typeof schema["x-component"] !== "string") return false; return ((_a = schema["x-component"]) == null ? void 0 : _a.indexOf("Addition")) > -1; }; const getDefaultRender = (schema) => { const content = schema["x-render"]; if (typeof content === "string") return content; if (typeof content === "function") return content; if (typeof content === "object" && !utils.isVueOptions(content) && content["default"]) return content["default"]; return void 0; }; const getArrayTableSources = (arrayFieldRef, schemaRef) => { const arrayField = arrayFieldRef.value; const parseSources = (schema) => { var _a, _b, _c; if (isColumnComponent(schema) || isOperationsComponent(schema) || isAdditionComponent(schema)) { if (!((_a = schema["x-component-props"]) == null ? void 0 : _a["prop"]) && !schema["name"]) return []; const name = ((_b = schema["x-component-props"]) == null ? void 0 : _b["prop"]) || schema["name"]; const field = arrayField.query(arrayField.address.concat(name)).take(); const fieldProps = (field == null ? void 0 : field.props) || schema.toFieldProps(); const columnProps = ((_c = field == null ? void 0 : field.component) == null ? void 0 : _c[1]) || schema["x-component-props"] || {}; const display = (field == null ? void 0 : field.display) || schema["x-display"] || "visible"; const required = schema.reduceProperties((required2, property) => { if (required2) { return required2; } return !!property.required; }, false); return [ { name, display, required, field, fieldProps, schema, columnProps, children: schema["x-table-header"] ? schema.reduceProperties((buf, schema2) => { return buf.concat(parseSources(schema2)); }, []) : void 0 } ]; } else if (schema.properties) { return schema.reduceProperties((buf, schema2) => { return buf.concat(parseSources(schema2)); }, []); } else { return []; } }; const parseArrayTable = (schema) => { if (!schema) return []; const sources = []; const items = shared.isArr(schema) ? schema : [schema]; return items.reduce((columns, schema2) => { const item = parseSources(schema2); if (item) { return columns.concat(item); } return columns; }, sources); }; if (!schemaRef.value) throw new Error("can not found schema object"); return parseArrayTable(schemaRef.value.items); }; const getArrayTableColumns = (sources) => { return sources.reduce( (buf, { name, columnProps, schema, display, required, field, children }, key) => { const { title, asterisk, ...props } = columnProps; if (display !== "visible") return buf; if (!isColumnComponent(schema) && !isOperationsComponent(schema)) return buf; if (isOperationsComponent(schema) && (field == null ? void 0 : field.pattern) !== "editable") return buf; const render = (startIndex) => { return (columnProps == null ? void 0 : columnProps.type) && (columnProps == null ? void 0 : columnProps.type) !== "default" && (columnProps == null ? void 0 : columnProps.type) !== "expand" ? void 0 : (props2) => { var _a; const index$2 = ((_a = startIndex == null ? void 0 : startIndex.value) != null ? _a : 0) + props2.$index; const children2 = vue$1.h( index.ArrayBase.Item, { index: index$2, record: props2.row, key: `${key}${index$2}` }, { default: () => { const defaultRender = getDefaultRender(schema); if (defaultRender) { return resolveComponent.resolveComponent(defaultRender, props2); } if (!schema.properties) { if (columnProps.valueType === "date") { return vue$1.h(index$1.PreviewText.DatePicker, { value: lodashEs.get(props2.row, name), format: columnProps.format || "YYYY-MM-DD" }); } else if (columnProps.valueType === "dateTime") { return vue$1.h(index$1.PreviewText.DatePicker, { value: lodashEs.get(props2.row, name) }); } else if (columnProps.valueType === "time") { return vue$1.h(index$1.PreviewText.TimePicker, { value: lodashEs.get(props2.row, name), format: columnProps.format }); } else if (columnProps.valueType === "currency") { return vue$1.h(index$1.PreviewText, { value: lodashEs.get(props2.row, name), format: columnProps.format || "$0,0.00" }); } else if (columnProps.valueType === "bytes") { return vue$1.h(index$1.PreviewText, { value: lodashEs.get(props2.row, name), format: columnProps.format || "0b" }); } else if (columnProps.valueType === "percent") { return vue$1.h(index$1.PreviewText, { value: lodashEs.get(props2.row, name), format: columnProps.format || "0%" }); } else if (columnProps.valueType === "address") { return vue$1.h(index$1.PreviewText.Cascader, { value: lodashEs.get(props2.row, name) }); } if (schema.enum) { return vue$1.h(index$1.PreviewText.Select, { value: lodashEs.get(props2.row, name), options: schema.enum, multiple: columnProps.multiple }); } return vue$1.h(index$1.PreviewText, { value: lodashEs.get(props2.row, name), format: columnProps.format }); } return vue$1.h( RecursionField, { schema, name: index$2, onlyRenderProperties: true }, {} ); } } ); return children2; }; }; const item = { label: title, ...props, key, prop: name, asterisk: asterisk != null ? asterisk : required, schema }; if (children && children.length > 0) { item.children = getArrayTableColumns(children); } else { item.render = render; } return buf.concat(item); }, [] ); }; const renderAddition = () => { const schema = vue.useFieldSchema(); return schema.value.reduceProperties((addition, schema2) => { if (isAdditionComponent(schema2) || schema2["x-addition"]) { return vue$1.h( RecursionField, { schema: schema2, name: "addition" }, {} ); } return addition; }, null); }; const createPagination = (options) => { const pagination = reactive.model({ current: (options == null ? void 0 : options.current) || 1, pageSize: (options == null ? void 0 : options.pageSize) || 10, pageSizes: (options == null ? void 0 : options.pageSizes) || [10, 20, 50, 100], total: (options == null ? void 0 : options.total) || 0, align: (options == null ? void 0 : options.align) || "right", onChange: options == null ? void 0 : options.onChange, setCurrent(current) { pagination.current = current; }, setPageSize(pageSize) { pagination.pageSize = pageSize; }, setPageSizes(pageSizes) { pagination.pageSizes = pageSizes; }, setTotal(total) { pagination.total = total; }, setAlign(align) { pagination.align = align; } }); return pagination; }; const createTable = () => { const table = { instance: null, expandRows: [], onInit(tableRef) { table.instance = tableRef; }, setExpandRow(row, expandedRows) { if (typeof expandedRows === "boolean") { if (expandedRows) { table.expandRows.push(row); } else { table.expandRows.splice(table.expandRows.indexOf(row), 1); } } else { table.expandRows = expandedRows; } }, clearSelection() { var _a; (_a = table.instance) == null ? void 0 : _a.clearSelection(); }, getSelectionRows() { var _a; return (_a = table.instance) == null ? void 0 : _a.getSelectionRows(); }, toggleRowSelection(row, selected) { var _a; (_a = table.instance) == null ? void 0 : _a.toggleRowSelection(row, selected); }, toggleAllSelection() { var _a; (_a = table.instance) == null ? void 0 : _a.toggleAllSelection(); }, toggleRowExpansion(row, expanded) { var _a; (_a = table.instance) == null ? void 0 : _a.toggleRowExpansion(row, expanded); }, expandAll(isTree) { var _a, _b, _c; const rows = []; if (isTree) { const { children, hasChildren } = (_a = table.instance) == null ? void 0 : _a.treeProps; eachTree( (_b = table.instance) == null ? void 0 : _b.data, (item) => { if (item[children] && item[children].length > 0 || item[hasChildren]) { rows.push(item); table.toggleRowExpansion(item, true); } }, { children } ); } else { lodashEs.each((_c = table.instance) == null ? void 0 : _c.data, (item) => { rows.push(item); table.toggleRowExpansion(item, true); }); } table.expandRows = rows; return rows; }, clearExpand() { lodashEs.each([...table.expandRows], (item) => { table.toggleRowExpansion(item, false); }); table.expandRows = []; }, setCurrentRow(row) { var _a; (_a = table.instance) == null ? void 0 : _a.setCurrentRow(row); }, clearSort() { var _a; (_a = table.instance) == null ? void 0 : _a.clearSort(); }, clearFilter(columnKeys) { var _a; (_a = table.instance) == null ? void 0 : _a.clearFilter(columnKeys); }, doLayout() { var _a; (_a = table.instance) == null ? void 0 : _a.doLayout(); }, sort(prop, order) { var _a; (_a = table.instance) == null ? void 0 : _a.sort(prop, order); }, scrollTo(options, yCoord) { var _a; (_a = table.instance) == null ? void 0 : _a.scrollTo(options, yCoord); }, setScrollTop(top) { var _a; (_a = table.instance) == null ? void 0 : _a.setScrollTop(top); }, setScrollLeft(left) { var _a; (_a = table.instance) == null ? void 0 : _a.setScrollLeft(left); } }; return table; }; const ArrayTablePagination = vue$1.defineComponent({ inheritAttrs: false, props: ["dataSource", "pagination"], setup(props, { attrs, slots }) { const prefixCls = `${index$2.stylePrefix}-array-table`; const dataSource = vue$1.computed(() => props.dataSource); const paginationRef = vue$1.computed(() => { var _a; return (_a = props.pagination) != null ? _a : createPagination(); }); const renderPagination = function() { var _a; const pagination = paginationRef.value; const current = pagination.current; const pageSize = pagination.pageSize; const pageSizes = pagination.pageSizes; const total = props.pagination ? pagination.total : (_a = dataSource.value) == null ? void 0 : _a.length; const totalPage = Math.ceil(total / pageSize); const align = pagination.align; if (totalPage <= 1) return; return vue$1.h( "div", { class: [`${prefixCls}-pagination`], style: { justifyContent: align } }, vue$1.h( index$3.Space, {}, { default: () => [ vue$1.h( elementPlus.ElPagination, { background: true, layout: "total, sizes, prev, pager, next, jumper", ...attrs, pageSize, pageSizes, pageCount: totalPage, currentPage: current, total, small: true, onCurrentChange: (val) => { pagination.setCurrent(val); }, onSizeChange: (val) => { reactive.batch(() => { pagination.setCurrent(1); pagination.setPageSize(val); }); } }, {} ) ] } ) ); }; return () => { const pagination = paginationRef.value; const startIndex = (pagination.current - 1) * pagination.pageSize; const endIndex = startIndex + pagination.pageSize - 1; return vue$1.h( vue.FragmentComponent, {}, { default: () => { var _a, _b; return (_b = slots == null ? void 0 : slots.default) == null ? void 0 : _b.call( slots, props.pagination ? dataSource.value : (_a = dataSource.value) == null ? void 0 : _a.slice(startIndex, endIndex + 1), renderPagination, props.pagination ? null : vue$1.toRef(startIndex) ); } } ); }; } }); const ArrayTableInner = reactiveVue.observer( vue$1.defineComponent({ name: "FArrayTable", inheritAttrs: false, props: arrayTableProps, setup(props, { attrs, slots }) { const fieldRef = vue.useField(); const schemaRef = vue.useFieldSchema(); const formRef = vue.useForm(); const prefixCls = `${index$2.stylePrefix}-array-table`; const { getKey, keyMap } = index.ArrayBase.useKey(schemaRef.value); const defaultRowKey = (record, index) => { return getKey(record, index); }; const tableRef = vue$1.computed(() => { var _a; return (_a = props.table) != null ? _a : createTable(); }); return () => { const field = fieldRef.value; const form = formRef.value; const dataSource = Array.isArray(field.value) ? field.value.slice() : []; const pagination = props.pagination; const sources = getArrayTableSources(fieldRef, schemaRef); const columns = getArrayTableColumns(sources); const renderColumns = (startIndex) => { const renderColumn = (columns2) => { return columns2.map( ({ key, render, asterisk, schema, children: headerChildren, ...props2 }) => { if (headerChildren) { return vue$1.h( elementPlus.ElTableColumn, { ...props2, key }, { default: () => renderColumn(headerChildren) } ); } const children = {}; if (render) { children.default = render(startIndex); } if (asterisk) { children.header = ({ column, $index }) => vue$1.h("span", {}, [ vue$1.h("span", { class: `${prefixCls}-inner-asterisk` }, "*"), schema["x-render"] && schema["x-render"]["header"] ? resolveComponent.resolveComponent(schema["x-render"]["header"], { column, $index }) : column.label ]); } else { if (schema["x-render"] && schema["x-render"]["header"]) { children.header = ({ column, $index }) => resolveComponent.resolveComponent(schema["x-render"]["header"], { column, $index }); } } return vue$1.h( elementPlus.ElTableColumn, { ...props2, key }, children ); } ); }; return renderColumn(columns); }; const renderStateManager = () => sources.map((column, key) => { if (!isColumnComponent(column.schema) && !isOperationsComponent(column.schema)) return; return vue$1.h( RecursionField, { name: column.name, schema: column.schema, onlyRenderSelf: true, key }, {} ); }); const renderTable = (dataSource2, pager, startIndex) => { return vue$1.h( "div", { class: prefixCls }, vue$1.h( index.ArrayBase, { keyMap }, { default: () => [ vue$1.withDirectives( vue$1.h( elementPlus.ElTable, { ...props, ...attrs, rowKey: props.rowKey ? props.rowKey : defaultRowKey, data: dataSource2, ref: (inst) => { tableRef.value.onInit(inst); field.inject({ getRef: () => inst }); }, onExpandChange: (row, expandedRows) => { var _a; tableRef.value.setExpandRow(row, expandedRows); (_a = attrs == null ? void 0 : attrs.onExpandChange) == null ? void 0 : _a.call(attrs, row, expandedRows); } }, { ...slots, default: () => renderColumns(startIndex) } ), [ [ elementPlus.ElLoading.directive, props.loading !== void 0 ? props.loading : (field == null ? void 0 : field.loading) || (form == null ? void 0 : form.submitting) ] ] ), pager == null ? void 0 : pager(), renderStateManager(), renderAddition() ] } ) ); }; if (!pagination) { return renderTable(dataSource); } return vue$1.h( ArrayTablePagination, { pagination: shared.isBool(pagination) ? null : pagination, dataSource }, { default: renderTable } ); }; } }) ); const ArrayTableColumn = { name: "FArrayTableColumn", render() { return null; } }; const ArrayTable = utils.composeExport(ArrayTableInner, { Column: ArrayTableColumn, Index: index.ArrayBase.Index, SortHandle: index.ArrayBase.SortHandle, Addition: index.ArrayBase.Addition, Remove: index.ArrayBase.Remove, MoveDown: index.ArrayBase.MoveDown, MoveUp: index.ArrayBase.MoveUp, useArray: index.ArrayBase.useArray, useIndex: index.ArrayBase.useIndex, useRecord: index.ArrayBase.useRecord, ButtonGroup: index.ArrayBase.ButtonGroup, Button: index.ArrayBase.Button, createPagination, createTable }); exports.ArrayTable = ArrayTable; exports.arrayTableProps = arrayTableProps; exports.default = ArrayTable; //# sourceMappingURL=index.js.map