-
Notifications
You must be signed in to change notification settings - Fork 0
/
toast.go
72 lines (57 loc) · 1.5 KB
/
toast.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
// +build js,wasm
package toast
import (
"syscall/js"
"github.com/dairaga/js/dom"
"github.com/dairaga/js/dom/bs"
)
// Toast represents bootstrap toast.
type Toast struct {
*bs.Component
}
// New returns alert component.
func New(msg string, autoHide bool, header ...interface{}) *Toast {
a := &Toast{bs.ComponentOf(dom.CreateElement("div"))}
a.AddClass("toast").
SetAttr("role", "alert").
SetAttr("aria-live", "assertive").
SetAttr("aria-atomic", "true")
if autoHide {
a.SetAttr("data-autohide", "true")
a.SetAttr("data-delay", "3000")
} else {
a.SetAttr("data-autohide", "false")
}
h := dom.CreateElement("div")
h.AddClass("toast-header")
if len(header) > 0 {
h.Append(header...)
}
closeBtn := dom.CreateElement("button")
closeBtn.AddClass("ml-2", "mb-1", "close").SetAttr("type", "button").SetAttr("data-dismiss", "toast").SetAttr("aria-label", "Close")
closeBtn.SetHTML(`<span aria-hidden="true">×</span>`)
h.Append(closeBtn)
a.Append(h)
b := dom.CreateElement("div")
b.AddClass("toast-body")
b.SetText(msg)
a.Append(b)
return a
}
// Show ...
func Show(parent *bs.Component, msg string, autoHide bool, header ...interface{}) *Toast {
a := New(msg, autoHide, header...)
parent.Prepend(a)
a.Show()
return a
}
// Show ...
func (a *Toast) Show() *Toast {
js.Global().Call("$", a.JSValue()).Call("toast", "show")
return a
}
// Close hide and dispose alert component.
func (a *Toast) Close() *Toast {
js.Global().Call("$", a.JSValue()).Call("toast", "hide")
return a
}