mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-22 01:36:43 +02:00
added some documentation
This commit is contained in:
@@ -6,16 +6,18 @@ let classes = {};
|
|||||||
/**
|
/**
|
||||||
* Function called by parser when a node definition has been found.
|
* Function called by parser when a node definition has been found.
|
||||||
* @param id
|
* @param id
|
||||||
|
* @public
|
||||||
*/
|
*/
|
||||||
export const addClass = function(id) {
|
export const addClass = function(id) {
|
||||||
if (typeof classes[id] === 'undefined') {
|
// Only add class if not exists
|
||||||
classes[id] = {
|
if (typeof classes[id] !== 'undefined') return;
|
||||||
id: id,
|
|
||||||
methods: [],
|
classes[id] = {
|
||||||
members: [],
|
id: id,
|
||||||
annotations: []
|
methods: [],
|
||||||
};
|
members: [],
|
||||||
}
|
annotations: []
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clear = function() {
|
export const clear = function() {
|
||||||
@@ -41,15 +43,34 @@ export const addRelation = function(relation) {
|
|||||||
relations.push(relation);
|
relations.push(relation);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
export const addAnnotation = function(className, annotation) {
|
export const addAnnotation = function(className, annotation) {
|
||||||
classes[className].annotations.push(annotation);
|
classes[className].annotations.push(annotation);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
export const addMember = function(className, member) {
|
||||||
const theClass = classes[className];
|
const theClass = classes[className];
|
||||||
if (typeof member === 'string') {
|
if (typeof member === 'string') {
|
||||||
|
// Member can contain white spaces, we trim them out
|
||||||
const memberString = member.trim();
|
const memberString = member.trim();
|
||||||
|
|
||||||
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
|
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
|
||||||
|
// Remove leading and trailing brackets
|
||||||
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
|
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
|
||||||
} else if (memberString.endsWith(')')) {
|
} else if (memberString.endsWith(')')) {
|
||||||
theClass.methods.push(memberString);
|
theClass.methods.push(memberString);
|
||||||
|
@@ -255,10 +255,13 @@ const drawClass = function(elem, classDef) {
|
|||||||
height: 0
|
height: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// add class group
|
||||||
const g = elem
|
const g = elem
|
||||||
.append('g')
|
.append('g')
|
||||||
.attr('id', id)
|
.attr('id', id)
|
||||||
.attr('class', 'classGroup');
|
.attr('class', 'classGroup');
|
||||||
|
|
||||||
|
// add title
|
||||||
const title = g
|
const title = g
|
||||||
.append('text')
|
.append('text')
|
||||||
.attr('y', conf.textHeight + conf.padding)
|
.attr('y', conf.textHeight + conf.padding)
|
||||||
@@ -278,6 +281,7 @@ const drawClass = function(elem, classDef) {
|
|||||||
.text(classDef.id)
|
.text(classDef.id)
|
||||||
.attr('class', 'title');
|
.attr('class', 'title');
|
||||||
|
|
||||||
|
// If class has annotations the title needs to have an offset of the text height
|
||||||
if (!isFirst) classTitle.attr('dy', conf.textHeight);
|
if (!isFirst) classTitle.attr('dy', conf.textHeight);
|
||||||
|
|
||||||
const titleHeight = title.node().getBBox().height;
|
const titleHeight = title.node().getBBox().height;
|
||||||
@@ -324,21 +328,25 @@ const drawClass = function(elem, classDef) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const classBox = g.node().getBBox();
|
const classBox = g.node().getBBox();
|
||||||
g.insert('rect', ':first-child')
|
const rect = g
|
||||||
|
.insert('rect', ':first-child')
|
||||||
.attr('x', 0)
|
.attr('x', 0)
|
||||||
.attr('y', 0)
|
.attr('y', 0)
|
||||||
.attr('width', classBox.width + 2 * conf.padding)
|
.attr('width', classBox.width + 2 * conf.padding)
|
||||||
.attr('height', classBox.height + conf.padding + 0.5 * conf.dividerMargin);
|
.attr('height', classBox.height + conf.padding + 0.5 * conf.dividerMargin);
|
||||||
|
|
||||||
|
const rectWidth = rect.node().getBBox().width;
|
||||||
|
|
||||||
// Center title
|
// Center title
|
||||||
|
// We subtract the width of each text element from the class box width and divide it by 2
|
||||||
title.node().childNodes.forEach(function(x) {
|
title.node().childNodes.forEach(function(x) {
|
||||||
x.setAttribute('x', (classBox.width + 2 * conf.padding - x.getBBox().width) / 2);
|
x.setAttribute('x', (rectWidth - x.getBBox().width) / 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
membersLine.attr('x2', classBox.width + 2 * conf.padding);
|
membersLine.attr('x2', rectWidth);
|
||||||
methodsLine.attr('x2', classBox.width + 2 * conf.padding);
|
methodsLine.attr('x2', rectWidth);
|
||||||
|
|
||||||
classInfo.width = classBox.width + 2 * conf.padding;
|
classInfo.width = rectWidth;
|
||||||
classInfo.height = classBox.height + conf.padding + 0.5 * conf.dividerMargin;
|
classInfo.height = classBox.height + conf.padding + 0.5 * conf.dividerMargin;
|
||||||
|
|
||||||
idCache[id] = classInfo;
|
idCache[id] = classInfo;
|
||||||
|
Reference in New Issue
Block a user