mermaid/src/diagrams/sequence/sequenceDb.js

189 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-09-12 21:55:10 +02:00
import { logger } from '../../logger';
2017-09-10 13:41:34 +02:00
2019-09-12 21:55:10 +02:00
let actors = {};
let messages = [];
const notes = [];
let title = '';
2017-04-11 16:14:25 +02:00
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
2019-09-12 21:55:10 +02:00
if (description == null) description = name;
actors[id] = { name: name, description: description };
};
export const addMessage = function(idFrom, idTo, message, answer) {
messages.push({ from: idFrom, to: idTo, message: message, answer: answer });
};
export const addSignal = function(idFrom, idTo, message, messageType) {
logger.debug(
'Adding message from=' + idFrom + ' to=' + idTo + ' message=' + message + ' type=' + messageType
);
messages.push({ from: idFrom, to: idTo, message: message, type: messageType });
};
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 clear = function() {
actors = {};
messages = [];
};
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
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 };
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,
type: LINETYPE.NOTE,
placement: placement
});
};
export const setTitle = function(titleText) {
title = titleText;
};
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,
getMessages,
getActors,
getActor,
getActorKeys,
getTitle,
clear,
LINETYPE,
ARROWTYPE,
PLACEMENT,
addNote,
setTitle,
apply
2019-09-12 21:55:10 +02:00
};