-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
75 lines (64 loc) · 1.43 KB
/
progress.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
package bootstrap
import (
"context"
"fmt"
"html"
"io"
)
type Progress struct {
ID string
Class string
Yield func()
}
func (r *Progress) Render(ctx context.Context, w io.Writer) {
io.WriteString(w, `<div`)
writeAttr(w, "id", r.ID)
r.renderClass(ctx, w)
io.WriteString(w, `>`)
if r.Yield != nil {
r.Yield()
}
io.WriteString(w, `</div>`)
}
func (r *Progress) renderClass(ctx context.Context, w io.Writer) {
io.WriteString(w, ` class="progress`)
if s := r.Class; s != "" {
fmt.Fprintf(w, " %s", html.EscapeString(s))
}
io.WriteString(w, `"`)
}
type ProgressBar struct {
ID string
Class string
Striped bool
Animated bool
Value float64
Yield func()
}
func (r *ProgressBar) Render(ctx context.Context, w io.Writer) {
io.WriteString(w, `<div`)
writeAttr(w, "id", r.ID)
r.renderClass(ctx, w)
fmt.Fprintf(w, ` style="width:%f%%"`, r.Value)
fmt.Fprintf(w, ` aria-valuenow="%f%%" `, r.Value)
io.WriteString(w, ` aria-valuemin="0"`)
io.WriteString(w, ` aria-valuemax="100"`)
io.WriteString(w, `>`)
if r.Yield != nil {
r.Yield()
}
io.WriteString(w, `</div>`)
}
func (r *ProgressBar) renderClass(ctx context.Context, w io.Writer) {
io.WriteString(w, ` class="progress-bar`)
if r.Striped {
io.WriteString(w, ` progress-bar-striped`)
}
if r.Animated {
io.WriteString(w, ` progress-bar-animated`)
}
if s := r.Class; s != "" {
fmt.Fprintf(w, " %s", html.EscapeString(s))
}
io.WriteString(w, `"`)
}