mermaid/src/diagrams/state/stateDb.js

115 lines
2.1 KiB
JavaScript
Raw Normal View History

import { logger } from '../../logger';
let relations = [];
2019-09-25 21:01:21 +02:00
let states = {};
2019-09-25 21:29:32 +02:00
let startCnt = 0;
let endCnt = 0;
/**
* Function called by parser when a node definition has been found.
* @param id
* @param text
* @param type
* @param style
*/
2019-09-25 21:29:32 +02:00
export const addState = function(id, type) {
2019-09-25 21:01:21 +02:00
if (typeof states[id] === 'undefined') {
states[id] = {
id: id,
2019-09-25 21:29:32 +02:00
descriptions: [],
type
};
}
};
export const clear = function() {
relations = [];
2019-09-25 21:01:21 +02:00
states = {};
};
2019-09-25 21:01:21 +02:00
export const getState = function(id) {
return states[id];
};
2019-09-25 21:29:32 +02:00
export const getStates = function() {
2019-09-25 21:01:21 +02:00
return states;
};
export const getRelations = function() {
2019-09-25 21:29:32 +02:00
// const relations1 = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }];
// return relations;
return relations;
};
2019-09-25 21:29:32 +02:00
export const addRelation = function(_id1, _id2) {
let id1 = _id1;
let id2 = _id2;
let type1 = 'default';
let type2 = 'default';
if (_id1 === '[*]') {
startCnt++;
id1 = 'start' + startCnt;
type1 = 'start';
}
if (_id2 === '[*]') {
endCnt++;
id2 = 'end' + startCnt;
type2 = 'end';
}
console.log(id1, id2);
addState(id1, type1);
addState(id2, type2);
relations.push({ id1, id2 });
};
export const addMember = function(className, member) {
2019-09-25 21:01:21 +02:00
const theState = states[className];
if (typeof member === 'string') {
if (member.substr(-1) === ')') {
2019-09-25 21:01:21 +02:00
theState.methods.push(member);
} else {
2019-09-25 21:01:21 +02:00
theState.members.push(member);
}
}
};
export const addMembers = function(className, MembersArr) {
if (Array.isArray(MembersArr)) {
MembersArr.forEach(member => addMember(className, member));
}
};
export const cleanupLabel = function(label) {
if (label.substring(0, 1) === ':') {
return label.substr(2).trim();
} else {
return label.trim();
}
};
export const lineType = {
LINE: 0,
DOTTED_LINE: 1
};
export const relationType = {
AGGREGATION: 0,
EXTENSION: 1,
COMPOSITION: 2,
DEPENDENCY: 3
};
export default {
2019-09-25 21:01:21 +02:00
addState,
clear,
2019-09-25 21:01:21 +02:00
getState,
2019-09-25 21:29:32 +02:00
getStates,
getRelations,
addRelation,
addMember,
addMembers,
cleanupLabel,
lineType,
relationType
};