1 |
|
2 | import {utils} from '@buggyorg/graphtools'
|
3 | import _ from 'lodash'
|
4 |
|
5 | export function convertPort (nodeName, type, port, portType) {
|
6 | return {
|
7 | id: nodeName + '_' + port + '_' + portType,
|
8 | meta: {
|
9 | type: type,
|
10 | name: port
|
11 | }
|
12 | }
|
13 | }
|
14 |
|
15 | export function convertNode (node) {
|
16 | return {
|
17 | id: node.v,
|
18 | labels: [{text: node.value.id || node.v, name: node.v}],
|
19 | ports: _.concat(
|
20 | _.map(node.value.inputPorts, _.partial(convertPort, node.v, _, _, 'in')),
|
21 | _.map(node.value.outputPorts, _.partial(convertPort, node.v, _, _, 'out'))
|
22 | ),
|
23 | meta: Object.assign({}, node.value, { style: _.get(node, 'value.meta.style') })
|
24 | }
|
25 | }
|
26 |
|
27 | export function convertNodes (nodes) {
|
28 | return _.map(nodes, convertNode)
|
29 | }
|
30 |
|
31 | export function convertEdge (graph, edge) {
|
32 | var sourceHierarchy = false
|
33 | var targetHierarchy = false
|
34 | if (graph.parent(edge.v) === edge.w) {
|
35 | sourceHierarchy = true
|
36 | } else if (graph.parent(edge.w) === edge.v) {
|
37 | targetHierarchy = true
|
38 | } else if (edge.v === edge.w) {
|
39 | sourceHierarchy = true
|
40 | targetHierarchy = true
|
41 | }
|
42 | return {
|
43 | id: edge.v + edge.w,
|
44 | source: edge.v,
|
45 | sourcePort: edge.v + '_' + edge.value.outPort + ((targetHierarchy) ? '_in' : '_out'),
|
46 | target: edge.w,
|
47 | targetPort: edge.w + '_' + edge.value.inPort + ((sourceHierarchy) ? '_out' : '_in'),
|
48 | meta: {
|
49 | sourceType: graph.node(edge.v)[(targetHierarchy) ? 'inputPorts' : 'outputPorts'][edge.value.outPort],
|
50 | targetType: graph.node(edge.w)[(sourceHierarchy) ? 'outputPorts' : 'inputPorts'][edge.value.inPort],
|
51 | sourceNode: edge.v,
|
52 | sourcePort: edge.value.outPort,
|
53 | targetNode: edge.w,
|
54 | targetPort: edge.value.inPort,
|
55 | style: _.get(edge, 'value.meta.style')
|
56 | }
|
57 | }
|
58 | }
|
59 |
|
60 | export function convertEdges (graph, edges) {
|
61 | return _.map(edges, _.partial(convertEdge, graph, _))
|
62 | }
|
63 |
|
64 | function combineNodes (graph, node, childMap, edgeMap) {
|
65 | if (_.has(childMap, node.id)) {
|
66 | node.children = _.map(childMap[node.id], _.partial(combineNodes, graph, _, childMap, edgeMap))
|
67 | }
|
68 | if (_.has(edgeMap, node.id)) {
|
69 | node.edges = convertEdges(graph, edgeMap[node.id])
|
70 | }
|
71 | return node
|
72 | }
|
73 |
|
74 | var edgeParent = function (graph, edge) {
|
75 | var outP = edge.v
|
76 | var inP = edge.w
|
77 | if (outP === inP) {
|
78 | return outP
|
79 | } else if (graph.parent(outP) === graph.parent(inP)) {
|
80 | return graph.parent(outP)
|
81 | } else if (graph.parent(outP) === inP) {
|
82 | return inP
|
83 | } else {
|
84 | return outP
|
85 | }
|
86 | }
|
87 |
|
88 | function setEdgeParent (edge, graph) {
|
89 | return _.merge({}, edge, {parent: edgeParent(graph, edge)})
|
90 | }
|
91 |
|
92 | export function convertGraph (graph) {
|
93 | var editGraph = utils.edit(graph)
|
94 | var nodes = _(editGraph.nodes)
|
95 | .groupBy('parent')
|
96 | .mapValues(convertNodes)
|
97 | .value()
|
98 | var edges = _(editGraph.edges)
|
99 | .map(_.partial(setEdgeParent, _, graph))
|
100 | .groupBy('parent')
|
101 | .value()
|
102 | return {
|
103 | id: 'root',
|
104 |
|
105 | children: _.map(nodes[undefined], _.partial(combineNodes, graph, _, nodes, edges)),
|
106 | edges: convertEdges(graph, edges[undefined])
|
107 | }
|
108 | }
|