456 lines
11 KiB
JavaScript
Raw Normal View History

2018-03-08 21:42:39 +08:00
import * as d3 from 'd3'
2019-07-22 02:18:09 -07:00
import { sanitizeUrl } from '@braintree/sanitize-url'
2017-09-10 19:41:34 +08:00
import { logger } from '../../logger'
import utils from '../../utils'
2019-07-13 22:50:53 -07:00
import { getConfig } from '../../config'
2017-04-11 22:14:25 +08:00
2019-07-13 22:50:53 -07:00
const config = getConfig()
2017-09-14 20:27:52 +08:00
let vertices = {}
let edges = []
let classes = []
let subGraphs = []
2019-05-24 17:08:45 +02:00
let subGraphLookup = {}
2017-09-14 20:27:52 +08:00
let tooltips = {}
let subCount = 0
let direction
// Functions to be run after graph rendering
2017-09-14 20:27:52 +08:00
let funs = []
2019-07-13 22:50:53 -07:00
const sanitize = text => {
let txt = text
if (config.securityLevel === 'strict') {
txt = txt.replace(/<br>/g, '#br#')
txt = txt.replace(/<br\S*?\/>/g, '#br#')
txt = txt.replace(/</g, '&lt;').replace(/>/g, '&gt;')
2019-07-22 02:18:09 -07:00
txt = txt.replace(/=/g, '&equals;')
txt = txt.replace(/#br#/g, '<br/>')
}
2019-07-13 22:50:53 -07:00
return txt
}
/**
* Function called by parser when a node definition has been found
* @param id
* @param text
* @param type
* @param style
2019-05-24 17:08:45 +02:00
* @param classes
*/
2019-05-24 17:08:45 +02:00
export const addVertex = function (id, text, type, style, classes) {
2017-09-14 20:27:52 +08:00
let txt
2014-12-04 17:58:05 +01:00
2017-04-11 22:14:25 +08:00
if (typeof id === 'undefined') {
return
}
if (id.trim().length === 0) {
return
}
2015-05-29 08:23:13 +02:00
2017-04-11 22:14:25 +08:00
if (typeof vertices[id] === 'undefined') {
2017-04-16 23:48:36 +08:00
vertices[id] = { id: id, styles: [], classes: [] }
2017-04-11 22:14:25 +08:00
}
if (typeof text !== 'undefined') {
2019-07-13 22:50:53 -07:00
txt = sanitize(text.trim())
2017-04-11 22:14:25 +08:00
2017-04-16 23:48:36 +08:00
// strip quotes if string starts and exnds with a quote
2017-04-11 22:14:25 +08:00
if (txt[0] === '"' && txt[txt.length - 1] === '"') {
txt = txt.substring(1, txt.length - 1)
}
2017-04-11 22:14:25 +08:00
vertices[id].text = txt
}
if (typeof type !== 'undefined') {
vertices[id].type = type
}
if (typeof style !== 'undefined') {
if (style !== null) {
style.forEach(function (s) {
vertices[id].styles.push(s)
})
}
2017-04-11 22:14:25 +08:00
}
2019-05-24 17:08:45 +02:00
if (typeof classes !== 'undefined') {
if (classes !== null) {
classes.forEach(function (s) {
vertices[id].classes.push(s)
})
}
}
2017-04-11 22:14:25 +08:00
}
2014-12-04 17:58:05 +01:00
/**
* Function called by parser when a link/edge definition has been found
* @param start
* @param end
* @param type
* @param linktext
*/
2017-09-10 21:23:04 +08:00
export const addLink = function (start, end, type, linktext) {
2017-09-10 19:41:34 +08:00
logger.info('Got edge...', start, end)
2017-09-14 20:27:52 +08:00
const edge = { start: start, end: end, type: undefined, text: '' }
2017-04-11 22:14:25 +08:00
linktext = type.text
if (typeof linktext !== 'undefined') {
2019-07-13 22:50:53 -07:00
edge.text = sanitize(linktext.trim())
2014-12-09 22:26:42 -08:00
2017-04-16 23:48:36 +08:00
// strip quotes if string starts and exnds with a quote
2017-04-11 22:14:25 +08:00
if (edge.text[0] === '"' && edge.text[edge.text.length - 1] === '"') {
edge.text = edge.text.substring(1, edge.text.length - 1)
}
2017-04-11 22:14:25 +08:00
}
2017-04-11 22:14:25 +08:00
if (typeof type !== 'undefined') {
edge.type = type.type
edge.stroke = type.stroke
}
edges.push(edge)
}
/**
* Updates a link's line interpolation algorithm
* @param pos
* @param interpolate
*/
export const updateLinkInterpolate = function (positions, interp) {
positions.forEach(function (pos) {
if (pos === 'default') {
edges.defaultInterpolate = interp
} else {
edges[pos].interpolate = interp
}
})
2017-04-11 22:14:25 +08:00
}
/**
* Updates a link with a style
* @param pos
* @param style
*/
export const updateLink = function (positions, style) {
positions.forEach(function (pos) {
if (pos === 'default') {
edges.defaultStyle = style
} else {
if (utils.isSubstringInArray('fill', style) === -1) {
style.push('fill:none')
}
edges[pos].style = style
}
})
2017-04-11 22:14:25 +08:00
}
2017-09-10 21:23:04 +08:00
export const addClass = function (id, style) {
2017-04-11 22:14:25 +08:00
if (typeof classes[id] === 'undefined') {
2017-04-16 23:48:36 +08:00
classes[id] = { id: id, styles: [] }
2017-04-11 22:14:25 +08:00
}
if (typeof style !== 'undefined') {
if (style !== null) {
style.forEach(function (s) {
classes[id].styles.push(s)
})
}
2017-04-11 22:14:25 +08:00
}
}
/**
* Called by parser when a graph definition is found, stores the direction of the chart.
* @param dir
*/
2017-09-10 21:23:04 +08:00
export const setDirection = function (dir) {
2017-04-11 22:14:25 +08:00
direction = dir
}
/**
* 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 setClass = function (ids, className) {
ids.split(',').forEach(function (id) {
if (typeof vertices[id] !== 'undefined') {
2017-04-11 22:14:25 +08:00
vertices[id].classes.push(className)
}
2019-05-24 17:08:45 +02:00
if (typeof subGraphLookup[id] !== 'undefined') {
subGraphLookup[id].classes.push(className)
}
})
2017-04-11 22:14:25 +08:00
}
const setTooltip = function (ids, tooltip) {
ids.split(',').forEach(function (id) {
if (typeof tooltip !== 'undefined') {
tooltips[id] = tooltip
}
})
2017-04-11 22:14:25 +08:00
}
2017-09-14 20:27:52 +08:00
const setClickFun = function (id, functionName) {
if (config.securityLevel === 'strict') {
2019-07-13 22:50:53 -07:00
return
}
2017-04-11 22:14:25 +08:00
if (typeof functionName === 'undefined') {
return
}
if (typeof vertices[id] !== 'undefined') {
funs.push(function (element) {
const elem = document.querySelector(`[id="${id}"]`)
2017-04-11 22:14:25 +08:00
if (elem !== null) {
2019-07-21 02:14:49 -07:00
elem.addEventListener('click', function () {
window[functionName](id)
}, false)
2017-04-11 22:14:25 +08:00
}
})
}
}
/**
* 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) {
if (typeof vertices[id] !== 'undefined') {
if (config.securityLevel === 'strict') {
2019-07-22 02:18:09 -07:00
vertices[id].link = sanitizeUrl(linkStr) //.replace(/javascript:.*/g, '')
} else {
vertices[id].link = linkStr
}
}
})
setTooltip(ids, tooltip)
setClass(ids, 'clickable')
2017-04-11 22:14:25 +08:00
}
2017-09-10 21:23:04 +08:00
export const getTooltip = function (id) {
2017-04-11 22:14:25 +08:00
return tooltips[id]
}
/**
* 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) { setClickFun(id, functionName) })
setTooltip(ids, tooltip)
setClass(ids, 'clickable')
2017-04-11 22:14:25 +08:00
}
2017-09-10 21:23:04 +08:00
export const bindFunctions = function (element) {
2017-04-11 22:14:25 +08:00
funs.forEach(function (fun) {
fun(element)
})
}
2017-09-10 21:23:04 +08:00
export const getDirection = function () {
2017-04-11 22:14:25 +08:00
return direction
}
/**
* Retrieval function for fetching the found nodes after parsing has completed.
* @returns {{}|*|vertices}
*/
2017-09-10 21:23:04 +08:00
export const getVertices = function () {
2017-04-11 22:14:25 +08:00
return vertices
}
/**
* Retrieval function for fetching the found links after parsing has completed.
* @returns {{}|*|edges}
*/
2017-09-10 21:23:04 +08:00
export const getEdges = function () {
2017-04-11 22:14:25 +08:00
return edges
}
/**
* Retrieval function for fetching the found class definitions after parsing has completed.
* @returns {{}|*|classes}
*/
2017-09-10 21:23:04 +08:00
export const getClasses = function () {
2017-04-11 22:14:25 +08:00
return classes
}
2017-09-14 20:27:52 +08:00
const setupToolTips = function (element) {
let tooltipElem = d3.select('.mermaidTooltip')
2018-03-08 21:07:18 +08:00
if ((tooltipElem._groups || tooltipElem)[0][0] === null) {
2017-04-11 22:14:25 +08:00
tooltipElem = d3.select('body')
2017-04-16 23:48:36 +08:00
.append('div')
.attr('class', 'mermaidTooltip')
.style('opacity', 0)
2017-04-11 22:14:25 +08:00
}
2017-09-14 20:27:52 +08:00
const svg = d3.select(element).select('svg')
2017-09-14 20:27:52 +08:00
const nodes = svg.selectAll('g.node')
2017-04-11 22:14:25 +08:00
nodes
2017-04-16 23:48:36 +08:00
.on('mouseover', function () {
2017-09-14 20:27:52 +08:00
const el = d3.select(this)
const title = el.attr('title')
2017-04-16 23:48:36 +08:00
// Dont try to draw a tooltip if no data is provided
if (title === null) {
return
}
2017-09-14 20:27:52 +08:00
const rect = this.getBoundingClientRect()
2017-04-16 23:48:36 +08:00
tooltipElem.transition()
.duration(200)
.style('opacity', '.9')
tooltipElem.html(el.attr('title'))
.style('left', (rect.left + (rect.right - rect.left) / 2) + 'px')
.style('top', (rect.top - 14 + document.body.scrollTop) + 'px')
el.classed('hover', true)
})
.on('mouseout', function () {
tooltipElem.transition()
.duration(500)
.style('opacity', 0)
2017-09-14 20:27:52 +08:00
const el = d3.select(this)
2017-04-16 23:48:36 +08:00
el.classed('hover', false)
})
2017-04-11 22:14:25 +08:00
}
funs.push(setupToolTips)
/**
* Clears the internal graph db so that a new graph can be parsed.
*/
2017-09-10 21:23:04 +08:00
export const clear = function () {
2017-04-11 22:14:25 +08:00
vertices = {}
classes = {}
edges = []
funs = []
funs.push(setupToolTips)
subGraphs = []
2019-05-24 17:08:45 +02:00
subGraphLookup = {}
2017-04-11 22:14:25 +08:00
subCount = 0
tooltips = []
}
/**
*
* @returns {string}
*/
2017-09-10 21:23:04 +08:00
export const defaultStyle = function () {
2017-04-11 22:14:25 +08:00
return 'fill:#ffa;stroke: #f66; stroke-width: 3px; stroke-dasharray: 5, 5;fill:#ffa;stroke: #666;'
}
2015-01-07 21:02:58 +01:00
/**
* Clears the internal graph db so that a new graph can be parsed.
*/
2019-05-24 17:08:45 +02:00
export const addSubGraph = function (id, list, title) {
2017-04-11 22:14:25 +08:00
function uniq (a) {
2017-09-14 20:27:52 +08:00
const prims = { 'boolean': {}, 'number': {}, 'string': {} }
const objs = []
2017-04-11 22:14:25 +08:00
return a.filter(function (item) {
2017-09-14 20:27:52 +08:00
const type = typeof item
2018-03-17 18:12:24 +08:00
if (item.trim() === '') {
2017-04-11 22:14:25 +08:00
return false
}
if (type in prims) { return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true) } else { return objs.indexOf(item) >= 0 ? false : objs.push(item) }
})
}
2017-09-14 20:27:52 +08:00
let nodeList = []
2017-04-11 22:14:25 +08:00
nodeList = uniq(nodeList.concat.apply(nodeList, list))
2019-05-24 17:08:45 +02:00
id = id || ('subGraph' + subCount)
title = title || ''
2019-07-13 22:50:53 -07:00
title = sanitize(title)
2017-04-11 22:14:25 +08:00
subCount = subCount + 1
2019-05-24 17:08:45 +02:00
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [] }
subGraphs.push(subGraph)
subGraphLookup[id] = subGraph
return id
2017-04-11 22:14:25 +08:00
}
2017-09-14 20:27:52 +08:00
const getPosForId = function (id) {
for (let i = 0; i < subGraphs.length; i++) {
2017-04-11 22:14:25 +08:00
if (subGraphs[i].id === id) {
return i
}
2017-04-11 22:14:25 +08:00
}
return -1
}
2017-09-14 20:27:52 +08:00
let secCount = -1
const posCrossRef = []
const indexNodes2 = function (id, pos) {
const nodes = subGraphs[pos].nodes
2017-04-11 22:14:25 +08:00
secCount = secCount + 1
if (secCount > 2000) {
return
}
posCrossRef[secCount] = pos
2017-04-16 23:48:36 +08:00
// Check if match
2017-04-11 22:14:25 +08:00
if (subGraphs[pos].id === id) {
return {
result: true,
count: 0
}
2017-04-11 22:14:25 +08:00
}
2017-09-14 20:27:52 +08:00
let count = 0
let posCount = 1
2017-04-11 22:14:25 +08:00
while (count < nodes.length) {
2017-09-14 20:27:52 +08:00
const childPos = getPosForId(nodes[count])
2017-04-16 23:48:36 +08:00
// Ignore regular nodes (pos will be -1)
2017-04-11 22:14:25 +08:00
if (childPos >= 0) {
2017-09-14 20:27:52 +08:00
const res = indexNodes2(id, childPos)
2017-04-11 22:14:25 +08:00
if (res.result) {
return {
result: true,
count: posCount + res.count
}
2017-04-11 22:14:25 +08:00
} else {
posCount = posCount + res.count
}
}
2017-04-11 22:14:25 +08:00
count = count + 1
}
2017-04-11 22:14:25 +08:00
return {
result: false,
count: posCount
}
}
2017-09-10 21:23:04 +08:00
export const getDepthFirstPos = function (pos) {
2017-04-11 22:14:25 +08:00
return posCrossRef[pos]
}
2017-09-10 21:23:04 +08:00
export const indexNodes = function () {
2017-04-11 22:14:25 +08:00
secCount = -1
if (subGraphs.length > 0) {
2017-09-10 21:23:04 +08:00
indexNodes2('none', subGraphs.length - 1, 0)
2017-04-11 22:14:25 +08:00
}
}
2017-09-10 21:23:04 +08:00
export const getSubGraphs = function () {
2017-04-11 22:14:25 +08:00
return subGraphs
}
2017-09-10 21:51:48 +08:00
export default {
addVertex,
addLink,
updateLinkInterpolate,
updateLink,
addClass,
setDirection,
setClass,
getTooltip,
setClickEvent,
setLink,
2017-09-10 21:51:48 +08:00
bindFunctions,
getDirection,
getVertices,
getEdges,
getClasses,
clear,
defaultStyle,
addSubGraph,
getDepthFirstPos,
indexNodes,
getSubGraphs
}