-
Notifications
You must be signed in to change notification settings - Fork 17
/
mixin.js
176 lines (162 loc) · 4.64 KB
/
mixin.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
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
import { set, isArray } from './utils'
function generateData(timers) {
return Object.keys(timers).reduce(function(acc, cur) {
return set(
cur,
{
isRunning: false,
time: timers[cur].time || 0,
instance: null
},
acc
)
}, {})
}
function setTimer(repeat) {
return repeat ? setInterval : setTimeout
}
function clearTimer(repeat) {
return repeat ? clearInterval : clearTimeout
}
function generateTimer(options, vm) {
return setTimer(options.repeat)(function() {
vm.$emit('timer-tick:' + options.name)
options.callback()
}, options.time)
}
function normalizeConfig(config, vm) {
if (process.env.NODE_ENV !== 'production') {
if (!config.name) {
throw new Error('[vue-timers.create] name is required')
}
if (!config.callback && typeof vm[config.name] !== 'function') {
throw new ReferenceError(
'[vue-timers.create] Cannot find method ' + config.name
)
}
if (config.callback && typeof config.callback !== 'function') {
throw new TypeError(
'[vue-timers.create] Timer callback should be a function, ' +
typeof config.callback +
' given'
)
}
}
return {
name: config.name,
time: config.time || 0,
repeat: 'repeat' in config ? config.repeat : false,
immediate: 'immediate' in config ? config.immediate : false,
autostart: 'autostart' in config ? config.autostart : false,
isSwitchTab: 'isSwitchTab' in config ? config.isSwitchTab : false,
callback: (config.callback && config.callback.bind(vm)) || vm[config.name]
}
}
function normalizeOptions(options, vm) {
return isArray(options)
? options.reduce(function(res, config) {
return set(config.name, normalizeConfig(config, vm), res)
}, {})
: Object.keys(options).reduce(function(res, key) {
return set(
key,
normalizeConfig(set('name', key, options[key]), vm),
res
)
}, {})
}
export default {
data: function() {
if (!this.$options.timers) return {}
this.$options.timers = normalizeOptions(this.$options.timers, this)
return {
timers: generateData(this.$options.timers)
}
},
created: function() {
if (!this.$options.timers) return
var vm = this
var data = vm.timers
var options = vm.$options.timers
vm.$timer = {
start: function(name) {
if (process.env.NODE_ENV !== 'production' && !(name in options)) {
throw new ReferenceError(
'[vue-timers.start] Cannot find timer ' + name
)
}
if (data[name].isRunning) return
data[name].isRunning = true
data[name].instance = generateTimer(
set('time', data[name].time, options[name]),
vm
)
if (options[name].immediate) {
vm.$emit('timer-tick:' + name)
options[name].callback()
}
vm.$emit('timer-start:' + name)
},
stop: function(name) {
if (process.env.NODE_ENV !== 'production' && !(name in options)) {
throw new ReferenceError(
'[vue-timers.stop] Cannot find timer ' + name
)
}
if (!data[name].isRunning) return
clearTimer(options[name].repeat)(data[name].instance)
data[name].isRunning = false
vm.$emit('timer-stop:' + name)
},
restart: function(name) {
if (process.env.NODE_ENV !== 'production' && !(name in options)) {
throw new ReferenceError(
'[vue-timers.restart] Cannot find timer ' + name
)
}
this.stop(name)
this.start(name)
vm.$emit('timer-restart:' + name)
}
}
},
mounted: function() {
if (!this.$options.timers) return
var vm = this
var options = vm.$options.timers
Object.keys(options).forEach(function(key) {
if (options[key].autostart) {
vm.$timer.start(key)
}
})
},
activated: function() {
if (!this.$options.timers) return
var vm = this
var data = vm.timers
var options = vm.$options.timers
Object.keys(options).forEach(function(key) {
if (options[key].isSwitchTab && data[key].instance) {
vm.$timer.start(key)
}
})
},
deactivated: function() {
if (!this.$options.timers) return
var vm = this
var data = vm.timers
var options = vm.$options.timers
Object.keys(options).forEach(function(key) {
if (options[key].isSwitchTab && data[key].instance) {
vm.$timer.stop(key)
}
})
},
beforeDestroy: function() {
if (!this.$options.timers) return
var vm = this
Object.keys(vm.$options.timers).forEach(function(key) {
vm.$timer.stop(key)
})
}
}