-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.js
70 lines (64 loc) · 1.54 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
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
var _ = require('../util')
var addClass = _.addClass
var removeClass = _.removeClass
module.exports = {
bind: function () {
// interpolations like class="{{abc}}" are converted
// to v-class, and we need to remove the raw,
// uninterpolated className at binding time.
var raw = this._descriptor._rawClass
if (raw) {
this.prevKeys = raw.trim().split(/\s+/)
}
},
update: function (value) {
if (this.arg) {
// single toggle
if (value) {
addClass(this.el, this.arg)
} else {
removeClass(this.el, this.arg)
}
} else {
if (value && typeof value === 'string') {
this.handleObject(stringToObject(value))
} else if (_.isPlainObject(value)) {
this.handleObject(value)
} else {
this.cleanup()
}
}
},
handleObject: function (value) {
this.cleanup(value)
var keys = this.prevKeys = Object.keys(value)
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i]
if (value[key]) {
addClass(this.el, key)
} else {
removeClass(this.el, key)
}
}
},
cleanup: function (value) {
if (this.prevKeys) {
var i = this.prevKeys.length
while (i--) {
var key = this.prevKeys[i]
if (!value || !value.hasOwnProperty(key)) {
removeClass(this.el, key)
}
}
}
}
}
function stringToObject (value) {
var res = {}
var keys = value.trim().split(/\s+/)
var i = keys.length
while (i--) {
res[keys[i]] = true
}
return res
}