rendering part
This commit is contained in:
+4
-14
@@ -1,11 +1,5 @@
|
|||||||
//const xml = require('xml-js');
|
|
||||||
const placing = require('./placing');
|
const placing = require('./placing');
|
||||||
let list = {};
|
const rendering = require('./rendering');
|
||||||
try {
|
|
||||||
list = require('../svg_list.json');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('fa-diagrams: SVG list could not be loaded', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge resources by reading object keys and keeping reference value only if it's type is different from the source
|
* 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 = {
|
const DEFAULT_OPTIONS = {
|
||||||
'placing': {
|
'placing': placing().defaultOptions,
|
||||||
'max-link-length': 2,
|
'rendering': rendering().defaultOptions
|
||||||
'diagonals': true,
|
|
||||||
},
|
|
||||||
'rendering': {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -57,8 +48,7 @@ const self = {
|
|||||||
if (!nodes)
|
if (!nodes)
|
||||||
throw 'Failed to place nodes';
|
throw 'Failed to place nodes';
|
||||||
|
|
||||||
//console.log(JSON.stringify(nodes, null, 2));
|
return rendering(options['rendering']).compute(nodes, links);
|
||||||
console.log(Object.values(nodes).map(n => `${n.name} : [${n.x}, ${n.y}]`).join('\n'));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
+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
|
* @property {string|undefined} direction
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = (options) => {
|
const DEFAULT_OPTIONS = {
|
||||||
|
'max-link-length': 2,
|
||||||
|
'diagonals': true,
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (options = DEFAULT_OPTIONS) => {
|
||||||
|
|
||||||
const self = {
|
const self = {
|
||||||
|
defaultOptions: DEFAULT_OPTIONS,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current bounds of the graph of nodes
|
* 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