feat: update combo tree structure for moving subtree.

This commit is contained in:
Yanyan-Wang 2020-04-09 10:49:55 +08:00 committed by Yanyan Wang
parent f0f97d4c00
commit 26e5601451
6 changed files with 50 additions and 22 deletions

View File

@ -864,8 +864,7 @@ export default class Graph extends EventEmitter implements IGraph {
});
// const comboGroup = this.get('comboGroup')
// comboGroup && comboGroup.sort();
}
else if (type === 'node' && isString(model.comboId) && comboTrees) {
} else if (type === 'node' && isString(model.comboId) && comboTrees) {
const parentCombo = this.findById(model.comboId as string);
if (!parentCombo || parentCombo.getType() !== 'combo') {
console.warn(`The combo ${model.comboId} for the node ${model.id} does not exist, please add the combo first.`);
@ -1243,15 +1242,21 @@ export default class Graph extends EventEmitter implements IGraph {
/**
*
* @param {String | Item} item Combo id
* @param {String | INode | ICombo} item Combo id
* @param {string | undefined} parentId combo idundefined combo
*/
public updateComboTree(item: String | INode | ICombo, parentId: String | undefined) {
const self = this;
let uItem: INode | ICombo;
if (isString(item)) {
item = self.findById(item) as INode | ICombo;
uItem = self.findById(item) as INode | ICombo;
} else {
uItem = item as INode | ICombo;
}
self.removeItem(item as INode | ICombo);
const newComboTrees = reconstructTree(this.get('comboTrees'), uItem.getModel().id, parentId);
this.set('comboTrees', newComboTrees);
this.updateCombos();
}
/**

View File

@ -13,6 +13,7 @@ import {
ShapeStyle,
Indexable,
ComboConfig,
ITEM_TYPE,
} from '../types';
// item 的配置项
@ -137,7 +138,7 @@ export interface IItemBase {
*
* @return {string}
*/
getType(): string;
getType(): ITEM_TYPE;
getShapeCfg(model: ModelConfig): ModelConfig;

View File

@ -3,7 +3,7 @@ import { each, isNil, isPlainObject,
isString, isBoolean, uniqueId, mix } from '@antv/util'
import { IItemBase, IItemBaseConfig } from '../interface/item';
import Shape from '../shape/shape';
import { IBBox, IPoint, IShapeBase, ModelConfig, ShapeStyle, Indexable, NodeConfig, EdgeConfig, ComboConfig } from '../types';
import { IBBox, IPoint, IShapeBase, ModelConfig, ShapeStyle, Indexable, NodeConfig, EdgeConfig, ComboConfig, ITEM_TYPE } from '../types';
import { getBBox } from '../util/graphic';
import { translate } from '../util/math';
@ -465,7 +465,7 @@ export default class ItemBase implements IItemBase {
*
* @return {string}
*/
public getType(): string {
public getType(): ITEM_TYPE {
return this.get('type');
}

View File

@ -9,7 +9,6 @@ import {
getEllipseIntersectByPoint,
getRectIntersectByPoint,
} from '../util/math';
import Edge from './edge';
import Item from './item';
const CACHE_ANCHOR_POINTS = 'anchorPointsCache';
@ -50,17 +49,17 @@ export default class Node extends Item implements INode {
/**
*
*/
public getInEdges(): Edge[] {
public getInEdges(): IEdge[] {
const self = this;
return this.get('edges').filter((edge: Edge) => edge.get('target') === self);
return this.get('edges').filter((edge: IEdge) => edge.get('target') === self);
}
/**
*
*/
public getOutEdges(): Edge[] {
public getOutEdges(): IEdge[] {
const self = this;
return this.get('edges').filter((edge: Edge) => edge.get('source') === self);
return this.get('edges').filter((edge: IEdge) => edge.get('source') === self);
}
/**

View File

@ -525,11 +525,13 @@ export const plainCombosToTrees = (array: ComboConfig[], nodes?: INode[]) => {
return result;
}
export const reconstructTree = (trees: ComboTree[]): ComboTree[] => {
export const reconstructTree = (trees: ComboTree[], subtreeId?: String, newParentId?: String | undefined): ComboTree[] => {
let brothers = trees;
let subtree;
trees.forEach(tree => {
traverseTree<ComboTree>(tree, child => {
if (child && child.removed && brothers) {
if (child && (child.removed || subtreeId === child.id) && brothers) {
subtree = child;
const index = brothers.indexOf(child);
brothers.splice(index, 1);
}
@ -537,6 +539,25 @@ export const reconstructTree = (trees: ComboTree[]): ComboTree[] => {
return true;
});
});
// append to new parent
if (subtreeId) {
let found = false;
// newParentId is undefined means the subtree will have no parent
if (newParentId) {
trees.forEach(tree => {
traverseTree<ComboTree>(tree, child => {
if (newParentId === child.id) {
found = true;
if (child.children) child.children.push(subtree);
else child.children = [ subtree ];
}
return true;
});
});
} else if (!newParentId || !found) {
trees.push(subtree);
}
}
return trees;
}

View File

@ -237,13 +237,13 @@ const DefaultCombo = () => {
graph.on('canvas:click', e => {
// graph.setItemState(graph.findById('A'), 'selected', true);
// console.log( graph.findAllByState('combo', 'selected'))
const hidedCombos = graph.findAll('combo', combo => {
if (!combo.isVisible()) return true;
return false;
});
hidedCombos.forEach(combo => {
graph.showItem(combo);
})
// const hidedCombos = graph.findAll('combo', combo => {
// if (!combo.isVisible()) return true;
// return false;
// });
// hidedCombos.forEach(combo => {
// graph.showItem(combo);
// })
// console.log(graph.getCombos()[0]);
// console.log(graph.getComboChildren(graph.getCombos()[0]));
//graph.focusItem(graph.getCombos()[0]);
@ -269,6 +269,8 @@ const DefaultCombo = () => {
// });
// console.log(graph.save());
graph.updateComboTree('A', 'M');
});
}
});