Skip to content

Commit

Permalink
split Element.ParseParams
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Apr 10, 2024
1 parent c145662 commit 28f23b1
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 44 deletions.
39 changes: 7 additions & 32 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,38 +193,13 @@ func (e *Element) Remove(htmlId string) {
e.queue(what.Remove, htmlId)
}

// ParseParams parses the parameters passed to UI() when creating a new Element,
// setting event handlers and returning a list of HTML attributes.
func (e *Element) ParseParams(params []any) (attrs []template.HTMLAttr) {
// ApplyParams parses the parameters passed to UI() when creating a new Element,
// adding UI tags, setting event handlers and returning a list of HTML attributes.
func (e *Element) ApplyParams(params []any) []template.HTMLAttr {
tags, handlers, attrs := ParseParams(params)
if !e.deleted {
for i := range params {
switch data := params[i].(type) {
case template.HTMLAttr:
attrs = append(attrs, data)
case []template.HTMLAttr:
attrs = append(attrs, data...)
case string:
attr := template.HTMLAttr(data) // #nosec G203
attrs = append(attrs, attr)
case []string:
for _, s := range data {
attr := template.HTMLAttr(s) // #nosec G203
attrs = append(attrs, attr)
}
case EventFn:
if data != nil {
e.handlers = append(e.handlers, eventFnWrapper{data})
}
default:
if h, ok := data.(ClickHandler); ok {
e.handlers = append(e.handlers, clickHandlerWapper{h})
}
if h, ok := data.(EventHandler); ok {
e.handlers = append(e.handlers, h)
}
e.Tag(data)
}
}
e.handlers = append(e.handlers, handlers...)
e.Tag(tags...)
}
return
return attrs
}
37 changes: 37 additions & 0 deletions parseparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jaws

import "html/template"

// ParseParams parses the parameters passed to UI() when creating a new Element,
// returning UI tags, event handlers and HTML attributes.
func ParseParams(params []any) (tags []any, handlers []EventHandler, attrs []template.HTMLAttr) {
for i := range params {
switch data := params[i].(type) {
case template.HTMLAttr:
attrs = append(attrs, data)
case []template.HTMLAttr:
attrs = append(attrs, data...)
case string:
attr := template.HTMLAttr(data) // #nosec G203
attrs = append(attrs, attr)
case []string:
for _, s := range data {
attr := template.HTMLAttr(s) // #nosec G203
attrs = append(attrs, attr)
}
case EventFn:
if data != nil {
handlers = append(handlers, eventFnWrapper{data})
}
default:
if h, ok := data.(ClickHandler); ok {
handlers = append(handlers, clickHandlerWapper{h})
}
if h, ok := data.(EventHandler); ok {
handlers = append(handlers, h)
}
tags = append(tags, data)
}
}
return
}
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (rq *Request) Register(tagitem any, params ...any) jid.Jid {
case jid.Jid:
if elem := rq.getElementByJid(data); elem != nil {
if _, ok := elem.Ui().(*UiHtml); ok {
elem.ParseParams(params)
elem.ApplyParams(params)
}
}
return data
Expand All @@ -300,7 +300,7 @@ func (rq *Request) Register(tagitem any, params ...any) jid.Jid {
uib := &UiHtml{}
elem := rq.NewElement(uib)
uib.parseGetter(elem, tagitem)
elem.ParseParams(params)
elem.ApplyParams(params)
return elem.Jid()
}

Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (t Template) JawsRender(e *Element, w io.Writer, params []any) error {
e.Request.tagExpanded(e, expandedtags)
}
var sb strings.Builder
for _, s := range e.ParseParams(params) {
for _, s := range e.ApplyParams(params) {
sb.WriteByte(' ')
sb.WriteString(string(s))
}
Expand Down
2 changes: 1 addition & 1 deletion uihtmlinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UiHtmlInner struct {

func (ui *UiHtmlInner) renderInner(e *Element, w io.Writer, htmltag, htmltype string, params []any) error {
ui.parseGetter(e, ui.HtmlGetter)
return WriteHtmlInner(w, e.Jid(), htmltag, htmltype, ui.JawsGetHtml(e), e.ParseParams(params)...)
return WriteHtmlInner(w, e.Jid(), htmltag, htmltype, ui.JawsGetHtml(e), e.ApplyParams(params)...)
}

func (ui *UiHtmlInner) JawsUpdate(e *Element) {
Expand Down
2 changes: 1 addition & 1 deletion uiimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UiImg struct {
func (ui *UiImg) JawsRender(e *Element, w io.Writer, params []any) error {
ui.parseGetter(e, ui.StringGetter)
srcattr := template.HTMLAttr("src=" + strconv.Quote(ui.JawsGetString(e))) // #nosec G203
attrs := append(e.ParseParams(params), srcattr)
attrs := append(e.ApplyParams(params), srcattr)
return WriteHtmlInner(w, e.Jid(), "img", "", "", attrs...)
}

Expand Down
2 changes: 1 addition & 1 deletion uiinputbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UiInputBool struct {

func (ui *UiInputBool) renderBoolInput(e *Element, w io.Writer, htmltype string, params ...any) error {
ui.parseGetter(e, ui.BoolSetter)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
v := ui.JawsGetBool(e)
ui.Last.Store(v)
if v {
Expand Down
2 changes: 1 addition & 1 deletion uiinputdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ui *UiInputDate) str() string {

func (ui *UiInputDate) renderDateInput(e *Element, w io.Writer, htmltype string, params ...any) error {
ui.parseGetter(e, ui.TimeSetter)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
ui.Last.Store(ui.JawsGetTime(e))
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs)
}
Expand Down
2 changes: 1 addition & 1 deletion uiinputfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ui *UiInputFloat) str() string {

func (ui *UiInputFloat) renderFloatInput(e *Element, w io.Writer, htmltype string, params ...any) error {
ui.parseGetter(e, ui.FloatSetter)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
ui.Last.Store(ui.JawsGetFloat(e))
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs)
}
Expand Down
2 changes: 1 addition & 1 deletion uiinputtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UiInputText struct {

func (ui *UiInputText) renderStringInput(e *Element, w io.Writer, htmltype string, params ...any) error {
ui.parseGetter(e, ui.StringSetter)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
v := ui.JawsGetString(e)
ui.Last.Store(v)
return WriteHtmlInput(w, e.Jid(), htmltype, v, attrs)
Expand Down
2 changes: 1 addition & 1 deletion uioption.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type UiOption struct{ *NamedBool }

func (ui UiOption) JawsRender(e *Element, w io.Writer, params []any) error {
e.Tag(ui.NamedBool)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
valattr := template.HTMLAttr(`value="` + html.EscapeString(ui.JawsGetString(e)) + `"`) // #nosec G203
attrs = append(attrs, valattr)
if ui.Checked() {
Expand Down
3 changes: 3 additions & 0 deletions uitemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func NewUiTemplate(t Template) UiTemplate {
//
// The templ argument can either be a string, in which case Jaws.Template.Lookup() will
// be used to resolve it. Or it can be a *template.Template directly.
//
// Note that templates are only rendered once; adding UI tags to a template
// have no effect, since JawsUpdate is a no-op for them.
func (rq RequestWriter) Template(templ, dot any, params ...any) error {
return rq.UI(NewUiTemplate(rq.rq.MakeTemplate(templ, dot)), params...)
}
2 changes: 1 addition & 1 deletion uitextarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UiTextarea struct {

func (ui *UiTextarea) JawsRender(e *Element, w io.Writer, params []any) error {
ui.parseGetter(e, ui.StringSetter)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
return WriteHtmlInner(w, e.Jid(), "textarea", "", template.HTML(ui.JawsGetString(e)), attrs...) // #nosec G203
}

Expand Down
2 changes: 1 addition & 1 deletion uiwrapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type uiWrapContainer struct {

func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag string, params []any) (err error) {
ui.parseGetter(e, ui.Container)
attrs := e.ParseParams(params)
attrs := e.ApplyParams(params)
b := e.Jid().AppendStartTagAttr(nil, outerhtmltag)
for _, attr := range attrs {
b = append(b, ' ')
Expand Down

0 comments on commit 28f23b1

Please sign in to comment.