2015-06-30 14:23:32 +02:00
|
|
|
/**
|
|
|
|
* 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-10-19 21:36:55 +02:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
module.exports.mermaidAPI = mermaidAPI
|
2015-05-26 20:59:23 +02:00
|
|
|
/**
|
2015-06-30 14:23:32 +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-09-26 12:09:47 +02:00
|
|
|
*
|
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`
|
2015-09-26 12:09:47 +02:00
|
|
|
*
|
2015-06-30 14:23:32 +02:00
|
|
|
* ```mermaid
|
2015-05-26 20:59:23 +02:00
|
|
|
* graph LR;
|
2015-06-30 14:23:32 +02:00
|
|
|
* 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
|
2015-06-30 14:23:32 +02:00
|
|
|
* @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
|
|
|
}
|
2015-07-15 11:39:46 +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')
|
|
|
|
}
|
2015-07-15 11:39:46 +02:00
|
|
|
}
|
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
|
|
|
|
|
2017-04-11 23:31:59 +08:00
|
|
|
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)
|
2015-06-21 17:25:58 +02:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
bindFunctions(element)
|
|
|
|
}
|
2015-06-21 17:25:58 +02:00
|
|
|
|
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()
|
2015-10-03 21:50:32 +02:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
mermaidAPI.render(id, txt, insertSvg, element)
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 21:50:32 +02:00
|
|
|
|
2017-09-09 16:52:09 +08:00
|
|
|
module.exports.init = init
|
|
|
|
module.exports.parse = mermaidAPI.parse
|
2015-05-26 20:59:23 +02:00
|
|
|
/**
|
2015-06-30 14:23:32 +02:00
|
|
|
* ## version
|
2015-05-26 20:59:23 +02:00
|
|
|
* Function returning version information
|
|
|
|
* @returns {string} A string containing the version info
|
|
|
|
*/
|
2017-09-09 16:52:09 +08:00
|
|
|
module.exports.version = function () {
|
2017-04-11 22:14:25 +08:00
|
|
|
return 'v' + require('../package.json').version
|
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
/**
|
|
|
|
* ## initialize
|
|
|
|
* This function overrides the default configuration.
|
|
|
|
* @param config
|
|
|
|
*/
|
2017-09-09 16:52:09 +08:00
|
|
|
module.exports.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
|
2015-07-15 11:39:46 +02:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var equals = function (val, variable) {
|
|
|
|
if (typeof variable === 'undefined') {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return (val === variable)
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
/**
|
|
|
|
* Global mermaid object. Contains the functions:
|
|
|
|
* * init
|
|
|
|
* * initialize
|
|
|
|
* * version
|
|
|
|
* * parse
|
|
|
|
* * parseError
|
|
|
|
* * render
|
|
|
|
*/
|
2015-05-26 20:59:23 +02:00
|
|
|
global.mermaid = {
|
2017-04-11 22:14:25 +08:00
|
|
|
startOnLoad: true,
|
|
|
|
htmlLabels: true,
|
|
|
|
|
|
|
|
init: function () {
|
|
|
|
init.apply(null, arguments)
|
|
|
|
},
|
|
|
|
initialize: function (config) {
|
2017-09-09 16:52:09 +08:00
|
|
|
module.exports.initialize(config)
|
2017-04-11 22:14:25 +08:00
|
|
|
},
|
|
|
|
version: function () {
|
|
|
|
return mermaidAPI.version()
|
|
|
|
},
|
|
|
|
parse: function (text) {
|
|
|
|
return mermaidAPI.parse(text)
|
|
|
|
},
|
|
|
|
parseError: function (err) {
|
|
|
|
log.debug('Mermaid Syntax error:')
|
|
|
|
log.debug(err)
|
|
|
|
},
|
|
|
|
render: function (id, text, callback, element) {
|
|
|
|
return mermaidAPI.render(id, text, callback, element)
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
/**
|
|
|
|
* ## parseError
|
|
|
|
* This function overrides the default configuration.
|
|
|
|
* @param config
|
|
|
|
*/
|
2017-09-09 16:52:09 +08:00
|
|
|
module.exports.parseError = global.mermaid.parseError
|
2015-06-30 14:23:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ##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 16:52:09 +08:00
|
|
|
module.exports.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
|
2017-04-11 23:31:59 +08:00
|
|
|
if (typeof global.mermaid_config !== 'undefined') {
|
2017-04-11 22:14:25 +08:00
|
|
|
if (equals(false, global.mermaid_config.htmlLabels)) {
|
|
|
|
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-04-11 22:14:25 +08:00
|
|
|
if (equals(true, global.mermaid_config.startOnLoad)) {
|
|
|
|
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 16:52:09 +08:00
|
|
|
module.exports.contentLoaded()
|
2017-04-11 22:14:25 +08:00
|
|
|
}, false)
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|