-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
82 lines (74 loc) · 2.07 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
'use strict'
import IdleJs from 'idle-js'
export default {
install (Vue, options) {
const {
eventEmitter,
store,
moduleName = 'idleVue',
idleTime = 60 * 1000,
events = ['mousemove', 'keydown', 'mousedown', 'touchstart'],
keepTracking = true,
startAtIdle = true
} = options || {}
if (!eventEmitter && !store) {
throw Error('Either `eventEmitter` or `store` must be passed in options')
}
store && store.registerModule(moduleName, {
state: { isIdle: startAtIdle },
mutations: {
[`${moduleName}/IDLE_CHANGED`]: function (state, isIdle) {
state.isIdle = isIdle
}
}
})
const onIdleStr = `${moduleName}_onIdle`
const onActiveStr = `${moduleName}_onActive`
const idle = new IdleJs({
idle: idleTime,
events,
keepTracking,
startAtIdle,
onIdle () {
eventEmitter && eventEmitter.$emit(onIdleStr)
store && store.commit(`${moduleName}/IDLE_CHANGED`, true)
},
onActive () {
eventEmitter && eventEmitter.$emit(onActiveStr)
store && store.commit(`${moduleName}/IDLE_CHANGED`, false)
}
})
idle.start()
Vue.mixin({
data () {
return {
[onIdleStr]: null,
[onActiveStr]: null
}
},
created () {
if (eventEmitter && this.$options.onIdle) {
this[onIdleStr] = this.$options.onIdle.bind(this)
eventEmitter.$on(onIdleStr, this[onIdleStr])
}
if (eventEmitter && this.$options.onActive) {
this[onActiveStr] = this.$options.onActive.bind(this)
eventEmitter.$on(onActiveStr, this[onActiveStr])
}
},
destroyed () {
if (eventEmitter && this[onIdleStr]) {
eventEmitter.$off(onIdleStr, this[onIdleStr])
}
if (eventEmitter && this[onActiveStr]) {
eventEmitter.$off(onActiveStr, this[onActiveStr])
}
},
computed: {
isAppIdle () {
return store && store.state[moduleName].isIdle
}
}
})
}
}