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

Commit

Permalink
Add auto-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jclem committed Aug 21, 2019
1 parent a658860 commit 9eb4b80
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
65 changes: 65 additions & 0 deletions workflow/uidata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package workflow

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)

for _, obj := range i.Objects {
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
}
10 changes: 3 additions & 7 deletions workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Info struct {
Name string `plist:"name,omitempty"`
Objects []map[string]interface{} `plist:"objects,omitempty"`
Readme string `plist:"readme,omitempty"`
UIData map[string]UIData `plist:"uidata,omitempty"`
UIData uidata `plist:"uidata,omitempty"`
WebAddress string `plist:"webaddress,omitempty"`
Variables map[string]string `plist:"variables,omitempty"`
VariablesDontExport []string `plist:"variablesdontexport,omitempty"`
Expand Down Expand Up @@ -73,6 +73,8 @@ func NewFromConfig(path string, c config.Config) (*Info, error) {
i.Objects = append(i.Objects, obj)
}

i.buildUIData()

return &i, nil
}

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

// Config is a generic object configuration object.
type Config map[string]interface{}

// UIData represents the position of an object.
type UIData struct {
XPos int64 `plist:"xpos,omitempty"`
YPos int64 `plist:"ypos,omitempty"`
}

0 comments on commit 9eb4b80

Please sign in to comment.