2020-05-11 07:10:04 +01:00
|
|
|
import { select } from 'd3';
|
2019-09-12 12:58:32 -07:00
|
|
|
import { logger } from '../../logger';
|
2020-07-29 18:38:59 +02:00
|
|
|
import * as configApi from '../../config';
|
2020-02-21 13:49:05 -08:00
|
|
|
import common from '../common/common';
|
2020-01-02 11:04:37 -08:00
|
|
|
import utils from '../../utils';
|
2020-07-27 05:29:21 -04:00
|
|
|
import mermaidAPI from '../../mermaidAPI';
|
2019-12-30 17:24:19 -08:00
|
|
|
|
2020-01-02 11:04:37 -08:00
|
|
|
const MERMAID_DOM_ID_PREFIX = 'classid-';
|
2019-12-30 17:24:19 -08:00
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
const config = configApi.getConfig();
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
let relations = [];
|
|
|
|
let classes = {};
|
2020-01-08 20:48:57 +01:00
|
|
|
let classCounter = 0;
|
2015-11-15 15:06:24 +01:00
|
|
|
|
2019-12-30 17:24:19 -08:00
|
|
|
let funs = [];
|
|
|
|
|
2020-07-27 05:29:21 -04:00
|
|
|
export const parseDirective = function(statement, context, type) {
|
2020-07-27 06:50:54 -04:00
|
|
|
mermaidAPI.parseDirective(this, statement, context, type);
|
2020-07-27 05:29:21 -04:00
|
|
|
};
|
|
|
|
|
2019-12-06 20:35:22 -08:00
|
|
|
const splitClassNameAndType = function(id) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let genericType = '';
|
2019-12-10 15:21:25 -08:00
|
|
|
let className = id;
|
2019-12-10 15:18:26 -08:00
|
|
|
|
2019-12-10 15:12:37 -08:00
|
|
|
if (id.indexOf('~') > 0) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let split = id.split('~');
|
|
|
|
className = split[0];
|
2020-08-03 12:41:10 -07:00
|
|
|
|
2019-12-05 12:55:46 -08:00
|
|
|
genericType = split[1];
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:35:22 -08:00
|
|
|
return { className: className, type: genericType };
|
|
|
|
};
|
2019-12-05 12:55:46 -08:00
|
|
|
|
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
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addClass = function(id) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let classId = splitClassNameAndType(id);
|
2019-10-05 12:38:29 +02:00
|
|
|
// Only add class if not exists
|
2019-12-05 12:55:46 -08:00
|
|
|
if (typeof classes[classId.className] !== 'undefined') return;
|
2019-10-05 12:38:29 +02:00
|
|
|
|
2019-12-05 12:55:46 -08:00
|
|
|
classes[classId.className] = {
|
|
|
|
id: classId.className,
|
|
|
|
type: classId.type,
|
2019-12-30 17:24:19 -08:00
|
|
|
cssClasses: [],
|
2019-10-05 12:38:29 +02:00
|
|
|
methods: [],
|
|
|
|
members: [],
|
2020-01-08 20:48:57 +01:00
|
|
|
annotations: [],
|
|
|
|
domId: MERMAID_DOM_ID_PREFIX + classId.className + '-' + classCounter
|
2019-10-05 12:38:29 +02:00
|
|
|
};
|
2020-08-03 12:41:10 -07:00
|
|
|
|
2020-01-08 20:48:57 +01:00
|
|
|
classCounter++;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to lookup domId from id in the graph definition.
|
|
|
|
* @param id
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export const lookUpDomId = function(id) {
|
|
|
|
const classKeys = Object.keys(classes);
|
|
|
|
for (let i = 0; i < classKeys.length; i++) {
|
|
|
|
if (classes[classKeys[i]].id === id) {
|
|
|
|
return classes[classKeys[i]].domId;
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const clear = function() {
|
|
|
|
relations = [];
|
|
|
|
classes = {};
|
2019-12-30 17:24:19 -08:00
|
|
|
funs = [];
|
|
|
|
funs.push(setupToolTips);
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const getClass = function(id) {
|
|
|
|
return classes[id];
|
|
|
|
};
|
|
|
|
export const getClasses = function() {
|
|
|
|
return classes;
|
|
|
|
};
|
2015-10-29 07:49:08 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const getRelations = function() {
|
|
|
|
return relations;
|
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addRelation = function(relation) {
|
|
|
|
logger.debug('Adding relation: ' + JSON.stringify(relation));
|
|
|
|
addClass(relation.id1);
|
|
|
|
addClass(relation.id2);
|
2019-12-05 12:55:46 -08:00
|
|
|
|
|
|
|
relation.id1 = splitClassNameAndType(relation.id1).className;
|
|
|
|
relation.id2 = splitClassNameAndType(relation.id2).className;
|
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
relations.push(relation);
|
|
|
|
};
|
2015-10-28 08:12:47 +01:00
|
|
|
|
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) {
|
2019-12-05 12:55:46 -08:00
|
|
|
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
|
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addMember = function(className, member) {
|
2019-12-05 12:55:46 -08:00
|
|
|
const validatedClassName = splitClassNameAndType(className).className;
|
|
|
|
const theClass = classes[validatedClassName];
|
|
|
|
|
2018-10-25 21:30:28 +02:00
|
|
|
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));
|
2019-12-09 18:13:06 -08:00
|
|
|
} else if (memberString.indexOf(')') > 0) {
|
2019-10-04 23:49:58 +02:00
|
|
|
theClass.methods.push(memberString);
|
2019-10-09 21:15:17 +02:00
|
|
|
} else if (memberString) {
|
2019-10-04 23:49:58 +02:00
|
|
|
theClass.members.push(memberString);
|
2015-11-01 19:00:14 +01:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-28 08:12:47 +01: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));
|
2018-10-25 21:30:28 +02:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2018-10-25 21:30:28 +02:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const cleanupLabel = function(label) {
|
2017-04-11 22:14:25 +08:00
|
|
|
if (label.substring(0, 1) === ':') {
|
2020-01-09 16:24:47 +01:00
|
|
|
return label.substr(1).trim();
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
2019-09-12 12:58:32 -07:00
|
|
|
return label.trim();
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-11-11 13:16:38 +01:00
|
|
|
|
2019-12-30 17:24:19 -08:00
|
|
|
/**
|
|
|
|
* Called by parser when a special node is found, e.g. a clickable element.
|
|
|
|
* @param ids Comma separated list of ids
|
|
|
|
* @param className Class to add
|
|
|
|
*/
|
|
|
|
export const setCssClass = function(ids, className) {
|
|
|
|
ids.split(',').forEach(function(_id) {
|
|
|
|
let id = _id;
|
|
|
|
if (_id[0].match(/\d/)) id = MERMAID_DOM_ID_PREFIX + id;
|
|
|
|
if (typeof classes[id] !== 'undefined') {
|
|
|
|
classes[id].cssClasses.push(className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by parser when a link is found. Adds the URL to the vertex data.
|
|
|
|
* @param ids Comma separated list of ids
|
|
|
|
* @param linkStr URL to create a link for
|
|
|
|
* @param tooltip Tooltip for the clickable element
|
|
|
|
*/
|
|
|
|
export const setLink = function(ids, linkStr, tooltip) {
|
|
|
|
ids.split(',').forEach(function(_id) {
|
|
|
|
let id = _id;
|
|
|
|
if (_id[0].match(/\d/)) id = MERMAID_DOM_ID_PREFIX + id;
|
|
|
|
if (typeof classes[id] !== 'undefined') {
|
2020-01-02 11:04:37 -08:00
|
|
|
classes[id].link = utils.formatUrl(linkStr, config);
|
2019-12-30 17:24:19 -08:00
|
|
|
|
|
|
|
if (tooltip) {
|
2020-02-21 13:49:05 -08:00
|
|
|
classes[id].tooltip = common.sanitizeText(tooltip, config);
|
2019-12-30 17:24:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setCssClass(ids, 'clickable');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by parser when a click definition is found. Registers an event handler.
|
|
|
|
* @param ids Comma separated list of ids
|
|
|
|
* @param functionName Function to be called on click
|
|
|
|
* @param tooltip Tooltip for the clickable element
|
|
|
|
*/
|
|
|
|
export const setClickEvent = function(ids, functionName, tooltip) {
|
|
|
|
ids.split(',').forEach(function(id) {
|
|
|
|
setClickFunc(id, functionName, tooltip);
|
|
|
|
});
|
|
|
|
setCssClass(ids, 'clickable');
|
|
|
|
};
|
|
|
|
|
2020-01-08 20:48:57 +01:00
|
|
|
const setClickFunc = function(domId, functionName, tooltip) {
|
|
|
|
let id = domId;
|
|
|
|
let elemId = lookUpDomId(id);
|
2020-01-02 11:04:37 -08:00
|
|
|
|
2019-12-30 17:24:19 -08:00
|
|
|
if (config.securityLevel !== 'loose') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof functionName === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof classes[id] !== 'undefined') {
|
2020-01-02 11:04:37 -08:00
|
|
|
if (tooltip) {
|
2020-02-21 13:49:05 -08:00
|
|
|
classes[id].tooltip = common.sanitizeText(tooltip, config);
|
2020-01-02 11:04:37 -08:00
|
|
|
}
|
|
|
|
|
2019-12-30 17:24:19 -08:00
|
|
|
funs.push(function() {
|
2020-01-02 11:04:37 -08:00
|
|
|
const elem = document.querySelector(`[id="${elemId}"]`);
|
2019-12-30 17:24:19 -08:00
|
|
|
if (elem !== null) {
|
|
|
|
elem.addEventListener(
|
|
|
|
'click',
|
|
|
|
function() {
|
2020-06-13 17:41:52 +07:00
|
|
|
utils.runFunc(functionName, elemId);
|
2019-12-30 17:24:19 -08:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const bindFunctions = function(element) {
|
|
|
|
funs.forEach(function(fun) {
|
|
|
|
fun(element);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const lineType = {
|
2017-04-11 22:14:25 +08:00
|
|
|
LINE: 0,
|
|
|
|
DOTTED_LINE: 1
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
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
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2017-09-10 21:51:48 +08:00
|
|
|
|
2019-12-30 17:24:19 -08:00
|
|
|
const setupToolTips = function(element) {
|
2020-05-11 07:10:04 +01:00
|
|
|
let tooltipElem = select('.mermaidTooltip');
|
2019-12-30 17:24:19 -08:00
|
|
|
if ((tooltipElem._groups || tooltipElem)[0][0] === null) {
|
2020-05-11 07:10:04 +01:00
|
|
|
tooltipElem = select('body')
|
2019-12-30 17:24:19 -08:00
|
|
|
.append('div')
|
|
|
|
.attr('class', 'mermaidTooltip')
|
|
|
|
.style('opacity', 0);
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:10:04 +01:00
|
|
|
const svg = select(element).select('svg');
|
2019-12-30 17:24:19 -08:00
|
|
|
|
|
|
|
const nodes = svg.selectAll('g.node');
|
|
|
|
nodes
|
|
|
|
.on('mouseover', function() {
|
2020-05-11 07:10:04 +01:00
|
|
|
const el = select(this);
|
2019-12-30 17:24:19 -08:00
|
|
|
const title = el.attr('title');
|
|
|
|
// Dont try to draw a tooltip if no data is provided
|
|
|
|
if (title === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const rect = this.getBoundingClientRect();
|
|
|
|
|
|
|
|
tooltipElem
|
|
|
|
.transition()
|
|
|
|
.duration(200)
|
|
|
|
.style('opacity', '.9');
|
|
|
|
tooltipElem
|
|
|
|
.html(el.attr('title'))
|
2020-06-11 15:59:02 +07:00
|
|
|
.style('left', window.scrollX + rect.left + (rect.right - rect.left) / 2 + 'px')
|
|
|
|
.style('top', window.scrollY + rect.top - 14 + document.body.scrollTop + 'px');
|
2019-12-30 17:24:19 -08:00
|
|
|
el.classed('hover', true);
|
|
|
|
})
|
|
|
|
.on('mouseout', function() {
|
|
|
|
tooltipElem
|
|
|
|
.transition()
|
|
|
|
.duration(500)
|
|
|
|
.style('opacity', 0);
|
2020-05-11 07:10:04 +01:00
|
|
|
const el = select(this);
|
2019-12-30 17:24:19 -08:00
|
|
|
el.classed('hover', false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
funs.push(setupToolTips);
|
|
|
|
|
2017-09-10 21:51:48 +08:00
|
|
|
export default {
|
2020-07-27 05:29:21 -04:00
|
|
|
parseDirective,
|
2020-07-27 13:24:23 -04:00
|
|
|
getConfig: () => configApi.getConfig().class,
|
2017-09-10 21:51:48 +08:00
|
|
|
addClass,
|
2019-12-30 17:24:19 -08:00
|
|
|
bindFunctions,
|
2017-09-10 21:51:48 +08:00
|
|
|
clear,
|
|
|
|
getClass,
|
|
|
|
getClasses,
|
2019-10-04 21:49:02 +02:00
|
|
|
addAnnotation,
|
2017-09-10 21:51:48 +08:00
|
|
|
getRelations,
|
|
|
|
addRelation,
|
2018-10-25 21:30:28 +02:00
|
|
|
addMember,
|
2017-09-10 21:51:48 +08:00
|
|
|
addMembers,
|
|
|
|
cleanupLabel,
|
|
|
|
lineType,
|
2019-12-30 17:24:19 -08:00
|
|
|
relationType,
|
|
|
|
setClickEvent,
|
|
|
|
setCssClass,
|
2020-01-08 20:48:57 +01:00
|
|
|
setLink,
|
|
|
|
lookUpDomId
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|