rendering part
This commit is contained in:
+4
-14
@@ -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);
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
+8
-1
@@ -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
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0"/>');
|
||||
});
|
||||
test('sample svg data', () => {
|
||||
const res = rendering({beautify: false}).toXML({
|
||||
'circle': {
|
||||
'_attributes': {
|
||||
'cx': 50,
|
||||
'cy': 50,
|
||||
'r': 50
|
||||
}
|
||||
}
|
||||
}, 100, 100);
|
||||
expect(res).toEqual('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="50"/></svg>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('compute', () => {
|
||||
test('no nodes no links', () => {
|
||||
const res = rendering({beautify: true}).compute({}, []);
|
||||
expect(res).toEqual('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0"/>');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user