UNPKG

@bos-alpha/data

Version:

数据管理

363 lines (362 loc) 21.7 kB
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; 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, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useState, useEffect } from 'react'; import { Input, Menu, Dropdown, Button, Switch } from 'antd'; import * as api from '@bos-alpha/common/lib/services'; import { Icon } from '@bos-alpha/common/lib/components'; import { AdvancedSearch } from '../advanced-search'; import moment from 'moment'; import styled from '@emotion/styled'; import { ResizableTable } from '@bos-alpha/common/lib/components/table/resizable-table'; import { InfoCard } from '../info-card'; import { prefixCls, Modal } from '@bos-alpha/common'; /** * * @param {classDefined} 当前类定义 * @param {attributes} 当前类模型属性 * @returns */ export var DocTable = function (_a) { var classDefined = _a.classDefined, attributes = _a.attributes; // 可选属性列 var _b = useState([]), attrList = _b[0], setAttrList = _b[1]; // 表格列 var _c = useState([]), columns = _c[0], setColumns = _c[1]; // 表格数据加载中 var _d = useState(false), loading = _d[0], setLoading = _d[1]; // 菜单是否显示 var _e = useState(false), visible = _e[0], setVisible = _e[1]; // 搜索框检索词 var _f = useState(''), keywords = _f[0], setKeywords = _f[1]; // 触发高级搜索重置 var _g = useState(), searchReset = _g[0], setSearchReset = _g[1]; // 检索参数 var _h = useState({ noRelation: true, page: 1, per_page: 10, conditions: [], keyword: '' }), searchParams = _h[0], setSearchParams = _h[1]; var _j = useState([]), tableData = _j[0], setTableData = _j[1]; var _k = useState(0), total = _k[0], setTotal = _k[1]; // 数据详情弹窗 var _l = useState(false), detailModalVisible = _l[0], setDetailModalVisible = _l[1]; // 数据详情 var _m = useState([]), detailInfo = _m[0], setDetailInfo = _m[1]; // 获取类数据列表 useEffect(function () { if (!classDefined) return; if (columns.length === 0) return; getDataList(); }, [searchParams]); // eslint-disable-line react-hooks/exhaustive-deps // 切换类 doc对象类 可选属性列 useEffect(function () { var list = attributes .filter(function (item) { return item.code !== 'bosclass'; }) .map(function (item) { item.checked = item.code === 'code'; return item; }); setAttrList(list); setSearchParams(__assign(__assign({}, searchParams), { page: 1, keyword: '' })); setKeywords(''); setSearchReset(undefined); }, [attributes]); // eslint-disable-line react-hooks/exhaustive-deps useEffect(function () { updateColumns(attrList); }, [attrList]); // eslint-disable-line react-hooks/exhaustive-deps // 数据格式转换 var dataTypeFix = function (data, dataCode, record) { // shares validTime时间类型 if (dataCode === 'validTime') { return data ? moment(data).format('YYYY-MM-DD HH:mm:ss') : '永久有效'; } // 文档升版时间 if (dataCode === 'gupgrade') { return data ? moment(data).format('YYYY-MM-DD HH:mm:ss') : ''; } // parentId可被点击 if (dataCode === 'parentId') { return (_jsx(Link, __assign({ onClick: function () { return dataKeyOnClick(record, true); } }, { children: data }), void 0)); } var type = typeof data; switch (type) { case 'string': case 'boolean': return "".concat(data); case 'undefined': return data; case 'object': return data === null ? '' : JSON.stringify(data); default: return data; } }; // 更新columns var updateColumns = function (list) { var bosclassObj = attributes.find(function (item) { return item.code === 'bosclass'; }); var base = baseColumns(bosclassObj); var appearKeys = attrList .filter(function (item) { return item.checked; }) .map(function (item) { return item.code; }); var appends = list .filter(function (item) { return appearKeys.indexOf(item.code) >= 0; }) .map(function (item) { return ({ dataIndex: item.code, ellipsis: true, minWidth: 70, width: 200, title: (_jsxs("div", { children: [_jsx("div", { children: item.code }, void 0), _jsxs("div", { children: ["(", item.description, ")"] }, void 0)] }, void 0)), render: function (val, record) { return dataTypeFix(val, item.code, record); } }); }); setColumns(__spreadArray(__spreadArray([], base, true), appends, true)); }; // 获取类数据 var getDataList = function () { return __awaiter(void 0, void 0, void 0, function () { var keyword, conditions, params, condition, className, res, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: keyword = searchParams.keyword, conditions = searchParams.conditions, params = __rest(searchParams, ["keyword", "conditions"]); condition = []; if (keyword) { condition.push({ field: '_key', operator: 'like', value: keyword, logic: 'and' }); } if (conditions.length) { condition = conditions; } className = classDefined.className; setLoading(true); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, api.queryEntity(className, { data: { condition: condition }, params: params })]; case 2: res = _a.sent(); setTableData(res.data); setTotal(res.count); return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); console.log(err_1); return [3 /*break*/, 4]; case 4: setLoading(false); return [2 /*return*/]; } }); }); }; // id被点击 var dataKeyOnClick = function (record, isParent) { return __awaiter(void 0, void 0, void 0, function () { var kvList, bosclass, parentId, res, list; return __generator(this, function (_a) { switch (_a.label) { case 0: kvList = record; if (!isParent) return [3 /*break*/, 2]; bosclass = record.bosclass, parentId = record.parentId; return [4 /*yield*/, api.getEntityKey(bosclass, parentId, { params: { noRelation: true } })]; case 1: res = _a.sent(); kvList = res; _a.label = 2; case 2: list = Object.keys(kvList) .sort(function (a, b) { return (a < b ? -1 : 1); }) .map(function (key) { var curLabel = attributes.find(function (item) { return item.code === key; }); var label = key; if (key === 'key') { label = 'id'; } if (curLabel) { label = curLabel.description ? "".concat(key, "(").concat(curLabel.description, ")") : key; if (key === 'key') { label = "id(".concat(curLabel.description, ")"); } } var value = kvList[key]; if (typeof value === 'object' && value !== null) { value = JSON.stringify(value); } return { label: label, value: value }; }); setDetailInfo(list); setDetailModalVisible(true); return [2 /*return*/]; } }); }); }; // 基础列 区分对象类 和 关系类 var baseColumns = function (bosclassObj) { return [ { dataIndex: 'bosclass', width: 200, minWidth: 70, title: (_jsxs("div", { children: [_jsx("div", { children: "bosclass" }, void 0), (bosclassObj === null || bosclassObj === void 0 ? void 0 : bosclassObj.description) ? (_jsxs("div", { children: ["(", bosclassObj === null || bosclassObj === void 0 ? void 0 : bosclassObj.description, ")"] }, void 0)) : null] }, void 0)) }, { dataIndex: 'key', title: 'id', width: 200, minWidth: 70, render: function (id, record) { return (_jsx(Link, __assign({ onClick: function () { return dataKeyOnClick(record); } }, { children: id }), void 0)); } } ]; }; // 选择显示列 var onAttrCheck = function (checked, record) { setAttrList(attrList.map(function (item) { if (item.code === record.code) { return __assign(__assign({}, item), { checked: checked }); } return item; })); }; // 高级搜索 var handleQuery = function (vals) { setSearchParams(__assign(__assign({}, searchParams), { page: 1, keyword: '', conditions: vals })); // 清空快捷搜索 关闭高级搜索 setKeywords(''); }; // 高级搜索下拉 var searchOptions = __spreadArray([ { label: 'id', value: '_key' } ], attributes.map(function (item) { return ({ label: item.code, value: item.code }); }), true); return (_jsxs("div", { children: [_jsxs(TopBar, { children: [_jsxs(Search, { children: [_jsx(Input.Search, { className: "ipt", placeholder: "\u901A\u8FC7ID\u5FEB\u901F\u68C0\u7D22", value: keywords, onInput: function (e) { return setKeywords(e.target.value); }, onSearch: function (val) { return setSearchParams(__assign(__assign({}, searchParams), { page: 1, keyword: val })); } }, void 0), _jsx(AdvancedSearch, { type: "doc", isReset: searchReset, fieldOptions: searchOptions, classDefined: classDefined, onQuery: handleQuery }, void 0), _jsx(Button, __assign({ type: "link", onClick: function (e) { setKeywords(''); setSearchReset(new Date().getTime()); } }, { children: "\u91CD\u7F6E" }), void 0)] }, void 0), _jsx(Dropdown, __assign({ overlay: _jsx(AttrMenu, { children: attrList.map(function (item, index) { return (_jsx(Menu.Item, { children: _jsxs(AttrItem, { children: [_jsxs("span", { children: [item.code, "(", item.description, ")"] }, void 0), _jsx(Switch, { checked: item.checked, onChange: function (checked) { return onAttrCheck(checked, item); } }, void 0)] }, void 0) }, index)); }) }, void 0), trigger: ['click'], visible: visible, onVisibleChange: setVisible }, { children: _jsxs(Button, __assign({ className: "attr-btn" }, { children: ["\u8BBE\u7F6E\u9700\u8981\u663E\u793A\u7684\u5C5E\u6027\u5217", _jsx(Icon, { type: "iconicon_arrowdown" }, void 0)] }), void 0) }), void 0)] }, void 0), _jsx(ResizableTable, { rowKey: function (record) { return record['key']; }, columns: columns, dataSource: tableData, loading: loading, style: { width: '100%' }, scroll: { x: 1000, y: 400 }, pagination: { size: 'small', total: total, pageSize: searchParams.per_page, current: searchParams.page, showSizeChanger: true, showQuickJumper: true, showTotal: function (total) { return "\u5171".concat(total, "\u6761\uFF0C\u5F53\u524D\u7B2C").concat(searchParams.page, "\u9875"); }, onChange: function (page, pageSize) { setSearchParams(__assign(__assign({}, searchParams), { page: page, per_page: pageSize })); } } }, void 0), _jsxs(Modal, __assign({ visible: detailModalVisible, title: "\u6570\u636E\u8BE6\u60C5", width: 814, onCancel: function () { return setDetailModalVisible(false); }, footer: null }, { children: [_jsx(InfoCard, { curClass: classDefined, isDetail: true }, void 0), detailInfo.map(function (item, index) { return (_jsxs(DetailRow, { children: [_jsx("div", __assign({ className: "detail-label" }, { children: item.label }), void 0), _jsx("div", __assign({ className: "detail-value", title: (item.value && item.value.toString()) || '' }, { children: item.value }), void 0)] }, index)); })] }), void 0)] }, void 0)); }; var Search = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n display: flex;\n margin-bottom: 16px;\n & > .ipt {\n width: 350px;\n margin-right: 15px;\n .", "-input-group > .", "-input:first-of-type {\n border-radius: 16px;\n border-bottom-right-radius: 16px;\n border-bottom-right-radius: 16px;\n position: relative;\n }\n .", "-input-group\n > .", "-input-group-addon:last-of-type {\n background: none;\n position: absolute;\n z-index: 2;\n left: inherit;\n right: 36px;\n .", "-btn {\n border: 0;\n background: none;\n }\n }\n }\n"], ["\n display: flex;\n margin-bottom: 16px;\n & > .ipt {\n width: 350px;\n margin-right: 15px;\n .", "-input-group > .", "-input:first-of-type {\n border-radius: 16px;\n border-bottom-right-radius: 16px;\n border-bottom-right-radius: 16px;\n position: relative;\n }\n .", "-input-group\n > .", "-input-group-addon:last-of-type {\n background: none;\n position: absolute;\n z-index: 2;\n left: inherit;\n right: 36px;\n .", "-btn {\n border: 0;\n background: none;\n }\n }\n }\n"])), prefixCls, prefixCls, prefixCls, prefixCls, prefixCls); var Link = styled.span(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n color: #2878ff;\n cursor: pointer;\n"], ["\n color: #2878ff;\n cursor: pointer;\n"]))); var AttrMenu = styled(Menu)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n max-height: 300px;\n overflow-y: auto;\n"], ["\n max-height: 300px;\n overflow-y: auto;\n"]))); var TopBar = styled.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n display: flex;\n justify-content: space-between;\n .attr-btn {\n color: rgba(0, 0, 0, 0.65);\n }\n"], ["\n display: flex;\n justify-content: space-between;\n .attr-btn {\n color: rgba(0, 0, 0, 0.65);\n }\n"]))); var AttrItem = styled.div(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n display: flex;\n justify-content: space-between;\n"], ["\n display: flex;\n justify-content: space-between;\n"]))); var DetailRow = styled.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n display: flex;\n height: 40px;\n line-height: 40px;\n color: rgba(0, 0, 0, 0.65);\n border-bottom: 1px solid #e8e8e8;\n & > .detail-label {\n width: 30%;\n font-weight: 500;\n padding-left: 16px;\n color: rgba(0, 0, 0, 0.85);\n font-weight: 500;\n background: #fafafa;\n }\n & > .detail-value {\n background-color: #fff;\n width: 70%;\n padding-left: 16px;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n }\n"], ["\n display: flex;\n height: 40px;\n line-height: 40px;\n color: rgba(0, 0, 0, 0.65);\n border-bottom: 1px solid #e8e8e8;\n & > .detail-label {\n width: 30%;\n font-weight: 500;\n padding-left: 16px;\n color: rgba(0, 0, 0, 0.85);\n font-weight: 500;\n background: #fafafa;\n }\n & > .detail-value {\n background-color: #fff;\n width: 70%;\n padding-left: 16px;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n }\n"]))); var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6;