
<template>
<div class="main">
<div class="main_left">
//。。。左侧其他代码
</div>
<div ref="container" class="main_container" @dragover="dragoverDiv"></div>
</div>
</template>
<script>
import {Graph, Shape} from '@antv/x6'
export default {
name: "TabDataSource",
data() {
return {
selectTypeValue: '',
graph: null,
cells: []
}
},
mounted() {
this.initGraph()
},
methods: {
onDrapEnd(draggingNode, endNode, position, event) {
console.log('draggingNode', draggingNode, 'endNode', endNode, 'position', position, 'event', event)
const containerDom = this.$refs.container
this.initNode(event.clientX - containerDom.offsetLeft, event.clientY - containerDom.offsetTop, `${draggingNode.data.code}_${(new Date()).getTime()}`, draggingNode.data)
},
initGraph() {
const LINE_HEIGHT = 24
const NODE_WIDTH = 150
let container = this.$refs.container
const graph = new Graph({
container: container,
width: container.offsetWidth,
height: container.offsetHeight,
background: false,
panning: {
enabled: false
},
mouseWheel: {
enabled: false
},
grid: {
type: 'mesh',
size: 20,
visible: true,
args: {
color: '#eeeeee',
thickness: 2,
},
},
connecting: {
snap: true,
allowBlank: false,
allowLoop: false,
allowNode: false,
router: {
name: 'er',
args: {
offset: 25,
direction: 'H'
}
},
createEdge() {
return new Shape.Edge({
attrs: {
line: {
stroke: '#A2B1C3',
strokeWidth: 2
}
}
})
},
highlighting: {
magnetAvailable: {
name: 'stroke',
args: {
padding: 4,
attrs: {
strokeWidth: 4,
stroke: '#6a6c8a'
}
}
}
},
validateConnection({targetMagnet}) {
return !!targetMagnet
},
},
})
Graph.registerPortLayout(
'erPortPosition',
(portsPositionArgs) => {
return portsPositionArgs.map((_, index) => {
return {
position: {
x: 0,
y: (index + 1) * LINE_HEIGHT,
},
angle: 0,
}
})
},
true,
)
Graph.registerNode('er-rect',
{
inherit: 'rect',
markup: [
{
tagName: 'rect',
selector: 'body',
},
{
tagName: 'text',
selector: 'label',
},
],
attrs: {
rect: {
strokeWidth: 1,
stroke: '#5F95FF',
fill: '#5F95FF',
},
label: {
fontWeight: 'bold',
fill: '#ffffff',
fontSize: 12,
},
},
ports: {
groups: {
list: {
markup: [
{
tagName: 'rect',
selector: 'portBody',
},
{
tagName: 'text',
selector: 'portNameLabel',
zIndex: 1
},
{
tagName: 'text',
selector: 'portTypeLabel',
},
],
attrs: {
portBody: {
width: NODE_WIDTH,
height: LINE_HEIGHT,
strokeWidth: 1,
stroke: '#5F95FF',
fill: '#EFF4FF',
magnet: true,
},
portNameLabel: {
ref: 'portBody',
refX: 6,
refY: 6,
fontSize: 10,
},
portTypeLabel: {
ref: 'portBody',
refX: 95,
refY: 6,
fontSize: 10,
},
},
position: 'erPortPosition',
},
},
},
},
true,)
graph.on('edge:added',({ edge, index, options }) => {
const edges = graph.getEdges()
edges.forEach(e =>{
if (e.target === edge.source && e.source === edge.target){
return
}
})
console.log('添加了一个边',edge,index,options)
console.log('获取所有便',graph.getEdges())
})
this.graph = graph
},
initNode(x, y, id, data) {
this.cells.push(this.graph.createNode({
id,
"shape": "er-rect",
"label": data.name,
"width": 150,
"height": 24,
"position": {
x,
y
},
"ports": [
{
"id": `${id}-1`,
"group": "list",
"attrs": {
"portNameLabel": {
"text": "ID"
},
"portTypeLabel": {
"text": `${data.name}-1`
}
}
},
{
"id": `${id}-2`,
"group": "list",
"attrs": {
"portNameLabel": {
"text": "Name"
},
"portTypeLabel": {
"text": `${data.name}-2`
}
}
},
{
"id": `${id}-3`,
"group": "list",
"attrs": {
"portNameLabel": {
"text": "Class"
},
"portTypeLabel": {
"text": `${data.name}-3`
}
}
},
{
"id": `${id}-4`,
"group": "list",
"attrs": {
"portNameLabel": {
"text": "Gender"
},
"portTypeLabel": {
"text": `${data.name}-4`
}
}
}
]
},))
this.graph.resetCells(this.cells)
},
dragoverDiv(ev) {
ev.preventDefault()
},
selectTypeChange(e) {
console.log(e)
},
isDrapFun(e, a) {
return e.level == 2
},
isDropFun() {
return false
},
}
}
</script>
<style lang="scss" scoped>
.main {
height: 100%;
display: flex;
.main_left {
width: 240px;
height: 100%;
border-right: 1px solid #d6d6d6;
.tree_top_line2 {
display: flex;
margin-bottom: 15px;
}
.custom_tree_node {
.tree_node_label {
margin-left: 5px;
}
}
}
.main_container {
position: relative;
flex: 1;
.empty {
position: absolute;
}
}
}
</style>