-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
521 lines (453 loc) · 11.7 KB
/
object.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package jse
import (
"strings"
"syscall/js"
"github.com/Nigel2392/jsext/v2"
"github.com/Nigel2392/jsext/v2/errs"
)
// AUTO_KEY is a function that returns a random string.
//
// This can be used to manage state more easily, essentially providing each element with a unique id.
var AUTO_KEY func(n int) string // = jsrand.String
type Element jsext.Element
// Create a new Element.
func NewElement(tag string, text ...string) *Element {
var e = jsext.CreateElement(tag)
if len(text) > 0 {
e.InnerHTML(strings.Join(text, "\n"))
}
var elem = (*Element)(&e)
if AUTO_KEY != nil {
elem.SetKey(AUTO_KEY(16))
}
return elem
}
type JavascriptConstraint interface {
jsext.Element | jsext.Value | SVG | FormElement | SelectElement | Element | *Element | *SVG | *FormElement | *SelectElement
}
func Make[T JavascriptConstraint](e T) *Element {
switch e := any(e).(type) {
case jsext.Element:
return (*Element)(&e)
case jsext.Value:
return (*Element)(&e)
case SVG:
return (*Element)(&e)
case FormElement:
return (*Element)(&e)
case SelectElement:
return (*Element)(&e)
case Element:
return &e
case *Element:
return e
case *SVG:
return (*Element)(e)
case *FormElement:
return (*Element)(e)
case *SelectElement:
return (*Element)(e)
}
panic("unreachable")
}
func (e *Element) Element() jsext.Element {
if e == nil {
return jsext.Element(jsext.Null())
}
return (jsext.Element)(*e)
}
func (e *Element) Value() jsext.Value {
if e == nil {
return jsext.Null()
}
return (jsext.Value)(*e)
}
func (e *Element) JSValue() js.Value {
if e == nil {
return js.Null()
}
return (js.Value)(*e)
}
func (e *Element) MarshalJS() js.Value {
return e.JSValue()
}
func (e *Element) Key() string {
var (
s string
key jsext.Value
)
if e == nil {
return s
}
key = e.Dataset().Get("key")
if !key.IsZero() && key.String() != "" {
return key.String()
}
key = e.Call("getAttribute", "key")
if !key.IsZero() && key.String() != "" {
return key.String()
}
key = e.Call("getAttribute", "id")
if !key.IsZero() && key.String() != "" {
return key.String()
}
key = e.Call("getAttribute", "name")
if !key.IsZero() && key.String() != "" {
return key.String()
}
return key.String()
}
func (e *Element) SetKey(key string) *Element {
if e == nil {
return e
}
e.Dataset().Set("key", key)
return e
}
type (
Value string
InnerHTML string
InnerText string
AddClasses []string
RemoveClasses []string
)
func (e *Element) EditState(value interface{}) error {
if e == nil {
return errs.Error("element is nil")
}
if value == nil {
return nil
}
switch value := value.(type) {
case string:
e.InnerHTML(value)
case Value:
e.Set("value", string(value))
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
e.Set("value", value)
case InnerHTML:
e.InnerHTML(string(value))
case InnerText:
e.InnerText(string(value))
case *Element:
e.Replace(value)
case AddClasses:
var classList = e.ClassList()
for _, class := range value {
classList.Add(class)
}
case RemoveClasses:
var classList = e.ClassList()
for _, class := range value {
classList.Remove(class)
}
default:
return errs.Error("unknown type")
}
return nil
}
func (e *Element) Replace(elem *Element) {
if elem == nil || e == nil ||
elem.IsZero() || e.IsZero() {
return
}
e.Call("replaceWith", elem.JSValue())
*e = *elem
}
func (e *Element) AppendChild(children ...*Element) *Element {
for _, child := range children {
if child.IsZero() {
continue
}
e.Call("appendChild", child.JSValue())
}
return e
}
func (e *Element) PrependChild(children ...*Element) *Element {
var firstChild = e.JSValue().Get("firstChild")
for _, child := range children {
if child.IsZero() {
continue
}
if firstChild.IsNull() || firstChild.IsUndefined() {
firstChild = child.JSValue()
e.Call("appendChild", firstChild)
continue
}
e.Call("insertBefore", child.JSValue(), firstChild)
firstChild = child.JSValue()
}
return e
}
func (e *Element) NewElement(typ string, innerText ...string) *Element {
var elem = NewElement(typ, innerText...)
e.AppendChild(elem)
return elem
}
func (e *Element) ClearInnerHTML() {
e.JSValue().Set("innerHTML", "")
}
func (e *Element) InnerHTML(s string) *Element {
e.Element().InnerHTML(s)
return e
}
func (e *Element) InnerText(s string) *Element {
e.Element().InnerText(s)
return e
}
func (e *Element) InlineClasses(classes ...string) *Element {
e.Element().ClassList(classes...)
return e
}
func (e *Element) ClassList(s ...string) jsext.Value {
return e.Element().ClassList(s...)
}
func (e *Element) Style() jsext.Style {
return e.Element().Style()
}
func (e *Element) SetAttrMap(m map[string]string) *Element {
for k, v := range m {
e.SetAttr(k, v)
}
return e
}
func (e *Element) SetAttr(p string, s ...string) *Element {
e.Value().Call("setAttribute", p, strings.Join(s, " "))
return e
}
func (e *Element) GetAttr(p string) string {
return e.Value().Call("getAttribute", p).String()
}
func (e *Element) DelAttr(p string) *Element {
e.Value().Call("removeAttribute", p)
return e
}
// RemoveChild removes a child from the Element
func (e *Element) removeChild(child *Element) *Element {
if child == nil {
return e
}
e.JSValue().Call("removeChild", child.JSValue())
return e
}
// RemoveChildren removes multiple children from the Element
func (e *Element) RemoveChildren(children ...*Element) *Element {
for _, child := range children {
e.removeChild(child)
}
return e
}
// Get the parentElement
func (e *Element) ParentElement() *Element {
var p = e.JSValue().Get("parentElement")
return (*Element)(&p)
}
// Get an inner element by ID.
func (e *Element) GetElementById(id string) *Element {
var elem = e.JSValue().Call("getElementById", id)
return (*Element)(&elem)
}
// Get an inner element by class name.
func (e *Element) GetElementsByClassName(className string) *Element {
var elem = e.Call("getElementsByClassName", className)
return (*Element)(&elem)
}
// Get an inner element by tag name.
func (e *Element) GetElementsByTagName(tagName string) *Element {
var elem = e.Call("getElementsByTagName", tagName)
return (*Element)(&elem)
}
// Add an event listener to the Element
//
// This will return the function that was added to the element.
func (e *Element) AddEventListener(event string, callback func(this *Element, event jsext.Event)) js.Func {
if e == nil {
return js.Func{Value: js.Null()}
}
if callback == nil {
return js.Func{Value: js.Null()}
}
var f = js.FuncOf(func(_ js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return nil
}
callback(e, jsext.Event(args[0]))
return nil
})
e.JSValue().Call("addEventListener", event, f)
return f
}
// Get the scroll height of the Element
func (e *Element) ScrollHeight() int {
return e.JSValue().Get("scrollHeight").Int()
}
// Get the scroll width of the Element
func (e *Element) ScrollWidth() int {
return e.JSValue().Get("scrollWidth").Int()
}
// Get the scroll top of the Element
func (e *Element) ScrollTop() int {
return e.JSValue().Get("scrollTop").Int()
}
// Get the scroll left of the Element
func (e *Element) ScrollLeft() int {
return e.JSValue().Get("scrollLeft").Int()
}
// Set the sccrollTo of the Element
func (e *Element) ScrollTo(x, y int) {
e.JSValue().Call("scrollTo", x, y)
}
// Scroll the *Element into view.
func (e *Element) ScrollIntoView(center bool) {
e.JSValue().Call("scrollIntoView", center)
}
// Scroll the *Element into view if needed.
func (e *Element) ScrollIntoViewIfNeeded(center bool) {
e.JSValue().Call("scrollIntoViewIfNeeded", center)
}
// Get the clientWidth of the Element
func (e *Element) Width() int {
return e.JSValue().Get("clientWidth").Int()
}
// Get the clientHeight of the Element
func (e *Element) Height() int {
return e.JSValue().Get("clientHeight").Int()
}
// Get the children of the Element
func (e *Element) Children() []*Element {
var children = e.JSValue().Get("children")
var length = children.Length()
var elems = make([]*Element, length)
for i := 0; i < length; i++ {
var child = children.Index(i)
elems[i] = (*Element)(&child)
}
return elems
}
// Get the clientHeight of the Element
func (e *Element) ClientHeight() int {
return e.JSValue().Get("clientHeight").Int()
}
// Get the clientLeft of the Element
func (e *Element) ClientLeft() int {
return e.JSValue().Get("clientLeft").Int()
}
// Get the clientTop of the Element
func (e *Element) ClientTop() int {
return e.JSValue().Get("clientTop").Int()
}
// Get the clientWidth of the Element
func (e *Element) ClientWidth() int {
return e.JSValue().Get("clientWidth").Int()
}
// Get the *Element's dataset.
func (e *Element) Dataset() jsext.Value {
return jsext.Value(e.JSValue().Get("dataset"))
}
// Return the *Element's dataset as map
func (e *Element) MapDataset() map[string]string {
return e.Element().MapDataset()
}
// Get the *Element's first child.
func (e *Element) FirstElementChild() Element {
return Element(e.JSValue().Get("firstElementChild"))
}
// Insert the *Element before the before Element
func (e *Element) InsertBefore(element, before *Element) {
e.JSValue().Call("insertBefore", element.JSValue(), before.JSValue())
}
// Replace the *Element with the before Element
func (e *Element) ReplaceChild(element, before *Element) {
if element == nil || before == nil {
return
}
if element.IsZero() || before.IsZero() {
return
}
e.JSValue().Call("replaceChild", element.JSValue(), before.JSValue())
}
// Remove the Element
func (e *Element) Remove() {
e.JSValue().Call("remove")
}
// Animate the Element
func (e *Element) Animate(keyframes []interface{}, options map[string]interface{}) jsext.Value {
return jsext.Value(e.JSValue().Call("animate", jsext.SliceToArray(keyframes).Value(), jsext.MapToObject(options).Value()))
}
// CallFunc is used for state management.
func (e *Element) CallFunc(name string, args ...interface{}) {
e.Call(name, args...)
}
// NodeName returns the node name of the Element
func (e *Element) NodeName() string {
return e.JSValue().Get("nodeName").String()
}
// /////////////////////////////////////////////////////////
//
// js.Value methods.
//
// /////////////////////////////////////////////////////////
func (e *Element) Bool() bool {
return e.JSValue().Bool()
}
func (e *Element) Call(m string, args ...any) jsext.Value {
args = jsext.MarshallableArguments(args...)
return jsext.Value(e.JSValue().Call(m, args...))
}
func (e *Element) Delete(p string) {
e.JSValue().Delete(p)
}
func (e *Element) Equal(other js.Value) bool {
return e.JSValue().Equal(other)
}
func (e *Element) Float() float64 {
return e.JSValue().Float()
}
func (e *Element) Get(p string) jsext.Value {
return jsext.Value(e.JSValue().Get(p))
}
func (e *Element) Index(i int) jsext.Value {
return jsext.Value(e.JSValue().Index(i))
}
func (e *Element) Int() int {
return e.JSValue().Int()
}
func (e *Element) Invoke(args ...any) jsext.Value {
args = jsext.MarshallableArguments(args...)
return jsext.Value(e.JSValue().Invoke(args...))
}
func (e *Element) IsNaN() bool {
return e.JSValue().IsNaN()
}
func (e *Element) IsNull() bool {
return e.JSValue().IsNull()
}
func (e *Element) IsUndefined() bool {
return e.JSValue().IsUndefined()
}
func (e *Element) Length() int {
return e.JSValue().Length()
}
func (e *Element) Set(p string, x any) {
x = jsext.MarshallableArguments(x)[0]
e.JSValue().Set(p, x)
}
func (e *Element) SetIndex(i int, x any) {
x = jsext.MarshallableArguments(x)[0]
e.JSValue().SetIndex(i, x)
}
func (e *Element) String() string {
return e.JSValue().String()
}
func (e *Element) Truthy() bool {
return e.JSValue().Truthy()
}
func (e *Element) Type() js.Type {
return e.JSValue().Type()
}
func (e *Element) IsZero() bool {
if e == nil {
return true
}
return e.JSValue().IsNull() || e.JSValue().IsUndefined()
}