diff --git a/element.go b/element.go index fbebcc5..5dec1aa 100644 --- a/element.go +++ b/element.go @@ -154,3 +154,35 @@ func (e *Element) Order(jidList []jid.Jid) { 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 []interface{}) (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: + attrs = append(attrs, template.HTMLAttr(data)) + case []string: + for _, s := range data { + attrs = append(attrs, template.HTMLAttr(s)) + } + 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) + } + } + return +} diff --git a/html.go b/html.go index 2f8d1de..baec402 100644 --- a/html.go +++ b/html.go @@ -34,7 +34,7 @@ func needClosingTag(tag string) bool { return !ok } -func getAttrsLen(attrs []string) (attrslen int) { +func getAttrsLen(attrs []template.HTMLAttr) (attrslen int) { for _, s := range attrs { if s != "" { attrslen += 1 + len(s) @@ -43,7 +43,7 @@ func getAttrsLen(attrs []string) (attrslen int) { return } -func appendAttrs(b []byte, attrs []string) []byte { +func appendAttrs(b []byte, attrs []template.HTMLAttr) []byte { for _, s := range attrs { if s != "" { b = append(b, ' ') @@ -53,7 +53,7 @@ func appendAttrs(b []byte, attrs []string) []byte { return b } -func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...string) (err error) { +func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...template.HTMLAttr) (err error) { need := 11 + jidPrealloc + 8 + len(typ) + 9 + len(val) + 1 + 1 + getAttrsLen(attrs) + 1 b := make([]byte, 0, need) b = jid.AppendStartTagAttr(b, "input") @@ -69,7 +69,7 @@ func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...string) return } -func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HTML, attrs ...string) (err error) { +func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HTML, attrs ...template.HTMLAttr) (err error) { need := 1 + len(tag)*2 + jidPrealloc + 8 + len(typ) + 1 + 1 + getAttrsLen(attrs) + 1 + len(inner) + 2 + 1 b := make([]byte, 0, need) b = jid.AppendStartTagAttr(b, tag) @@ -89,7 +89,7 @@ func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HT return } -func WriteHtmlSelect(w io.Writer, jid jid.Jid, nba *NamedBoolArray, attrs ...string) (err error) { +func WriteHtmlSelect(w io.Writer, jid jid.Jid, nba *NamedBoolArray, attrs ...template.HTMLAttr) (err error) { need := 12 + jidPrealloc + 2 + getAttrsLen(attrs) + 2 + 10 nba.ReadLocked(func(nba []*NamedBool) { for _, nb := range nba { diff --git a/html_test.go b/html_test.go index 27d8831..f1bef69 100644 --- a/html_test.go +++ b/html_test.go @@ -13,7 +13,7 @@ func TestHtmlInput(t *testing.T) { jid jid.Jid typ string val string - attrs []string + attrs []template.HTMLAttr } tests := []struct { name string @@ -35,7 +35,7 @@ func TestHtmlInput(t *testing.T) { jid: 2, typ: "input_type2", val: "initial_val2", - attrs: []string{""}, + attrs: []template.HTMLAttr{""}, }, want: ``, }, @@ -45,7 +45,7 @@ func TestHtmlInput(t *testing.T) { jid: 3, typ: "input_type2", val: "initial_val2", - attrs: []string{"some_attr"}, + attrs: []template.HTMLAttr{"some_attr"}, }, want: ``, }, @@ -55,7 +55,7 @@ func TestHtmlInput(t *testing.T) { jid: 4, typ: "input_type2", val: "initial_val2", - attrs: []string{"some_attr1", "", "some_attr2"}, + attrs: []template.HTMLAttr{"some_attr1", "", "some_attr2"}, }, want: ``, }, @@ -79,7 +79,7 @@ func TestHtmlInner(t *testing.T) { tag string typ string inner template.HTML - attrs []string + attrs []template.HTMLAttr } tests := []struct { name string @@ -113,7 +113,7 @@ func TestHtmlInner(t *testing.T) { tag: "tag1", typ: "typ1", inner: "inner_text", - attrs: []string{"some_attr1", "some_attr2", ""}, + attrs: []template.HTMLAttr{"some_attr1", "some_attr2", ""}, }, want: `inner_text`, }, @@ -135,7 +135,7 @@ func TestHtmlSelect(t *testing.T) { type args struct { jid jid.Jid val *NamedBoolArray - attrs []string + attrs []template.HTMLAttr } tests := []struct { name string @@ -147,7 +147,7 @@ func TestHtmlSelect(t *testing.T) { args: args{ jid: 2, val: NewNamedBoolArray(), - attrs: []string{"attr1"}, + attrs: []template.HTMLAttr{"attr1"}, }, want: "\n", }, @@ -163,7 +163,7 @@ func TestHtmlSelect(t *testing.T) { nba.Set("two", true) return }(), - attrs: []string{"", "attr2"}, + attrs: []template.HTMLAttr{"", "attr2"}, }, want: `