-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathnotify.web.ts
70 lines (47 loc) · 1.81 KB
/
notify.web.ts
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
namespace $ {
/** Manages system notifications. Notifications of same context are auto joined to one notification. */
export class $mol_notify_web extends $mol_notify {
@ $mol_mem
static override allowed( next?: boolean ) {
let perm = this.$.$mol_dom_context.Notification.permission
if( next === undefined ) return perm === 'granted'
if( perm === 'granted' ) return true
perm = $mol_wire_sync( this ).request_permissions()
return perm === 'granted'
}
static request_permissions() {
return new Promise< NotificationPermission >( done =>
this.$.$mol_dom_context.Notification.requestPermission( perm => {
done( perm )
} )
)
}
}
$.$mol_notify = $mol_notify_web
export class $mol_notify_service_web extends $mol_notify_service {
static override show({ context: title, message: body, uri: data }: $mol_notify_info) {
const tag = data
const existen = this.$.$mol_service_self_web.notifications({ tag })
for( const not of existen ) {
if( not.body.indexOf( body ) !== -1 ) body = not.body
else if( body.indexOf( not.body ) === -1 ) body = not.body + '\n' + body
not.close()
}
// const vibrate = [ 100, 200, 300, 400, 500 ]
this.$.$mol_service_self_web.notification_show( title, { body, data, /*vibrate,*/ tag } )
}
static override notification( notification: Notification ) {
const matched = this.$.$mol_service_self_web.clients_grab({ includeUncontrolled: true, type: 'window' })
const last = matched.at(-1)
if( last ) {
last.focus()
last.navigate( notification.data )
return null
}
this.$.$mol_service_self_web.window_open( notification.data )
return null
}
}
$.$mol_notify_service = $mol_notify_service_web
$mol_service_plugin.$mol_notify_service = $mol_notify_service_web
}