@bos-alpha/data
Version:
数据管理
160 lines (159 loc) • 9.31 kB
JavaScript
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 __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 } from 'react';
import { Select, message, Table } from 'antd';
import styled from '@emotion/styled';
import { uuid, prefixCls, Icon, Modal } from '@bos-alpha/common';
import { ConditionsMaker } from './conditions-maker';
var Option = Select.Option;
export var JoinsList = function (_a) {
var joinList = _a.joinList, apiList = _a.apiList, mainInfo = _a.mainInfo, onCheckForm = _a.onCheckForm, setJoinList = _a.setJoinList;
// 从连接(关联数据) 可多个
var _b = useState(false), visible = _b[0], setVisible = _b[1];
var _c = useState({}), editInfo = _c[0], setEditInfo = _c[1];
// 添加新连接
var addRow = function (isInit) {
setJoinList(__spreadArray(__spreadArray([], (isInit ? [] : joinList), true), [
{
uid: uuid(),
type: 'left join'
}
], false));
};
// 移除从数据源
var onRemove = function (item) {
if (joinList.length <= 1)
return message.warning('必须保留一项');
Modal.confirm({
title: '删除连接',
content: "\u5C06\u8981\u5220\u9664\u8BE5\u8FDE\u63A5\uFF0C\u662F\u5426\u786E\u5B9A\uFF1F",
centered: true,
onOk: function () {
setJoinList(joinList.filter(function (el) { return el.uid !== item.uid; }));
}
});
};
// 添加从链接
var addFromSource = function () {
if (!mainInfo.id) {
onCheckForm && onCheckForm();
return;
}
addRow();
};
// 选择连接类型
var onJoinTypeChange = function (val, record) {
setJoinList(joinList.map(function (item) {
if (item.uid === record.uid) {
return __assign(__assign({}, item), { type: val });
}
return item;
}));
};
// 选择从表 须清除连接条件
var onJoinTableChange = function (option, record) {
var alias = option.name;
// 判断是否重复
var isSameMain = option.id === mainInfo.id;
var sameJoins = joinList.filter(function (item) { return item.dataset === option.id; });
var allSameApis = __spreadArray(__spreadArray([], sameJoins, true), (isSameMain ? [option] : []), true);
if (allSameApis.length > 0) {
alias = "".concat(option.name, "_").concat(allSameApis.length);
}
setJoinList(joinList.map(function (item) {
if (item.uid === record.uid) {
return __assign(__assign({}, item), {
// 选择的从表
dataset: option.id,
// 从表属性字段
fields: option.fields,
// 从表别名
alias: alias,
// 切换了从表 须清除连接条件
conditions: undefined });
}
return item;
}));
};
// 编辑连接条件
var onEdit = function (record) {
console.log('edit:', record);
if (!mainInfo.id) {
onCheckForm && onCheckForm();
return;
}
if (!record.dataset) {
return message.error('请先选择从表');
}
setEditInfo(record);
setVisible(true);
};
var onConditionsChange = function (conditions) {
setJoinList(joinList.map(function (item) {
if (item.uid === editInfo.uid) {
return __assign(__assign({}, item), { conditions: conditions });
}
return item;
}));
};
// console.log('joinList: ', joinList);
var columns = [
{
dataIndex: 'type',
title: '连接类型',
width: 100,
render: function (type, record) { return (_jsxs(Select, __assign({ className: "select", placeholder: "\u8BF7\u9009\u62E9", defaultValue: "left join", onChange: function (val) { return onJoinTypeChange(val, record); }, value: type }, { children: [_jsx(Option, __assign({ value: "left join" }, { children: "\u5DE6\u8FDE\u63A5" }), void 0), _jsx(Option, __assign({ value: "right join" }, { children: "\u53F3\u8FDE\u63A5" }), void 0), _jsx(Option, __assign({ value: "full join" }, { children: "\u5168\u8FDE\u63A5" }), void 0)] }), void 0)); }
},
{
dataIndex: 'dataset',
title: '从表',
render: function (dataset, record) { return (_jsx(Select, { options: apiList, className: "select", placeholder: "\u8BF7\u9009\u62E9", onChange: function (val, option) {
return onJoinTableChange(option, record);
}, value: dataset }, void 0)); }
},
{
dataIndex: 'alias',
title: '从表别名',
ellipsis: true
},
{
dataIndex: '',
title: '连接条件',
width: 100,
render: function (_, record) { return (_jsx("div", __assign({ className: "condition-btn", onClick: function () { return onEdit(record); } }, { children: "..." }), void 0)); }
},
{
dataIndex: 'option',
title: '操作',
width: 120,
render: function (_, record) { return (_jsx("span", __assign({ onClick: function () { return onRemove(record); }, className: "icon" }, { children: "\u5220\u9664" }), void 0)); }
}
];
return (_jsxs(Container, { children: [_jsx(CustomTable, { rowKey: function (record) { return record.uid; }, dataSource: joinList, columns: columns, pagination: false }, void 0), _jsx("div", __assign({ className: "add-btn" }, { children: _jsxs("span", __assign({ onClick: addFromSource }, { children: [_jsx(Icon, { className: "add-icon", type: "iconicon_add" }, void 0), _jsx("span", { children: "\u65B0\u5EFA..." }, void 0)] }), void 0) }), void 0), _jsx(ConditionsMaker, { visible: visible, mainInfo: mainInfo, editInfo: editInfo, setVisible: setVisible, onChange: onConditionsChange }, void 0)] }, void 0));
};
var Container = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n padding-bottom: 24px;\n .ellipsis {\n overflow: hidden;\n white-space: nowrap;\n text-overflow: ellipsis;\n }\n .icon {\n cursor: pointer;\n color: #2787ff;\n font-size: 14px;\n margin-right: 10px;\n }\n .condition-btn {\n width: 38px;\n height: 18px;\n line-height: 8px;\n border: 1px solid #2787ff;\n border-radius: 4px;\n color: #2787ff;\n font-size: 16px;\n text-align: center;\n cursor: pointer;\n margin-left: 4px;\n }\n .select {\n width: 100%;\n }\n .add-btn {\n padding: 5px 10px;\n border-bottom: 1px solid #f0f0f0;\n & > span {\n cursor: pointer;\n color: #2787ff;\n font-size: 14px;\n & > .add-icon {\n margin-right: 4px;\n }\n }\n }\n"], ["\n padding-bottom: 24px;\n .ellipsis {\n overflow: hidden;\n white-space: nowrap;\n text-overflow: ellipsis;\n }\n .icon {\n cursor: pointer;\n color: #2787ff;\n font-size: 14px;\n margin-right: 10px;\n }\n .condition-btn {\n width: 38px;\n height: 18px;\n line-height: 8px;\n border: 1px solid #2787ff;\n border-radius: 4px;\n color: #2787ff;\n font-size: 16px;\n text-align: center;\n cursor: pointer;\n margin-left: 4px;\n }\n .select {\n width: 100%;\n }\n .add-btn {\n padding: 5px 10px;\n border-bottom: 1px solid #f0f0f0;\n & > span {\n cursor: pointer;\n color: #2787ff;\n font-size: 14px;\n & > .add-icon {\n margin-right: 4px;\n }\n }\n }\n"])));
var CustomTable = styled(Table)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n .", "-table-cell {\n padding: 6px 4px;\n }\n"], ["\n .", "-table-cell {\n padding: 6px 4px;\n }\n"])), prefixCls);
var templateObject_1, templateObject_2;