Skip to content

Commit

Permalink
export Element.JawsRender/Update
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Feb 21, 2024
1 parent 9479003 commit e011129
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 43 deletions.
37 changes: 32 additions & 5 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"io"
"strings"

"github.com/linkdata/jaws/jid"
"github.com/linkdata/jaws/what"
Expand Down Expand Up @@ -47,16 +48,42 @@ func (e *Element) Ui() UI {
return e.ui
}

// render calls Request.render() for this Element.
func (e *Element) render(w io.Writer, params []any) (err error) {
func (e *Element) renderDebug(w io.Writer) {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "<!-- id=%q %T tags=[", e.Jid(), e.Ui())
if e.mu.TryRLock() {
defer e.mu.RUnlock()
for i, tag := range e.tagsOfLocked(e) {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(TagString(tag))
}
} else {
sb.WriteString("n/a")
}
sb.WriteByte(']')
_, _ = w.Write([]byte(strings.ReplaceAll(sb.String(), "-->", "==>") + " -->"))
}

// JawsRender calls Ui().JawsRender() for this Element.
//
// Do not call this yourself unless it's from within another JawsRender implementation.
func (e *Element) JawsRender(w io.Writer, params []any) (err error) {
if !e.deleted {
err = e.Request.render(e, w, params)
if err = e.Ui().JawsRender(e, w, params); err == nil {
if e.Jaws.Debug {
e.renderDebug(w)
}
}
}
return
}

// update calls Ui().JawsUpdate() for this Element.
func (e *Element) update() {
// JawsUpdate calls Ui().JawsUpdate() for this Element.
//
// Do not call this yourself unless it's from within another JawsUpdate implementation.
func (e *Element) JawsUpdate() {
if !e.deleted {
e.Ui().JawsUpdate(e)
}
Expand Down
31 changes: 2 additions & 29 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,6 @@ func (rq *Request) clearLocked() *Request {
return rq
}

func (rq *Request) renderDebug(elem *Element, w io.Writer) {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "<!-- id=%q %T tags=[", elem.Jid(), elem.Ui())
if rq.mu.TryRLock() {
defer rq.mu.RUnlock()
for i, tag := range rq.tagsOfLocked(elem) {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(TagString(tag))
}
} else {
sb.WriteString("n/a")
}
sb.WriteByte(']')
_, _ = w.Write([]byte(strings.ReplaceAll(sb.String(), "-->", "==>") + " -->"))
}

func (rq *Request) render(elem *Element, w io.Writer, params []any) (err error) {
if err = elem.Ui().JawsRender(elem, w, params); err == nil {
if rq.Jaws.Debug {
rq.renderDebug(elem, w)
}
}
return
}

// HeadHTML writes the HTML code needed in the HTML page's HEAD section.
func (rq *Request) HeadHTML(w io.Writer) (err error) {
rq.mu.RLock()
Expand Down Expand Up @@ -496,7 +469,7 @@ func (rq *Request) process(broadcastMsgCh chan Message, incomingMsgCh <-chan wsM
// for identified elements. This queues up wsMsg's
// in elem.wsQueue.
for _, elem := range rq.makeUpdateList() {
elem.update()
elem.JawsUpdate()
}

rq.sendQueue(outboundMsgCh)
Expand Down Expand Up @@ -577,7 +550,7 @@ func (rq *Request) process(broadcastMsgCh chan Message, incomingMsgCh <-chan wsM
})
}
case what.Update:
elem.update()
elem.JawsUpdate()
default:
rq.queue(wsMsg{
Data: tagmsg.Data,
Expand Down
4 changes: 2 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func TestRequest_renderDebugLocked(t *testing.T) {
e.Tag(Tag("zomg"))

var sb strings.Builder
rq.renderDebug(e, &sb)
e.renderDebug(&sb)

txt := sb.String()
is.Equal(strings.Contains(txt, "zomg"), true)
Expand All @@ -724,7 +724,7 @@ func TestRequest_renderDebugLocked(t *testing.T) {
rq.mu.Lock()
defer rq.mu.Unlock()
sb.Reset()
rq.renderDebug(e, &sb)
e.renderDebug(&sb)

txt = sb.String()
is.Equal(strings.Contains(txt, "zomg"), false)
Expand Down
2 changes: 1 addition & 1 deletion requestwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RequestWriter struct {
}

func (rw RequestWriter) UI(ui UI, params ...any) error {
return rw.rq.render(rw.rq.NewElement(ui), rw, params)
return rw.rq.NewElement(ui).JawsRender(rw, params)
}

func (rw RequestWriter) Write(p []byte) (n int, err error) {
Expand Down
2 changes: 1 addition & 1 deletion uicontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestRequest_Container_Alteration(t *testing.T) {
t.Fatal(err)
}
tt.c.contents = tt.l
elem.update()
elem.JawsUpdate()
if !slices.Equal(rq.wsQueue, tt.want) {
t.Errorf("got %v, want %v", rq.wsQueue, tt.want)
}
Expand Down
2 changes: 1 addition & 1 deletion uihtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestUiHtml_JawsEvent(t *testing.T) {

elem := rq.getElementByJid(id2)
var sb strings.Builder
if err := elem.render(&sb, []any{"attr"}); err != nil {
if err := elem.JawsRender(&sb, []any{"attr"}); err != nil {
t.Fatal(err)
}
select {
Expand Down
4 changes: 2 additions & 2 deletions uiradiogroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func (rw RequestWriter) RadioGroup(nba *NamedBoolArray) (rel []RadioElement) {
// Radio renders a HTML input element of type 'radio'.
func (re RadioElement) Radio(params ...any) template.HTML {
var sb strings.Builder
maybePanic(re.radio.render(&sb, append(params, re.nameAttr)))
maybePanic(re.radio.JawsRender(&sb, append(params, re.nameAttr)))
return template.HTML(sb.String()) // #nosec G203
}

// Label renders a HTML label element.
func (re RadioElement) Label(params ...any) template.HTML {
var sb strings.Builder
forAttr := string(re.radio.Jid().AppendQuote([]byte("for=")))
maybePanic(re.label.render(&sb, append(params, forAttr)))
maybePanic(re.label.JawsRender(&sb, append(params, forAttr)))
return template.HTML(sb.String()) // #nosec G203
}
4 changes: 2 additions & 2 deletions uiwrapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag
if err == nil {
elem := e.Request.NewElement(cui)
ui.contents = append(ui.contents, elem)
err = elem.render(w, nil)
err = elem.JawsRender(w, nil)
}
}
b = b[:0]
Expand Down Expand Up @@ -84,7 +84,7 @@ func (ui *uiWrapContainer) JawsUpdate(e *Element) {

for _, elem := range toAppend {
var sb strings.Builder
maybePanic(elem.render(&sb, nil))
maybePanic(elem.JawsRender(&sb, nil))
e.Append(template.HTML(sb.String())) // #nosec G203
}

Expand Down

0 comments on commit e011129

Please sign in to comment.