import React from 'react'
import { Layout, Row, Col, Button, Icon, Card, Select, Menu, Dropdown } from 'antd'
import { connect } from 'dva'
import DeleteBox from '../common/deleteBox/deleteBox'
import EmptyContent from '../common/emptyContent/index'
import DataConnectBox from '../dataSourceDetail/dataConnectBox'
import ListFilter from '../common/listFilter/index'
import CusIcon from '../common/cusIcon/index'
import './list.less'
const CardGrid = Card.Grid
const { Content } = Layout
const { Option } = Select
class DataConnect extends React.Component {
constructor(props) {
super(props);
this.state = {
visibleDeleteBox: false,
}
this.bodyRef = React.createRef();
};
componentDidMount() {
const { dispatch } = this.props;
this.setBodyWidth();
dispatch({ type: 'dataConnect/fetchList' });
window.addEventListener('resize', this.setBodyWidth);
}
componentWillUnmount() {
window.removeEventListener('resize', this.setBodyWidth)
}
/**
* 设置卡片容器宽度 = 每行最大卡片数量 * 卡片宽度
*/
setBodyWidth = () => {
const cardBody = this.bodyRef.current; // 卡片容器
const parent = cardBody.parentNode; // 父级容器
const pWidth = parent.offsetWidth; // 父级容器宽度
const pPadding = 10 + 10; // 父级容器左右padding
const cWidth = 160; // 每个卡片宽度
const cMargin = 8 + 8; // 每个卡片左右margin
const pTrueWidth = pWidth - pPadding; // 父容器实际可用宽度
const cTrueWidth = cWidth + cMargin; // 卡片实际占用宽度
const count = Math.floor(pTrueWidth/cTrueWidth); // 每行最大卡片数量
const cardBodyWidth = (count - 1) * cTrueWidth; // 可能有滚动条,减少一个
cardBodyWidth > 0 ? cardBody.style.width = cardBodyWidth + 'px' : void(0);
}
onSearch(list, text) {
const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
let filterLabel = (text || '').replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1'); // 添加转义符号
return list.map(l => {
let o = Object.assign({}, l);
let reg = new RegExp('('+ filterLabel +'){1}', 'ig');
if(o.name && o.name.search(reg) !== -1) {
return o;
}else if(o.description && o.description.search(reg) !== -1) {
return o;
}else {
return null
}
}).filter(a => a!==null);
}
onSort(list) {
return list.sort((a, b) => {
return new Date(b.createDate) - new Date(a.createDate);
});
}
handleVisibleChange = (flag) => {
this.setState({ visibleGroupMenu: flag });
}
generateOperationMenu = (l) => {
const { dispatch } = this.props;
return (
)
}
generateCard() {
const { dataConnect, dispatch } = this.props;
const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
let filterLabel = dataConnect.filterLabel ? (dataConnect.filterLabel + '').replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') : ''; // 添加转义符号
let filterItem = dataConnect.filterItem
let filterReg = new RegExp('(' + filterLabel + '){1}', 'ig');
let cards = dataConnect.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 {
return ((o[filterItem.name] + '').search(filterReg) > -1) ? o : null
}
}).filter(a => a!==null).map( (l, i) => (
{
// 选中项设置
dispatch({ type: 'dataConnect/setSelected', selected: l });
dispatch({ type: 'dataConnect/setNewModel', model: l });
dispatch({ type: 'dataConnect/setNewModelFields', fields: [
{ name: 'visibleBox', value: true },
{ name: 'boxOperation', value: 'modify' }
] });
}}
>
{ (filterItem.name === 'name' && filterLabel) ?
((l.name || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : l.name
}
}
actions={[
{l.dbType === 'file' ?
:
{l.dbType[0].toUpperCase() + l.dbType.slice(1)}
}
,
]}
>
{
// 选中项设置
dispatch({ type: 'dataConnect/setSelected', selected: l });
dispatch({ type: 'dataConnect/setNewModel', model: l });
dispatch({ type: 'dataConnect/setNewModelFields', fields: [
{ name: 'visibleBox', value: true },
{ name: 'boxOperation', value: 'modify' }
] });
}}
>
{ /** TODO 这里只考虑了数据库的展示情况,需要兼容展示文件类型的数据链接 */ }
{ filterLabel && filterItem.name === 'address' ?
((l.address || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : l.address
}
{ filterLabel && filterItem.name === 'userName' ?
((l.userName || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
return (
fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
{fragment} :
fragment
)
}
)) : l.userName
}
))
if(cards.length === 0) {
return
}
return cards;
}
generateFilterItems = () => {
const { filterItems } = this.props.dataConnect;
return filterItems.map(t => );
}
render() {
const { dataConnect, dispatch } = this.props;
const { selected } = dataConnect;
const { visibleDeleteBox } = this.state;
return (
}>
{ this.generateCard() }
{visibleDeleteBox && 确定要删除数据链接【{selected.name}】吗?}
hideBox={() => {
this.setState({
visibleDeleteBox: false
})
}}
okHandler={() =>{
dispatch({ type: 'dataConnect/remoteDelete', code: selected.code })
}}
/>}
{dataConnect.newOne.visibleBox && }
)
}
}
function mapStateToProps({present: {dataConnect}}) {
return { dataConnect }
}
export default connect(mapStateToProps)(DataConnect)