mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-21 16:29:40 +02:00
#1704 handling of subgraph data
This commit is contained in:
@@ -454,19 +454,8 @@ export const addSubGraph = function(_id, list, _title) {
|
||||
subCount = subCount + 1;
|
||||
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [] };
|
||||
|
||||
/**
|
||||
* Deletes an id from all subgraphs
|
||||
*/
|
||||
const del = _id => {
|
||||
subGraphs.forEach(sg => {
|
||||
const pos = sg.nodes.indexOf(_id);
|
||||
if (pos >= 0) {
|
||||
sg.nodes.splice(pos, 1);
|
||||
}
|
||||
});
|
||||
};
|
||||
// Removes the members of this subgraph from any other subgraphs, a node only belong to one subgraph
|
||||
subGraph.nodes.forEach(_id => del(_id));
|
||||
// Remove the members in the new subgraph if they already belong to another subgraph
|
||||
subGraph.nodes.nodes = makeUniq(subGraph, subGraphs);
|
||||
subGraphs.push(subGraph);
|
||||
subGraphLookup[id] = subGraph;
|
||||
return id;
|
||||
@@ -664,6 +653,31 @@ const destructLink = (_str, _startStr) => {
|
||||
return info;
|
||||
};
|
||||
|
||||
// Todo optimizer this by caching existing nodes
|
||||
const exists = (allSgs, _id) => {
|
||||
let res = false;
|
||||
allSgs.forEach(sg => {
|
||||
const pos = sg.nodes.indexOf(_id);
|
||||
if (pos >= 0) {
|
||||
res = true;
|
||||
}
|
||||
});
|
||||
return res;
|
||||
};
|
||||
/**
|
||||
* Deletes an id from all subgraphs
|
||||
*/
|
||||
const makeUniq = (sg, allSubgraphs) => {
|
||||
const res = [];
|
||||
sg.nodes.forEach((_id, pos) => {
|
||||
console.log('Checking: ', _id);
|
||||
if (!exists(allSubgraphs, _id)) {
|
||||
res.push(sg.nodes[pos]);
|
||||
}
|
||||
});
|
||||
return { nodes: res };
|
||||
};
|
||||
|
||||
export default {
|
||||
parseDirective,
|
||||
defaultConfig: () => configApi.defaultConfig.flowchart,
|
||||
@@ -693,5 +707,7 @@ export default {
|
||||
destructLink,
|
||||
lex: {
|
||||
firstGraph
|
||||
}
|
||||
},
|
||||
exists,
|
||||
makeUniq
|
||||
};
|
||||
|
44
src/diagrams/flowchart/flowDb.spec.js
Normal file
44
src/diagrams/flowchart/flowDb.spec.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import flowDb from './flowDb';
|
||||
|
||||
describe('flow db subgraphs', () => {
|
||||
let subgraphs;
|
||||
beforeEach( ()=>{
|
||||
subgraphs = [
|
||||
{nodes:['a', 'b', 'c', 'e']},
|
||||
{nodes:['f', 'g', 'h']},
|
||||
{nodes:['i', 'j']},
|
||||
{nodes:['k']},
|
||||
];
|
||||
});
|
||||
describe('exist', () => {
|
||||
it('should return true when the is exists in a subgraph', () => {
|
||||
expect(flowDb.exists(subgraphs, 'a')).toBe(true);
|
||||
expect(flowDb.exists(subgraphs, 'h')).toBe(true);
|
||||
expect(flowDb.exists(subgraphs, 'j')).toBe(true);
|
||||
expect(flowDb.exists(subgraphs, 'k')).toBe(true);
|
||||
});
|
||||
it('should return false when the is exists in a subgraph', () => {
|
||||
expect(flowDb.exists(subgraphs, 'a2')).toBe(false);
|
||||
expect(flowDb.exists(subgraphs, 'l')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeUniq', () => {
|
||||
it('should remove ids from sungraph that already exists in another subgraph even if it gets empty', () => {
|
||||
const subgraph = flowDb.makeUniq({nodes:['i', 'j']}, subgraphs);
|
||||
|
||||
expect(subgraph.nodes).toEqual([]);
|
||||
});
|
||||
it('should remove ids from sungraph that already exists in another subgraph', () => {
|
||||
const subgraph = flowDb.makeUniq({nodes:['i', 'j', 'o']}, subgraphs);
|
||||
|
||||
expect(subgraph.nodes).toEqual(['o']);
|
||||
});
|
||||
it('should not remove ids from subgraph if they are unique', () => {
|
||||
const subgraph = flowDb.makeUniq({nodes:['q', 'r', 's']}, subgraphs);
|
||||
|
||||
expect(subgraph.nodes).toEqual(['q', 'r', 's']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user