forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmermaid.js
265 lines (230 loc) · 7.21 KB
/
mermaid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
* the diagrams to svg code.
*/
var Logger = require('./logger');
var log = new Logger.Log();
var mermaidAPI = require('./mermaidAPI');
var nextId = 0;
var he = require('he');
module.exports.mermaidAPI = mermaidAPI;
/**
* ## init
* 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`
*
* ```mermaid
* graph LR;
* a(Find elements)-->b{Processed}
* b-->|Yes|c(Leave element)
* b-->|No |d(Transform)
* ```
* Renders the mermaid diagrams
* @param nodes a css selector or an array of nodes
*/
var init = function () {
var conf= mermaidAPI.getConfig();
log.debug('Starting rendering diagrams');
var nodes;
if(arguments.length >= 2){
/*! sequence config was passed as #1 */
if(typeof arguments[0] !== 'undefined'){
global.mermaid.sequenceConfig = arguments[0];
}
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{
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');
}
}
}
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
: nodes instanceof Node ? [nodes]
: nodes; // Last case - sequence config was passed pick next
var i;
if(typeof mermaid_config !== 'undefined'){
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);
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){
element.innerHTML = svgCode;
if(typeof callback !== 'undefined'){
callback(id);
}
bindFunctions(element);
};
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);
} else {
continue;
}
var id = 'mermaidChart' + nextId++;
// Fetch the graph definition including tags
txt = element.innerHTML;
//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;
/**
* ## version
* Function returning version information
* @returns {string} A string containing the version info
*/
exports.version = function(){
return 'v'+require('../package.json').version;
};
/**
* ## initialize
* This function overrides the default configuration.
* @param config
*/
exports.initialize = function(config){
log.debug('Initializing mermaid');
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);
};
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
*/
global.mermaid = {
startOnLoad: true,
htmlLabels: true,
init: function() {
init.apply(null, arguments);
},
initialize: function(config) {
exports.initialize(config);
},
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);
}
};
/**
* ## 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.
*/
exports.contentLoaded = function(){
var config;
// Check state of start config mermaid namespace
if (typeof mermaid_config !== 'undefined') {
if (equals(false, global.mermaid_config.htmlLabels)) {
global.mermaid.htmlLabels = false;
}
}
if(global.mermaid.startOnLoad) {
// For backwards compatability reasons also check mermaid_config variable
if (typeof global.mermaid_config !== 'undefined') {
// Check if property startOnLoad is set
if (equals(true, global.mermaid_config.startOnLoad)) {
global.mermaid.init();
}
}
else {
// No config found, do check API config
config = mermaidAPI.getConfig();
if(config.startOnLoad){
global.mermaid.init();
}
}
}else{
//if(typeof global.mermaid === 'undefined' ){
if(typeof global.mermaid.startOnLoad === 'undefined' ){
log.debug('In start, no config');
config = mermaidAPI.getConfig();
if(config.startOnLoad){
global.mermaid.init();
}
//}else{
//
//}
}
}
};
if(typeof document !== 'undefined'){
/*!
* Wait for document loaded before starting the execution
*/
window.addEventListener('load', function(){
exports.contentLoaded();
}, false);
}
// // Your actual module
// return module.exports;
//}));