fix:topo图相同节点出现两次的bug

This commit is contained in:
wang-liang0615 2023-09-26 20:13:12 +08:00
parent 9f5979c1b1
commit 9bb787d3f4
2 changed files with 224 additions and 219 deletions

View File

@ -1,163 +1,168 @@
<template> <template>
<div <div
id="ci-detail-relation-topo" id="ci-detail-relation-topo"
class="ci-detail-relation-topo" class="ci-detail-relation-topo"
:style="{ width: '100%', marginTop: '20px', height: 'calc(100vh - 136px)' }" :style="{ width: '100%', marginTop: '20px', height: 'calc(100vh - 136px)' }"
></div> ></div>
</template> </template>
<script> <script>
import _ from 'lodash' import _ from 'lodash'
import { TreeCanvas } from 'butterfly-dag' import { TreeCanvas } from 'butterfly-dag'
import { searchCIRelation } from '@/modules/cmdb/api/CIRelation' import { searchCIRelation } from '@/modules/cmdb/api/CIRelation'
import Node from './node.js' import Node from './node.js'
import 'butterfly-dag/dist/index.css' import 'butterfly-dag/dist/index.css'
import './index.less' import './index.less'
export default { export default {
name: 'CiDetailRelationTopo', name: 'CiDetailRelationTopo',
data() { data() {
return { return {
topoData: {}, topoData: {},
} exsited_ci: [],
}, }
inject: ['ci_types'], },
mounted() {}, inject: ['ci_types'],
methods: { mounted() {},
init() { methods: {
const root = document.getElementById('ci-detail-relation-topo') init() {
this.canvas = new TreeCanvas({ const root = document.getElementById('ci-detail-relation-topo')
root: root, this.canvas = new TreeCanvas({
disLinkable: false, // 可删除连线 root: root,
linkable: false, // 可连线 disLinkable: false, // 可删除连线
draggable: true, // 可拖动 linkable: false, // 可连线
zoomable: true, // 可放大 draggable: true, // 可拖动
moveable: true, // 可平移 zoomable: true, // 可放大
theme: { moveable: true, // 可平移
edge: { theme: {
shapeType: 'AdvancedBezier', edge: {
arrow: true, shapeType: 'AdvancedBezier',
arrowPosition: 1, arrow: true,
}, arrowPosition: 1,
}, },
layout: { },
type: 'mindmap', layout: {
options: { type: 'mindmap',
direction: 'H', options: {
getSide(d) { direction: 'H',
return d.data.side || 'right' getSide(d) {
}, return d.data.side || 'right'
getHeight(d) { },
return 10 getHeight(d) {
}, return 10
getWidth(d) { },
return 40 getWidth(d) {
}, return 40
getHGap(d) { },
return 80 getHGap(d) {
}, return 80
getVGap(d) { },
return 40 getVGap(d) {
}, return 40
}, },
}, },
}) },
this.canvas.setZoomable(true, true) })
this.canvas.on('events', ({ type, data }) => { this.canvas.setZoomable(true, true)
const sourceNode = data?.id || null this.canvas.on('events', ({ type, data }) => {
if (type === 'custom:clickLeft') { const sourceNode = data?.id || null
searchCIRelation(`root_id=${Number(sourceNode)}&&level=1&&reverse=1&&count=10000`).then((res) => { if (type === 'custom:clickLeft') {
this.redrawData(res, sourceNode, 'left') searchCIRelation(`root_id=${Number(sourceNode)}&&level=1&&reverse=1&&count=10000`).then((res) => {
}) this.redrawData(res, sourceNode, 'left')
} })
if (type === 'custom:clickRight') { }
searchCIRelation(`root_id=${Number(sourceNode)}&&level=1&&reverse=0&&count=10000`).then((res) => { if (type === 'custom:clickRight') {
this.redrawData(res, sourceNode, 'right') searchCIRelation(`root_id=${Number(sourceNode)}&&level=1&&reverse=0&&count=10000`).then((res) => {
}) this.redrawData(res, sourceNode, 'right')
} })
}) }
}, })
setTopoData(data) { },
this.canvas = null setTopoData(data) {
this.init() this.canvas = null
this.topoData = _.cloneDeep(data) this.init()
this.canvas.draw(data, {}, () => { this.topoData = _.cloneDeep(data)
this.canvas.focusCenterWithAnimate() this.canvas.draw(data, {}, () => {
}) this.canvas.focusCenterWithAnimate()
}, })
redrawData(res, sourceNode, side) { },
const newNodes = [] redrawData(res, sourceNode, side) {
const newEdges = [] const newNodes = []
if (!res.result.length) { const newEdges = []
this.$message.info('无层级关系!') if (!res.result.length) {
return this.$message.info('无层级关系!')
} return
const ci_types_list = this.ci_types() }
res.result.forEach((r) => { const ci_types_list = this.ci_types()
const _findCiType = ci_types_list.find((item) => item.id === r._type) res.result.forEach((r) => {
newNodes.push({ if (!this.exsited_ci.includes(r._id)) {
id: `${r._id}`, const _findCiType = ci_types_list.find((item) => item.id === r._type)
Class: Node, newNodes.push({
title: r.ci_type_alias || r.ci_type, id: `${r._id}`,
name: r.ci_type, Class: Node,
side: side, title: r.ci_type_alias || r.ci_type,
unique_alias: r.unique_alias, name: r.ci_type,
unique_name: r.unique, side: side,
unique_value: r[r.unique], unique_alias: r.unique_alias,
children: [], unique_name: r.unique,
icon: _findCiType?.icon || '', unique_value: r[r.unique],
endpoints: [ children: [],
{ icon: _findCiType?.icon || '',
id: 'left', endpoints: [
orientation: [-1, 0], {
pos: [0, 0.5], id: 'left',
}, orientation: [-1, 0],
{ pos: [0, 0.5],
id: 'right', },
orientation: [1, 0], {
pos: [0, 0.5], id: 'right',
}, orientation: [1, 0],
], pos: [0, 0.5],
}) },
newEdges.push({ ],
id: `${r._id}`, })
source: 'right', }
target: 'left', newEdges.push({
sourceNode: side === 'right' ? sourceNode : `${r._id}`, id: `${r._id}`,
targetNode: side === 'right' ? `${r._id}` : sourceNode, source: 'right',
type: 'endpoint', target: 'left',
}) sourceNode: side === 'right' ? sourceNode : `${r._id}`,
}) targetNode: side === 'right' ? `${r._id}` : sourceNode,
const { nodes, edges } = this.canvas.getDataMap() type: 'endpoint',
// 删除原节点和边 })
this.canvas.removeNodes(nodes.map((node) => node.id)) })
this.canvas.removeEdges(edges) const { nodes, edges } = this.canvas.getDataMap()
// 删除原节点和边
const _topoData = _.cloneDeep(this.topoData) this.canvas.removeNodes(nodes.map((node) => node.id))
let result this.canvas.removeEdges(edges)
const getTreeItem = (data, id) => {
for (let i = 0; i < data.length; i++) { const _topoData = _.cloneDeep(this.topoData)
if (data[i].id === id) { _topoData.edges.push(...newEdges)
result = data[i] // 结果赋值 let result
break const getTreeItem = (data, id) => {
} else { for (let i = 0; i < data.length; i++) {
if (data[i].children && data[i].children.length) { if (data[i].id === id) {
getTreeItem(data[i].children, id) result = data[i] // 结果赋值
} result.edges = _topoData.edges
} break
} } else {
} if (data[i].children && data[i].children.length) {
getTreeItem(data[i].children, id)
getTreeItem(_topoData.nodes.children, sourceNode) }
result.children.push(...newNodes) }
_topoData.edges.push(...newEdges) }
}
this.topoData = _topoData
this.canvas.draw(_topoData, {}, () => {}) getTreeItem(_topoData.nodes.children, sourceNode)
}, result.children.push(...newNodes)
},
} this.topoData = _topoData
</script> this.canvas.draw(_topoData, {}, () => {})
this.exsited_ci = [...new Set([...this.exsited_ci, ...res.result.map((r) => r._id)])]
<style></style> },
},
}
</script>
<style></style>

View File

@ -1,56 +1,56 @@
/* eslint-disable no-useless-constructor */ /* eslint-disable no-useless-constructor */
import { TreeNode } from 'butterfly-dag' import { TreeNode } from 'butterfly-dag'
import $ from 'jquery' import $ from 'jquery'
class BaseNode extends TreeNode { class BaseNode extends TreeNode {
constructor(opts) { constructor(opts) {
super(opts) super(opts)
} }
draw = (opts) => { draw = (opts) => {
const container = $(`<div class="${opts.id.startsWith('Root') ? 'root' : ''} ci-detail-relation-topo-node"></div>`) const container = $(`<div class="${opts.id.startsWith('Root') ? 'root' : ''} ci-detail-relation-topo-node"></div>`)
.css('top', opts.top) .css('top', opts.top)
.css('left', opts.left) .css('left', opts.left)
.attr('id', opts.id) .attr('id', opts.id)
let icon let icon
if (opts.options.icon) { if (opts.options.icon) {
if (opts.options.icon.split('$$')[2]) { if (opts.options.icon.split('$$')[2]) {
icon = $(`<img style="max-width:16px;max-height:16px;" src="/api/common-setting/v1/file/${opts.options.icon.split('$$')[3]}" />`) icon = $(`<img style="max-width:16px;max-height:16px;" src="/api/common-setting/v1/file/${opts.options.icon.split('$$')[3]}" />`)
} else { } else {
icon = $(`<svg class="icon" style="color:${opts.options.icon.split('$$')[1]}" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><use data-v-5bd421da="" xlink:href="#${opts.options.icon.split('$$')[0]}"></use></svg>`) icon = $(`<svg class="icon" style="color:${opts.options.icon.split('$$')[1]}" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><use data-v-5bd421da="" xlink:href="#${opts.options.icon.split('$$')[0]}"></use></svg>`)
} }
} else { } else {
icon = $(`<span class="icon icon-default">${opts.options.name[0].toUpperCase()}</span>`) icon = $(`<span class="icon icon-default">${opts.options.name[0].toUpperCase()}</span>`)
} }
const titleContent = $(`<div title=${opts.options.title} class="title">${opts.options.title}</div>`) const titleContent = $(`<div title=${opts.options.title} class="title">${opts.options.title}</div>`)
const uniqueDom = $(`<div class="unique">${opts.options.unique_alias || opts.options.unique_name}${opts.options.unique_value}<div>`) const uniqueDom = $(`<div class="unique">${opts.options.unique_alias || opts.options.unique_name}${opts.options.unique_value}<div>`)
container.append(icon) container.append(icon)
container.append(titleContent) container.append(titleContent)
container.append(uniqueDom) container.append(uniqueDom)
if (opts.options.side && !opts.options.children.length) { if (opts.options.side && (!opts.options.children.length && !(opts.options.edges && opts.options.edges.length && opts.options.edges.find(e => e.source === opts.options.side && e.sourceNode === opts.options.id)))) {
const addIcon = $(`<i aria-label="图标: plus-square" class="anticon anticon-plus-square add-icon-${opts.options.side}"><svg viewBox="64 64 896 896" data-icon="plus-square" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><path d="M328 544h152v152c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V544h152c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8H544V328c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v152H328c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8z"></path><path d="M880 112H144c-17.7 0-32 14.3-32 32v736c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V144c0-17.7-14.3-32-32-32zm-40 728H184V184h656v656z"></path></svg></i>`) const addIcon = $(`<i aria-label="图标: plus-square" class="anticon anticon-plus-square add-icon-${opts.options.side}"><svg viewBox="64 64 896 896" data-icon="plus-square" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><path d="M328 544h152v152c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V544h152c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8H544V328c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v152H328c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8z"></path><path d="M880 112H144c-17.7 0-32 14.3-32 32v736c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V144c0-17.7-14.3-32-32-32zm-40 728H184V184h656v656z"></path></svg></i>`)
container.append(addIcon) container.append(addIcon)
addIcon.on('click', () => { addIcon.on('click', () => {
if (opts.options.side === 'left') { if (opts.options.side === 'left') {
this.emit('events', { this.emit('events', {
type: 'custom:clickLeft', type: 'custom:clickLeft',
data: { ...this } data: { ...this }
}) })
} }
if (opts.options.side === 'right') { if (opts.options.side === 'right') {
this.emit('events', { this.emit('events', {
type: 'custom:clickRight', type: 'custom:clickRight',
data: { ...this } data: { ...this }
}) })
} }
}) })
} }
return container[0] return container[0]
} }
} }
export default BaseNode export default BaseNode