UNPKG

ze-react-component-library

Version:
1,683 lines (1,393 loc) 58.7 kB
import "antd/es/message/style"; import _message from "antd/es/message"; import "antd/es/card/style"; import _Card from "antd/es/card"; import "antd/es/cascader/style"; import _Cascader from "antd/es/cascader"; import "antd/es/spin/style"; import _Spin from "antd/es/spin"; import "antd/es/input/style"; import _Input from "antd/es/input"; import "antd/es/image/style"; import _Image from "antd/es/image"; import "antd/es/select/style"; import _Select from "antd/es/select"; import "antd/es/space/style"; import _Space from "antd/es/space"; import "antd/es/auto-complete/style"; import _AutoComplete from "antd/es/auto-complete"; import "antd/es/switch/style"; import _Switch from "antd/es/switch"; import "antd/es/input-number/style"; import _InputNumber from "antd/es/input-number"; import "antd/es/radio/style"; import _Radio from "antd/es/radio"; import "antd/es/tag/style"; import _Tag from "antd/es/tag"; import "antd/es/typography/style"; import _Typography from "antd/es/typography"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = this && this.__spreadArray || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) { to[j] = from[i]; } return to; }; import React, { Fragment } from "react"; import { getHierarchyCodeLength, getIDProperty, findPropByName, getNameProperty, isRelativeDateForm, normaliseRelativeDateForm } from "zeroetp-api-sdk"; import flatten from "flat"; import numeral from "numeral"; import { ObjectFormRenderItem, ObjectArrayFormRenderItem } from "./components/ObjectFormRenderItem"; import DragSortEditableTable from "./components/DragSortEditableTable"; import { useRequest } from "@umijs/hooks"; import { requestLogicform } from "./request"; import "antd/lib/cascader/style/index"; import ImageUpload from "./ImageUpload"; import { leastCommonFormatterAndUnit } from "./formatNumeral"; import Entity from "./components/Entity"; import moment from "moment"; import ZEJsonEditor from "./ZEJsonEditor"; import ErrorDisplayer from "./ZECard/ErrorDisplayer"; import CheckboxGroup from "./components/CheckboxGroup"; import Cron from "./components/Cron"; import Rule from "./components/Rule"; import { getFilterValue, revertFilterValue } from "./ZEUtil/rule.util"; var useMobileDetect = require("use-mobile-detect-hook"); var Paragraph = _Typography.Paragraph; var findProFieldKey = function findProFieldKey(props) { var res = (props === null || props === void 0 ? void 0 : props.proFieldKey) || ("" + ((props === null || props === void 0 ? void 0 : props.id) || "")).split("_").reverse()[0]; return res; }; /** * 相比zeroetp-api-sdk里面的findPropByName,多了对.号的predChain的支持 * @param schema * @param propName * @returns */ export var findProperty = function findProperty(schema, propName, splitter) { if (splitter === void 0) { splitter = "."; } // 前端的predChain,要获取正确的property var property; if (propName.indexOf(splitter) > 0) { var chain = propName.split(splitter); var currentSchema = schema; var _loop_1 = function _loop_1(chainItem) { property = currentSchema.properties.find(function (p) { return chainItem.startsWith(p.name); }); if (property) { currentSchema = property.schema; if (!currentSchema) return "break"; } }; for (var _i = 0, chain_1 = chain; _i < chain_1.length; _i++) { var chainItem = chain_1[_i]; var state_1 = _loop_1(chainItem); if (state_1 === "break") break; } if (property) { // 记得要改个名字 property = __assign(__assign({}, property), { name: propName }); } } else { try { // 可以找不到属性 property = findPropByName(schema, propName); } catch (error) {} } return property; }; export var valueTypeMapping = function valueTypeMapping(property) { var _a, _b, _c; if (property.type === "tag" && ((_b = (_a = property === null || property === void 0 ? void 0 : property.constraints) === null || _a === void 0 ? void 0 : _a.enum) === null || _b === void 0 ? void 0 : _b.length) > 0) { return "checkboxGroup"; } if (property.isArray && (property.is_name || property.is_categorical)) { return "tag"; } switch (property.type) { case "currency": return "number"; // 不用money,不然导出Excel之后,这些列无法被操作 case "percentage": return "percentage"; case "object": return "object"; case "json": return "json"; case "boolean": return "boolean"; case "image": return "image"; case "text": return "textarea"; case "file": return "file"; case "tag": return "tag"; case "url": return "url"; case "cron": return "cron"; default: break; } switch (property.primal_type) { case "date": if (property.granularity) { switch (property.granularity) { case "second": case "minute": case "hour": return "dateTime"; case "day": return "date"; case "week": case "month": // case "quarter": case "year": return "date" + property.granularity.toUpperCase().substring(0, 1) + property.granularity.substring(1); } } return "date"; case "number": return "number"; case "string": var distincts = (_c = property.constraints) === null || _c === void 0 ? void 0 : _c.enum; if (distincts && (distincts === null || distincts === void 0 ? void 0 : distincts.length) > 0) { if (distincts.length >= 10) { return "select"; } return "radio"; } if (property.is_categorical) { return "category"; } return "string"; default: return property.primal_type; } }; export var valueEnumMapping = function valueEnumMapping(property) { var _a, _b, _c, _d; var valueEnum = undefined; if (property.primal_type === "boolean") { valueEnum = { true: { text: "是" }, false: { text: "否" } }; } else if ((_a = property.constraints) === null || _a === void 0 ? void 0 : _a.enum) { valueEnum = {}; (_b = property.constraints) === null || _b === void 0 ? void 0 : _b.enum.forEach(function (enumItem) { var enumValue = Array.isArray(enumItem) ? enumItem[0] : enumItem; var enumText = property.constraints.enumText || {}; // 显示在UI上的 valueEnum[enumValue] = { text: enumText[enumValue] || enumValue }; }); } else if (((_c = property.stats) === null || _c === void 0 ? void 0 : _c.distincts) && ((_d = property.stats) === null || _d === void 0 ? void 0 : _d.distincts.length) <= 30) { valueEnum = {}; property.stats.distincts.forEach(function (enumItem) { if ((enumItem === null || enumItem === void 0 ? void 0 : enumItem.length) > 0) { valueEnum[enumItem] = { text: enumItem }; } }); } return valueEnum; }; export var isSearchFilter = function isSearchFilter(property) { var _a, _b, _c; return (property.primal_type === "string" || property.primal_type === "object") && !(((_a = property.stats) === null || _a === void 0 ? void 0 : _a.distincts) && ((_c = (_b = property.stats) === null || _b === void 0 ? void 0 : _b.distincts) === null || _c === void 0 ? void 0 : _c.length) <= 30); }; export var getFormatter = function getFormatter(property) { var _a, _b, _c, _d; // 单个的formatter优先。因为table模式下要决定单个formatter if ((_a = property.ui) === null || _a === void 0 ? void 0 : _a.formatter) { return { formatter: (_b = property.ui) === null || _b === void 0 ? void 0 : _b.formatter, prefix: "", postfix: "" }; } if ((_c = property.ui) === null || _c === void 0 ? void 0 : _c.formatters) { // 拿第一个 var formatter = ((_d = property.ui) === null || _d === void 0 ? void 0 : _d.formatters)[0]; if (formatter) { if (!formatter.prefix) formatter.prefix = ""; if (!formatter.postfix) formatter.postfix = ""; return formatter; } } return null; }; export var getFormatterString = function getFormatterString(property) { var formatter = getFormatter(property); if (formatter) { return formatter.formatter; } if (property.type === "percentage") { return "0.0%"; } if (property.primal_type === "number") { return "0,0"; } return null; }; export var autoUnitForData = function autoUnitForData(resData, t) { var _a; if (t === void 0) { t = function t(s) { return s; }; } // Feat: auto unit var autoUnitProperties = resData.columnProperties.filter(function (property) { var _a, _b; return property.primal_type === "number" && ((_a = property.ui) === null || _a === void 0 ? void 0 : _a.formatter) && ((_b = property.ui) === null || _b === void 0 ? void 0 : _b.formatter.toLowerCase().endsWith("a")); }); if (autoUnitProperties.length > 0) { var autoUnitResults = []; if ("drilldown" in resData) { autoUnitResults = autoUnitProperties.map(function (property) { return resData.drilldown.map(function (item) { var _a; return (_a = item === null || item === void 0 ? void 0 : item.result) === null || _a === void 0 ? void 0 : _a.map(function (i) { return i[property.name]; }); }); }).flat(10); } else { autoUnitResults = autoUnitProperties.map(function (property) { return resData.result.map(function (i) { return i[property.name]; }); }).flat(); } var lcf_1 = leastCommonFormatterAndUnit(autoUnitResults, (_a = autoUnitProperties[0].ui) === null || _a === void 0 ? void 0 : _a.formatter.charAt(autoUnitProperties[0].ui.formatter.length - 1), t); autoUnitProperties.forEach(function (property) { property.ui.formatter = "" + property.ui.formatter.substring(0, property.ui.formatter.length - 1) + lcf_1.formatter; if (!property.unit) property.unit = ""; property.unit = "" + lcf_1.unit + property.unit; }); } }; export var formatWithProperty = function formatWithProperty(property, value) { if (!property) { return value; } if (value === null) return "-"; var formatter = getFormatter(property); if (property.type === "object" && _typeof(value) === "object") { var nameProperty = getNameProperty(property.schema); return value[nameProperty.name]; } if (property.primal_type === "date") { if (formatter) { return moment(value).format(formatter.formatter); } } if (typeof value === "string") return value; if (formatter) { return numeral(value).format(formatter.formatter); } if (property.type === "percentage") { return numeral(value).format("0.0%"); } if (property.primal_type === "number") { return numeral(value).format("0,0"); } return value; }; var getPropNameFromProFieldKey = function getPropNameFromProFieldKey(key) { if (!(key === null || key === void 0 ? void 0 : key.includes("-"))) { return key; } return key.split("-").pop(); }; var tagRender = function tagRender(tagProps) { var label = tagProps.label, closable = tagProps.closable, onClose = tagProps.onClose; return /*#__PURE__*/React.createElement(_Tag, { color: "blue", onMouseDown: function onMouseDown(event) { event.preventDefault(); event.stopPropagation(); }, closable: closable, onClose: onClose, style: { marginRight: 3 } }, label); }; // config里面目前有table的defaultColWidth.用于计算object的ellipsis。 // TODO:上述解决方案很不行。如果有更好的解决方案就好了。 export var customValueTypes = function customValueTypes(schema, entityTooltipCardProps) { return { percentage: { render: function render(number, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([number, props], rest)); } var property; if (props) { var propName = getPropNameFromProFieldKey(findProFieldKey(props)); property = findPropByName(schema, propName); } if (!property) { property = { type: "percentage" }; } return formatWithProperty(property, number); }, renderFormItem: function renderFormItem(text, props, form) { var _a; if (props.renderFormItem) { return props.renderFormItem(text, props, form); } var propName = getPropNameFromProFieldKey(findProFieldKey(props)); var property = findPropByName(schema, propName); // enum // 对于其他number类型的props,也要有enum的支持,这个之后用到了再说 if ((_a = property.constraints) === null || _a === void 0 ? void 0 : _a.enum) { return /*#__PURE__*/React.createElement(_Radio.Group, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps), property.constraints.enum.map(function (item) { return /*#__PURE__*/React.createElement(_Radio, { key: "" + item, value: item }, item * 100, "%"); })); } return /*#__PURE__*/React.createElement(_InputNumber, __assign({ min: 0, max: 1, step: 0.01, formatter: function formatter(value) { return value * 100 + "%"; }, parser: function parser(value) { return parseFloat(value.replace("%", "")) / 100; } }, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, object: { render: function render(entity, props) { var _a, _b, _c, _d; var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } var isMobile = (_a = useMobileDetect()) === null || _a === void 0 ? void 0 : _a.isMobile(); if (props.render) { return props.render.apply(props, __spreadArray([entity, props], rest)); } if (entity == null) return "N/A"; var width = (((_b = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _b === void 0 ? void 0 : _b.width) || 200) - 16; var text; var isSingleEntity = true; var propName = getPropNameFromProFieldKey(findProFieldKey(props)); var property = findPropByName(schema, propName); // 哪里错了,先显示'-' if (!property.schema || !property.schema.properties) { return "-"; } var entityNameProperty = getNameProperty(property.schema); var entityIDProperty = getIDProperty(property.schema); if (typeof entity === "string") { text = entity; } else { if (!property.isArray) { text = entity[entityNameProperty.name]; } else { isSingleEntity = false; text = entity.map(function (i) { return i ? i[entityNameProperty.name] : "" + i; }); } } var entityProps = { property: property, entityNameProperty: entityNameProperty, entityIDProperty: entityIDProperty, entityTooltipCardProps: entityTooltipCardProps, entity: entity }; var content = /*#__PURE__*/React.createElement(React.Fragment, null, text instanceof Array ? /*#__PURE__*/React.createElement(React.Fragment, null, text.map(function (t, i) { return /*#__PURE__*/React.createElement(Entity, __assign({ key: t }, entityProps, { entity: entity[i] }), t, i === text.length - 1 ? "" : ","); })) : /*#__PURE__*/React.createElement(Entity, __assign({}, entityProps), text)); if (isMobile) { return /*#__PURE__*/React.createElement("div", { style: { width: width, overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis" } }, content); } return /*#__PURE__*/React.createElement("div", { style: { display: "flex", width: "100%" } }, /*#__PURE__*/React.createElement(Paragraph, { ellipsis: { rows: ((_d = (_c = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _c === void 0 ? void 0 : _c.ellipsis) === null || _d === void 0 ? void 0 : _d.row) || 1, expandable: false, tooltip: text instanceof Array ? text.join(",") : text }, style: { margin: 0, width: width, maxWidth: "100%", flexGrow: 1, flexShrink: 0 } }, content)); // 下面解决方案不太成熟,先关了。 // 如果用下面的话,上面的文字应该是ellipsis的,但是不弹出tooltip // if (!isSingleEntity || !entityNameProperty || !property) { // } // return ( // <Popover // overlayStyle={{ width: "50%" }} // placement="top" // arrowPointAtCenter // zIndex={999} // content={ // <ZECard // logicform={{ // operator: "$ent", // field: entityNameProperty.name, // name: text, // schema: property.schema._id, // }} // showMainContentOnly // /> // } // > // {ellipsisedText} // </Popover> // ); }, renderFormItem: function renderFormItem(_text, props, form) { var _a; if (props.renderFormItem) { return props.renderFormItem(_text, props, form); } var propName = ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.propName) || getPropNameFromProFieldKey(findProFieldKey(props)); var property = findPropByName(schema, propName); if (property.isArray) { return /*#__PURE__*/React.createElement(ObjectArrayFormRenderItem, { schema: property.schema, props: props }); } // Object有两种表现模式,带Hierarchy的和不带Hierarchy的 if (property.schema.hierarchy) { return renderObjectFormItemHierarchy(property, props); } return /*#__PURE__*/React.createElement(ObjectFormRenderItem, { schema: property.schema, props: props, queryProperty: getNameProperty(property.schema) }); } }, category: { renderFormItem: function renderFormItem(_text, props, form) { var _a; if (props.renderFormItem) { return props.renderFormItem(_text, props, form); } var propName = ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.propName) || getPropNameFromProFieldKey(findProFieldKey(props)); var property = findPropByName(schema, propName); return /*#__PURE__*/React.createElement(ObjectFormRenderItem, { schema: schema, props: props, groupby: property.name, hideIdInLabel: true, type: "autocomplete", queryProperty: property }); }, render: function render(v) { return v; } }, boolean: { render: function render(v, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } return /*#__PURE__*/React.createElement("div", null, v ? "是" : "否"); }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } var _a = (props === null || props === void 0 ? void 0 : props.fieldProps) || {}, value = _a.value, fieldProps = __rest(_a, ["value"]); return /*#__PURE__*/React.createElement(_Switch, __assign({}, fieldProps, { checked: value })); } }, table: { renderFormItem: function renderFormItem(_text, props, form) { if (props.renderFormItem) { return props.renderFormItem(_text, props, form); } return /*#__PURE__*/React.createElement(DragSortEditableTable, __assign({ columns: props.columns }, props.fieldProps)); } }, number: { render: function render(number, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } // 如果column传入了render,优先级应该最高 if (props.render) { return props.render.apply(props, __spreadArray([number, props], rest)); } var property; if (props) { var propName = getPropNameFromProFieldKey(findProFieldKey(props)); property = findPropByName(schema, propName); } if (!property) { property = { primal_type: "number" }; } return formatWithProperty(property, number); }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } return /*#__PURE__*/React.createElement(_InputNumber, __assign({ placeholder: props === null || props === void 0 ? void 0 : props.placeholder }, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, // 用户自定义的render具有最高优先级 string: { render: function render(v, props) { var _a; var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } if (v instanceof Array) { return v.join(((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.delimiter) || " "); } return v; }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } return /*#__PURE__*/React.createElement(_AutoComplete, __assign({ placeholder: props.placeholder || "请输入" }, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, tag: { render: function render(v, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } if (v instanceof Array) { return /*#__PURE__*/React.createElement(_Space, { size: "small" }, v.map(function (d) { return /*#__PURE__*/React.createElement(Fragment, { key: d }, tagRender({ label: d, closable: false })); })); } return v; }, renderFormItem: function renderFormItem(text, props, form) { var _a; if (props.renderFormItem) { return props.renderFormItem(text, props, form); } var options = Object.entries((_a = props.valueEnum) !== null && _a !== void 0 ? _a : {}).map(function (_a) { var k = _a[0], v = _a[1]; return { label: v === null || v === void 0 ? void 0 : v.text, value: k }; }); return /*#__PURE__*/React.createElement(_Select, __assign({ placeholder: props.placeholder || "请输入", mode: "tags", tagRender: tagRender, options: options }, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, checkboxGroup: { render: function render(v, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } if (v instanceof Array) { v.join(","); } return v; }, renderFormItem: function renderFormItem(text, props, form) { var _a; if (props.renderFormItem) { return props.renderFormItem(text, props, form); } var options = Object.entries((_a = props.valueEnum) !== null && _a !== void 0 ? _a : {}).map(function (_a) { var k = _a[0], v = _a[1]; return k; }); return /*#__PURE__*/React.createElement(CheckboxGroup, __assign({ options: options }, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, image: { render: function render(v, props) { var _a; var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } return /*#__PURE__*/React.createElement(_Image, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps, { src: v, width: ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.width) || 32 })); }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } return /*#__PURE__*/React.createElement(ImageUpload, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, file: { render: function render(v, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } if (v) { return /*#__PURE__*/React.createElement("a", { href: v, target: "_blank" }, "\u70B9\u51FB\u4E0B\u8F7D"); } return "-"; }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } return /*#__PURE__*/React.createElement(ImageUpload, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps, { type: "file" })); } }, url: { render: function render(v, props) { var rest = []; for (var _i = 2; _i < arguments.length; _i++) { rest[_i - 2] = arguments[_i]; } if (props.render) { return props.render.apply(props, __spreadArray([v, props], rest)); } if (v) { return /*#__PURE__*/React.createElement("a", { href: v, target: "_blank" }, "\u70B9\u51FB\u8BBF\u95EE"); } return "-"; }, renderFormItem: function renderFormItem(text, props, form) { if (props.renderFormItem) { return props.renderFormItem(text, props, form); } return /*#__PURE__*/React.createElement(_Input, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps)); } }, json: { render: function render(v) { if (typeof v === "string") { return JSON.stringify(JSON.parse(v), null, 2); } return v; }, renderFormItem: function renderFormItem(v, props, form) { var _a, _b; var newV = v; if (typeof v === "string") { if (v.length === 0) { newV = null; } else { try { newV = JSON.parse(v); } catch (error) { newV = { error: "原始json有错误", json: v }; } } } if ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.value) { props.fieldProps.value = newV; } return (_b = ZEJsonEditor.ZEJsonEditorRender) === null || _b === void 0 ? void 0 : _b.renderFormItem(newV, props, form); } }, rule: { render: function render(v) { if (typeof v === "string") { return JSON.stringify(JSON.parse(v), null, 2); } return v; }, renderFormItem: function renderFormItem(v, props, form) { var _a; var newV = v; if (typeof v === "string") { if (v.length === 0) { newV = null; } else { try { newV = JSON.parse(v); } catch (error) { newV = { error: "原始json有错误", json: v }; } } } if ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.value) { props.fieldProps.value = newV; } return /*#__PURE__*/React.createElement(Rule, __assign({}, props.fieldProps)); } }, cron: { render: function render(v) { return v; }, renderFormItem: function renderFormItem(v, props, dom) { return /*#__PURE__*/React.createElement(Cron, __assign({}, props.fieldProps)); } } }; }; // 带Hierarchy的,渲染object类型的 var renderObjectFormItemHierarchy = function renderObjectFormItemHierarchy(property, props) { var _a, _b, _c; var schema = property.schema; var entityNameProperty = getNameProperty(schema); // 设定其实的cascade的level var startCodeLength = 1; if ((_a = property.ui) === null || _a === void 0 ? void 0 : _a.startLevel) { startCodeLength = getHierarchyCodeLength(schema, property.ui.startLevel); } var _d = useRequest(function () { var _a; var _b; var query = {}; var idProperty = getIDProperty(schema); if (property.level) { var codeLength = getHierarchyCodeLength(schema, property.level); if (codeLength > 0) { query[idProperty.name] = { $regex: "^.{" + startCodeLength + "," + codeLength + "}$" }; } } return requestLogicform({ schema: schema._id, limit: -1, query: query, sort: (_a = {}, _a[(_b = idProperty === null || idProperty === void 0 ? void 0 : idProperty.name) !== null && _b !== void 0 ? _b : "_id"] = 1, _a) }); }, { formatResult: function formatResult(res) { // 注意,这套转化算法的前提是,服务器 var result = res.result; if (result.length === 0) return []; var cascaded = []; var cascadedMap = {}; result.forEach(function (item) { var option = { value: item._id, label: item[entityNameProperty.name], children: [] }; cascadedMap[option.value] = option; // 第一个item if (cascaded.length === 0) { cascaded.push(option); return; } // 其他与第一个item平级的 if (cascaded[0].value.length === item._id.length) { cascaded.push(option); return; } // 计算parentID的length var parentIDLength = schema.hierarchy[0].code_length; for (var i = 1; i < schema.hierarchy.length; i++) { var levelLength = schema.hierarchy[i].code_length; if (parentIDLength + levelLength >= item._id.length) { break; } parentIDLength += levelLength; } var parentID; if (parentIDLength < item._id.length) { parentID = item._id.substring(0, parentIDLength); } if (!parentID) { cascaded.push(option); return; } if (cascadedMap[parentID]) { cascadedMap[parentID].children.push(option); } else {// 跳脱了原来的层次结构。目前出现于香港和澳门 // console.log(option); // console.log(parentID); // console.log(cascadedMap); } }); return cascaded; }, initialData: [] }), data = _d.data, loading = _d.loading; // Cascader组件所维护的值是一个数组,这里要做一些改造 // 设定value var value = undefined; if ((_b = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _b === void 0 ? void 0 : _b.value) { value = (_c = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _c === void 0 ? void 0 : _c.value; if (!Array.isArray(value) && _typeof(value) === "object") { value = value._id; } var newValue_1 = []; var totalCodeLength_1 = 0; schema.hierarchy.forEach(function (h) { totalCodeLength_1 += h.code_length; if (startCodeLength <= totalCodeLength_1) { var v = value.slice(0, totalCodeLength_1); if (v && !newValue_1.includes(v)) { newValue_1.push(v); } } }); value = newValue_1; } return /*#__PURE__*/React.createElement(_Spin, { spinning: loading }, /*#__PURE__*/React.createElement(_Cascader, __assign({}, props === null || props === void 0 ? void 0 : props.fieldProps, { expandTrigger: "hover", options: data, showSearch: true, value: value, onChange: function onChange(val) { var _a; if ((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.onChange) { props.fieldProps.onChange(val[val.length - 1]); } } }))); }; // 主要是把chained的query给弄回来,方便显示和做filter change // 支持多层 export var unnormalizeQuery = function unnormalizeQuery(query, withoutEntityQuery) { var newQuery = {}; var unnormalize = function unnormalize(value, key, entityQuery) { if (key === void 0) { key = ""; } if (value === null) { // 全局筛选器会用 newQuery[key] = value; } else if (_typeof(value) === "object") { var keys = Object.keys(value); if (keys.some(function (o) { return o.startsWith("$"); })) { if (key) { var operator = keys.filter(function (k) { return k.startsWith("$"); })[0]; if (operator === "$entityQuery" && withoutEntityQuery) { newQuery[key] = value.value; } else if (entityQuery && ["$eq", "$in"].includes(operator) && !withoutEntityQuery) { newQuery[key] = { value: value, $entityQuery: entityQuery }; } else { newQuery[key] = value; } } else { keys.forEach(function (k) { if (k === "$or" || k === "$and") { if (!Array.isArray(value[k])) { throw new Error("Logicform\u683C\u5F0F\u6709\u8BEF\uFF0C" + k + "\u7684\u503C\u5E94\u4E3A\u6570\u7EC4\uFF0C\u8BF7\u8054\u7CFB\u6280\u672F\u4EBA\u5458\u6392\u67E5\u95EE\u9898\u3002"); } value[k].forEach(function (row) { return unnormalize(row); }); } else { newQuery[k] = value[k]; } }); } } else if (value.schema && value.query) { unnormalize(value.query, key, __assign(__assign({}, value), { key: key })); } else { Object.entries(value).forEach(function (_a) { var _b; var k = _a[0], v = _a[1]; unnormalize(v, [key, k].filter(function (f) { return f; }).join("_"), entityQuery ? __assign(__assign({}, entityQuery), { query: (_b = {}, _b[k] = v, _b) }) : undefined); }); } } else if (key) { if (entityQuery && !withoutEntityQuery) { newQuery[key] = { value: value, $entityQuery: entityQuery }; } else { newQuery[key] = value; } } }; unnormalize(query); return newQuery; }; export var genYoyAndMomLogicform = function genYoyAndMomLogicform(normedLF, op) { var retLF = JSON.parse(JSON.stringify(normedLF)); if (retLF.preds.length !== 1) throw new Error("LF的preds长度必须为1"); var predItem = retLF.preds[0]; if (!predItem.operator) throw new Error("predItem必须要有operator"); var name = predItem.name, query = predItem.query, pred = predItem.pred, operator = predItem.operator, others = __rest(predItem, ["name", "query", "pred", "operator"]); if (!operator.startsWith("$") || operator === "$count") { // 自定义函数 retLF.preds[0] = { operator: op, name: (name || operator) + "\u540C\u6BD4", query: query, pred: __assign(__assign({ name: name, operator: operator }, others), { pred: pred }) }; } else if (operator === "$sum") { retLF.preds[0] = { operator: op, name: name, query: query, pred: pred }; } else { throw new Error("genYoyAndMomLogicform\u6682\u4E0D\u652F\u6301\u7684operator: " + operator); } return retLF; }; export var ErrorFallBack = function ErrorFallBack(_a) { var error = _a.error, cardProps = _a.cardProps; return /*#__PURE__*/React.createElement("div", { className: "ze-card" }, /*#__PURE__*/React.createElement(_Card, __assign({}, cardProps), /*#__PURE__*/React.createElement(ErrorDisplayer, { error: error.message + "<br />\u5982\u9700\u5E2E\u52A9\uFF0C\u8BF7\u8054\u7CFB\u6280\u672F\u652F\u6301\u3002" }))); }; export var getColumnPublicProps = function getColumnPublicProps(p, col, propertyConfig) { var _a, _b; // valueType var valueType = "string"; if (propertyConfig && propertyConfig[p.name] && "valueType" in propertyConfig[p.name]) { valueType = propertyConfig[p.name].valueType; } else { valueType = valueTypeMapping(p); } if (col === null || col === void 0 ? void 0 : col.valueType) { valueType = col.valueType; } var valueEnum = valueEnumMapping(p); var valueOptions = valueEnum ? Object.keys(valueEnum).map(function (k) { var _a; return { label: ((_a = valueEnum[k]) === null || _a === void 0 ? void 0 : _a.text) || k, value: k }; }) : undefined; var colFieldProps = __assign(__assign({}, (col === null || col === void 0 ? void 0 : col.fieldProps) || {}), ((_a = propertyConfig === null || propertyConfig === void 0 ? void 0 : propertyConfig[p.name]) === null || _a === void 0 ? void 0 : _a.fieldProps) || {}); var fieldProps = valueType === "string" ? __assign({ options: valueOptions, delimiter: (_b = p.ui) === null || _b === void 0 ? void 0 : _b.delimiter }, colFieldProps) : colFieldProps; return { valueType: valueType, valueEnum: valueEnum, fieldProps: fieldProps, valueOptions: valueOptions }; }; export var getExportFileName = function getExportFileName(filename, suffix) { if (filename === void 0) { filename = "未命名"; } if (suffix === void 0) { suffix = ".xlsx"; } var date = moment().format("YYYY_MM_DD_HH_mm_SS"); var excelName = "" + (filename + " " + date) + suffix; return excelName; }; /** antd表格导出excel */ export function exportTableToExcel(parentNode, filename, XLSX // 外部的XLSX库 ) { if (!XLSX) return _message.error("需要传入XLSX库才能使用导出功能"); var nodes = parentNode.getElementsByTagName("table"); if (!nodes || nodes.length === 0) { return _message.error("表格不存在"); } var cloneNode = nodes[0].cloneNode(true); // header fixed的情况下,会有两个table if (nodes.length === 2) { var bodyNode = nodes[1].getElementsByTagName("tbody")[0].cloneNode(true); cloneNode.appendChild(bodyNode); } // 有时候ProTable会有一个hidden row,给删了。有时候没有 if (cloneNode.lastChild.firstChild.getAttribute("aria-hidden")) { cloneNode.lastChild.firstChild.remove(); } var wb = XLSX.utils.table_to_book(cloneNode, { display: true }); XLSX.writeFileXLSX(wb, getExportFileName(filename), { compression: true }); } export var typePropertyOfSchema = { name: "type", type: "string", primal_type: "string", constraints: { required: true, enum: ["ID", "timestamp", "name", "category", "string", "object", "tag", "number", "int", "currency", "percentage", "duration", "order", "boolean", "date", // "birthday", // "deathday", "image", "file" // "map", ], enumText: { timestamp: "时间戳", string: "字符串(仅显示,不参与问答)", name: "名称", category: "分类", object: "实体", tag: "标签", number: "数字", int: "整数", currency: "货币", percentage: "百分比", duration: "持续时间(秒)", order: "序号", boolean: "布尔值", date: "日期时间", // "birthday", // "deathday", image: "图像", file: "文件", // "map", report: "报表" } } }; export var getHierarchyDrillDownLevel = function getHierarchyDrillDownLevel(_currentLevel, result) { var _a, _b, _c, _d, _e, _f; var currentLevel = _currentLevel || ((_b = (_a = result.schema.hierarchy) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.name); var currentLevelIndex = (_d = (_c = result.schema.hierarchy) === null || _c === void 0 ? void 0 : _c.findIndex(function (d) { return d.name === currentLevel; })) !== null && _d !== void 0 ? _d : 0; var nextLevel = (_f = (_e = result.schema.hierarchy) === null || _e === void 0 ? void 0 : _e[currentLevelIndex + 1]) === null || _f === void 0 ? void 0 : _f.name; return nextLevel; }; export var getHierarchyListDrillDownLogicform = function getHierarchyListDrillDownLogicform(logicform, result, record) { var _a; var _b; var nextLevel = getHierarchyDrillDownLevel(record === null || record === void 0 ? void 0 : record["$level"], result); var parentIDLength = getHierarchyCodeLength(result.schema, (record === null || record === void 0 ? void 0 : record["$level"]) || result.schema.hierarchy[0].name); var idKey = (_b = getIDProperty(result === null || result === void 0 ? void 0 : result.schema)) === null || _b === void 0 ? void 0 : _b.name; if (idKey && nextLevel !== undefined) { return __assign(__assign({}, logicform), { query: (_a = {}, _a[idKey] = { $startsWith: record === null || record === void 0 ? void 0 : record[idKey].slice(0, parentIDLength), $level: nextLevel }, _a), limit: -1 }); } else { throw new Error("HierarchyList下钻参数获取失败"); } }; export var getPropertyQueryKey = function getPropertyQueryKey(name, schema, withName) { if (withName === void 0) { withName = true; } var property = findPropByName(schema, name); var targetKey = name; if ((property === null || property === void 0 ? void 0 : property.primal_type) === "object" && withName) { var namePropInRef = getNameProperty(property.schema); targetKey = name.split("(")[0] + "_" + namePropInRef.name; } return targetKey; }; export var filterValueOperators = ["$eq", "$ne", "$gt", "$lt", "$gte", "$lte", "$contains", "$in", "$nin"]; export var operatorDisplay = function operatorDisplay(o) { switch (o) { case "$eq": return "等于"; case "$ne": return "不等于"; case "$gt": return "大于"; case "$lt": return "小于"; case "$gte": return "大于等于"; case "$lte": return "小于等于"; case "$in": return "等于"; case "$nin": return "不等于"; case "$contains": return "包含"; default: return ""; } }; export var basicValueDisplay = function basicValueDisplay(oldV, t) { if (t === void 0) { t = function t(s) { return s; }; } if (_typeof(oldV) === "object" && oldV && "value" in oldV) { return basicValueDisplay(oldV.value, t); } if (_typeof(oldV) === "object" && oldV && "entity_id" in oldV) { return oldV.entity_id; } var v = oldV; if (v === undefined || v === null) { return t("全部"); } if (typeof v === "boolean") { return v ? t("是") : t("否"); } if (_typeof(v) !== "object") { return v; } if (v.$lte && isRelativeDateForm(v.$lte)) { v.$lte = normaliseRelativeDateForm(v.$lte); v.$lte = v.$lte.$lte; } if (v.$gte && isRelativeDateForm(v.$gte)) { v.$gte = normaliseRelativeDateForm(v.$gte); v.$gte = v.$gte.$gte; } if (isRelativeDateForm(v)) { v = normaliseRelativeDateForm(v); } if (typeof v.$lte === "number" || typeof v.$gte === "number") { if (typeof v.$lte === "number" && typeof v.$gte === "number") { return v.$gte + "~" + v.$lte; } return v.$gte || v.$lte; } if (v.$lte && v.$gte && (typeof v.$lte === "string" && moment(v.$lte).isValid() || v.$lte instanceof Date || v.$lte instanceof moment)) { var dateFormatString = "YYYY.MM.DD HH:mm:ss"; var startDate = moment(v.$gte); var endDate = moment(v.$lte); var startDateString = startDate.format(dateFormatString); var endDateString = endDate.format(dateFormatString).replace("00:00:00", "23:59:59"); var unit = ""; // 优化一下显示方式 if (startDate.isSame(moment(startDate).startOf("year"), "second") && endDate.isSame(moment(endDate).endOf("year"), "second")) { startDateString = startDate.format("YYYY"); endDateString = endDate.format("YYYY"); unit = t("年"); } else if (startDate.isSame(moment(startDate).startOf("month"), "second") && endDate.isSame(moment(endDate).endOf("month"), "second")) { startDateString = startDate.format("YYYY.MM"); endDateString = endDate.format("YYYY.MM"); } else if (startDateString.endsWith(" 00:00:00") && endDateString.endsWith(" 23:59:59")) { startDateString = startDateString.substring(0, 10); endDateString = endDateString.substring(0, 10); } if (startDateString === endDateString) { return "" + startDateString + unit; } return startDateString + " ~ " + endDateString + unit; } if (v.$in) { return basicValueDisplay(v.$in); } if (v.$and) { return basicValueDisplay(v.$and); } if (v.$nin) { return basicValueDisplay(v.$nin); } if (v instanceof Array) { return v.map(function (m) { return basicValueDisplay(m); }).join(" , "); } if (v.$contains) { return t("包含") + " " + v.$contains; } if (v.$level) { return t("级别") + " " + v.$level; } if (_typeof(v) === "object" && v.operator === "$ent") { return v.name; } if (_typeof(v) === "object" && v.entity_id) { return v.query[Object.keys(v.query)[0]]; } if (_typeof(v) === "object") { var key = Object.keys(v)[0]; return v[key]; } return ""; }; export var isHttps = function isHttps() { return window.location.protocol.startsWith("https"); }; export var logicformValueToColumnFilter = function logicformValueToColumnFilter(oldV, prop, nameProp) { var _a, _b, _c; var v = oldV; if (oldV && _typeof(oldV) === "object" && oldV.query && nameProp) { v = oldV.query[nameProp.name]; } if ([undefined, null].includes(v) || _typeof(v) !== "object") { if (typeof v === "boolean") { return JSON.stringify(v); } if (v) { if (prop && isSearchFilter(prop)) { return [v, true]; } return [v]; } return []; } if (_typeof(v) != "object") { return [v]; } if ((prop === null || prop === void 0 ? void 0 : prop.primal_type) === "date") { return [revertFilterValue(v)]; } if (v.$lte && isRelativeDateForm(v.$lte)) { v.$lte = normaliseRelativeDateForm(v.$lte); v.$lte = v.$lte.$lte; } if (v.$gte && isRelativeDateForm(v.$gte)) { v.$gte = normaliseRelativeDateForm(v.$gte); v.$gte = v.$gte.$gte; } if (isRelativeDateForm(v)) { v = normaliseRelativeDateForm(v); } if (typeof v.$lte === "number" || typeof v.$gte === "number" || typeof v.$lt === "number" || typeof v.$gt === "number") { return [(_a = v.$gt) !== null && _a !== void 0 ? _a : v.$gte, (_b = v.$lt) !== null && _b !== void 0 ? _b : v.$lte]; } if (v.$lte && v.$gte && (typeof v.$lte === "string" && moment(v.$lte).isValid() || v.$lte instanceof Date || v.$lte instanceof moment)) { return [moment(v.$gte).format("YYYY-MM-DD"), moment(v.$lte).format("YYYY-MM-DD")]; } if (v.$in) { if ((prop === null || prop === void 0 ? void 0 : prop.type) === "boolean" && v.$in instanceof Array) { return v.$in.map(function (m) { return String(m); }); } if (prop && isSearchFilter(prop)) { return [basicValueDisplay(v.$in), true];