forked from razonyang/clevergo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
81 lines (69 loc) · 1.62 KB
/
alert.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
package adminlte
import (
"bytes"
"encoding/gob"
"html/template"
"github.com/clevergo/demo/pkg/bootstrap"
)
var alertTemplate = template.Must(template.New("bootstrapAlert").Parse(`
<div class="alert alert-{{.Style}} {{ if .Dismiss }} alert-dismissible{{ end }}">
{{ if .Dismiss }}
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ end }}
{{ if .Heading }}
<h5>
{{ if .Icon }}
<i class="{{ .Icon }}"></i>
{{ end }}
{{ .Heading }}
</h5>
{{ end }}
{{ .Text }}
</div>
`))
func init() {
gob.Register(Alert{})
}
type Alert struct {
Text string
Style string
Heading string
Icon string
Dismiss bool
}
func NewAlert(heading, text, style string) Alert {
return Alert{
Heading: heading,
Text: text,
Style: style,
Dismiss: true,
}
}
func NewSuccessAlert(heading, text string) Alert {
alert := NewAlert(heading, text, bootstrap.AlertStyleSuccess)
alert.Icon = "icon fas fa-check"
return alert
}
func NewDangerAlert(heading, text string) Alert {
alert := NewAlert(heading, text, bootstrap.AlertStyleDanger)
alert.Icon = "icon fas fa-ban"
return alert
}
func NewWarningAlert(heading, text string) Alert {
alert := NewAlert(heading, text, bootstrap.AlertStyleWarning)
alert.Icon = "icon fas fa-exclamation-triangle"
return alert
}
func NewInfoAlert(heading, text string) Alert {
alert := NewAlert(heading, text, bootstrap.AlertStyleInfo)
alert.Icon = "icon fas fa-info-circle"
return alert
}
func (a Alert) Message() string {
msg := &bytes.Buffer{}
alertTemplate.Execute(msg, a)
return msg.String()
}
func (a Alert) Output() string {
return a.Message()
}