-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (36 loc) · 947 Bytes
/
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
let installed = false
const VueOnline = {}
function updateOnlineStatus() {
if (VueOnline && VueOnline.instance) {
VueOnline.instance.status = navigator.onLine
}
}
/**
* Install AmIOnline
*/
VueOnline.install = function(Vue) {
if (!installed) {
// We only need one instance of the connectivity checker
VueOnline.instance = new Vue({
data: function() {
return {
status: navigator.onLine // the status has to be a reactive property
}
}
})
// can get $online, but don't let components set $online
Object.defineProperty(Vue.prototype, "$online", {
get: function get() {
return VueOnline.instance.status
}
})
// listen to "online" and "offline" events
window.addEventListener("online", updateOnlineStatus)
window.addEventListener("offline", updateOnlineStatus)
}
installed = true
}
if (typeof window !== "undefined" && window.Vue) {
window.Vue.use(VueOnline)
}
export default VueOnline