dmitri.shuralyov.com/html/belt

add OnClick field

It will be used for setting the onclick property for anchor elements.
This is needed for https://github.com/shurcooL/home/issues/40.
dmitshur committed 4 years ago commit f19ad8cb3e56f0fce6b45d5b68d7130214a0470a
Showing partial commit. Full Commit
Collapse all
belt.go
@@ -16,18 +16,20 @@ import (
// Issue is a component that displays an issue, with a state icon and title.
type Issue struct {
	State   state.Issue
	Title   string
	HTMLURL string
	OnClick string // Optional.
	Short   bool
}

func (i Issue) Render() []*html.Node {
	n := iconLink{
		Text:    i.Title,
		Tooltip: i.Title,
		URL:     i.HTMLURL,
		OnClick: i.OnClick,
	}
	if i.Short {
		n.Text = shortTitle(i.Title)
	}
	switch i.State {
@@ -44,18 +46,20 @@ func (i Issue) Render() []*html.Node {
// Change is a component that displays a change, with a state icon and title.
type Change struct {
	State   state.Change
	Title   string
	HTMLURL string
	OnClick string // Optional.
	Short   bool
}

func (c Change) Render() []*html.Node {
	n := iconLink{
		Text:    c.Title,
		Tooltip: c.Title,
		URL:     c.HTMLURL,
		OnClick: c.OnClick,
	}
	if c.Short {
		n.Text = shortTitle(c.Title)
	}
	switch c.State {
@@ -83,20 +87,24 @@ func shortTitle(s string) string {
// Icon must be not nil.
type iconLink struct {
	Text      string
	Tooltip   string
	URL       string
	OnClick   string            // Optional.
	Black     bool              // Black link.
	Icon      func() *html.Node // Not nil.
	IconColor *rgb              // Optional icon color override.
}

func (d iconLink) Render() []*html.Node {
	a := &html.Node{
		Type: html.ElementNode, Data: atom.A.String(),
		Attr: []html.Attribute{{Key: atom.Href.String(), Val: d.URL}},
	}
	if d.OnClick != "" {
		a.Attr = append(a.Attr, html.Attribute{Key: atom.Onclick.String(), Val: d.OnClick})
	}
	if d.Tooltip != "" {
		a.Attr = append(a.Attr, html.Attribute{Key: atom.Title.String(), Val: d.Tooltip})
	}
	if d.Black {
		a.Attr = append(a.Attr, html.Attribute{Key: atom.Class.String(), Val: "black"})