-
Notifications
You must be signed in to change notification settings - Fork 106
/
html.go
167 lines (139 loc) · 3.68 KB
/
html.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package html
import (
"context"
"encoding/json"
"fmt"
"html"
"io"
"io/fs"
"net/url"
"github.com/benbjohnson/wtf"
"github.com/benbjohnson/wtf/http/assets"
)
// Alert displays an error message.
type Alert struct {
Err error
}
func (r *Alert) Render(ctx context.Context, w io.Writer) {
if r.Err == nil {
return
}
fmt.Fprint(w, `<div class="card bg-light mb-3">`)
fmt.Fprint(w, `<div class="card-body p-3">`)
fmt.Fprint(w, `<p class="fs--1 mb-0 text-danger">`)
fmt.Fprint(w, `<i class="fas fa-exclamation-circle mr-2"></i>`)
fmt.Fprint(w, html.EscapeString(wtf.ErrorMessage(r.Err)))
fmt.Fprint(w, `</p>`)
fmt.Fprint(w, `</div>`)
fmt.Fprint(w, `</div>`)
}
// Flash displays the flash message, if available.
type Flash struct{}
func (r *Flash) Render(ctx context.Context, w io.Writer) {
s := wtf.FlashFromContext(ctx)
if s == "" {
return
}
fmt.Fprint(w, `<div class="card bg-light mb-3">`)
fmt.Fprint(w, `<div class="card-body p-3">`)
fmt.Fprint(w, `<p class="fs--1 mb-0">`)
fmt.Fprint(w, `<i class="fas fa-exclamation-circle mr-2"></i>`)
fmt.Fprint(w, html.EscapeString(s))
fmt.Fprint(w, `</p>`)
fmt.Fprint(w, `</div>`)
fmt.Fprint(w, `</div>`)
}
type Pagination struct {
URL url.URL
Offset int
Limit int
N int
}
func (r *Pagination) Render(ctx context.Context, w io.Writer) {
// Do not render if no limit exists or there is only one page.
if r.Limit == 0 || r.N <= r.Limit {
return
}
// Determine the page range & current page.
current := (r.Offset / r.Limit) + 1
pageN := ((r.N - 1) / r.Limit) + 1
prev := current - 1
if prev <= 0 {
prev = 1
}
next := current + 1
if next >= pageN {
next = pageN
}
// Print container & "previous" link.
fmt.Fprint(w, `<nav aria-label="Page navigation">`)
fmt.Fprint(w, `<ul class="pagination pagination-sm justify-content-end mb-0">`)
fmt.Fprintf(w, `<li class="page-item"><a class="page-link" href="%s">Previous</a></li>`, r.pageURL(current-1))
// Print a button for each page number.
for page := 1; page <= pageN; page++ {
className := ""
if page == current {
className = " active"
}
fmt.Fprintf(w, `<li class="page-item %s"><a class="page-link" href="%s">%d</a></li>`, className, r.pageURL(page), page)
}
// Print "next" link & close container.
fmt.Fprintf(w, `<li class="page-item"><a class="page-link" href="%s">Next</a></li>`, r.pageURL(current+1))
fmt.Fprint(w, `</ul>`)
fmt.Fprint(w, `</nav>`)
}
func (r *Pagination) pageURL(page int) string {
// Ensure page number is within min/max.
pageN := ((r.N - 1) / r.Limit) + 1
if page < 1 {
page = 1
} else if page > pageN {
page = pageN
}
q := r.URL.Query()
q.Set("offset", fmt.Sprint((page-1)*r.Limit))
u := url.URL{Path: r.URL.Path, RawQuery: q.Encode()}
return u.String()
}
type WTFBadge struct {
DialID int
DialMembershipID int
Value int
}
func (r *WTFBadge) Render(ctx context.Context, w io.Writer) {
prefix := "bg-"
if HasTheme {
prefix = "badge-soft-"
}
var class string
switch {
case r.Value < 25:
class = prefix + "success"
case r.Value < 50:
class = prefix + "info"
case r.Value < 75:
class = prefix + "warning"
default:
class = prefix + "danger"
}
fmt.Fprintf(w, `<span`)
fmt.Fprintf(w, ` class="wtf-badge wtf-value badge rounded-pill %s"`, class)
if r.DialID != 0 {
fmt.Fprintf(w, ` data-dial-id="%d"`, r.DialID)
}
if r.DialMembershipID != 0 {
fmt.Fprintf(w, ` data-dial-membership-id="%d"`, r.DialMembershipID)
}
fmt.Fprint(w, `>`)
fmt.Fprint(w, r.Value)
fmt.Fprint(w, `</span>`)
}
func marshalJSONTo(w io.Writer, v interface{}) {
json.NewEncoder(w).Encode(v)
}
const ThemePath = "css/theme.css"
var HasTheme bool
func init() {
_, err := fs.Stat(assets.FS, ThemePath)
HasTheme = (err == nil)
}