-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
473 lines (351 loc) · 12.2 KB
/
element.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//go:build js && wasm
package js
import (
"fmt"
"github.com/dairaga/js/v2/builtin"
)
type HTML string
func (h HTML) JSValue() Value {
return ValueOf(string(h))
}
// -----------------------------------------------------------------------------
type Plain string
func (p Plain) JSValue() Value {
return ValueOf(string(p))
}
// -----------------------------------------------------------------------------
func (p Plain) Ref() Value {
return p.JSValue()
}
// -----------------------------------------------------------------------------
type HandlerFunc func(Element, Event)
// -----------------------------------------------------------------------------
// Element represents a DOM element, usually is a HTML element.
type Element interface {
Appendable
Parent() Element
Tattoo() string
Enable(enable bool) Element
Query(selector string) Element
QueryAll(selector string) Elements
Append(child Appendable, at ...string) Element
Prepend(child Appendable, at ...string) Element
Prop(p string, at ...string) Value
SetProp(p string, val any, at ...string) Element
Attr(a string, at ...string) string
SetAttr(a, value string, at ...string) Element
RemoveAttr(a string, at ...string) Element
Text() string
SetText(txt string) Element
HTML() HTML
SetHTML(html HTML) Element
Value() string
SetValue(val string) Element
Var(val *string) Element
Files() []File
Add(clz string, at ...string) Element
Remove(clz string, at ...string) Element
Has(clz string, at ...string) bool
Replace(oldClz, newClz string, at ...string) Element
Toggle(clz string, at ...string) Element
On(typ string, fn HandlerFunc, at ...string) Element
OnClick(fn HandlerFunc, at ...string) Element
OnChange(fn HandlerFunc, at ...string) Element
Click(at ...string) Element
Foucs(at ...string) Element
Blur(at ...string) Element
Style() Style
OuterHeight(includeMargin bool) int
SetOuterHeight(h int, includeMargin bool) Element
OuterWidth(includeMargin bool) int
Empty() Element
//Relese()
Dispose()
}
// -----------------------------------------------------------------------------
type element Value
var _ Appendable = element{}
// -----------------------------------------------------------------------------
func (e element) JSValue() Value {
return Value(e)
}
// -----------------------------------------------------------------------------
func (e element) Ref() Value {
return e.JSValue()
}
// -----------------------------------------------------------------------------
func (e element) tattoo() element {
val := Value(e).Call("getAttribute", _tattoo)
if val.Truthy() {
return e
}
Value(e).Call("setAttribute", _tattoo, tattoo(10))
return e
}
// -----------------------------------------------------------------------------
func (e element) String() string {
return fmt.Sprintf(`{"tag": %q, "tattoo": %q, "id": %q, "class": %q}`, Value(e).Get("tagName").String(), e.Tattoo(), e.Attr("id"), e.Prop("classList").Get("value").String())
}
// -----------------------------------------------------------------------------
func (e element) Parent() Element {
p := e.Prop("parentElement")
if p.Truthy() && builtin.Element.Is(p) {
return elementOf(p)
}
return nil
}
// -----------------------------------------------------------------------------
func (e element) Tattoo() string {
return e.Attr(_tattoo)
}
// -----------------------------------------------------------------------------
func (e element) Enable(enable bool) Element {
if enable {
e.RemoveAttr("disabled")
} else {
e.SetAttr("disabled", "disabled")
}
return e
}
// -----------------------------------------------------------------------------
func (e element) Query(selector string) Element {
return elementOf(query(Value(e), selector))
}
// -----------------------------------------------------------------------------
func (e element) QueryAll(selector string) Elements {
return ElementsOf(queryAll(Value(e), selector))
}
// -----------------------------------------------------------------------------
func (e element) Append(child Appendable, selector ...string) Element {
//at(Value(e), selector...).Call("append", child.Ref())
appendNode(at(Value(e), selector...), child)
return e
}
// -----------------------------------------------------------------------------
func (e element) Prepend(child Appendable, selector ...string) Element {
//at(Value(e), selector...).Call("prepend", child.Ref())
prependNode(at(Value(e), selector...), child)
return e
}
// -----------------------------------------------------------------------------
func (e element) Prop(p string, selector ...string) Value {
return at(Value(e), selector...).Get(p)
}
// -----------------------------------------------------------------------------
func (e element) SetProp(p string, val any, selector ...string) Element {
at(Value(e), selector...).Set(p, val)
return e
}
// -----------------------------------------------------------------------------
func (e element) Attr(a string, selector ...string) string {
return attr(at(Value(e), selector...), a)
}
// -----------------------------------------------------------------------------
func (e element) SetAttr(a, val string, selector ...string) Element {
if _tattoo != a {
setAttr(at(Value(e), selector...), a, val)
}
return e
}
// -----------------------------------------------------------------------------
func (e element) RemoveAttr(a string, selector ...string) Element {
if a != _tattoo {
removeAttr(at(Value(e), selector...), a)
}
return e
}
// -----------------------------------------------------------------------------
func (e element) Text() string {
return Value(e).Get("innerText").String()
}
// -----------------------------------------------------------------------------
func (e element) SetText(txt string) Element {
Value(e).Set("innerText", txt)
return e
}
// -----------------------------------------------------------------------------
func (e element) HTML() HTML {
return HTML(Value(e).Get("innerHTML").String())
}
// -----------------------------------------------------------------------------
func (e element) SetHTML(html HTML) Element {
Value(e).Set("innerHTML", html.JSValue())
return e
}
// -----------------------------------------------------------------------------
func (e element) Value() string {
if builtin.HasValueProperty(Value(e)) {
return Value(e).Get("value").String()
}
return ""
}
// -----------------------------------------------------------------------------
func (e element) SetValue(val string) Element {
if builtin.HasValueProperty(Value(e)) {
Value(e).Set("value", val)
}
return e
}
// -----------------------------------------------------------------------------
func (e element) Files() []File {
if builtin.HTMLInputElement.Is(Value(e)) && e.Attr("type") == "file" {
lst := Value(e).Get("files")
size := lst.Length()
ret := make([]File, size)
for i := 0; i < size; i++ {
ret[i] = FileOf(lst.Index(i))
}
}
return []File{}
}
// -----------------------------------------------------------------------------
func (e element) Add(clz string, selector ...string) Element {
addClz(at(Value(e), selector...), clz)
//e.Prop("classList", at...).Call("add", clz)
return e
}
// -----------------------------------------------------------------------------
func (e element) Remove(clz string, selector ...string) Element {
//e.Prop("classList", at...).Call("remove", clz)
removeClz(at(Value(e), selector...), clz)
return e
}
// -----------------------------------------------------------------------------
func (e element) Has(clz string, selector ...string) bool {
//return e.Prop("classList", at...).Call("contains", clz).Bool()
return hasClz(at(Value(e), selector...), clz)
}
// -----------------------------------------------------------------------------
func (e element) Replace(old, new string, selector ...string) Element {
//e.Prop("classList", at...).Call("replace", oldClz, newClz)
replaceClz(at(Value(e), selector...), old, new)
return e
}
// -----------------------------------------------------------------------------
func (e element) Toggle(clz string, selector ...string) Element {
//e.Prop("classList", at...).Call("toggle", clz)
toggleClz(at(Value(e), selector...), clz)
return e
}
// -----------------------------------------------------------------------------
func (e element) On(typ string, fn HandlerFunc, selector ...string) Element {
cb := FuncOf(func(_this Value, args []Value) any {
evt := event(args[0])
//elm := elementOf(evt.Get("target"))
elm := elementOf(_this)
fn(elm, evt)
return nil
})
at(Value(e), selector...).Call("addEventListener", typ, cb)
return e
}
// -----------------------------------------------------------------------------
func (e element) OnClick(fn HandlerFunc, at ...string) Element {
return e.On("click", fn, at...)
}
// -----------------------------------------------------------------------------
func (e element) OnChange(fn HandlerFunc, at ...string) Element {
return e.On("change", fn, at...)
}
// -----------------------------------------------------------------------------
func (e element) Click(selector ...string) Element {
at(Value(e), selector...).Call("click")
return e
}
// -----------------------------------------------------------------------------
func (e element) Foucs(selector ...string) Element {
at(Value(e), selector...).Call("focus")
return e
}
// -----------------------------------------------------------------------------
func (e element) Blur(selector ...string) Element {
at(Value(e), selector...).Call("blur")
return e
}
// -----------------------------------------------------------------------------
func (e element) Style() Style {
//return Style(global.Call("getComputedStyle", Value(e)))
return Style(Value(e).Get("style"))
}
// -----------------------------------------------------------------------------
func (e element) OuterHeight(includeMargin bool) int {
if includeMargin {
s := e.Style()
mt, _ := ParseInt(s.GetPropertyValue("margin-top"), 10)
mb, _ := ParseInt(s.GetPropertyValue("margin-bottom"), 10)
return Value(e).Get("offsetHeight").Int() + mt + mb
}
return Value(e).Get("offsetHeight").Int()
}
// -----------------------------------------------------------------------------
func (e element) SetOuterHeight(h int, includeMargin bool) Element {
s := e.Style()
if includeMargin {
mt, _ := ParseInt(s.GetPropertyValue("margin-top"), 10)
mb, _ := ParseInt(s.GetPropertyValue("margin-bottom"), 10)
h -= mt + mb
}
s.SetProperty("height", fmt.Sprintf("%dpx", h))
return e
}
// -----------------------------------------------------------------------------
func (e element) OuterWidth(includeMargin bool) int {
if includeMargin {
s := e.Style()
ml, _ := ParseInt(s.GetPropertyValue("margin-left"), 10)
mr, _ := ParseInt(s.GetPropertyValue("margin-right"), 10)
return Value(e).Get("offsetWidth").Int() + ml + mr
}
return Value(e).Get("offsetWidth").Int()
}
// -----------------------------------------------------------------------------
func (e element) Empty() Element {
v := Value(e)
for v.Get("firstChild").Truthy() {
v.Get("firstChild").Call("remove")
}
return e
}
// -----------------------------------------------------------------------------
func (e element) Dispose() {
Value(e).Call("remove")
}
// -----------------------------------------------------------------------------
func (e element) Var(val *string) Element {
if e.Attr("type") == "checkbox" && !e.Prop("checked").Bool() {
*val = ""
} else {
*val = e.Value()
}
return e
}
// -----------------------------------------------------------------------------
func elementOf(v Value) element {
if builtin.Element.Is(v) {
return element(v).tattoo()
}
panic(fmt.Sprintf("%s is not an Element", v.Type().String()))
}
// -----------------------------------------------------------------------------
func ElementOf(x any) Element {
switch v := x.(type) {
case Element:
return v
case HTML:
tmpl := createElement("template")
tmpl.Set("innerHTML", v.JSValue())
return elementOf(fragment(tmpl.Get("content")))
case string:
return Query(v)
case Wrapper:
return elementOf(v.JSValue())
case Value:
return elementOf(v)
}
panic(fmt.Sprintf("unsupport type %T", x))
}
// -----------------------------------------------------------------------------
func Var(elm any, val *string) {
e := ElementOf(elm)
e.Var(val)
}