forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.js
45 lines (42 loc) · 1.02 KB
/
class.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
var _ = require('../util')
var addClass = _.addClass
var removeClass = _.removeClass
module.exports = {
update: function (value) {
if (this.arg) {
// single toggle
var method = value ? addClass : removeClass
method(this.el, this.arg)
} else {
this.cleanup()
if (value && typeof value === 'string') {
// raw class text
addClass(this.el, value)
this.lastVal = value
} else if (_.isPlainObject(value)) {
// object toggle
for (var key in value) {
if (value[key]) {
addClass(this.el, key)
} else {
removeClass(this.el, key)
}
}
this.prevKeys = Object.keys(value)
}
}
},
cleanup: function (value) {
if (this.lastVal) {
removeClass(this.el, this.lastVal)
}
if (this.prevKeys) {
var i = this.prevKeys.length
while (i--) {
if (!value || !value[this.prevKeys[i]]) {
removeClass(this.el, this.prevKeys[i])
}
}
}
}
}