mermaid/src/diagrams/class/classDb.js

146 lines
3.5 KiB
JavaScript
Raw Normal View History

import { logger } from '../../logger';
2015-10-26 08:03:30 +01:00
let relations = [];
let classes = {};
2015-11-15 15:06:24 +01:00
2019-12-06 20:35:22 -08:00
const splitClassNameAndType = function(id) {
let genericType = '';
let className = id;
2019-12-06 20:35:22 -08:00
if(id.indexOf('~') > 0) {
let split = id.split('~');
className = split[0];
genericType = split[1];
}
2019-12-06 20:35:22 -08:00
return { className: className, type: genericType };
};
2015-10-26 08:03:30 +01:00
/**
2015-12-27 14:18:21 +01:00
* Function called by parser when a node definition has been found.
2015-10-26 08:03:30 +01:00
* @param id
2019-10-05 12:38:29 +02:00
* @public
2015-10-26 08:03:30 +01:00
*/
export const addClass = function(id) {
let classId = splitClassNameAndType(id);
2019-10-05 12:38:29 +02:00
// Only add class if not exists
if (typeof classes[classId.className] !== 'undefined') return;
2019-10-05 12:38:29 +02:00
classes[classId.className] = {
id: classId.className,
type: classId.type,
2019-10-05 12:38:29 +02:00
methods: [],
members: [],
annotations: []
};
};
2015-10-26 08:03:30 +01:00
export const clear = function() {
relations = [];
classes = {};
};
2015-10-26 08:03:30 +01:00
export const getClass = function(id) {
return classes[id];
};
export const getClasses = function() {
return classes;
};
export const getRelations = function() {
return relations;
};
2015-10-26 08:03:30 +01:00
export const addRelation = function(relation) {
logger.debug('Adding relation: ' + JSON.stringify(relation));
addClass(relation.id1);
addClass(relation.id2);
relation.id1 = splitClassNameAndType(relation.id1).className;
relation.id2 = splitClassNameAndType(relation.id2).className;
relations.push(relation);
};
2019-10-05 12:38:29 +02:00
/**
* Adds an annotation to the specified class
* Annotations mark special properties of the given type (like 'interface' or 'service')
* @param className The class name
* @param annotation The name of the annotation without any brackets
* @public
*/
2019-10-04 21:49:02 +02:00
export const addAnnotation = function(className, annotation) {
const validatedClassName = splitClassNameAndType(className).className;
classes[validatedClassName].annotations.push(annotation);
2019-10-04 21:49:02 +02:00
};
2019-10-05 12:38:29 +02:00
/**
* Adds a member to the specified class
* @param className The class name
* @param member The full name of the member.
* If the member is enclosed in <<brackets>> it is treated as an annotation
* If the member is ending with a closing bracket ) it is treated as a method
* Otherwise the member will be treated as a normal property
* @public
*/
export const addMember = function(className, member) {
const validatedClassName = splitClassNameAndType(className).className;
const theClass = classes[validatedClassName];
if (typeof member === 'string') {
2019-10-05 12:38:29 +02:00
// Member can contain white spaces, we trim them out
2019-10-04 23:49:58 +02:00
const memberString = member.trim();
2019-10-05 12:38:29 +02:00
2019-10-04 23:49:58 +02:00
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
2019-10-05 12:38:29 +02:00
// Remove leading and trailing brackets
2019-10-04 23:49:58 +02:00
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
} else if (memberString.endsWith(')')) {
theClass.methods.push(memberString);
} else if (memberString) {
2019-10-04 23:49:58 +02:00
theClass.members.push(memberString);
}
2017-04-11 22:14:25 +08:00
}
};
2019-10-08 22:57:40 +02:00
export const addMembers = function(className, members) {
if (Array.isArray(members)) {
members.reverse();
members.forEach(member => addMember(className, member));
}
};
export const cleanupLabel = function(label) {
2017-04-11 22:14:25 +08:00
if (label.substring(0, 1) === ':') {
return label.substr(2).trim();
2017-04-11 22:14:25 +08:00
} else {
return label.trim();
2017-04-11 22:14:25 +08:00
}
};
2017-09-10 21:23:04 +08:00
export const lineType = {
2017-04-11 22:14:25 +08:00
LINE: 0,
DOTTED_LINE: 1
};
2015-10-26 08:03:30 +01:00
2017-09-10 21:23:04 +08:00
export const relationType = {
2017-04-11 22:14:25 +08:00
AGGREGATION: 0,
EXTENSION: 1,
COMPOSITION: 2,
DEPENDENCY: 3
};
2017-09-10 21:51:48 +08:00
export default {
addClass,
clear,
getClass,
getClasses,
2019-10-04 21:49:02 +02:00
addAnnotation,
2017-09-10 21:51:48 +08:00
getRelations,
addRelation,
addMember,
2017-09-10 21:51:48 +08:00
addMembers,
cleanupLabel,
lineType,
relationType
};