StructV2/demoV2/Layouter/GeneralizedList.js

241 lines
6.6 KiB
JavaScript
Raw Normal View History

2021-05-17 06:02:19 +00:00
SV.registerShape('three-cell-node', {
draw(cfg, group) {
cfg.size = cfg.size || [30, 10];
const width = cfg.size[0],
height = cfg.size[1];
const wrapperRect = group.addShape('rect', {
attrs: {
x: width / 2,
y: height / 2,
width: width,
height: height,
stroke: cfg.style.stroke,
fill: '#eee'
},
name: 'wrapper',
draggable: true
});
group.addShape('rect', {
attrs: {
x: width / 2,
y: height / 2,
width: width / 3,
height: height,
fill: cfg.style.fill,
stroke: cfg.style.stroke
},
name: 'left-rect',
draggable: true
});
group.addShape('rect', {
attrs: {
x: width * (5 / 6),
y: height / 2,
width: width / 3,
height: height,
fill: '#eee',
stroke: cfg.style.stroke
},
name: 'mid-rect',
draggable: true
});
if (cfg.label) {
const style = (cfg.labelCfg && cfg.labelCfg.style) || {};
group.addShape('text', {
attrs: {
x: width * (2 / 3),
y: height,
textAlign: 'center',
textBaseline: 'middle',
text: cfg.label,
fill: style.fill || '#000',
fontSize: style.fontSize || 16
},
name: 'text',
draggable: true
2022-02-23 14:48:01 +00:00
});
}
//节点没有原子时
if(!cfg.sub){
group.addShape('text', {
attrs: {
x: width,
y: height * ( 8 / 7),
textAlign: 'center',
textBaseline: 'middle',
text: '^',
fill: '#000',
fontSize: 20,
},
name: 'text',
draggable: true
});
}
//节点没有子表时
if(!cfg.next){
group.addShape('text', {
attrs: {
x: width * (4 / 3),
y: height * ( 8 / 7),
textAlign: 'center',
textBaseline: 'middle',
text: '^',
fill: '#000',
fontSize: 20,
},
name: 'text',
draggable: true
2021-05-17 06:02:19 +00:00
});
}
return wrapperRect;
},
getAnchorPoints() {
return [
2021-07-31 16:43:01 +00:00
[1 / 6, 0],
2021-05-17 06:02:19 +00:00
[0.5, 0],
2021-07-31 16:43:01 +00:00
[0.5, 0.5],
[5 / 6, 0.5],
[0.5, 1],
[0, 0.5]
2021-05-17 06:02:19 +00:00
];
}
});
2021-10-29 07:46:18 +00:00
SV.registerLayout('GeneralizedList', {
2021-05-17 06:02:19 +00:00
defineOptions() {
return {
element: {
2021-07-31 16:43:01 +00:00
table: {
2021-05-17 06:02:19 +00:00
type: 'three-cell-node',
2021-07-31 16:43:01 +00:00
label: '[id]',
2021-05-17 06:02:19 +00:00
size: [90, 30],
style: {
stroke: '#333',
fill: '#b83b5e'
}
},
2021-07-31 16:43:01 +00:00
atom: {
2021-05-17 06:02:19 +00:00
type: 'two-cell-node',
2021-07-31 16:43:01 +00:00
label: ['[id]', 'dcd'],
2021-05-17 06:02:19 +00:00
size: [60, 30],
style: {
stroke: '#333',
fill: '#b83b5e'
}
}
},
link: {
2021-08-17 15:44:28 +00:00
loopSub: {},
2021-09-09 15:22:11 +00:00
loopNext: {
type: 'quadratic',
curveOffset: -50,
sourceAnchor: 2,
targetAnchor: 4,
style: {
stroke: '#333',
endArrow: 'default',
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
},
2021-05-17 06:02:19 +00:00
sub: {
type: 'line',
2021-07-31 16:43:01 +00:00
sourceAnchor: 2,
targetAnchor: 0,
2021-05-17 06:02:19 +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: {
type: 'line',
sourceAnchor: 3,
2021-07-31 16:43:01 +00:00
targetAnchor: 5,
2021-05-17 06:02:19 +00:00
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
}
},
layout: {
xInterval: 40,
yInterval: 20,
}
};
2021-07-31 16:43:01 +00:00
},
2021-05-17 06:02:19 +00:00
/**
* 对子树进行递归布局
* @param node
* @param parent
*/
layoutItem(node, prev, layoutOptions) {
2021-07-31 16:43:01 +00:00
if(!node) {
return;
}
2021-05-17 06:02:19 +00:00
let [width, height] = node.get('size');
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);
}
// 存在子节点
if(node.sub) {
node.sub.set('y', node.get('y') + layoutOptions.yInterval + height);
// 子结点还是广义表
if(node.sub.tag === 1) {
2021-07-31 16:43:01 +00:00
node.sub.set('x', node.get('x') + width / 3);
2021-05-17 06:02:19 +00:00
this.layoutItem(node.sub, null, layoutOptions);
}
else {
let subWidth = node.sub.get('size')[0];
node.sub.set('x', node.get('x') + width - subWidth);
}
}
2021-07-31 16:43:01 +00:00
},
2021-05-17 06:02:19 +00:00
layout(elements, layoutOptions) {
2021-07-31 16:43:01 +00:00
let tableNodes = elements.filter(item => item.type === 'table'),
tableRootNode = tableNodes.filter(item => item.root)[0];
this.layoutItem(tableRootNode, null, layoutOptions);
2021-05-17 06:02:19 +00:00
}
2021-07-31 16:43:01 +00:00
})