mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-09 10:36:43 +02:00
#931 Last code standard fixes
This commit is contained in:
132
src/mermaid.js
132
src/mermaid.js
@@ -2,10 +2,10 @@
|
||||
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
|
||||
* the diagrams to svg code.
|
||||
*/
|
||||
import he from 'he'
|
||||
import he from 'he';
|
||||
|
||||
import mermaidAPI from './mermaidAPI'
|
||||
import { logger } from './logger'
|
||||
import mermaidAPI from './mermaidAPI';
|
||||
import { logger } from './logger';
|
||||
|
||||
/**
|
||||
* ## init
|
||||
@@ -28,126 +28,142 @@ import { logger } from './logger'
|
||||
* Renders the mermaid diagrams
|
||||
* @param nodes a css selector or an array of nodes
|
||||
*/
|
||||
const init = function () {
|
||||
const conf = mermaidAPI.getConfig()
|
||||
logger.debug('Starting rendering diagrams')
|
||||
let nodes
|
||||
const init = function() {
|
||||
const conf = mermaidAPI.getConfig();
|
||||
logger.debug('Starting rendering diagrams');
|
||||
let nodes;
|
||||
if (arguments.length >= 2) {
|
||||
/*! sequence config was passed as #1 */
|
||||
if (typeof arguments[0] !== 'undefined') {
|
||||
mermaid.sequenceConfig = arguments[0]
|
||||
mermaid.sequenceConfig = arguments[0];
|
||||
}
|
||||
|
||||
nodes = arguments[1]
|
||||
nodes = arguments[1];
|
||||
} else {
|
||||
nodes = arguments[0]
|
||||
nodes = arguments[0];
|
||||
}
|
||||
|
||||
// if last argument is a function this is the callback function
|
||||
let callback
|
||||
let callback;
|
||||
if (typeof arguments[arguments.length - 1] === 'function') {
|
||||
callback = arguments[arguments.length - 1]
|
||||
logger.debug('Callback function found')
|
||||
callback = arguments[arguments.length - 1];
|
||||
logger.debug('Callback function found');
|
||||
} else {
|
||||
if (typeof conf.mermaid !== 'undefined') {
|
||||
if (typeof conf.mermaid.callback === 'function') {
|
||||
callback = conf.mermaid.callback
|
||||
logger.debug('Callback function found')
|
||||
callback = conf.mermaid.callback;
|
||||
logger.debug('Callback function found');
|
||||
} else {
|
||||
logger.debug('No Callback function found')
|
||||
logger.debug('No Callback function found');
|
||||
}
|
||||
}
|
||||
}
|
||||
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
|
||||
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
|
||||
: nodes instanceof window.Node ? [nodes]
|
||||
: nodes // Last case - sequence config was passed pick next
|
||||
nodes =
|
||||
nodes === undefined
|
||||
? document.querySelectorAll('.mermaid')
|
||||
: typeof nodes === 'string'
|
||||
? document.querySelectorAll(nodes)
|
||||
: nodes instanceof window.Node
|
||||
? [nodes]
|
||||
: nodes; // Last case - sequence config was passed pick next
|
||||
|
||||
logger.debug('Start On Load before: ' + mermaid.startOnLoad)
|
||||
logger.debug('Start On Load before: ' + mermaid.startOnLoad);
|
||||
if (typeof mermaid.startOnLoad !== 'undefined') {
|
||||
logger.debug('Start On Load inner: ' + mermaid.startOnLoad)
|
||||
mermaidAPI.initialize({ startOnLoad: mermaid.startOnLoad })
|
||||
logger.debug('Start On Load inner: ' + mermaid.startOnLoad);
|
||||
mermaidAPI.initialize({ startOnLoad: mermaid.startOnLoad });
|
||||
}
|
||||
|
||||
if (typeof mermaid.ganttConfig !== 'undefined') {
|
||||
mermaidAPI.initialize({ gantt: mermaid.ganttConfig })
|
||||
mermaidAPI.initialize({ gantt: mermaid.ganttConfig });
|
||||
}
|
||||
|
||||
let txt
|
||||
let txt;
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
const element = nodes[i]
|
||||
const element = nodes[i];
|
||||
|
||||
/*! Check if previously processed */
|
||||
if (!element.getAttribute('data-processed')) {
|
||||
element.setAttribute('data-processed', true)
|
||||
element.setAttribute('data-processed', true);
|
||||
} else {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const id = `mermaid-${Date.now()}`
|
||||
const id = `mermaid-${Date.now()}`;
|
||||
|
||||
// Fetch the graph definition including tags
|
||||
txt = element.innerHTML
|
||||
txt = element.innerHTML;
|
||||
|
||||
// transforms the html to pure text
|
||||
txt = he.decode(txt).trim().replace(/<br>/ig, '<br/>')
|
||||
txt = he
|
||||
.decode(txt)
|
||||
.trim()
|
||||
.replace(/<br>/gi, '<br/>');
|
||||
|
||||
mermaidAPI.render(id, txt, (svgCode, bindFunctions) => {
|
||||
element.innerHTML = svgCode
|
||||
if (typeof callback !== 'undefined') {
|
||||
callback(id)
|
||||
}
|
||||
if (bindFunctions) bindFunctions(element)
|
||||
}, element)
|
||||
mermaidAPI.render(
|
||||
id,
|
||||
txt,
|
||||
(svgCode, bindFunctions) => {
|
||||
element.innerHTML = svgCode;
|
||||
if (typeof callback !== 'undefined') {
|
||||
callback(id);
|
||||
}
|
||||
if (bindFunctions) bindFunctions(element);
|
||||
},
|
||||
element
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const initialize = function (config) {
|
||||
logger.debug('Initializing mermaid ')
|
||||
const initialize = function(config) {
|
||||
logger.debug('Initializing mermaid ');
|
||||
if (typeof config.mermaid !== 'undefined') {
|
||||
if (typeof config.mermaid.startOnLoad !== 'undefined') {
|
||||
mermaid.startOnLoad = config.mermaid.startOnLoad
|
||||
mermaid.startOnLoad = config.mermaid.startOnLoad;
|
||||
}
|
||||
if (typeof config.mermaid.htmlLabels !== 'undefined') {
|
||||
mermaid.htmlLabels = config.mermaid.htmlLabels
|
||||
mermaid.htmlLabels = config.mermaid.htmlLabels;
|
||||
}
|
||||
}
|
||||
mermaidAPI.initialize(config)
|
||||
}
|
||||
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.
|
||||
*/
|
||||
const contentLoaded = function () {
|
||||
let config
|
||||
const contentLoaded = function() {
|
||||
let config;
|
||||
|
||||
if (mermaid.startOnLoad) {
|
||||
// No config found, do check API config
|
||||
config = mermaidAPI.getConfig()
|
||||
config = mermaidAPI.getConfig();
|
||||
if (config.startOnLoad) {
|
||||
mermaid.init()
|
||||
mermaid.init();
|
||||
}
|
||||
} else {
|
||||
if (typeof mermaid.startOnLoad === 'undefined') {
|
||||
logger.debug('In start, no config')
|
||||
config = mermaidAPI.getConfig()
|
||||
logger.debug('In start, no config');
|
||||
config = mermaidAPI.getConfig();
|
||||
if (config.startOnLoad) {
|
||||
mermaid.init()
|
||||
mermaid.init();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof document !== 'undefined') {
|
||||
/*!
|
||||
* Wait for document loaded before starting the execution
|
||||
*/
|
||||
window.addEventListener('load', function () {
|
||||
contentLoaded()
|
||||
}, false)
|
||||
window.addEventListener(
|
||||
'load',
|
||||
function() {
|
||||
contentLoaded();
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
const mermaid = {
|
||||
@@ -162,6 +178,6 @@ const mermaid = {
|
||||
initialize,
|
||||
|
||||
contentLoaded
|
||||
}
|
||||
};
|
||||
|
||||
export default mermaid
|
||||
export default mermaid;
|
||||
|
Reference in New Issue
Block a user