StructV2/demo/Layouter/PCTree.js

198 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-03-19 16:37:28 +00:00
SV.registerLayout('PCTree', {
2022-01-25 18:09:13 +00:00
2022-03-19 16:37:28 +00:00
sourcesPreprocess(sources) {
2022-01-25 18:09:13 +00:00
2022-03-19 16:37:28 +00:00
let headNodes = sources.filter(item => item.type === 'PCTreeHead');
2022-01-25 18:09:13 +00:00
2022-03-19 16:37:28 +00:00
for(let i = 0; i < headNodes.length; i++){
2022-02-17 14:30:44 +00:00
2022-03-19 16:37:28 +00:00
let dataNode = {
type: 'PCTreePreHead',
id: headNodes[i].id + '_0',
data: headNodes[i].preData,
indexLeft: headNodes[i].index,
root: headNodes[i].root
2022-02-17 14:30:44 +00:00
}
2022-03-19 16:37:28 +00:00
if(dataNode.root){
dataNode.indexTop = 'data';
headNodes[i].indexTop = ' parent firstChild'
}
sources.push(dataNode)
2022-02-17 14:30:44 +00:00
}
2022-01-25 18:09:13 +00:00
return sources;
},
defineOptions() {
return {
node: {
2022-03-19 16:37:28 +00:00
PCTreePreHead: {
type: 'rect',
label: '[data]',
size: [60, 34],
labelOptions: {
style: { fontSize: 16 }
},
2022-01-25 18:09:13 +00:00
style: {
stroke: '#333',
2022-03-19 16:37:28 +00:00
fill: '#95e1d3',
offset: 25
}
},
PCTreeHead: {
type: 'two-cell-node',
label: '[data]',
size: [120, 34],
style: {
stroke: '#333',
2022-01-25 18:09:13 +00:00
fill: '#95e1d3'
}
},
PCTreeNode: {
type: 'link-list-node',
label: '[data]',
size: [60, 27],
style: {
stroke: '#333',
2022-02-17 14:08:05 +00:00
fill: '#00AF92'
2022-01-25 18:09:13 +00:00
}
}
},
2022-03-19 16:37:28 +00:00
indexLabel: {
indexTop: { position: 'top' },
indexLeft: { position: 'left' }
},
2022-01-25 18:09:13 +00:00
link: {
headNext: {
sourceAnchor: 1,
targetAnchor: 6,
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
},
next: {
sourceAnchor: 2,
targetAnchor: 6,
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
}
},
2022-03-19 16:37:28 +00:00
marker: {
external: {
type: 'pointer',
anchor: 0,
offset: 8,
style: {
fill: '#f08a5d'
}
}
},
2022-01-25 18:09:13 +00:00
layout: {
2022-03-19 16:37:28 +00:00
xInterval: 50,
yInterval: 86
2022-01-25 18:09:13 +00:00
},
behavior: {
2022-02-17 14:08:05 +00:00
dragNode: ['PCTreeNode']
2022-01-25 18:09:13 +00:00
}
};
},
2022-03-19 16:37:28 +00:00
//判断node节点是否之前布局过
isUnique(value, allNodeIdValue){
let re = new RegExp("" + value);
return !re.test(allNodeIdValue);
},
2022-01-25 18:09:13 +00:00
/**
* 对子树进行递归布局
* @param node
* @param parent
*/
2022-03-19 16:37:28 +00:00
layoutItem(node, prev, layoutOptions, allNodeId) {
2022-01-25 18:09:13 +00:00
if(!node) {
return null;
}
2022-03-19 16:37:28 +00:00
let width = node.get('size')[0],
idValue = node.id.split('(')[1].slice(0, -1);
//有y型链表的情况不用再布局
if(this.isUnique(idValue, allNodeId.value)){
if(prev) {
node.set('y', prev.get('y'));
node.set('x', prev.get('x') + layoutOptions.xInterval + width);
}
allNodeId.value += idValue;
if(node.next) {
this.layoutItem(node.next, node, layoutOptions, allNodeId);
}
2022-01-25 18:09:13 +00:00
}
},
layout(elements, layoutOptions) {
let headNode = elements.filter(item => item.type === 'PCTreeHead'),
2022-03-19 16:37:28 +00:00
preHeadNode = elements.filter(item => item.type === 'PCTreePreHead'),
roots = elements.filter(item => item.type === 'PCTreeNode' && item.root),
height = headNode[0].get('size')[1],
width = headNode[0].get('size')[0],
i,
allNodeId = { value: ''}; //引用类型用于传参
2022-01-25 18:09:13 +00:00
for(i = 0; i < headNode.length; i++) {
let node = headNode[i],
2022-03-19 16:37:28 +00:00
preNode = preHeadNode[i];
2022-01-25 18:09:13 +00:00
node.set({
x: 0,
2022-03-19 16:37:28 +00:00
y: i * height
2022-01-25 18:09:13 +00:00
});
2022-03-19 16:37:28 +00:00
preNode.set({
x: width / 4,
y: (i + 1) * height
})
2022-01-25 18:09:13 +00:00
if(node.headNext) {
let y = node.get('y') + height - node.headNext.get('size')[1],
2022-03-19 16:37:28 +00:00
x = width + layoutOptions.xInterval * 2;
2022-01-25 18:09:13 +00:00
node.headNext.set({ x, y });
2022-03-19 16:37:28 +00:00
this.layoutItem(node.headNext, null, layoutOptions, allNodeId);
2022-01-25 18:09:13 +00:00
}
}
2022-03-19 16:37:28 +00:00
for(i = 0; i < roots.length; i++) {
let nodeWidth = roots[0].get('size')[0],
nodeInternalSum = i * (nodeWidth + layoutOptions.xInterval);
roots[i].set({
x: headNode[0].get('x') + width + layoutOptions.xInterval * 2 + nodeInternalSum,
y: headNode[0].get('y') - layoutOptions.yInterval
})
}
2022-01-25 18:09:13 +00:00
}
});