From 2328f97133630550058f6913ba52f8096af586af Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 12 Jul 2019 10:28:53 +0200 Subject: [PATCH] rendering part --- src/index.js | 18 ++++------------ src/placing.js | 9 +++++++- src/rendering.js | 49 ++++++++++++++++++++++++++++++++++++++++++ test/rendering.test.js | 28 ++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/rendering.js create mode 100644 test/rendering.test.js diff --git a/src/index.js b/src/index.js index 21b8b8e..c434f86 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,5 @@ -//const xml = require('xml-js'); const placing = require('./placing'); -let list = {}; -try { - list = require('../svg_list.json'); -} catch (err) { - console.error('fa-diagrams: SVG list could not be loaded', err); -} +const rendering = require('./rendering'); /** * Merge resources by reading object keys and keeping reference value only if it's type is different from the source @@ -30,11 +24,8 @@ const merge = (ref, src) => { }; const DEFAULT_OPTIONS = { - 'placing': { - 'max-link-length': 2, - 'diagonals': true, - }, - 'rendering': {} + 'placing': placing().defaultOptions, + 'rendering': rendering().defaultOptions }; @@ -57,8 +48,7 @@ const self = { if (!nodes) throw 'Failed to place nodes'; - //console.log(JSON.stringify(nodes, null, 2)); - console.log(Object.values(nodes).map(n => `${n.name} : [${n.x}, ${n.y}]`).join('\n')); + return rendering(options['rendering']).compute(nodes, links); }, }; diff --git a/src/placing.js b/src/placing.js index 3c8d6ea..e878038 100644 --- a/src/placing.js +++ b/src/placing.js @@ -16,8 +16,15 @@ const newmap = (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill( * @property {string|undefined} direction */ -module.exports = (options) => { +const DEFAULT_OPTIONS = { + 'max-link-length': 2, + 'diagonals': true, +}; + +module.exports = (options = DEFAULT_OPTIONS) => { + const self = { + defaultOptions: DEFAULT_OPTIONS, /** * Get the current bounds of the graph of nodes diff --git a/src/rendering.js b/src/rendering.js new file mode 100644 index 0000000..fcc7acc --- /dev/null +++ b/src/rendering.js @@ -0,0 +1,49 @@ +const convert = require('xml-js'); + +let list = {}; +try { + list = require('../svg_list.json'); +} catch (err) { + console.error('fa-diagrams: SVG list could not be loaded', err); +} + +const DEFAULT_OPTIONS = { + 'beautify': false +}; + +module.exports = (options = DEFAULT_OPTIONS) => { + const self = { + defaultOptions: DEFAULT_OPTIONS, + + /** + * Convert xml-js data into correct svg xml string + * @param {Object} data + * @param {number} width + * @param {number} height + * @returns {string} + */ + toXML: (data, width, height) => { + const xml = { + 'svg': { + '_attributes': { + 'xmlns': 'http://www.w3.org/2000/svg', + 'viewBox': `0 0 ${width} ${height}` + } + } + }; + Object.keys(data).forEach(key => { + xml['svg'][key] = data[key]; + }); + return convert.js2xml(xml, { + compact: true, + spaces: options['beautify'] ? '\t' : 0 + }); + }, + + compute: () => { + return self.toXML({}, 0, 0); //TODO temporary + } + }; + + return self; +}; \ No newline at end of file diff --git a/test/rendering.test.js b/test/rendering.test.js new file mode 100644 index 0000000..663a1a9 --- /dev/null +++ b/test/rendering.test.js @@ -0,0 +1,28 @@ +/* jshint -W117 */ +const rendering = require('../src/rendering'); + +describe('toXML', () => { + test('no data', () => { + const res = rendering({beautify: false}).toXML({}, 0, 0); + expect(res).toEqual(''); + }); + test('sample svg data', () => { + const res = rendering({beautify: false}).toXML({ + 'circle': { + '_attributes': { + 'cx': 50, + 'cy': 50, + 'r': 50 + } + } + }, 100, 100); + expect(res).toEqual(''); + }); +}); + +describe('compute', () => { + test('no nodes no links', () => { + const res = rendering({beautify: true}).compute({}, []); + expect(res).toEqual(''); + }); +}); \ No newline at end of file