-
Notifications
You must be signed in to change notification settings - Fork 19
/
ui.go
84 lines (70 loc) · 2.01 KB
/
ui.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
77
78
79
80
81
82
83
84
package definition
import (
"encoding/json"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
//------------------------------------------------------------------------------------------
// Top level UI section
//------------------------------------------------------------------------------------------
type ui struct {
nodes map[flows.NodeUUID]flows.UINodeDetails
stickies map[utils.UUID]flows.Sticky
}
// NewUI creates a new UI section
func NewUI() flows.UI {
return ui{
nodes: make(map[flows.NodeUUID]flows.UINodeDetails),
stickies: make(map[utils.UUID]flows.Sticky),
}
}
func (u ui) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"nodes": u.nodes,
"stickies": u.stickies,
})
}
// AddNode adds information about a node
func (u ui) AddNode(uuid flows.NodeUUID, nodeDetails flows.UINodeDetails) {
u.nodes[uuid] = nodeDetails
}
func (u ui) GetNode(uuid flows.NodeUUID) flows.UINodeDetails {
return u.nodes[uuid]
}
// AddSticky adds a new sticky note
func (u ui) AddSticky(sticky flows.Sticky) {
u.stickies[utils.NewUUID()] = sticky
}
//------------------------------------------------------------------------------------------
// Details for a specific node's configuration
//------------------------------------------------------------------------------------------
type position struct {
left int
top int
}
func (p position) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"left": p.left,
"top": p.top,
})
}
type uiNodeDetails struct {
NodeType_ flows.UINodeType `json:"type,omitempty"`
UiNodeConfig_ flows.UINodeConfig `json:"config,omitempty"`
Position_ flows.Position `json:"position"`
}
// NewUINodeDetails creates a ui configuration for a specific
func NewUINodeDetails(
x, y int,
nodeType flows.UINodeType,
uiNodeConfig flows.UINodeConfig,
) flows.UINodeDetails {
return uiNodeDetails{
NodeType_: nodeType,
UiNodeConfig_: uiNodeConfig,
Position_: &position{
left: x,
top: y,
},
}
}