Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var dagree = require("dagre");
- var org_result
- function getNodeConnections(result) { // Call this function with parsed flows.json
- org_result = result
- result = nodeElements(result)
- result = sortByWeight(result)
- parseAndPosition(result)
- return org_result
- }
- function sortByWeight(result) {
- result.sort((a, b) => {
- const weightA = calculateWeight(a);
- const weightB = calculateWeight(b);
- return weightB - weightA;
- });
- return result;
- }
- function calculateWeight(obj) {
- let weight = 0;
- if (obj.hasOwnProperty('wires')) {
- weight += obj.wires.length;
- obj.wires.forEach(childArr => {
- weight += childArr.length;
- });
- }
- return weight;
- }
- function nodeElements(result) {
- var subflowID =[]
- result.forEach(element => {
- if (element.type =="subflow"){
- subflowID.push(element.id)
- }
- });
- var res =[]
- for (var o of result){
- if (!o.x && !o.y){
- continue
- }
- if (!subflowID.includes(o.z)){
- res.push(o)
- continue
- }
- }
- return res
- }
- function defineHandW(node){
- const X = 100
- const Y = 10
- const OUTPUTADD = 2
- const LETTERADD = 20
- const MARGIN_X = 10
- const MARGIN_Y = 10
- //define Height
- node.height = Y + MARGIN_Y
- node.width = X + MARGIN_X
- node.height += node.wires.length * OUTPUTADD
- //define Width
- if (node.name.length > 5 ){
- node.width += ((node.name.length- 5)/2) * LETTERADD
- }
- console.log(node)
- return node
- }
- function parseAndPosition(res){
- var g = new dagree.graphlib.Graph()
- g.setGraph({});
- g.setDefaultEdgeLabel(function() { return {}; });
- res.forEach(element => {
- element = defineHandW(element)
- g.setNode(element.id, {width: element.width , height: element.height})
- element.wires.forEach(output => {
- output.forEach(wire => {
- g.setEdge(`${element.id}`, `${wire}`)
- })
- })
- });
- g.graph().rankdir = "LR"
- g.graph().marginx = 20
- g.graph().marginy = 20
- dagree.layout(g)
- g.nodes().forEach(function(v) {
- org_result.forEach(final => {
- if (final.id == v){
- final.x = g.node(v).x
- final.y = g.node(v).y
- }
- })
- });
- console.log( JSON.stringify(org_result))
- }
- module.exports.getNodeConnections = getNodeConnections
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement