-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoaster.go
138 lines (118 loc) · 4.34 KB
/
toaster.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
package goaster
import (
"github.com/a-h/templ"
"github.com/indaco/goaster/components"
"github.com/indaco/goaster/internal/viewmodel"
)
// Toaster holds configuration for toast notifications.
type Toaster struct {
Variant Variant // Style variant
Border bool // Display border
Rounded bool // Use rounded corners
ShowIcon bool // Show icon
Button bool // Show close button
AutoDismiss bool // Auto-dismiss after timeout
Animation bool // Animate entrance/exit
ProgressBar bool // Show progress bar
Position Position // Screen position
Icons map[Level]string // Custom icons
queue *Queue // Toast queue
}
func ToasterDefaults() *Toaster {
return &Toaster{
Border: true,
Rounded: true,
ShowIcon: true,
Button: true,
AutoDismiss: true,
Animation: true,
ProgressBar: true,
Position: BottomRight,
Icons: defaultIcons(),
queue: NewQueue(),
}
}
func (t *Toaster) ToViewModel() viewmodel.ToasterViewModel {
var toasts []viewmodel.ToastViewModel
for _, toast := range t.Queue().GetMessagesAndDequeue() {
toasts = append(toasts, viewmodel.ToastViewModel{
Message: toast.Message,
Level: toast.Level.String(),
Icon: t.Icons[toast.Level],
})
}
return viewmodel.ToasterViewModel{
Variant: t.Variant.String(),
Border: t.Border,
Rounded: t.Rounded,
ShowIcon: t.ShowIcon,
Button: t.Button,
AutoDismiss: t.AutoDismiss,
Animation: t.Animation,
ProgressBar: t.ProgressBar,
Position: t.Position.String(),
Toasts: toasts,
}
}
/* ------------------------------------------------------------------------- */
/* QUEUE ACCESS */
/* ------------------------------------------------------------------------- */
// Queue returns the internal toast queue.
func (t *Toaster) Queue() *Queue {
return t.queue
}
/* ------------------------------------------------------------------------- */
/* PUSH METHODS */
/* ------------------------------------------------------------------------- */
// PushDefault adds a default toast notification.
func (t *Toaster) PushDefault(message string) {
t.queue.Enqueue(NewToast(message, DefaultLevel))
}
// PushSuccess adds a success toast notification.
func (t *Toaster) PushSuccess(message string) {
t.queue.Enqueue(NewToast(message, SuccessLevel))
}
// PushError adds an error toast notification.
func (t *Toaster) PushError(message string) {
t.queue.Enqueue(NewToast(message, ErrorLevel))
}
// PushWarning adds a warning toast notification.
func (t *Toaster) PushWarning(message string) {
t.queue.Enqueue(NewToast(message, WarningLevel))
}
// PushInfo adds an info toast notification.
func (t *Toaster) PushInfo(message string) {
t.queue.Enqueue(NewToast(message, InfoLevel))
}
/* ------------------------------------------------------------------------- */
/* RENDER METHODS */
/* ------------------------------------------------------------------------- */
// RenderAll prints all the toast notifications in the queue.
func (t *Toaster) RenderAll() templ.Component {
return components.Container(t.ToViewModel())
}
// Default displays a default toast notification.
func (t *Toaster) Default(message string) templ.Component {
t.queue.Enqueue(NewToast(message, DefaultLevel))
return components.Container(t.ToViewModel())
}
// Success displays a success toast notification.
func (t *Toaster) Success(message string) templ.Component {
t.queue.Enqueue(NewToast(message, SuccessLevel))
return components.Container(t.ToViewModel())
}
// Error displays an error toast notification.
func (t *Toaster) Error(message string) templ.Component {
t.queue.Enqueue(NewToast(message, ErrorLevel))
return components.Container(t.ToViewModel())
}
// Warning displays a warning toast notification.
func (t *Toaster) Warning(message string) templ.Component {
t.queue.Enqueue(NewToast(message, WarningLevel))
return components.Container(t.ToViewModel())
}
// Info displays an info toast notification.
func (t *Toaster) Info(message string) templ.Component {
t.queue.Enqueue(NewToast(message, InfoLevel))
return components.Container(t.ToViewModel())
}