-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCheckbox.go
More file actions
231 lines (146 loc) · 4.16 KB
/
Checkbox.go
File metadata and controls
231 lines (146 loc) · 4.16 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//go:build wasm
package ui
import "github.com/cookiengineer/gooey/bindings/dom"
import "github.com/cookiengineer/gooey/components"
import "github.com/cookiengineer/gooey/components/utils"
import "github.com/cookiengineer/gooey/components/interfaces"
import "github.com/cookiengineer/gooey/components/types"
import "strings"
import "syscall/js"
type Checkbox struct {
Label string `json:"label"`
Type types.Input `json:"type"`
Value bool `json:"value"`
Disabled bool `json:"disabled"`
Component *components.Component `json:"component"`
}
func NewCheckbox(label string, value string) Checkbox {
var checkbox Checkbox
element := dom.GetDocument().CreateElement("input")
component := components.NewComponent(element)
element.SetAttribute("type", "checkbox")
checkbox.Component = &component
checkbox.Label = label
checkbox.Type = types.InputCheckbox
checkbox.Value = false
checkbox.Disabled = false
return checkbox
}
func ToCheckbox(element *dom.Element) *Checkbox {
var checkbox Checkbox
component := components.NewComponent(element)
checkbox.Component = &component
checkbox.Label = strings.TrimSpace(element.GetAttribute("title"))
checkbox.Type = types.InputCheckbox
checkbox.Value = element.Value.Get("checked").Bool()
checkbox.Disabled = element.HasAttribute("disabled")
return &checkbox
}
func (checkbox *Checkbox) Disable() bool {
checkbox.Disabled = true
checkbox.Render()
return true
}
func (checkbox *Checkbox) Enable() bool {
checkbox.Disabled = false
checkbox.Render()
return true
}
func (checkbox *Checkbox) Mount() bool {
if checkbox.Component != nil {
checkbox.Component.InitEvent("change-value")
}
if checkbox.Component.Element != nil {
checkbox.Component.Element.AddEventListener("change", dom.ToEventListener(func(_ *dom.Event) {
checkbox.Value = checkbox.Component.Element.Value.Get("checked").Bool()
checked := "false"
if checkbox.Value == true {
checked = "true"
}
checkbox.Component.FireEventListeners("change-value", map[string]any{
"value": checked,
})
}))
return true
} else {
return false
}
}
func (checkbox *Checkbox) Query(query string) interfaces.Component {
selectors := utils.SplitQuery(query)
if len(selectors) == 1 {
if checkbox.Component.Element != nil {
if utils.MatchesQuery(checkbox.Component.Element, selectors[0]) == true {
return checkbox
}
}
}
return nil
}
func (checkbox *Checkbox) Render() *dom.Element {
if checkbox.Component.Element != nil {
if checkbox.Label != "" {
checkbox.Component.Element.SetAttribute("title", checkbox.Label)
} else {
checkbox.Component.Element.RemoveAttribute("title")
}
if checkbox.Value == true {
checkbox.Component.Element.Value.Set("checked", true)
} else {
checkbox.Component.Element.Value.Set("checked", false)
}
if checkbox.Disabled == true {
checkbox.Component.Element.SetAttribute("disabled", "")
} else {
checkbox.Component.Element.RemoveAttribute("disabled")
}
}
return checkbox.Component.Element
}
func (checkbox *Checkbox) Reset() bool {
checkbox.Value = false
checkbox.Render()
return true
}
func (checkbox *Checkbox) SetLabel(label string) {
checkbox.Label = strings.TrimSpace(label)
checkbox.Render()
}
func (checkbox *Checkbox) SetValue(value bool) {
checkbox.Value = value
checkbox.Render()
}
func (checkbox *Checkbox) String() string {
html := "<input type=\"checkbox\""
if checkbox.Label != "" {
html += " title=\"" + checkbox.Label + "\""
}
if checkbox.Value == true {
html += " checked"
}
if checkbox.Disabled == true {
html += " disabled"
}
html += "/>"
return html
}
func (checkbox *Checkbox) ToValue() js.Value {
var result js.Value
if checkbox.Component.Element != nil {
tmp := checkbox.Component.Element.Value.Get("checked")
if !tmp.IsNull() && !tmp.IsUndefined() {
if tmp.Bool() == true {
result = js.ValueOf(true)
} else {
result = js.ValueOf(false)
}
}
}
return result
}
func (checkbox *Checkbox) Unmount() bool {
if checkbox.Component.Element != nil {
checkbox.Component.Element.RemoveEventListener("change", nil)
}
return true
}