StructV2/demo/Layouter/ChainHashTable.js

133 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-04-16 07:38:52 +00:00
/**
2021-04-20 11:12:41 +00:00
* 连地址哈希表
2021-04-16 07:38:52 +00:00
*/
2021-10-29 07:46:18 +00:00
SV.registerLayout('ChainHashTable', {
2021-04-16 07:38:52 +00:00
defineOptions() {
return {
element: {
2021-04-20 11:12:41 +00:00
head: {
type: 'two-cell-node',
2021-04-16 07:38:52 +00:00
label: '[id]',
2021-04-20 11:12:41 +00:00
size: [70, 40],
2021-04-16 07:38:52 +00:00
style: {
stroke: '#333',
fill: '#b83b5e'
}
},
node: {
type: 'link-list-node',
label: '[id]',
size: [60, 30],
style: {
stroke: '#333',
fill: '#b83b5e'
}
}
},
link: {
2021-04-20 11:12:41 +00:00
start: {
type: 'line',
sourceAnchor: 1,
2021-12-22 13:56:52 +00:00
targetAnchor: 6,
2021-04-20 11:12:41 +00:00
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
},
next: {
2021-04-16 07:38:52 +00:00
type: 'line',
2021-07-31 16:43:01 +00:00
sourceAnchor: 2,
targetAnchor: 6,
2021-04-16 07:38:52 +00:00
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
}
},
pointer: {
external: {
2021-07-31 16:43:01 +00:00
anchor: 3,
2021-04-20 11:12:41 +00:00
offset: 8,
2021-04-16 07:38:52 +00:00
style: {
fill: '#f08a5d'
}
}
},
layout: {
xInterval: 50,
yInterval: 50
2021-04-20 11:12:41 +00:00
},
2021-12-22 13:56:52 +00:00
behavior: {
2021-04-20 11:12:41 +00:00
dragNode: ['node']
2021-04-16 07:38:52 +00:00
}
};
2021-07-31 16:43:01 +00:00
},
2021-04-16 07:38:52 +00:00
/**
* 对子树进行递归布局
* @param node
* @param parent
*/
layoutItem(node, prev, layoutOptions) {
if(!node) {
return null;
}
let width = node.get('size')[0];
if(prev) {
node.set('y', prev.get('y'));
node.set('x', prev.get('x') + layoutOptions.xInterval + width);
}
if(node.next) {
this.layoutItem(node.next, node, layoutOptions);
}
2021-07-31 16:43:01 +00:00
},
2021-04-16 07:38:52 +00:00
layout(elements, layoutOptions) {
2021-05-17 06:02:19 +00:00
let headNode = elements.filter(item => item.type === 'head');
2021-04-20 11:12:41 +00:00
for(let i = 0; i < headNode.length; i++) {
let node = headNode[i],
height = node.get('size')[1];
2021-04-16 07:38:52 +00:00
2021-04-20 11:12:41 +00:00
node.set('y', node.get('y') + i * height);
2021-04-16 07:38:52 +00:00
2021-04-20 11:12:41 +00:00
if(node.start) {
let y = node.get('y') + height - node.start.get('size')[1],
x = layoutOptions.xInterval * 2.5;
node.start.set({ x, y });
this.layoutItem(node.start, null, layoutOptions);
}
2021-04-16 07:38:52 +00:00
}
}
2021-07-31 16:43:01 +00:00
});
2021-04-20 11:12:41 +00:00
2021-04-16 07:38:52 +00:00