This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uidata.go
76 lines (61 loc) · 1.53 KB
/
uidata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package workflow
import "sort"
const (
xPadding = 20
yPadding = 20
xGap = 245
yGap = 125
)
// uidata represents all uidata of a workflow.
type uidata = map[string]uidatum
// uidatum represents the position of an object.
type uidatum struct {
XPos int64 `plist:"xpos,omitempty"`
YPos int64 `plist:"ypos,omitempty"`
}
func (i *Info) buildUIData() {
i.UIData = make(uidata)
// A map of object depths to object UIDs at that depth
depthMap := make(map[int64][]string)
// Sort for testing stability
sortedObjs := make([]map[string]interface{}, len(i.Objects))
copy(sortedObjs, i.Objects)
sort.Slice(sortedObjs, func(i, j int) bool {
iType := sortedObjs[i]["type"].(string)
jType := sortedObjs[j]["type"].(string)
return iType < jType
})
for _, obj := range sortedObjs {
uid := obj["uid"].(string)
depth := findDepth(uid, i.Connections)
depthMap[depth] = append(depthMap[depth], uid)
}
for depth, uids := range depthMap {
for idx, uid := range uids {
xpos := int64(xPadding + depth*xGap)
ypos := int64(yPadding + idx*yGap)
i.UIData[uid] = uidatum{
XPos: xpos,
YPos: ypos,
}
}
}
}
func findDepth(uid string, conns map[string][]Connection) int64 {
pointers := make([]string, 0)
for connUID, conns := range conns {
for _, conn := range conns {
if conn.To == uid {
pointers = append(pointers, connUID)
}
}
}
depth := int64(0)
for _, pointer := range pointers {
pointerDepth := findDepth(pointer, conns)
if pointerDepth >= depth {
depth = pointerDepth + 1
}
}
return depth
}