2019-09-12 12:55:10 -07:00
|
|
|
import { logger } from '../../logger';
|
2020-06-08 14:48:03 -04:00
|
|
|
import { getConfig, setConfig } from '../../config';
|
2020-06-11 15:31:59 -04:00
|
|
|
import mermaidAPI from '../../mermaidAPI';
|
2017-09-10 19:41:34 +08:00
|
|
|
|
2020-04-23 17:02:37 +03:00
|
|
|
let prevActor = undefined;
|
2019-09-12 12:55:10 -07:00
|
|
|
let actors = {};
|
|
|
|
let messages = [];
|
|
|
|
const notes = [];
|
|
|
|
let title = '';
|
2020-06-11 15:31:59 -04:00
|
|
|
let titleWrapped = false;
|
2020-02-16 15:26:08 +01:00
|
|
|
let sequenceNumbersEnabled = false;
|
2020-06-08 14:48:03 -04:00
|
|
|
let wrapEnabled = false;
|
2020-06-11 15:31:59 -04:00
|
|
|
let configUpdated = false;
|
|
|
|
|
|
|
|
export const handleDirective = function(directive) {
|
|
|
|
switch (directive.type) {
|
|
|
|
case 'init':
|
|
|
|
case 'initialize':
|
|
|
|
mermaidAPI.reinitialize(directive.args);
|
|
|
|
break;
|
|
|
|
case 'config':
|
|
|
|
updateConfig(directive.args);
|
|
|
|
break;
|
|
|
|
case 'wrap':
|
|
|
|
wrapEnabled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2020-04-23 17:02:37 +03:00
|
|
|
|
2019-09-12 12:55:10 -07:00
|
|
|
export const addActor = function(id, name, description) {
|
2017-04-16 23:48:36 +08:00
|
|
|
// Don't allow description nulling
|
2019-09-12 12:55:10 -07:00
|
|
|
const old = actors[id];
|
|
|
|
if (old && name === old.name && description == null) return;
|
2015-12-06 05:53:08 -05:00
|
|
|
|
2017-04-16 23:48:36 +08:00
|
|
|
// Don't allow null descriptions, either
|
2020-06-11 15:31:59 -04:00
|
|
|
if (description == null || description.text == null) {
|
|
|
|
description = { text: name, wrap: null };
|
|
|
|
}
|
2019-09-12 12:55:10 -07:00
|
|
|
|
2020-06-11 15:31:59 -04:00
|
|
|
actors[id] = {
|
|
|
|
name: name,
|
|
|
|
description: description.text,
|
|
|
|
wrap: (description.wrap === null && autoWrap()) || !!description.wrap,
|
|
|
|
prevActor: prevActor
|
|
|
|
};
|
2020-04-23 17:02:37 +03:00
|
|
|
if (prevActor && actors[prevActor]) {
|
2020-04-23 18:31:22 +03:00
|
|
|
actors[prevActor].nextActor = id;
|
2020-04-23 17:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
prevActor = id;
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
|
|
|
|
2020-01-15 20:27:31 +01:00
|
|
|
const activationCount = part => {
|
2020-06-11 15:31:59 -04:00
|
|
|
let i;
|
2020-01-15 20:27:31 +01:00
|
|
|
let count = 0;
|
|
|
|
for (i = 0; i < messages.length; i++) {
|
|
|
|
// console.warn(i, messages[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 12:55:10 -07:00
|
|
|
export const addMessage = function(idFrom, idTo, message, answer) {
|
2020-06-11 15:31:59 -04:00
|
|
|
messages.push({
|
|
|
|
from: idFrom,
|
|
|
|
to: idTo,
|
|
|
|
message: message.text,
|
|
|
|
wrap: (message.wrap === null && autoWrap()) || !!message.wrap,
|
|
|
|
answer: answer
|
|
|
|
});
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
|
|
|
|
2020-06-11 15:31:59 -04:00
|
|
|
export const addSignal = function(idFrom, idTo, message = { text: null, wrap: null }, messageType) {
|
2019-09-12 12:55:10 -07:00
|
|
|
logger.debug(
|
2020-06-11 15:31:59 -04:00
|
|
|
'Adding message from=' +
|
|
|
|
idFrom +
|
|
|
|
' to=' +
|
|
|
|
idTo +
|
|
|
|
' message=' +
|
|
|
|
message.text +
|
|
|
|
' wrap=' +
|
|
|
|
message.wrap +
|
|
|
|
' type=' +
|
|
|
|
messageType
|
2019-09-12 12:55:10 -07:00
|
|
|
);
|
2020-01-15 20:27:31 +01:00
|
|
|
|
|
|
|
if (messageType === LINETYPE.ACTIVE_END) {
|
|
|
|
const cnt = activationCount(idFrom.actor);
|
|
|
|
logger.debug('Adding message from=', messages, cnt);
|
|
|
|
if (cnt < 1) {
|
|
|
|
// Bail out as there is an activation signal from an inactive participant
|
2020-06-11 15:31:59 -04:00
|
|
|
let error = new Error('Trying to inactivate an inactive participant (' + idFrom.actor + ')');
|
2020-01-15 20:27:31 +01:00
|
|
|
error.hash = {
|
|
|
|
text: '->>-',
|
|
|
|
token: '->>-',
|
|
|
|
line: '1',
|
|
|
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
|
|
|
expected: ["'ACTIVE_PARTICIPANT'"]
|
|
|
|
};
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 15:31:59 -04:00
|
|
|
messages.push({
|
|
|
|
from: idFrom,
|
|
|
|
to: idTo,
|
|
|
|
message: message.text,
|
|
|
|
wrap: (message.wrap === null && autoWrap()) || !!message.wrap,
|
|
|
|
type: messageType
|
|
|
|
});
|
2020-01-15 20:27:31 +01:00
|
|
|
return true;
|
2019-09-12 12:55:10 -07: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;
|
|
|
|
};
|
2020-06-11 15:31:59 -04:00
|
|
|
export const getTitleWrapped = function() {
|
|
|
|
return titleWrapped;
|
|
|
|
};
|
2020-02-16 15:26:08 +01:00
|
|
|
export const enableSequenceNumbers = function() {
|
|
|
|
sequenceNumbersEnabled = true;
|
|
|
|
};
|
|
|
|
export const showSequenceNumbers = () => sequenceNumbersEnabled;
|
2019-09-12 12:55:10 -07:00
|
|
|
|
2020-06-08 14:48:03 -04:00
|
|
|
export const enableWrap = function() {
|
|
|
|
wrapEnabled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const disableWrap = function() {
|
|
|
|
wrapEnabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const autoWrap = () => wrapEnabled;
|
|
|
|
|
2019-09-12 12:55:10 -07:00
|
|
|
export const clear = function() {
|
|
|
|
actors = {};
|
|
|
|
messages = [];
|
2020-06-11 15:31:59 -04:00
|
|
|
configUpdated = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseMessage = function(str) {
|
|
|
|
const _str = str.trim();
|
|
|
|
return {
|
|
|
|
text: _str.replace(/^[:]?(?:no)?wrap:/, '').trim(),
|
|
|
|
wrap:
|
|
|
|
_str.match(/^[:]?(?:no)?wrap:/) === null
|
|
|
|
? autoWrap()
|
|
|
|
: _str.match(/^[:]?wrap:/) !== null
|
|
|
|
? true
|
|
|
|
: _str.match(/^[:]?nowrap:/) !== null
|
|
|
|
? false
|
|
|
|
: autoWrap()
|
|
|
|
};
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const LINETYPE = {
|
2017-04-11 22:14:25 +08: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-22 22:47:49 -07:00
|
|
|
PAR_END: 21,
|
|
|
|
RECT_START: 22,
|
|
|
|
RECT_END: 23
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const ARROWTYPE = {
|
2017-04-11 22:14:25 +08:00
|
|
|
FILLED: 0,
|
|
|
|
OPEN: 1
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const PLACEMENT = {
|
2017-04-11 22:14:25 +08:00
|
|
|
LEFTOF: 0,
|
|
|
|
RIGHTOF: 1,
|
|
|
|
OVER: 2
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2019-09-12 12:55:10 -07:00
|
|
|
export const addNote = function(actor, placement, message) {
|
2020-06-11 15:31:59 -04:00
|
|
|
const note = {
|
|
|
|
actor: actor,
|
|
|
|
placement: placement,
|
|
|
|
message: message.text,
|
|
|
|
wrap: (message.wrap === null && autoWrap()) || !!message.wrap
|
|
|
|
};
|
2014-12-04 17:58:05 +01:00
|
|
|
|
2017-04-16 23:48:36 +08:00
|
|
|
// Coerce actor into a [to, from, ...] array
|
2019-09-12 12:55:10 -07:00
|
|
|
const actors = [].concat(actor, actor);
|
|
|
|
|
|
|
|
notes.push(note);
|
|
|
|
messages.push({
|
|
|
|
from: actors[0],
|
|
|
|
to: actors[1],
|
2020-06-11 15:31:59 -04:00
|
|
|
message: message.text,
|
|
|
|
wrap: (message.wrap === null && autoWrap()) || !!message.wrap,
|
2019-09-12 12:55:10 -07:00
|
|
|
type: LINETYPE.NOTE,
|
|
|
|
placement: placement
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-11 15:31:59 -04:00
|
|
|
export const setTitle = function(titleWrap) {
|
|
|
|
title = titleWrap.text;
|
|
|
|
titleWrapped = (titleWrap.wrap === null && autoWrap()) || !!titleWrap.wrap;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateConfig = function(config = getConfig()) {
|
|
|
|
try {
|
|
|
|
setConfig(config);
|
|
|
|
configUpdated = true;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Error: unable to parse config');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const hasConfigChange = function() {
|
|
|
|
return configUpdated;
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export const apply = function(param) {
|
2017-04-11 22:14:25 +08:00
|
|
|
if (param instanceof Array) {
|
2019-09-12 12:55:10 -07:00
|
|
|
param.forEach(function(item) {
|
|
|
|
apply(item);
|
|
|
|
});
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
|
|
|
switch (param.type) {
|
|
|
|
case 'addActor':
|
2019-09-12 12:55:10 -07:00
|
|
|
addActor(param.actor, param.actor, param.description);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'activeStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(param.actor, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'activeEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(param.actor, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'addNote':
|
2019-09-12 12:55:10 -07:00
|
|
|
addNote(param.actor, param.placement, param.text);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'addMessage':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(param.from, param.to, param.msg, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'loopStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.loopText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'loopEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2019-07-22 22:47:49 -07:00
|
|
|
case 'rectStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.color, param.signalType);
|
|
|
|
break;
|
2019-07-22 22:47:49 -07:00
|
|
|
case 'rectEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'optStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.optText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'optEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'altStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.altText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'else':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.altText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'altEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'setTitle':
|
2019-09-12 12:55:10 -07:00
|
|
|
setTitle(param.text);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'parStart':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.parText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'and':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, param.parText, param.signalType);
|
|
|
|
break;
|
2017-04-11 22:14:25 +08:00
|
|
|
case 'parEnd':
|
2019-09-12 12:55:10 -07:00
|
|
|
addSignal(undefined, undefined, undefined, param.signalType);
|
|
|
|
break;
|
2015-01-04 17:56:58 +01:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|
2017-09-10 21:51:48 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
addActor,
|
|
|
|
addMessage,
|
|
|
|
addSignal,
|
2020-06-08 14:48:03 -04:00
|
|
|
enableWrap,
|
|
|
|
disableWrap,
|
2020-02-16 15:26:08 +01:00
|
|
|
enableSequenceNumbers,
|
|
|
|
showSequenceNumbers,
|
2020-06-08 14:48:03 -04:00
|
|
|
autoWrap,
|
2017-09-10 21:51:48 +08:00
|
|
|
getMessages,
|
|
|
|
getActors,
|
|
|
|
getActor,
|
|
|
|
getActorKeys,
|
|
|
|
getTitle,
|
2020-06-11 15:31:59 -04:00
|
|
|
handleDirective,
|
|
|
|
hasConfigChange,
|
|
|
|
getConfig,
|
|
|
|
updateConfig,
|
|
|
|
getTitleWrapped,
|
2017-09-10 21:51:48 +08:00
|
|
|
clear,
|
2020-06-11 15:31:59 -04:00
|
|
|
parseMessage,
|
2017-09-10 21:51:48 +08:00
|
|
|
LINETYPE,
|
|
|
|
ARROWTYPE,
|
|
|
|
PLACEMENT,
|
|
|
|
addNote,
|
|
|
|
setTitle,
|
|
|
|
apply
|
2019-09-12 12:55:10 -07:00
|
|
|
};
|