import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Modal, Button, notification, Spin, Input } from 'antd'
|
import { ForkOutlined } from '@ant-design/icons'
|
|
import Api from '@/api'
|
import G6 from "@antv/g6"
|
import Utils from '@/utils/utils.js'
|
import './index.scss'
|
|
const { Search } = Input
|
|
class TableNodes extends Component {
|
static propTpyes = {
|
config: PropTypes.object
|
}
|
|
state = {
|
visible: false,
|
loading: false,
|
nodes: null,
|
empty: false
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
getTbs = (config) => {
|
let tbs = []
|
let ptbs = []
|
|
let traversal = (components) => {
|
components.forEach(item => {
|
if (item.$tables) {
|
ptbs.push(...item.$tables)
|
item.$tables.forEach(tb => {
|
tbs.push({
|
label: item.name,
|
table: tb,
|
color: '#5AD8A6',
|
id: Utils.getuuid(),
|
direction: 'left'
|
})
|
})
|
}
|
if (item.type === 'tabs') {
|
item.subtabs.forEach(tab => {
|
traversal(tab.components)
|
})
|
} else if (item.type === 'group') {
|
traversal(item.components)
|
}
|
})
|
}
|
|
traversal(config.components)
|
|
return {tbs, ptbs}
|
}
|
|
trigger = () => {
|
const { config } = this.props
|
|
if (!config) return
|
|
this.setState({visible: true, loading: true, empty: false}, () => {
|
let param = {
|
func: 's_get_menus_tb_list',
|
TypeCharOne: sessionStorage.getItem('kei_no') || '',
|
typename: sessionStorage.getItem('typename') || '',
|
MenuID: config.uuid
|
}
|
|
Api.getSystemConfig(param).then(result => {
|
if (!result.status) {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
|
this.setState({empty: true, loading: false})
|
|
return
|
}
|
|
let data = {
|
label: config.MenuName || '空',
|
id: config.uuid,
|
MenuID: config.MenuID,
|
children: []
|
}
|
|
let { tbs, ptbs } = this.getTbs(config)
|
|
ptbs = Array.from(new Set(ptbs))
|
ptbs.sort()
|
if (ptbs.length && sessionStorage.getItem('mk_tb_names')) {
|
let names = sessionStorage.getItem('mk_tb_names')
|
ptbs = ptbs.filter(tb => names.indexOf(',' + tb.toLowerCase() + ',') > -1)
|
}
|
|
if (ptbs.length) {
|
ptbs.forEach((item, i) => {
|
let cell = {
|
label: item,
|
id: 'par' + i,
|
direction: 'left',
|
color: '#5AD8A6',
|
children: []
|
}
|
|
tbs.forEach(t => {
|
if (t.table === item) {
|
cell.children.push(t)
|
}
|
})
|
|
data.children.push(cell)
|
})
|
}
|
|
if (result.tb_list) {
|
result.tb_list.sort((a, b) => a.tbname > b.tbname ? 1 : -1)
|
result.tb_list.forEach((item, i) => {
|
let cell = {
|
label: item.tbname,
|
name: item.tbname.toLowerCase(),
|
id: 'table' + i,
|
direction: 'right',
|
color: '#1890ff',
|
children: []
|
}
|
|
if (item[item.tbname]) {
|
item[item.tbname].forEach((m, i) => {
|
if (m.debug_url) {
|
let _param = JSON.parse(window.decodeURIComponent(window.atob(m.debug_url)))
|
cell.children.push({
|
label: _param.MenuName,
|
id: item.tbname + 'menu' + i,
|
direction: 'right',
|
color: '#1890ff',
|
param: _param
|
})
|
}
|
})
|
}
|
|
data.children.push(cell)
|
})
|
}
|
|
if (data.children.length === 0) {
|
this.setState({empty: true, loading: false})
|
} else {
|
this.setState({loading: false, nodes: data})
|
this.getForks(data)
|
}
|
})
|
})
|
}
|
|
changeMenu = (menu) => {
|
if (menu.direction !== 'right') return
|
|
if (menu.depth === 1) {
|
window.open('#/hs')
|
} else if (menu.param) {
|
if (menu.param.type === 'admin') {
|
if (menu.param.MenuType === 'custom') {
|
let _param = {...menu.param}
|
delete _param.type
|
_param = window.btoa(window.encodeURIComponent(JSON.stringify(_param)))
|
window.open(`#/menudesign/${_param}`)
|
}
|
}
|
}
|
}
|
|
resetNodes = (key) => {
|
let data = fromJS(this.state.nodes).toJS()
|
key = key ? key.toLowerCase() : ''
|
|
data.children.forEach(cell => {
|
if (cell.direction === 'right') {
|
cell.fontcolor = ''
|
if (key && cell.name.indexOf(key) > -1) {
|
cell.fontcolor = 'orange'
|
}
|
}
|
})
|
let _element = document.getElementById('mkTableNode')
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
|
this.setState({}, () => {
|
this.getForks(data)
|
})
|
}
|
|
getForks = (data) => {
|
const { Util } = G6
|
const that = this
|
|
G6.registerNode(
|
'dice-mind-map-root', {
|
jsx: (cfg) => {
|
const width = Util.getTextSize(cfg.label, 14)[0] + 12;
|
const stroke = cfg.style.stroke || '#096dd9';
|
|
return `
|
<group>
|
<rect draggable="true" style={{width: ${width}, height: 30, stroke: ${stroke}, radius: 4}} keyshape>
|
<text style={{ fontSize: 14, marginLeft: 6, marginTop: 6 }}>${cfg.label}</text>
|
</rect>
|
</group>
|
`;
|
},
|
getAnchorPoints() {
|
return [
|
[0, 0.5],
|
[1, 0.5],
|
];
|
},
|
},
|
'single-node',
|
);
|
|
G6.registerNode(
|
'dice-mind-map-leaf', {
|
jsx: (cfg) => {
|
const width = Util.getTextSize(cfg.label, 12)[0] + 24;
|
const color = cfg.color;
|
|
return `
|
<group>
|
<rect draggable="true" style={{width: ${width}, height: 26, cursor: ${cfg.direction !== 'left' ? 'pointer' : 'default'}, fill: 'transparent' }}>
|
<text style={{ fontSize: 12, fill: ${cfg.fontcolor ? cfg.fontcolor : 'black'}, cursor: ${cfg.direction !== 'left' ? 'pointer' : 'default'}, marginLeft: 12, marginTop: 6 }}>${cfg.label}</text>
|
</rect>
|
<rect style={{ fill: ${color}, width: ${width}, cursor: ${cfg.direction !== 'left' ? 'pointer' : 'default'}, height: 2, x: 0, y: 32 }} />
|
</group>
|
`;
|
},
|
getAnchorPoints() {
|
return [
|
[0, 0.965],
|
[1, 0.965],
|
];
|
},
|
},
|
'single-node',
|
);
|
G6.registerBehavior('dice-mindmap', {
|
getEvents() {
|
return {
|
'node:dblclick': 'editNode',
|
};
|
},
|
editNode(evt) {
|
const item = evt.item;
|
const model = item.get('model');
|
|
that.changeMenu(model)
|
}
|
});
|
G6.registerBehavior('scroll-canvas', {
|
getEvents: function getEvents() {
|
return {
|
wheel: 'onWheel',
|
};
|
},
|
|
onWheel: function onWheel(ev) {
|
const {
|
graph
|
} = this;
|
if (!graph) {
|
return;
|
}
|
if (ev.ctrlKey) {
|
const canvas = graph.get('canvas');
|
const point = canvas.getPointByClient(ev.clientX, ev.clientY);
|
let ratio = graph.getZoom();
|
if (ev.wheelDelta > 0) {
|
ratio += ratio * 0.05;
|
} else {
|
ratio *= ratio * 0.05;
|
}
|
graph.zoomTo(ratio, {
|
x: point.x,
|
y: point.y,
|
});
|
} else {
|
const x = ev.deltaX || ev.movementX;
|
const y = ev.deltaY || ev.movementY || (-ev.wheelDelta * 125) / 3;
|
graph.translate(-x, -y);
|
}
|
ev.preventDefault();
|
},
|
});
|
|
const dataTransform = (data) => {
|
const changeData = (d, level = 0, color) => {
|
const data = {
|
...d,
|
};
|
switch (level) {
|
case 0:
|
data.type = 'dice-mind-map-root';
|
break;
|
default:
|
data.type = 'dice-mind-map-leaf';
|
break;
|
}
|
|
data.hover = false;
|
|
if (color) {
|
data.color = color;
|
}
|
|
if (d.children) {
|
data.children = d.children.map((child) => changeData(child, level + 1, data.color));
|
}
|
return data;
|
};
|
return changeData(data);
|
};
|
|
const tree = new G6.TreeGraph({
|
container: 'mkTableNode',
|
width: this.wrap.offsetWidth,
|
height: this.wrap.offsetHeight,
|
fitView: true,
|
fitViewPadding: [10, 20],
|
layout: {
|
type: 'mindmap',
|
direction: 'H',
|
getHeight: () => {
|
return 16;
|
},
|
getWidth: (node) => {
|
return node.level === 0 ?
|
Util.getTextSize(node.label, 16)[0] + 12 :
|
Util.getTextSize(node.label, 12)[0];
|
},
|
getVGap: () => {
|
return 10;
|
},
|
getHGap: () => {
|
return 60;
|
},
|
getSide: (node) => {
|
return node.data.direction;
|
},
|
},
|
defaultEdge: {
|
type: 'cubic-horizontal',
|
style: {
|
lineWidth: 2,
|
},
|
},
|
minZoom: 0.5,
|
modes: {
|
default: ['drag-canvas', 'zoom-canvas', 'dice-mindmap'],
|
},
|
});
|
|
tree.data(dataTransform(data));
|
|
tree.render();
|
}
|
|
render() {
|
const { visible, loading, empty } = this.state
|
|
return (
|
<div style={{display: 'inline-block'}}>
|
<Button style={{borderColor: '#8E44AD', color: '#8E44AD'}} onClick={this.trigger}><ForkOutlined /> 表关系图</Button>
|
<Modal
|
title=""
|
wrapClassName="view-table-modal"
|
visible={visible}
|
width={'90vw'}
|
closable={false}
|
maskClosable={false}
|
footer={[]}
|
destroyOnClose
|
>
|
<div className="header">表关系图</div>
|
<Search className="tb-search" placeholder="请输入表名" onSearch={value => this.resetNodes(value)} enterButton />
|
<div className="wrap">
|
{loading ? <Spin size="large" /> : null}
|
{empty ? <div className="empty">未查询到表名信息。</div> : null}
|
<div className="mountNode" id="mkTableNode" ref={ref => this.wrap = ref}></div>
|
</div>
|
<div className="footer">
|
<Button key="cancel" onClick={() => { this.setState({ visible: false })}}>关闭</Button>
|
</div>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
export default TableNodes
|