import React from 'react'
import { Layout, Row, Col, Button, Table, Icon, Menu, Dropdown, Card, Breadcrumb, Tag, Checkbox, Select } from 'antd'
import { connect } from 'dva'
import { arrayToTree, dateFormat } from '../../utils/baseUtils'
import GroupManagementBox from '../common/groupManageMentBox/box'
import GroupSelector from '../common/groupSelector/popover'
import TransferBox from '../common/selectUserBox/selectUserBox'
import CopyBox from './copyBox'
import DeleteBox from '../common/deleteBox/deleteBox'
import DataPreview from '../common/dataPreview/dataPreview'
import ListFilter from '../common/listFilter/index'
import './list.less'
const { Content } = Layout
const { Option } = Select
class DataSource extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedRecord: null, // 当前选中的dataSource
visibleGroupMenu: false, // 显示分组菜单
visibleTransferBox: false,
visibleDeleteBox: false,
visibleCopyBox: false,
visibleDataPreviewBox: false, // 显示数据预览
visibleGroupManageMentBox: false, // 显示分组管理
noGroup: false, // 显示未分组数据
}
};
hideTransferBox = () => {
this.setState({ visibleTransferBox: false})
}
componentDidMount() {
const { dispatch } = this.props;
this.setScrollTableHeight();
dispatch({ type: 'dataSource/fetchList' });
dispatch({ type: 'dataSource/remoteGroupList' });
}
/**
* 根据视图设置表格高度以呈现滚动条
*/
setScrollTableHeight() {
const mainContent = document.getElementsByClassName('main-content')[0];
const toolbar = mainContent.getElementsByClassName('datasource-tools')[0];
const tableHeader = mainContent.getElementsByClassName('ant-table-header')[0];
const tableBody = mainContent.getElementsByClassName('ant-table-body')[0];
tableBody.style.maxHeight=`${mainContent.offsetHeight - toolbar.offsetHeight - tableHeader.offsetHeight - 58}px`;
}
onGroup() {
const { dataSource } = this.props;
const { noGroup } = this.state;
const { currentGroup, list } = dataSource;
if(noGroup) {
return list.filter(l => l.groupCode === '-1' );
}else if(currentGroup) {
return list.filter(l => l.groupCode === currentGroup.code );
}else {
return list;
}
}
onSearch(list, dataSource) {
const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
let filterItem = dataSource.filterItem
let filterLabel = dataSource.filterLabel ? (dataSource.filterLabel + '').replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') : ''; // 添加转义符号
let filterReg = new RegExp('('+ filterLabel +'){1}', 'ig');
return list.map(l => {
let o = Object.assign({}, l);
if(filterItem.type === 'date') {
if(filterLabel===""){
return o;
}else if(filterLabel.indexOf('#')>-1){
let start = filterLabel.split('#')[0]
let end = filterLabel.split('#')[1]
let nowTime = o[filterItem.name].getTime();
if(nowTime>=start && nowTime<=end){
return o;
}
return null
}else{
return null
}
}else {
let arr = filterItem.name.split('.');
let v = o;
for(let i = 0; i < arr.length; i++) {
v = v[arr[i]]
}
return ((v + '').search(filterReg) > -1) ? o : null
}
}).filter(a => a!==null);
}
onSort(list) {
return list.sort((a, b) => {
return new Date(b.createTime) - new Date(a.createTime);
});
}
handleVisibleChange = (flag) => {
this.setState({ visibleGroupMenu: flag });
}
getParens = (group) => {
const groupData = this.props.dataSource.groupList;
let pgroups = [group];
let fgroup = groupData.find(g => g.code === group.pcode);
if(fgroup) {
pgroups = this.getParens(fgroup).concat(pgroups);
}
return pgroups;
}
generateGroupTags = () => {
const { dataSource, dispatch } = this.props;
const { noGroup } = this.state;
const { currentGroup } = dataSource;
const pGroups = currentGroup ? [{ code: '-1', label: '全部分组' }].concat(this.getParens(currentGroup)) : [{ code: '-1', label: '全部分组' }];
return
{ pGroups.map(g => (
{
let obj = {};
obj['visibleGroupSelector' + g.code] = visible
this.setState(obj);
}}
pGroup={g}
groupList={dataSource.groupList}
selectGroup={(group) => {
let obj = {};
obj['visibleGroupSelector' + g.code] = false;
this.setState(obj);
dispatch({ type: 'dataSource/setCurrentGroup', group });
}}
>
{g.label}
)) }
}
createGroupMenu = (treeData) => {
const { dispatch } = this.props;
const { selectedRecord } = this.state;
return treeData.map(t => {
if(t.children && t.children.length > 0) {
return
{t.label} : t.label}
onTitleClick={() => {
dispatch({ type: 'dataSource/remoteSetGroup', dataSource: selectedRecord, group: t });
dispatch({ type: 'dataSource/setCurrentGroup', group: t });
}}
>
{this.createGroupMenu(t.children)}
}else {
return {
dispatch({ type: 'dataSource/remoteSetGroup', dataSource: selectedRecord, group: t });
dispatch({ type: 'dataSource/setCurrentGroup', group: t });
}}>
{selectedRecord.groupCode === t.code ? {t.label} : t.label}
}
})
}
hideGroupMenu = () => {
this.setState({
visibleGroupMenu: false
});
}
onDrop = (info) => {
const { dispatch } = this.props;
const dropCode = info.node.props.eventKey;
const dragCode = info.dragNode.props.eventKey;
const dropPos = info.node.props.pos.split('-');
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]); // -1/0/1 -> 兄/子/弟
dispatch({ type: 'dataSource/remoteMoveGroup', dragCode, dropCode, dropPosition });
}
generateFilterItems = () => {
const { filterItems } = this.props.dataSource;
return filterItems.map(t => );
}
render() {
const { main, dataSource, dispatch } = this.props;
const { selectedRecord, visibleTransferBox, visibleGroupManageMentBox, visibleCopyBox, visibleDeleteBox, visibleDataPreviewBox, noGroup } = this.state;
const { currentUser } = main;
const treeData = arrayToTree(dataSource.groupList, '-1', 'code', 'pcode', 'children');
const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
let filterItem = dataSource.filterItem;
let filterLabel = dataSource.filterLabel ? (dataSource.filterLabel + '').replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') : ''; // 添加转义符号
const moreOperatingMenu = (
);
const dataSourceColumns = [{
title: '名称',
dataIndex: 'name',
key: 'name',
width: 100,
render: (text, record) => {
return
{record.type === 'database' ? : }
{
dispatch({ type: 'dataSource/resetNewModel' });
dispatch({type: 'main/redirect', path: {pathname: '/workshop/datasource/'+ record.type +'/' + record.code + '/base'}});
}}>
{ (filterItem.name === 'name' && filterLabel) ?
((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : text
}
}
}, {
title: '数据链接',
dataIndex: 'dbConfig.name',
key: 'dbConfig.name',
width: 100
}, {
title: '说明',
dataIndex: 'description',
key: 'description',
width: 200,
render: (text, record) => {
return (
{ (filterItem.name === 'description' && filterLabel) ?
((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : text
}
)
}
}, {
title: '创建人',
dataIndex: 'creatorName',
key: 'creatorName',
width: 100,
render: (text, record) => {
return (
{ (filterItem.name === 'creatorName' && filterLabel) ?
((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : text
}
)
}
}, {
title: '创建时间',
dataIndex: 'createTime',
key: 'createTime',
render: (text, record) => dateFormat(text, 'yyyy-MM-dd hh:mm:ss'),
width: 100
}, {
title: '操作',
key: 'action',
render: (text, record, index) => (
),
width: 50
}];
return (
{
this.setState({
visibleGroupManageMentBox: true
});
}}/>
{
this.setState({noGroup: e.target.checked})
}}>未分组
{ this.generateGroupTags() }
{
const type = item.key;
dispatch({ type: 'dataSource/resetNewModel' });
dispatch({ type: 'dataConnect/resetSelected' });
dispatch({ type: 'dataSource/setNewModelField', name: 'type', value: type });
dispatch({type: 'main/redirect', path: {pathname: '/workshop/datasource/'+ type +'/create/base'}});
}}>
{ currentUser.role === 'admin' && 来自数据库}
来自文件
)} trigger={['click']}>
}>
{
return {
onClick: () => {this.setState({ selectedRecord: record})}
}
}}
/>
{visibleGroupManageMentBox && {
this.setState({
visibleGroupManageMentBox: false
})
}}
okHandler={(aGroups, mGroups, dGroups) => {
dispatch({ type: 'dataSource/remoteSetGroups', aGroups, mGroups, dGroups });
}}
/>}
{visibleTransferBox && {
dispatch({ type: 'dataSource/transfer', dataSourceCode: this.state.selectedRecord.code, userCode: user.code });
}}
hideBox={() => {
this.setState({
visibleTransferBox: false
})
}}
onlyAdmin={true}
/>}
{visibleCopyBox && { this.setState({ visibleCopyBox: false })
}}
/>}
{visibleDeleteBox && 确定要删除数据源【{selectedRecord.name}】吗?}
hideBox={() => {
this.setState({
visibleDeleteBox: false
})
}}
okHandler={() =>{
dispatch({ type: 'dataSource/remoteDelete', code: selectedRecord.code }).then(() => {
dispatch({ type: 'chart/fetchList', mandatory: true });
})
}}
/>}
{visibleDataPreviewBox && {
this.setState({
visibleDataPreviewBox: false
});
}}
fetchFunction={(page, pageSize) => {
dispatch({ type: 'dataSource/remoteDataList', code: selectedRecord.code, page, pageSize });
}}
/>}
)
}
}
function mapStateToProps({present: {main, dataSource, dataConnect}}) {
return { main, dataSource, dataConnect }
}
export default connect(mapStateToProps)(DataSource)