| 1 | /** Created by knut on 14-12-11. */ |
| 2 | import { select } from 'd3'; |
| 3 | import { log, getConfig, setupGraphViewbox } from './mermaidUtils.js'; |
| 4 | |
| 5 | /** |
| 6 | * Draws an info picture in the tag with id: id based on the graph definition in text. |
| 7 | * |
| 8 | * @param {any} text |
| 9 | * @param {any} id |
| 10 | * @param {any} version |
| 11 | */ |
| 12 | export const draw = (text, id, version) => { |
| 13 | try { |
| 14 | const conf = getConfig(); |
| 15 | log.debug('Rendering example diagram\n' + text, 'Conf: '); |
| 16 | const THEME_COLOR_LIMIT = getConfig().themeVariables.THEME_COLOR_LIMIT; |
| 17 | const securityLevel = getConfig().securityLevel; |
| 18 | // Handle root and Document for when rendering in sandbox mode |
| 19 | let sandboxElement; |
| 20 | if (securityLevel === 'sandbox') { |
| 21 | sandboxElement = select('#i' + id); |
| 22 | } |
| 23 | const root = |
| 24 | securityLevel === 'sandbox' |
| 25 | ? select(sandboxElement.nodes()[0].contentDocument.body) |
| 26 | : select('body'); |
| 27 | |
| 28 | const svg = root.select('#' + id); |
| 29 | |
| 30 | const g = svg.append('g'); |
| 31 | |
| 32 | let i; |
| 33 | for (i = 0; i < THEME_COLOR_LIMIT; i++) { |
| 34 | const section = g.append('g').attr('class', 'section-' + i); |
| 35 | section |
| 36 | .append('rect') |
| 37 | .attr('x', (i % 5) * 110) |
| 38 | .attr('y', Math.floor(i / 5) * 90 + 60) |
| 39 | .attr('width', 100) |
| 40 | .attr('height', 60); |
| 41 | section |
| 42 | .append('rect') |
| 43 | .attr('x', (i % 5) * 110) |
| 44 | .attr('y', Math.floor(i / 5) * 90 + 120) |
| 45 | .attr('class', 'inverted') |
| 46 | .attr('width', 100) |
| 47 | .attr('height', 20); |
| 48 | section |
| 49 | .append('text', 'section-' + i) |
| 50 | .text('Section ' + i) |
| 51 | .attr('x', (i % 5) * 110 + 15) |
| 52 | .attr('y', Math.floor(i / 5) * 90 + 95) |
| 53 | .attr('class', 'section-text-' + i); |
| 54 | } |
| 55 | |
| 56 | g.append('text') // text label for the x axis |
| 57 | .attr('x', 100) |
| 58 | .attr('y', 40) |
| 59 | .attr('class', 'version') |
| 60 | .attr('font-size', '32px') |
| 61 | .style('text-anchor', 'middle') |
| 62 | .text('v ' + version); |
| 63 | |
| 64 | // Setup the view box and size of the svg element |
| 65 | setupGraphViewbox(undefined, svg, conf.mindmap.padding, conf.mindmap.useMaxWidth); |
| 66 | } catch (e) { |
| 67 | log.error('Error while rendering info diagram'); |
| 68 | log.error(e.message); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | export default { |
| 73 | draw, |
| 74 | }; |
| 75 | |