-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtoast_js.go
264 lines (234 loc) · 5.95 KB
/
toast_js.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//go:build js && wasm
package toast
import (
"syscall/js"
"time"
)
// WithIcon
//
// The URL of the image used as an icon of the notification
func WithIcon(urlIcon string) NotificationOption {
return func(n *notification) {
n._options["icon"] = urlIcon
}
}
// WithTextDirection
//
// The text direction of the notification
func WithTextDirection(dir TextDirection) NotificationOption {
return func(n *notification) {
n._options["dir"] = string(dir)
}
}
// WithLang
//
// The language code of the notification
func WithLang(lang string) NotificationOption {
return func(n *notification) {
n._options["lang"] = lang
}
}
// WithNotificationID
//
// The ID of the notification (if any)
func WithNotificationID(tag string) NotificationOption {
return func(n *notification) {
n._options["tag"] = tag
}
}
// WithImage
//
// The URL of an image to be displayed as part of the notification
func WithImage(urlImage string) NotificationOption {
return func(n *notification) {
n._options["image"] = urlImage
}
}
// WithRenotify
//
// Specifies whether the user should be notified after a new notification replaces an old one
func WithRenotify(b bool) NotificationOption {
return func(n *notification) {
n._options["renotify"] = b
}
}
// WithRequireInteraction
//
// indicating that a notification should remain active until the user clicks or dismisses it,
// rather than closing automatically.
func WithRequireInteraction(b bool) NotificationOption {
return func(n *notification) {
n._options["requireInteraction"] = b
}
}
// WithSilent
//
// Specifies whether the notification should be silent — i.e., no sounds or vibrations should be issued,
// regardless of the device settings.
func WithSilent(b bool) NotificationOption {
return func(n *notification) {
n._options["silent"] = b
}
}
// WithTimestamp
//
// Specifies the time at which a notification is created or applicable (past, present, or future).
func WithTimestamp(t time.Time) NotificationOption {
return func(n *notification) {
n._options["timestamp"] = t.Unix()
}
}
// WithVibrate
//
// Specifies a vibration pattern for devices with vibration hardware to emit.
func WithVibrate(v []int) NotificationOption {
return func(n *notification) {
_v := make([]interface{}, len(v))
for i := range v {
_v[i] = v[i]
}
n._options["vibrate"] = _v
}
}
// WithOnClick
//
// A handler for the click event.
// It is triggered each time the user clicks on the notification.
func WithOnClick(fn func(event interface{})) NotificationOption {
return func(n *notification) {
n._onClick = fn
}
}
// WithOnShow
//
// A handler for the show event.
// It is triggered when the notification is displayed.
func WithOnShow(fn func()) NotificationOption {
return func(n *notification) {
n._onShow = fn
}
}
// WithOnClose
//
// A handler for the close event.
// It is triggered when the user closes the notification.
func WithOnClose(fn func()) NotificationOption {
return func(n *notification) {
n._onClose = fn
}
}
// WithOnError
//
// A handler for the error event.
// It is triggered each time the notification encounters an error.
func WithOnError(fn func()) NotificationOption {
return func(n *notification) {
n._onError = fn
}
}
type TextDirection string
const (
// Auto adopts the browser's language setting behavior (the default.)
Auto TextDirection = "auto"
// LTR left to right
LTR TextDirection = "ltr"
// RTL right to left
RTL TextDirection = "rtl"
)
var _ notifier = (*notification)(nil)
func newNotification(message string, opts ...NotificationOption) *notification {
n := ¬ification{
Title: js.Global().Get("location").Get("href").String(),
Message: message,
_options: make(map[string]interface{}, 16),
}
for _, fn := range opts {
fn(n)
}
return n
}
func (n *notification) push() error {
// check if the browser supports notifications
if !isSupported() {
alert("This browser does not support desktop notification")
return nil
}
// check whether notification permissions have already been granted
if isGranted() {
n.createNotification()
return nil
}
// need to ask the user for permission
if !isDenied() {
// If the user accepts, let's create a notification
js.Global().Get("Notification").
Call("requestPermission").
Call("then",
js.FuncOf(func(this js.Value, args []js.Value) interface{} {
n.createNotification()
return nil
}),
)
}
return nil
}
func (n *notification) createNotification() {
notify := js.Global().Get("Notification").New(n.Title, js.ValueOf(n.generateOptions()))
if n._onClick != nil {
notify.Set("onclick", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
n._onClick(this)
return nil
}))
}
if n._onShow != nil {
notify.Set("onshow", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
n._onShow()
return nil
}))
}
if n._onClose != nil {
notify.Set("onclose", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
n._onClose()
return nil
}))
}
if n._onError != nil {
notify.Set("onerror", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
n._onError()
return nil
}))
}
}
func (n *notification) generateOptions() (options map[string]interface{}) {
options = make(map[string]interface{}, 16)
options["body"] = n.Message
for k, v := range n._options {
options[k] = v
}
return
}
// https://developer.mozilla.org/en-US/docs/Web/API/notification
type notification struct {
// The title of the notification
Title string
// The body string of the notification
Message string
Audio Audio
_options map[string]interface{}
_onClick func(event interface{})
_onShow func()
_onClose func()
_onError func()
}
func alert(msg string) {
js.Global().Call("alert", msg)
}
func isSupported() bool {
return js.Global().Call("hasOwnProperty", "Notification").Bool()
}
func isGranted() bool {
return js.Global().Get("Notification").Get("permission").String() == "granted"
}
func isDenied() bool {
return js.Global().Get("Notification").Get("permission").String() == "denied"
}