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.
|
|
|
|
*/
|
2015-05-26 20:59:23 +02:00
|
|
|
var he = require('he');
|
|
|
|
var mermaidAPI = require('./mermaidAPI');
|
|
|
|
var nextId = 0;
|
2015-06-30 14:23:32 +02:00
|
|
|
var log = require('./logger').create();
|
2015-05-26 20:59:23 +02:00
|
|
|
|
2015-06-07 09:21:19 +02: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.
|
|
|
|
*
|
|
|
|
* 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-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 () {
|
2015-07-15 11:39:46 +02:00
|
|
|
log.debug('Starting rendering diagrams');
|
2015-05-26 20:59:23 +02:00
|
|
|
var nodes;
|
2015-07-15 11:39:46 +02:00
|
|
|
if(arguments.length >= 2){
|
2015-06-30 14:23:32 +02:00
|
|
|
/*! sequence config was passed as #1 */
|
2015-05-26 20:59:23 +02:00
|
|
|
if(typeof arguments[0] !== 'undefined'){
|
|
|
|
mermaid.sequenceConfig = arguments[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = arguments[1];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
nodes = arguments[0];
|
|
|
|
}
|
2015-07-15 11:39:46 +02:00
|
|
|
|
|
|
|
// if last argument is a function this is the callback function
|
|
|
|
var callback;
|
|
|
|
if(typeof arguments[arguments.length-1] === 'function'){
|
|
|
|
callback = arguments[arguments.length-1];
|
|
|
|
log.debug('Callback function found');
|
|
|
|
}else{
|
|
|
|
if(typeof mermaidAPI.getConfig().mermaid.callback === 'function'){
|
|
|
|
callback = mermaidAPI.getConfig().mermaid.callback;
|
|
|
|
log.debug('Callback function found');
|
|
|
|
}else{
|
|
|
|
log.debug('No Callback function found');
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
|
|
|
|
: typeof nodes === "string" ? document.querySelectorAll(nodes)
|
|
|
|
: nodes instanceof Node ? [nodes]
|
2015-06-30 14:23:32 +02:00
|
|
|
/*! Last case - sequence config was passed pick next */
|
2015-05-26 20:59:23 +02:00
|
|
|
: nodes;
|
|
|
|
|
|
|
|
var i;
|
2015-06-07 09:21:19 +02:00
|
|
|
|
2015-05-31 08:19:26 +02:00
|
|
|
if(typeof mermaid_config !== 'undefined'){
|
|
|
|
mermaidAPI.initialize(mermaid_config);
|
2015-06-21 17:25:58 +02:00
|
|
|
|
2015-05-31 08:19:26 +02:00
|
|
|
}
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2015-07-15 11:39:46 +02:00
|
|
|
log.debug('Start On Load before: '+mermaid.startOnLoad);
|
2015-06-30 14:23:32 +02:00
|
|
|
if(typeof mermaid.startOnLoad !== 'undefined'){
|
2015-07-15 11:39:46 +02:00
|
|
|
log.debug('Start On Load inner: '+mermaid.startOnLoad);
|
2015-06-30 14:23:32 +02:00
|
|
|
mermaidAPI.initialize({startOnLoad:mermaid.startOnLoad});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-21 17:25:58 +02:00
|
|
|
if(typeof mermaid.ganttConfig !== 'undefined'){
|
|
|
|
mermaidAPI.initialize({gantt:mermaid.ganttConfig});
|
|
|
|
}
|
|
|
|
|
2015-05-26 20:59:23 +02:00
|
|
|
var insertSvg = function(svgCode){
|
|
|
|
element.innerHTML = svgCode;
|
2015-07-15 11:39:46 +02:00
|
|
|
if(typeof callback !== 'undefined'){
|
|
|
|
callback(id);
|
|
|
|
}
|
2015-05-26 20:59:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < nodes.length; i++) {
|
|
|
|
var element = nodes[i];
|
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
/*! Check if previously processed */
|
2015-05-26 20:59:23 +02:00
|
|
|
if(!element.getAttribute("data-processed")) {
|
|
|
|
element.setAttribute("data-processed", true);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = 'mermaidChart' + nextId++;
|
|
|
|
|
|
|
|
var txt = element.innerHTML;
|
|
|
|
txt = txt.replace(/>/g,'>');
|
|
|
|
txt = txt.replace(/</g,'<');
|
|
|
|
txt = he.decode(txt).trim();
|
|
|
|
|
2015-06-16 10:40:08 +02:00
|
|
|
mermaidAPI.render(id,txt,insertSvg, element);
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-06-07 09:21:19 +02:00
|
|
|
exports.init = init;
|
2015-06-30 14:23:32 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
exports.version = function(){
|
2015-06-07 09:21:19 +02: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
|
|
|
|
*/
|
2015-06-07 09:21:19 +02:00
|
|
|
exports.initialize = function(config){
|
2015-07-15 11:39:46 +02:00
|
|
|
log.debug('Initializing mermaid');
|
|
|
|
if(typeof config.mermaid.startOnLoad !== 'undefined'){
|
|
|
|
global.mermaid.startOnLoad = config.mermaid.startOnLoad;
|
|
|
|
}
|
|
|
|
if(typeof config.mermaid.htmlLabels !== 'undefined'){
|
|
|
|
global.mermaid.htmlLabels = config.mermaid.htmlLabels;
|
|
|
|
}
|
2015-06-07 09:21:19 +02:00
|
|
|
mermaidAPI.initialize(config);
|
2015-06-07 16:51:56 +02:00
|
|
|
};
|
2015-06-07 09:21:19 +02:00
|
|
|
|
2015-06-30 14:23:32 +02:00
|
|
|
|
2015-05-26 20:59:23 +02:00
|
|
|
var equals = function (val, variable){
|
|
|
|
if(typeof variable === 'undefined'){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return (val === variable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 = {
|
|
|
|
startOnLoad: true,
|
|
|
|
htmlLabels: true,
|
|
|
|
|
|
|
|
init: function(sequenceConfig, nodes) {
|
|
|
|
init.apply(null, arguments);
|
|
|
|
},
|
2015-06-07 09:21:19 +02:00
|
|
|
initialize: function(config) {
|
2015-07-15 11:39:46 +02:00
|
|
|
exports.initialize(config);
|
2015-06-07 09:21:19 +02:00
|
|
|
},
|
2015-05-26 20:59:23 +02:00
|
|
|
version: function() {
|
|
|
|
return mermaidAPI.version();
|
|
|
|
},
|
|
|
|
parse: function(text) {
|
|
|
|
return mermaidAPI.parse(text);
|
|
|
|
},
|
|
|
|
parseError: function(err, hash) {
|
2015-06-30 14:23:32 +02:00
|
|
|
log.debug('Mermaid Syntax error:');
|
|
|
|
log.debug(err);
|
2015-05-26 20:59:23 +02:00
|
|
|
},
|
2015-06-20 20:58:58 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
exports.parseError = global.mermaid.parseError;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ##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.
|
|
|
|
*/
|
2015-05-26 20:59:23 +02:00
|
|
|
exports.contentLoaded = function(){
|
2015-06-07 16:51:56 +02:00
|
|
|
var config;
|
2015-05-26 20:59:23 +02:00
|
|
|
// Check state of start config mermaid namespace
|
|
|
|
if (typeof mermaid_config !== 'undefined') {
|
|
|
|
if (equals(false, mermaid_config.htmlLabels)) {
|
|
|
|
global.mermaid.htmlLabels = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(global.mermaid.startOnLoad) {
|
|
|
|
// For backwards compatability reasons also check mermaid_config variable
|
|
|
|
if (typeof mermaid_config !== 'undefined') {
|
|
|
|
// Check if property startOnLoad is set
|
|
|
|
if (equals(true, mermaid_config.startOnLoad)) {
|
|
|
|
global.mermaid.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-06-30 14:23:32 +02:00
|
|
|
mermaidAPI.initialize({startOnLoad:global.mermaid.startOnLoad});
|
2015-06-07 09:21:19 +02:00
|
|
|
// No config found, do check API config
|
2015-06-07 16:51:56 +02:00
|
|
|
config = mermaidAPI.getConfig();
|
2015-06-07 09:21:19 +02:00
|
|
|
if(config.startOnLoad){
|
|
|
|
global.mermaid.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2015-06-07 16:51:56 +02:00
|
|
|
config = mermaidAPI.getConfig();
|
2015-06-07 09:21:19 +02:00
|
|
|
if(config.startOnLoad){
|
2015-05-26 20:59:23 +02:00
|
|
|
global.mermaid.init();
|
|
|
|
}
|
2015-06-07 09:21:19 +02:00
|
|
|
|
2015-05-26 20:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(typeof document !== 'undefined'){
|
2015-06-30 14:23:32 +02:00
|
|
|
/*!
|
2015-05-26 20:59:23 +02:00
|
|
|
* Wait for document loaded before starting the execution
|
|
|
|
*/
|
|
|
|
document.addEventListener('DOMContentLoaded', function(){
|
|
|
|
exports.contentLoaded();
|
|
|
|
}, false);
|
|
|
|
}
|