mermaid/src/diagrams/sequenceDiagram/sequenceDb.js

155 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-09-10 13:41:34 +02:00
import { logger } from '../../logger'
2017-04-11 16:14:25 +02:00
var actors = {}
var messages = []
var notes = []
var title = ''
2017-09-10 15:23:04 +02:00
export const addActor = function (id, name, description) {
2017-04-16 17:48:36 +02:00
// Don't allow description nulling
2017-04-11 16:14:25 +02:00
var 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
2017-04-11 16:14:25 +02:00
if (description == null) description = name
2017-04-16 17:48:36 +02:00
actors[id] = { name: name, description: description }
2017-04-11 16:14:25 +02:00
}
2017-09-10 15:23:04 +02:00
export const addMessage = function (idFrom, idTo, message, answer) {
2017-04-16 17:48:36 +02:00
messages.push({ from: idFrom, to: idTo, message: message, answer: answer })
2017-04-11 16:14:25 +02:00
}
2017-09-10 15:23:04 +02:00
export const addSignal = function (idFrom, idTo, message, messageType) {
2017-09-10 13:41:34 +02:00
logger.debug('Adding message from=' + idFrom + ' to=' + idTo + ' message=' + message + ' type=' + messageType)
2017-04-16 17:48:36 +02:00
messages.push({ from: idFrom, to: idTo, message: message, type: messageType })
2017-04-11 16:14:25 +02:00
}
2017-09-10 15:23:04 +02:00
export const getMessages = function () {
2017-04-11 16:14:25 +02:00
return messages
}
2017-09-10 15:23:04 +02:00
export const getActors = function () {
2017-04-11 16:14:25 +02:00
return actors
}
2017-09-10 15:23:04 +02:00
export const getActor = function (id) {
2017-04-11 16:14:25 +02:00
return actors[id]
}
2017-09-10 15:23:04 +02:00
export const getActorKeys = function () {
2017-04-11 16:14:25 +02:00
return Object.keys(actors)
}
2017-09-10 15:23:04 +02:00
export const getTitle = function () {
2017-04-11 16:14:25 +02:00
return title
}
2017-09-10 15:23:04 +02:00
export const clear = function () {
2017-04-11 16:14:25 +02:00
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,
PAR_END: 21
}
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
}
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
}
2014-12-04 17:58:05 +01:00
2017-09-10 15:23:04 +02:00
export const addNote = function (actor, placement, message) {
2017-04-16 17:48:36 +02:00
var 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
2017-04-11 16:14:25 +02:00
var actors = [].concat(actor, actor)
2014-12-04 17:58:05 +01:00
2017-04-11 16:14:25 +02:00
notes.push(note)
2017-09-10 15:23:04 +02:00
messages.push({ from: actors[0], to: actors[1], message: message, type: LINETYPE.NOTE, placement: placement })
}
2017-09-10 15:23:04 +02:00
export const setTitle = function (titleText) {
2017-04-11 16:14:25 +02:00
title = titleText
}
2014-12-04 17:58:05 +01:00
2017-09-10 15:23:04 +02:00
export const apply = function (param) {
2017-04-11 16:14:25 +02:00
if (param instanceof Array) {
param.forEach(function (item) {
2017-09-10 15:23:04 +02:00
apply(item)
2017-04-11 16:14:25 +02:00
})
} else {
switch (param.type) {
case 'addActor':
2017-09-10 15:23:04 +02:00
addActor(param.actor, param.actor, param.description)
2017-04-11 16:14:25 +02:00
break
case 'activeStart':
2017-09-10 15:23:04 +02:00
addSignal(param.actor, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'activeEnd':
2017-09-10 15:23:04 +02:00
addSignal(param.actor, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'addNote':
2017-09-10 15:23:04 +02:00
addNote(param.actor, param.placement, param.text)
2017-04-11 16:14:25 +02:00
break
case 'addMessage':
2017-09-10 15:23:04 +02:00
addSignal(param.from, param.to, param.msg, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'loopStart':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.loopText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'loopEnd':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'optStart':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.optText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'optEnd':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'altStart':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.altText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'else':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.altText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'altEnd':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'setTitle':
2017-09-10 15:23:04 +02:00
setTitle(param.text)
2017-04-11 16:14:25 +02:00
break
case 'parStart':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.parText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'and':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, param.parText, param.signalType)
2017-04-11 16:14:25 +02:00
break
case 'parEnd':
2017-09-10 15:23:04 +02:00
addSignal(undefined, undefined, undefined, param.signalType)
2017-04-11 16:14:25 +02:00
break
}
2017-04-11 16:14:25 +02:00
}
}