mermaid/src/diagrams/sequence/sequenceDb.js

304 lines
7.2 KiB
JavaScript
Raw Normal View History

import mermaidAPI from '../../mermaidAPI';
import * as configApi from '../../config';
import { logger } from '../../logger';
2017-09-10 13:41:34 +02:00
let prevActor = undefined;
2019-09-12 21:55:10 +02:00
let actors = {};
let messages = [];
const notes = [];
let title = '';
let titleWrapped = false;
let sequenceNumbersEnabled = false;
let wrapEnabled = false;
export const parseDirective = function(statement, context, type) {
mermaidAPI.parseDirective(this, statement, context, type);
};
2019-09-12 21:55:10 +02:00
export const addActor = function(id, name, description) {
2017-04-16 17:48:36 +02:00
// Don't allow description nulling
2019-09-12 21:55:10 +02:00
const old = actors[id];
if (old && name === old.name && description == null) return;
2017-04-16 17:48:36 +02:00
// Don't allow null descriptions, either
if (description == null || description.text == null) {
description = { text: name, wrap: null };
}
2019-09-12 21:55:10 +02:00
actors[id] = {
name: name,
description: description.text,
2020-06-17 11:54:24 +02:00
wrap: (description.wrap === undefined && autoWrap()) || !!description.wrap,
prevActor: prevActor
};
if (prevActor && actors[prevActor]) {
actors[prevActor].nextActor = id;
}
prevActor = id;
2019-09-12 21:55:10 +02:00
};
const activationCount = part => {
let i;
let count = 0;
for (i = 0; i < messages.length; i++) {
if (messages[i].type === LINETYPE.ACTIVE_START) {
if (messages[i].from.actor === part) {
count++;
}
}
if (messages[i].type === LINETYPE.ACTIVE_END) {
if (messages[i].from.actor === part) {
count--;
}
}
}
return count;
};
2019-09-12 21:55:10 +02:00
export const addMessage = function(idFrom, idTo, message, answer) {
messages.push({
from: idFrom,
to: idTo,
message: message.text,
2020-06-17 11:54:24 +02:00
wrap: (message.wrap === undefined && autoWrap()) || !!message.wrap,
answer: answer
});
2019-09-12 21:55:10 +02:00
};
2020-06-17 11:54:24 +02:00
export const addSignal = function(
idFrom,
idTo,
message = { text: undefined, wrap: undefined },
messageType
) {
if (messageType === LINETYPE.ACTIVE_END) {
const cnt = activationCount(idFrom.actor);
if (cnt < 1) {
// Bail out as there is an activation signal from an inactive participant
let error = new Error('Trying to inactivate an inactive participant (' + idFrom.actor + ')');
error.hash = {
text: '->>-',
token: '->>-',
line: '1',
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
expected: ["'ACTIVE_PARTICIPANT'"]
};
throw error;
}
}
messages.push({
from: idFrom,
to: idTo,
message: message.text,
2020-06-17 11:54:24 +02:00
wrap: (message.wrap === undefined && autoWrap()) || !!message.wrap,
type: messageType
});
return true;
2019-09-12 21:55:10 +02:00
};
export const getMessages = function() {
return messages;
};
export const getActors = function() {
return actors;
};
export const getActor = function(id) {
return actors[id];
};
export const getActorKeys = function() {
return Object.keys(actors);
};
export const getTitle = function() {
return title;
};
export const getTitleWrapped = function() {
return titleWrapped;
};
export const enableSequenceNumbers = function() {
sequenceNumbersEnabled = true;
};
export const showSequenceNumbers = () => sequenceNumbersEnabled;
2019-09-12 21:55:10 +02:00
2020-06-17 11:54:24 +02:00
export const setWrap = function(wrapSetting) {
wrapEnabled = wrapSetting;
};
export const autoWrap = () => wrapEnabled;
2019-09-12 21:55:10 +02:00
export const clear = function() {
actors = {};
messages = [];
};
export const parseMessage = function(str) {
const _str = str.trim();
const message = {
text: _str.replace(/^[:]?(?:no)?wrap:/, '').trim(),
wrap:
_str.match(/^[:]?wrap:/) !== null
? true
: _str.match(/^[:]?nowrap:/) !== null
? false
: undefined
};
logger.debug('parseMessage:', message);
return message;
2019-09-12 21:55:10 +02:00
};
2014-12-04 17:58:05 +01:00
2017-09-10 15:23:04 +02:00
export const LINETYPE = {
2017-04-11 16:14:25 +02:00
SOLID: 0,
DOTTED: 1,
NOTE: 2,
SOLID_CROSS: 3,
DOTTED_CROSS: 4,
SOLID_OPEN: 5,
DOTTED_OPEN: 6,
LOOP_START: 10,
LOOP_END: 11,
ALT_START: 12,
ALT_ELSE: 13,
ALT_END: 14,
OPT_START: 15,
OPT_END: 16,
ACTIVE_START: 17,
ACTIVE_END: 18,
PAR_START: 19,
PAR_AND: 20,
2019-07-23 07:47:49 +02:00
PAR_END: 21,
RECT_START: 22,
RECT_END: 23,
SOLID_POINT: 24,
DOTTED_POINT: 25
2019-09-12 21:55:10 +02:00
};
2014-12-04 17:58:05 +01:00
2017-09-10 15:23:04 +02:00
export const ARROWTYPE = {
2017-04-11 16:14:25 +02:00
FILLED: 0,
OPEN: 1
2019-09-12 21:55:10 +02:00
};
2014-12-04 17:58:05 +01:00
2017-09-10 15:23:04 +02:00
export const PLACEMENT = {
2017-04-11 16:14:25 +02:00
LEFTOF: 0,
RIGHTOF: 1,
OVER: 2
2019-09-12 21:55:10 +02:00
};
2014-12-04 17:58:05 +01:00
2019-09-12 21:55:10 +02:00
export const addNote = function(actor, placement, message) {
const note = {
actor: actor,
placement: placement,
message: message.text,
2020-06-17 11:54:24 +02:00
wrap: (message.wrap === undefined && autoWrap()) || !!message.wrap
};
2014-12-04 17:58:05 +01:00
2017-04-16 17:48:36 +02:00
// Coerce actor into a [to, from, ...] array
2019-09-12 21:55:10 +02:00
const actors = [].concat(actor, actor);
notes.push(note);
messages.push({
from: actors[0],
to: actors[1],
message: message.text,
2020-06-17 11:54:24 +02:00
wrap: (message.wrap === undefined && autoWrap()) || !!message.wrap,
2019-09-12 21:55:10 +02:00
type: LINETYPE.NOTE,
placement: placement
});
};
export const setTitle = function(titleWrap) {
title = titleWrap.text;
2020-06-17 11:54:24 +02:00
titleWrapped = (titleWrap.wrap === undefined && autoWrap()) || !!titleWrap.wrap;
2019-09-12 21:55:10 +02:00
};
export const apply = function(param) {
2017-04-11 16:14:25 +02:00
if (param instanceof Array) {
2019-09-12 21:55:10 +02:00
param.forEach(function(item) {
apply(item);
});
2017-04-11 16:14:25 +02:00
} else {
switch (param.type) {
case 'addActor':
2019-09-12 21:55:10 +02:00
addActor(param.actor, param.actor, param.description);
break;
2017-04-11 16:14:25 +02:00
case 'activeStart':
2019-09-12 21:55:10 +02:00
addSignal(param.actor, undefined, undefined, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'activeEnd':
2019-09-12 21:55:10 +02:00
addSignal(param.actor, undefined, undefined, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'addNote':
2019-09-12 21:55:10 +02:00
addNote(param.actor, param.placement, param.text);
break;
2017-04-11 16:14:25 +02:00
case 'addMessage':
2019-09-12 21:55:10 +02:00
addSignal(param.from, param.to, param.msg, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'loopStart':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.loopText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'loopEnd':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, undefined, param.signalType);
break;
2019-07-23 07:47:49 +02:00
case 'rectStart':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.color, param.signalType);
break;
2019-07-23 07:47:49 +02:00
case 'rectEnd':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, undefined, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'optStart':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.optText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'optEnd':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, undefined, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'altStart':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.altText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'else':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.altText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'altEnd':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, undefined, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'setTitle':
2019-09-12 21:55:10 +02:00
setTitle(param.text);
break;
2017-04-11 16:14:25 +02:00
case 'parStart':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.parText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'and':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, param.parText, param.signalType);
break;
2017-04-11 16:14:25 +02:00
case 'parEnd':
2019-09-12 21:55:10 +02:00
addSignal(undefined, undefined, undefined, param.signalType);
break;
}
2017-04-11 16:14:25 +02:00
}
2019-09-12 21:55:10 +02:00
};
2017-09-10 15:51:48 +02:00
export default {
addActor,
addMessage,
addSignal,
2020-06-17 11:54:24 +02:00
autoWrap,
setWrap,
enableSequenceNumbers,
showSequenceNumbers,
2017-09-10 15:51:48 +02:00
getMessages,
getActors,
getActor,
getActorKeys,
getTitle,
parseDirective,
getConfig: () => configApi.getConfig().sequence,
getTitleWrapped,
2017-09-10 15:51:48 +02:00
clear,
parseMessage,
2017-09-10 15:51:48 +02:00
LINETYPE,
ARROWTYPE,
PLACEMENT,
addNote,
setTitle,
apply
2019-09-12 21:55:10 +02:00
};