This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
124 lines (104 loc) · 3.34 KB
/
index.js
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
// Notification object
var Notification = window.Notification || window.webkitNotification
const onerror = function onerror (event) {
// console.log('On error event was called')
}
const onclick = function onclick (event) {
// console.log('On click event was called')
event.preventDefault()
window.focus()
event.target.close()
}
const onclose = function onclose (event) {
// console.log('On close event was called')
}
const onshow = function onshow (event) {
// console.log('On show event was called')
}
const defaultEvents = {
onerror: onerror,
onclick: onclick,
onclose: onclose,
onshow: onshow
}
// Plugin
const VueNativeNotification = {
install: function (Vue, options) {
options = options || {}
options.requestOnNotify = options.requestOnNotify || true
Vue.notification = {}
Vue.prototype.$notification = {}
// Manual permission request
var requestPermission = function () {
return Notification.requestPermission()
}
Vue.notification.requestPermission = requestPermission
Vue.prototype.$notification.requestPermission = requestPermission
// Show function
var show = function (title, opts, e) {
if (!e.onerror) e.onerror = function () { }
if (!e.onclick) e.onclick = function () { }
if (!e.onclose) e.onclose = function () { }
if (!e.onshow) e.onshow = function () { }
return Promise.resolve()
.then(function () {
if (options.requestOnNotify && Notification.permission !== 'granted') {
return requestPermission()
}
return Notification.permission
})
.then(function (permission) {
// "default" doesn't mean "denied"
// It means the user has dismissed the request
if (permission === 'denied') {
return new Error('No permission to show notification')
}
const bindOnError = function (event) {
'use strict'
defaultEvents.onerror(event)
e.onerror(event)
}
const bindOnClick = function (event) {
'use strict'
defaultEvents.onclick(event)
e.onclick(event)
}
const bindOnClose = function (event) {
'use strict'
defaultEvents.onclose(event)
e.onclose(event)
}
const bindOnShow = function (event) {
'use strict'
defaultEvents.onshow(event)
e.onshow(event)
}
// Create Notification object
try {
const notification = new Notification(title, opts)
notification.onerror = bindOnError
notification.onclick = bindOnClick
notification.onclose = bindOnClose
notification.onshow = bindOnShow
return notification
} catch (e) {
if (e.name !== 'TypeError') {
return e
}
return navigator.serviceWorker.ready.then(
function (reg) {
reg.showNotification(title, opts)
}).then(bindOnShow, bindOnError)
}
})
}
Vue.notification.show = show
Vue.prototype.$notification.show = show
}
}
// Automatic installation
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(VueNativeNotification)
}
// Export plugin
export default VueNativeNotification