fix: fix collapse reference error

This commit is contained in:
elaine 2019-04-28 16:15:29 +08:00
parent 126a471ae8
commit 9ec1e9bd15
3 changed files with 15 additions and 14 deletions

View File

@ -14,17 +14,19 @@ module.exports = {
},
onNodeClick(e) {
const item = e.item;
const model = item.getModel();
const children = model.children;
// 如果节点进行过更新model 会进行 merge直接改 model 就不能改布局,所以需要去改源数据
const sourceData = this.graph.findDataById(item.get('id'));
const children = sourceData.children;
// 叶子节点的收缩和展开没有意义
if (!children || children.length === 0) {
return;
}
const collapsed = !model.collapsed;
const collapsed = !sourceData.collapsed;
if (!this.shouldBegin(e, collapsed)) {
return;
}
model.collapsed = collapsed;
sourceData.collapsed = collapsed;
item.getModel().collapsed = collapsed;
this.graph.emit('itemcollapsed', { item: e.item, collapsed });
if (!this.shouldUpdate(e, collapsed)) {
return;

View File

@ -51,7 +51,7 @@ class TreeGraph extends Graph {
if (!Util.isString(parent)) {
parent = parent.get('id');
}
const parentData = self.findById(parent).getModel();
const parentData = self.findDataById(parent);
if (!parentData.children) {
parentData.children = [];
}
@ -175,8 +175,8 @@ class TreeGraph extends Graph {
y: model.y
});
}
model.x = data.x;
model.y = data.y;
current.set('model', data.data);
current.updatePosition({ x: data.x, y: data.y });
}
/**
* 删除子树
@ -190,7 +190,7 @@ class TreeGraph extends Graph {
}
const parent = node.get('parent');
if (parent && !parent.destroyed) {
const siblings = parent.getModel().children;
const siblings = self.findDataById(parent.get('id')).children;
const index = indexOfChild(siblings, node.getModel());
siblings.splice(index, 1);
}
@ -279,7 +279,7 @@ class TreeGraph extends Graph {
self._updateChild(layoutData, null, animate);
if (!animate) {
// 如果没有动画,目前仅更新了节点的位置,刷新一下边的样式
self.refreshPositions();
self.refresh();
self.paint();
} else {
self.layoutAnimate(layoutData, null);
@ -298,7 +298,6 @@ class TreeGraph extends Graph {
*/
layoutAnimate(data, onFrame) {
const self = this;
const autoPaint = this.get('autoPaint');
this.setAutoPaint(false);
const animateCfg = this.get('animateCfg');
self.emit('beforeanimate', { data });
@ -351,7 +350,7 @@ class TreeGraph extends Graph {
animateCfg.callback();
}
self.paint();
this.setAutoPaint(autoPaint);
this.setAutoPaint(true);
self.emit('afteranimate', { data });
}, animateCfg.delay);
}

View File

@ -188,7 +188,7 @@ describe('tree graph with animate', () => {
expect(edge.get('target')).to.equal(graph.findById('SubTreeNode1'));
expect(graph.save()).to.equal(data);
expect(JSON.stringify(data)).not.to.throw;
graph.on('layoutanimateend', () => { done(); });
graph.on('afteranimate', () => { done(); });
});
it('changeData', done => {
graph.removeEvent();
@ -217,7 +217,7 @@ describe('tree graph with animate', () => {
};
graph.changeData(data);
expect(graph.save()).to.equal(data);
graph.on('layoutanimateend', () => {
graph.on('afteranimate', () => {
expect(Object.keys(graph.get('itemMap')).length).to.equal(13);
expect(graph.findById('SubTreeNode2')).to.be.undefined;
expect(graph.findById('SubTreeNode3')).not.to.be.undefined;
@ -234,7 +234,7 @@ describe('tree graph with animate', () => {
const parent = graph.findById('SubTreeNode1');
let child = graph.findById('SubTreeNode1.1');
let collapsed = true;
graph.on('layoutanimateend', () => {
graph.on('afteranimate', () => {
if (collapsed) {
expect(parent.getModel().collapsed).to.be.true;
expect(child.destroyed).to.be.true;