-
Notifications
You must be signed in to change notification settings - Fork 1
/
display_model.go
171 lines (147 loc) · 3.83 KB
/
display_model.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package reactor
import "reflect"
type DisplayModel struct {
ID string `json:"id,omitempty"`
Element string `json:"el,omitempty"`
Text string `json:"te,omitempty"`
Children []*DisplayModel `json:"ch,omitempty"`
Attributes map[string]interface{} `json:"at,omitempty"`
ReportEvents []ReportEvent `json:"ev,omitempty"`
}
type ReportEvent struct {
Name string `json:"name,omitempty"`
StopPropagation bool `json:"sp,omitempty"`
PreventDefault bool `json:"pd,omitempty"`
ExtraValues []string `json:"xv,omitempty"`
}
type DisplayUpdate struct {
Model *DisplayModel `json:"model,omitempty"`
Eval string `json:"eval,omitempty"`
Title string `json:"title,omitempty"`
Location string `json:"location,omitempty"`
}
func (d *DisplayUpdate) DeepEqual(other *DisplayUpdate) bool {
return reflect.DeepEqual(d, other)
}
func (m *DisplayModel) FindElementByID(id string) *DisplayModel {
if m.ID == id {
return m
}
for _, child := range m.Children {
found := child.FindElementByID(id)
if found != nil {
return found
}
}
return nil
}
func (m *DisplayModel) ReplaceElementWithPath(path []int, replacement *DisplayModel) {
if len(path) == 0 {
panic("can't replace myself")
}
if len(path) == 1 {
m.Children[path[0]] = replacement
} else {
m.Children[path[0]].ReplaceElementWithPath(path[1:], replacement)
}
}
func (m *DisplayModel) findElementPathByID(id string, path []int) *[]int {
if m.ID == id {
return &path
}
for i, child := range m.Children {
p := append(path, i)
found := child.findElementPathByID(id, p)
if found != nil {
return found
}
}
return nil
}
func (m *DisplayModel) FindElementPathByID(id string) *[]int {
return m.findElementPathByID(id, []int{})
}
func (m *DisplayModel) SetElementAttribute(id, name string, value interface{}) {
element := m.FindElementByID(id)
if element != nil {
element.Attributes[name] = value
}
}
func (m *DisplayModel) SetElementText(id, text string) *DisplayModel {
element := m.FindElementByID(id)
if element != nil && text != "" {
element.Children = []*DisplayModel{
&DisplayModel{
Text: text,
},
}
}
return element
}
func (m *DisplayModel) ReplaceChild(id string, replacement *DisplayModel) *DisplayModel {
for index, child := range m.Children {
if child.ID == id {
m.Children[index] = replacement
} else {
child.ReplaceChild(id, replacement)
}
}
return m
}
func (m *DisplayModel) DeleteChild(id string) {
indexToDelete := -1
for index, child := range m.Children {
if child.ID == id {
indexToDelete = index
break
} else {
child.DeleteChild(id)
}
}
if indexToDelete >= 0 {
m.Children = append(m.Children[:indexToDelete], m.Children[indexToDelete+1:]...)
}
}
func (m *DisplayModel) AppendChild(id string, child *DisplayModel) {
element := m.FindElementByID(id)
if element != nil {
element.Children = append(element.Children, child)
}
}
func (m *DisplayModel) DeepCopy() *DisplayModel {
result := *m
result.Children = []*DisplayModel{}
for _, c := range m.Children {
result.Children = append(result.Children, c.DeepCopy())
}
result.Attributes = map[string]interface{}{}
for k, v := range m.Attributes {
result.Attributes[k] = v
}
return &result
}
func (m *DisplayModel) DeepEqual(other *DisplayModel) bool {
if m == other {
return true
}
if m == nil {
return false
}
if other == nil {
return false
}
if m.ID != other.ID ||
m.Element != other.Element ||
m.Text != other.Text ||
!reflect.DeepEqual(m.Attributes, other.Attributes) ||
!reflect.DeepEqual(m.ReportEvents, other.ReportEvents) ||
len(m.Children) != len(other.Children) {
return false
}
for i, child := range m.Children {
if !child.DeepEqual(other.Children[i]) {
return false
}
}
return true
}