Advertisement
Guest User

Untitled

a guest
May 15th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. var dagree = require("dagre");
  2. var org_result
  3.  
  4. function getNodeConnections(result) { // Call this function with parsed flows.json
  5. org_result = result
  6. result = nodeElements(result)
  7. result = sortByWeight(result)
  8. parseAndPosition(result)
  9. return org_result
  10. }
  11. function sortByWeight(result) {
  12. result.sort((a, b) => {
  13. const weightA = calculateWeight(a);
  14. const weightB = calculateWeight(b);
  15. return weightB - weightA;
  16. });
  17.  
  18. return result;
  19. }
  20. function calculateWeight(obj) {
  21. let weight = 0;
  22. if (obj.hasOwnProperty('wires')) {
  23. weight += obj.wires.length;
  24. obj.wires.forEach(childArr => {
  25. weight += childArr.length;
  26. });
  27. }
  28. return weight;
  29. }
  30. function nodeElements(result) {
  31. var subflowID =[]
  32. result.forEach(element => {
  33. if (element.type =="subflow"){
  34. subflowID.push(element.id)
  35. }
  36. });
  37. var res =[]
  38. for (var o of result){
  39. if (!o.x && !o.y){
  40. continue
  41. }
  42. if (!subflowID.includes(o.z)){
  43. res.push(o)
  44. continue
  45. }
  46. }
  47. return res
  48. }
  49.  
  50. function defineHandW(node){
  51. const X = 100
  52. const Y = 10
  53. const OUTPUTADD = 2
  54. const LETTERADD = 20
  55. const MARGIN_X = 10
  56. const MARGIN_Y = 10
  57. //define Height
  58. node.height = Y + MARGIN_Y
  59. node.width = X + MARGIN_X
  60. node.height += node.wires.length * OUTPUTADD
  61. //define Width
  62. if (node.name.length > 5 ){
  63. node.width += ((node.name.length- 5)/2) * LETTERADD
  64. }
  65. console.log(node)
  66. return node
  67. }
  68.  
  69. function parseAndPosition(res){
  70. var g = new dagree.graphlib.Graph()
  71.  
  72. g.setGraph({});
  73. g.setDefaultEdgeLabel(function() { return {}; });
  74.  
  75. res.forEach(element => {
  76. element = defineHandW(element)
  77. g.setNode(element.id, {width: element.width , height: element.height})
  78.  
  79. element.wires.forEach(output => {
  80. output.forEach(wire => {
  81. g.setEdge(`${element.id}`, `${wire}`)
  82. })
  83. })
  84. });
  85. g.graph().rankdir = "LR"
  86. g.graph().marginx = 20
  87. g.graph().marginy = 20
  88. dagree.layout(g)
  89. g.nodes().forEach(function(v) {
  90.  
  91. org_result.forEach(final => {
  92.  
  93. if (final.id == v){
  94. final.x = g.node(v).x
  95. final.y = g.node(v).y
  96. }
  97.  
  98.  
  99. })
  100.  
  101.  
  102. });
  103.  
  104. console.log( JSON.stringify(org_result))
  105.  
  106. }
  107. module.exports.getNodeConnections = getNodeConnections
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement