72 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-10-05 00:39:07 +02:00
import { Base64 } from 'js-base64';
import mermaid2 from '../../src/mermaid';
/**
* ##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.
*/
2019-10-05 00:39:07 +02:00
const contentLoaded = function() {
let pos = document.location.href.indexOf('?graph=');
if (pos > 0) {
2019-10-05 00:39:07 +02:00
pos = pos + 7;
const graphBase64 = document.location.href.substr(pos);
const graphObj = JSON.parse(Base64.decode(graphBase64));
// const graph = 'hello'
2019-10-05 00:39:07 +02:00
console.log(graphObj);
const div = document.createElement('div');
div.id = 'block';
div.className = 'mermaid';
div.innerHTML = graphObj.code;
document.getElementsByTagName('body')[0].appendChild(div);
2019-10-05 00:46:56 +02:00
global.mermaid.initialize(graphObj.mermaid);
// console.log('graphObj.mermaid', graphObj.mermaid)
2019-10-05 00:39:07 +02:00
global.mermaid.init();
}
2019-10-05 00:39:07 +02:00
};
const contentLoadedApi = function() {
let pos = document.location.href.indexOf('?graph=');
if (pos > 0) {
2019-10-05 00:39:07 +02:00
pos = pos + 7;
const graphBase64 = document.location.href.substr(pos);
const graphObj = JSON.parse(Base64.decode(graphBase64));
// const graph = 'hello'
2019-10-05 00:39:07 +02:00
const div = document.createElement('div');
div.id = 'block';
div.className = 'mermaid';
// div.innerHTML = graphObj.code
2019-10-05 00:39:07 +02:00
document.getElementsByTagName('body')[0].appendChild(div);
2019-10-05 00:46:56 +02:00
global.mermaid.initialize(graphObj.mermaid);
2019-10-05 00:39:07 +02:00
mermaid2.render(
'newid',
graphObj.code,
(svgCode, bindFunctions) => {
div.innerHTML = svgCode;
2019-10-05 00:39:07 +02:00
bindFunctions(div);
},
div
);
}
2019-10-05 00:39:07 +02:00
};
if (typeof document !== 'undefined') {
/*!
* Wait for document loaded before starting the execution
*/
window.addEventListener(
'load',
2019-10-05 00:39:07 +02:00
function() {
if (this.location.href.match('xss.html')) {
2019-10-05 00:39:07 +02:00
this.console.log('Using api');
contentLoadedApi();
} else {
2019-10-05 00:39:07 +02:00
this.console.log('Not using api');
contentLoaded();
}
},
false
2019-10-05 00:39:07 +02:00
);
}