mermaid/src/mermaid.js

193 lines
5.3 KiB
JavaScript
Raw Normal View History

/**
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
* the diagrams to svg code.
*/
2017-09-09 16:52:09 +08:00
var he = require('he')
2015-11-11 18:37:53 +01:00
2017-04-11 22:14:25 +08:00
var Logger = require('./logger')
var log = Logger.Log
var mermaidAPI = require('./mermaidAPI')
var nextId = 0
2015-05-26 20:59:23 +02:00
/**
* ## init
2015-05-26 20:59:23 +02:00
* Function that goes through the document to find the chart definitions in there and render them.
*
* The function tags the processed attributes with the attribute data-processed and ignores found elements with the
* attribute already set. This way the init function can be triggered several times.
*
2015-05-26 20:59:23 +02:00
* Optionally, `init` can accept in the second argument one of the following:
* - a DOM Node
* - an array of DOM nodes (as would come from a jQuery selector)
* - a W3C selector, a la `.mermaid`
*
* ```mermaid
2015-05-26 20:59:23 +02:00
* graph LR;
* a(Find elements)-->b{Processed}
* b-->|Yes|c(Leave element)
* b-->|No |d(Transform)
2015-05-26 20:59:23 +02:00
* ```
* Renders the mermaid diagrams
* @param nodes a css selector or an array of nodes
2015-05-26 20:59:23 +02:00
*/
var init = function () {
2017-04-11 22:14:25 +08:00
var conf = mermaidAPI.getConfig()
log.debug('Starting rendering diagrams')
var nodes
if (arguments.length >= 2) {
2017-04-16 23:08:37 +08:00
/*! sequence config was passed as #1 */
2017-04-11 22:14:25 +08:00
if (typeof arguments[0] !== 'undefined') {
global.mermaid.sequenceConfig = arguments[0]
2015-05-26 20:59:23 +02:00
}
2017-04-11 22:14:25 +08:00
nodes = arguments[1]
} else {
nodes = arguments[0]
}
2017-04-16 23:08:37 +08:00
// if last argument is a function this is the callback function
2017-04-11 22:14:25 +08:00
var callback
if (typeof arguments[arguments.length - 1] === 'function') {
callback = arguments[arguments.length - 1]
log.debug('Callback function found')
} else {
if (typeof conf.mermaid !== 'undefined') {
if (typeof conf.mermaid.callback === 'function') {
callback = conf.mermaid.callback
log.debug('Callback function found')
} else {
log.debug('No Callback function found')
}
}
2017-04-11 22:14:25 +08:00
}
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
2017-04-16 23:08:37 +08:00
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
: nodes instanceof window.Node ? [nodes]
2017-04-11 22:14:25 +08:00
: nodes // Last case - sequence config was passed pick next
if (typeof global.mermaid_config !== 'undefined') {
2017-04-11 22:14:25 +08:00
mermaidAPI.initialize(global.mermaid_config)
}
log.debug('Start On Load before: ' + global.mermaid.startOnLoad)
if (typeof global.mermaid.startOnLoad !== 'undefined') {
log.debug('Start On Load inner: ' + global.mermaid.startOnLoad)
2017-04-16 23:08:37 +08:00
mermaidAPI.initialize({ startOnLoad: global.mermaid.startOnLoad })
2017-04-11 22:14:25 +08:00
}
if (typeof global.mermaid.ganttConfig !== 'undefined') {
2017-04-16 23:08:37 +08:00
mermaidAPI.initialize({ gantt: global.mermaid.ganttConfig })
2017-04-11 22:14:25 +08:00
}
var txt
var insertSvg = function (svgCode, bindFunctions) {
element.innerHTML = svgCode
if (typeof callback !== 'undefined') {
callback(id)
}
2017-04-11 22:14:25 +08:00
bindFunctions(element)
}
2017-04-16 23:08:37 +08:00
for (var i = 0; i < nodes.length; i++) {
2017-04-11 22:14:25 +08:00
var element = nodes[i]
2015-05-26 20:59:23 +02:00
2017-04-16 23:08:37 +08:00
/*! Check if previously processed */
2017-04-11 22:14:25 +08:00
if (!element.getAttribute('data-processed')) {
element.setAttribute('data-processed', true)
} else {
continue
}
2015-05-26 20:59:23 +02:00
2017-04-11 22:14:25 +08:00
var id = 'mermaidChart' + nextId++
2015-05-26 20:59:23 +02:00
2017-04-16 23:08:37 +08:00
// Fetch the graph definition including tags
2017-04-11 22:14:25 +08:00
txt = element.innerHTML
2015-05-26 20:59:23 +02:00
2017-04-16 23:08:37 +08:00
// transforms the html to pure text
2017-04-11 22:14:25 +08:00
txt = he.decode(txt).trim()
2017-04-11 22:14:25 +08:00
mermaidAPI.render(id, txt, insertSvg, element)
}
}
2017-09-09 21:47:21 +08:00
const version = function () {
2017-04-11 22:14:25 +08:00
return 'v' + require('../package.json').version
}
2015-05-26 20:59:23 +02:00
2017-09-09 21:47:21 +08:00
const initialize = function (config) {
2017-04-11 22:14:25 +08:00
log.debug('Initializing mermaid')
if (typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') {
global.mermaid.startOnLoad = config.mermaid.startOnLoad
}
2017-04-11 22:14:25 +08:00
if (typeof config.mermaid.htmlLabels !== 'undefined') {
global.mermaid.htmlLabels = config.mermaid.htmlLabels
2015-05-26 20:59:23 +02:00
}
2017-04-11 22:14:25 +08:00
}
mermaidAPI.initialize(config)
}
/**
* ##contentLoaded
* Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and
* calls init for rendering the mermaid diagrams on the page.
*/
2017-09-09 21:47:21 +08:00
const contentLoaded = function () {
2017-04-11 22:14:25 +08:00
var config
2017-04-16 23:08:37 +08:00
// Check state of start config mermaid namespace
if (typeof global.mermaid_config !== 'undefined') {
2017-09-09 21:47:21 +08:00
if (global.mermaid_config.htmlLabels === false) {
2017-04-11 22:14:25 +08:00
global.mermaid.htmlLabels = false
2015-05-26 20:59:23 +02:00
}
2017-04-11 22:14:25 +08:00
}
2015-05-26 20:59:23 +02:00
2017-04-11 22:14:25 +08:00
if (global.mermaid.startOnLoad) {
2017-04-16 23:08:37 +08:00
// For backwards compatability reasons also check mermaid_config variable
2017-04-11 22:14:25 +08:00
if (typeof global.mermaid_config !== 'undefined') {
2017-04-16 23:08:37 +08:00
// Check if property startOnLoad is set
2017-09-09 21:47:21 +08:00
if (global.mermaid_config.startOnLoad === true) {
2017-04-11 22:14:25 +08:00
global.mermaid.init()
}
} else {
2017-04-16 23:08:37 +08:00
// No config found, do check API config
2017-04-11 22:14:25 +08:00
config = mermaidAPI.getConfig()
if (config.startOnLoad) {
global.mermaid.init()
}
}
} else {
if (typeof global.mermaid.startOnLoad === 'undefined') {
log.debug('In start, no config')
config = mermaidAPI.getConfig()
if (config.startOnLoad) {
global.mermaid.init()
}
2015-05-26 20:59:23 +02:00
}
2017-04-11 22:14:25 +08:00
}
}
2015-05-26 20:59:23 +02:00
2017-04-11 22:14:25 +08:00
if (typeof document !== 'undefined') {
2017-04-16 23:08:37 +08:00
/*!
* Wait for document loaded before starting the execution
*/
2017-04-11 22:14:25 +08:00
window.addEventListener('load', function () {
2017-09-09 21:47:21 +08:00
contentLoaded()
2017-04-11 22:14:25 +08:00
}, false)
2015-05-26 20:59:23 +02:00
}
2017-09-09 21:47:21 +08:00
const mermaid = {
startOnLoad: true,
htmlLabels: true,
2017-09-10 10:19:15 +08:00
mermaidAPI,
parse: mermaidAPI.parse,
render: mermaidAPI.render,
2017-09-09 21:47:21 +08:00
init,
initialize,
version,
2017-09-10 10:19:15 +08:00
contentLoaded
2017-09-09 21:47:21 +08:00
}
export default mermaid