-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewSwal.go
43 lines (35 loc) · 1.08 KB
/
NewSwal.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
package hb
import (
"encoding/json"
)
type SwalOptions struct {
Icon string `json:"icon,omitempty"`
Text string `json:"text,omitempty"`
Title string `json:"title,omitempty"`
ShowCancelButton bool `json:"showCancelButton,omitempty"`
CancelButtonColor string `json:"cancelButtonColor,omitempty"`
CancelButtonText string `json:"cancelButtonText,omitempty"`
ConfirmButtonText string `json:"confirmButtonText,omitempty"`
ConfirmButtonColor string `json:"confirmButtonColor,omitempty"`
ConfirmCallback string `json:"-"`
}
// NewSwal generates a script with a Sweetalert2 dialog
// Note! you must include the library yourself (i.e. CDN)
func NewSwal(options SwalOptions) *Tag {
optionsBytes, err := json.Marshal(options)
var optionsJSON string
if err != nil {
optionsJSON = ""
} else {
optionsJSON = string(optionsBytes)
}
swal := `Swal.fire(` + optionsJSON + `)`
if options.ConfirmCallback != "" {
swal += `.then((result) => {
if (result.isConfirmed) {
` + options.ConfirmCallback + `
}
});`
}
return NewScript(swal)
}