mermaid/src/mermaid.js

266 lines
7.2 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.
*/
2015-11-11 18:37:53 +01:00
2015-11-15 15:06:24 +01:00
var Logger = require('./logger');
2015-11-11 18:37:53 +01:00
var log = new Logger.Log();
2015-05-26 20:59:23 +02:00
var mermaidAPI = require('./mermaidAPI');
var nextId = 0;
var he = require('he');
2015-05-26 20:59:23 +02:00
module.exports.mermaidAPI = mermaidAPI;
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 () {
2015-10-04 19:30:53 +02:00
var conf= mermaidAPI.getConfig();
log.debug('Starting rendering diagrams');
2015-05-26 20:59:23 +02:00
var nodes;
if(arguments.length >= 2){
/*! sequence config was passed as #1 */
2015-05-26 20:59:23 +02:00
if(typeof arguments[0] !== 'undefined'){
global.mermaid.sequenceConfig = arguments[0];
2015-05-26 20:59:23 +02:00
}
nodes = arguments[1];
}
else{
nodes = arguments[0];
}
// 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{
2015-07-15 11:59:01 +02:00
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-05-26 20:59:23 +02:00
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
2015-05-26 20:59:23 +02:00
: nodes instanceof Node ? [nodes]
: nodes; // Last case - sequence config was passed pick next
2015-05-26 20:59:23 +02:00
var i;
2015-05-31 08:19:26 +02:00
if(typeof mermaid_config !== 'undefined'){
mermaidAPI.initialize(global.mermaid_config);
2015-05-31 08:19:26 +02:00
}
log.debug('Start On Load before: '+global.mermaid.startOnLoad);
if(typeof global.mermaid.startOnLoad !== 'undefined'){
log.debug('Start On Load inner: '+global.mermaid.startOnLoad);
mermaidAPI.initialize({startOnLoad:global.mermaid.startOnLoad});
}
if(typeof global.mermaid.ganttConfig !== 'undefined'){
mermaidAPI.initialize({gantt:global.mermaid.ganttConfig});
}
var txt;
var insertSvg = function(svgCode, bindFunctions){
2015-05-26 20:59:23 +02:00
element.innerHTML = svgCode;
if(typeof callback !== 'undefined'){
callback(id);
}
bindFunctions(element);
2015-05-26 20:59:23 +02:00
};
2015-05-26 20:59:23 +02:00
for (i = 0; i < nodes.length; i++) {
var element = nodes[i];
/*! Check if previously processed */
if(!element.getAttribute('data-processed')) {
element.setAttribute('data-processed', true);
2015-05-26 20:59:23 +02:00
} else {
continue;
}
var id = 'mermaidChart' + nextId++;
// Fetch the graph definition including tags
txt = element.innerHTML;
2015-05-26 20:59:23 +02:00
//console.warn('delivererd from the browser: ');
//console.warn(txt);
// transforms the html to pure text
txt = he.decode(txt).trim();
//console.warn('he decode: ');
//console.warn(txt);
mermaidAPI.render(id,txt,insertSvg, element);
}
};
exports.init = init;
exports.parse = mermaidAPI.parse;
2015-05-26 20:59:23 +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(){
return 'v'+require('../package.json').version;
2015-05-26 20:59:23 +02:00
};
/**
* ## initialize
* This function overrides the default configuration.
* @param config
*/
exports.initialize = function(config){
log.debug('Initializing mermaid');
2015-07-15 11:59:01 +02:00
if(typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') {
global.mermaid.startOnLoad = config.mermaid.startOnLoad;
}
if (typeof config.mermaid.htmlLabels !== 'undefined') {
global.mermaid.htmlLabels = config.mermaid.htmlLabels;
}
}
mermaidAPI.initialize(config);
2015-06-07 16:51:56 +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);
}
};
/**
* 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() {
2015-05-26 20:59:23 +02:00
init.apply(null, arguments);
},
initialize: function(config) {
exports.initialize(config);
},
2015-05-26 20:59:23 +02:00
version: function() {
return mermaidAPI.version();
},
parse: function(text) {
return mermaidAPI.parse(text);
},
parseError: function(err) {
log.debug('Mermaid Syntax error:');
log.debug(err);
2015-05-26 20:59:23 +02:00
},
render:function(id, text,callback, element){
return mermaidAPI.render(id, text,callback, element);
2015-05-26 20:59:23 +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, global.mermaid_config.htmlLabels)) {
2015-05-26 20:59:23 +02:00
global.mermaid.htmlLabels = false;
}
}
if(global.mermaid.startOnLoad) {
// For backwards compatability reasons also check mermaid_config variable
if (typeof global.mermaid_config !== 'undefined') {
2015-05-26 20:59:23 +02:00
// Check if property startOnLoad is set
if (equals(true, global.mermaid_config.startOnLoad)) {
2015-05-26 20:59:23 +02:00
global.mermaid.init();
}
}
else {
// No config found, do check API config
2015-06-07 16:51:56 +02:00
config = mermaidAPI.getConfig();
if(config.startOnLoad){
global.mermaid.init();
}
}
}else{
2015-09-26 13:00:30 +02:00
//if(typeof global.mermaid === 'undefined' ){
if(typeof global.mermaid.startOnLoad === 'undefined' ){
2015-09-26 13:00:30 +02:00
log.debug('In start, no config');
config = mermaidAPI.getConfig();
if(config.startOnLoad){
global.mermaid.init();
}
//}else{
//
//}
2015-05-26 20:59:23 +02:00
}
2015-05-26 20:59:23 +02:00
}
};
if(typeof document !== 'undefined'){
/*!
2015-05-26 20:59:23 +02:00
* Wait for document loaded before starting the execution
*/
2015-11-15 15:06:24 +01:00
window.addEventListener('load', function(){
2015-05-26 20:59:23 +02:00
exports.contentLoaded();
}, false);
}
// // Your actual module
// return module.exports;
//}));