Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 9eb4b80

Browse files
committed
Add auto-layout
1 parent a658860 commit 9eb4b80

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

workflow/uidata.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package workflow
2+
3+
const (
4+
xPadding = 20
5+
yPadding = 20
6+
xGap = 245
7+
yGap = 125
8+
)
9+
10+
// uidata represents all uidata of a workflow.
11+
type uidata = map[string]uidatum
12+
13+
// uidatum represents the position of an object.
14+
type uidatum struct {
15+
XPos int64 `plist:"xpos,omitempty"`
16+
YPos int64 `plist:"ypos,omitempty"`
17+
}
18+
19+
func (i *Info) buildUIData() {
20+
i.UIData = make(uidata)
21+
22+
// A map of object depths to object UIDs at that depth
23+
depthMap := make(map[int64][]string)
24+
25+
for _, obj := range i.Objects {
26+
uid := obj["uid"].(string)
27+
depth := findDepth(uid, i.Connections)
28+
depthMap[depth] = append(depthMap[depth], uid)
29+
}
30+
31+
for depth, uids := range depthMap {
32+
for idx, uid := range uids {
33+
xpos := int64(xPadding + depth*xGap)
34+
ypos := int64(yPadding + idx*yGap)
35+
36+
i.UIData[uid] = uidatum{
37+
XPos: xpos,
38+
YPos: ypos,
39+
}
40+
}
41+
}
42+
}
43+
44+
func findDepth(uid string, conns map[string][]Connection) int64 {
45+
pointers := make([]string, 0)
46+
47+
for connUID, conns := range conns {
48+
for _, conn := range conns {
49+
if conn.To == uid {
50+
pointers = append(pointers, connUID)
51+
}
52+
}
53+
}
54+
55+
depth := int64(0)
56+
57+
for _, pointer := range pointers {
58+
pointerDepth := findDepth(pointer, conns)
59+
if pointerDepth >= depth {
60+
depth = pointerDepth + 1
61+
}
62+
}
63+
64+
return depth
65+
}

workflow/workflow.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Info struct {
1515
Name string `plist:"name,omitempty"`
1616
Objects []map[string]interface{} `plist:"objects,omitempty"`
1717
Readme string `plist:"readme,omitempty"`
18-
UIData map[string]UIData `plist:"uidata,omitempty"`
18+
UIData uidata `plist:"uidata,omitempty"`
1919
WebAddress string `plist:"webaddress,omitempty"`
2020
Variables map[string]string `plist:"variables,omitempty"`
2121
VariablesDontExport []string `plist:"variablesdontexport,omitempty"`
@@ -73,6 +73,8 @@ func NewFromConfig(path string, c config.Config) (*Info, error) {
7373
i.Objects = append(i.Objects, obj)
7474
}
7575

76+
i.buildUIData()
77+
7678
return &i, nil
7779
}
7880

@@ -94,9 +96,3 @@ type Object struct {
9496

9597
// Config is a generic object configuration object.
9698
type Config map[string]interface{}
97-
98-
// UIData represents the position of an object.
99-
type UIData struct {
100-
XPos int64 `plist:"xpos,omitempty"`
101-
YPos int64 `plist:"ypos,omitempty"`
102-
}

0 commit comments

Comments
 (0)