-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.js
101 lines (92 loc) · 2.1 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
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
import {
addClass,
removeClass,
isArray,
isObject
} from '../../util/index'
export default {
deep: true,
update (value) {
if (!value) {
this.cleanup()
} else if (typeof value === 'string') {
this.setClass(value.trim().split(/\s+/))
} else {
this.setClass(normalize(value))
}
},
setClass (value) {
this.cleanup(value)
for (var i = 0, l = value.length; i < l; i++) {
var val = value[i]
if (val) {
apply(this.el, val, addClass)
}
}
this.prevKeys = value
},
cleanup (value) {
const prevKeys = this.prevKeys
if (!prevKeys) return
var i = prevKeys.length
while (i--) {
var key = prevKeys[i]
if (!value || value.indexOf(key) < 0) {
apply(this.el, key, removeClass)
}
}
}
}
/**
* Normalize objects and arrays (potentially containing objects)
* into array of strings.
*
* @param {Object|Array<String|Object>} value
* @return {Array<String>}
*/
function normalize (value) {
const res = []
if (isArray(value)) {
for (var i = 0, l = value.length; i < l; i++) {
const key = value[i]
if (key) {
if (typeof key === 'string') {
res.push(key)
} else {
for (var k in key) {
if (key[k]) res.push(k)
}
}
}
}
} else if (isObject(value)) {
for (var key in value) {
if (value[key]) res.push(key)
}
}
return res
}
/**
* Add or remove a class/classes on an element
*
* @param {Element} el
* @param {String} key The class name. This may or may not
* contain a space character, in such a
* case we'll deal with multiple class
* names at once.
* @param {Function} fn
*/
function apply (el, key, fn) {
key = key.trim()
if (key.indexOf(' ') === -1) {
fn(el, key)
return
}
// The key contains one or more space characters.
// Since a class name doesn't accept such characters, we
// treat it as multiple classes.
var keys = key.split(/\s+/)
for (var i = 0, l = keys.length; i < l; i++) {
fn(el, keys[i])
}
}