-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsfunc.go
73 lines (64 loc) · 1.51 KB
/
jsfunc.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
package jaws
import (
"io"
"strconv"
"github.com/linkdata/jaws/what"
)
type JsFunc struct {
Arg IsJsVar
Retv IsJsVar
}
func (ui JsFunc) JawsRender(e *Element, w io.Writer, params []any) (err error) {
e.ApplyGetter(ui.Arg)
jsvarname := params[0].(string)
attrs := e.ApplyParams(params[1:])
var b []byte
b = append(b, `<div id=`...)
b = e.Jid().AppendQuote(b)
b = append(b, ` data-jawsname=`...)
b = strconv.AppendQuote(b, jsvarname)
b = appendAttrs(b, attrs)
b = append(b, ` hidden></div>`...)
_, err = w.Write(b)
return
}
func (ui JsFunc) JawsUpdate(e *Element) {
e.JsCall(string(ui.Arg.AppendJSON(nil, e)))
}
func (ui JsFunc) JawsEvent(e *Element, wht what.What, val string) (err error) {
err = ErrEventUnhandled
if ui.Retv != nil {
err = ui.Retv.JawsEvent(e, wht, val)
}
return
}
func NewJsFunc(arg IsJsVar, retv IsJsVar) JsFunc {
return JsFunc{
Arg: arg,
Retv: retv,
}
}
func (rq RequestWriter) JsFunc(jsfuncname string, getter any, params ...any) (err error) {
var arg IsJsVar
var retv IsJsVar
var newparams []any
if arg, err = makeJsVar(rq.Request(), getter); err == nil {
newparams = append(newparams, jsfuncname)
for _, param := range params {
if err == nil {
if vm, ok := param.(VarMaker); ok {
if param, err = vm.JawsVarMake(rq.Request()); err != nil {
return
}
}
if jsvar, ok := param.(IsJsVar); ok {
retv = jsvar
} else {
newparams = append(newparams, param)
}
}
}
err = rq.UI(NewJsFunc(arg, retv), newparams...)
}
return
}