@@ -2,74 +2,75 @@ import {
2
2
addClass ,
3
3
removeClass ,
4
4
isArray ,
5
- isPlainObject
5
+ isObject
6
6
} from '../../util/index'
7
7
8
8
export default {
9
9
10
10
deep : true ,
11
11
12
12
update ( value ) {
13
- if ( value && typeof value === 'string' ) {
14
- this . handleObject ( stringToObject ( value ) )
15
- } else if ( isPlainObject ( value ) ) {
16
- this . handleObject ( value )
17
- } else if ( isArray ( value ) ) {
18
- this . handleArray ( value )
19
- } else {
13
+ if ( ! value ) {
20
14
this . cleanup ( )
15
+ } else if ( typeof value === 'string' ) {
16
+ this . setClass ( value . trim ( ) . split ( / \s + / ) )
17
+ } else {
18
+ this . setClass ( normalize ( value ) )
21
19
}
22
20
} ,
23
21
24
- handleObject ( value ) {
25
- this . cleanup ( value )
26
- this . prevKeys = Object . keys ( value )
27
- setObjectClasses ( this . el , value )
28
- } ,
29
-
30
- handleArray ( value ) {
22
+ setClass ( value ) {
31
23
this . cleanup ( value )
32
24
for ( var i = 0 , l = value . length ; i < l ; i ++ ) {
33
25
var val = value [ i ]
34
- if ( val && isPlainObject ( val ) ) {
35
- setObjectClasses ( this . el , val )
36
- } else if ( val && typeof val === 'string' ) {
37
- addClass ( this . el , val )
26
+ if ( val ) {
27
+ apply ( this . el , val , addClass )
38
28
}
39
29
}
40
- this . prevKeys = value . slice ( )
30
+ this . prevKeys = value
41
31
} ,
42
32
43
33
cleanup ( value ) {
44
- if ( ! this . prevKeys ) return
45
-
46
- var i = this . prevKeys . length
34
+ const prevKeys = this . prevKeys
35
+ if ( ! prevKeys ) return
36
+ var i = prevKeys . length
47
37
while ( i -- ) {
48
- var key = this . prevKeys [ i ]
38
+ var key = prevKeys [ i ]
49
39
if ( ! key ) continue
50
-
51
- var keys = isPlainObject ( key ) ? Object . keys ( key ) : [ key ]
52
- for ( var j = 0 , l = keys . length ; j < l ; j ++ ) {
53
- toggleClasses ( this . el , keys [ j ] , removeClass )
40
+ if ( key && ( ! value || value . indexOf ( key ) < 0 ) ) {
41
+ apply ( this . el , key , removeClass )
54
42
}
55
43
}
56
44
}
57
45
}
58
46
59
- function setObjectClasses ( el , obj ) {
60
- var keys = Object . keys ( obj )
61
- for ( var i = 0 , l = keys . length ; i < l ; i ++ ) {
62
- var key = keys [ i ]
63
- if ( ! obj [ key ] ) continue
64
- toggleClasses ( el , key , addClass )
65
- }
66
- }
47
+ /**
48
+ * Normalize objects and arrays (potentially containing objects)
49
+ * into array of strings.
50
+ *
51
+ * @param {Object|Array<String|Object> } value
52
+ * @return {Array<String> }
53
+ */
67
54
68
- function stringToObject ( value ) {
69
- var res = { }
70
- var keys = value . trim ( ) . split ( / \s + / )
71
- for ( var i = 0 , l = keys . length ; i < l ; i ++ ) {
72
- res [ keys [ i ] ] = true
55
+ function normalize ( value ) {
56
+ const res = [ ]
57
+ if ( isArray ( value ) ) {
58
+ for ( var i = 0 , l = value . length ; i < l ; i ++ ) {
59
+ const key = value [ i ]
60
+ if ( key ) {
61
+ if ( typeof key === 'string' ) {
62
+ res . push ( key )
63
+ } else {
64
+ for ( var k in key ) {
65
+ if ( key [ k ] ) res . push ( k )
66
+ }
67
+ }
68
+ }
69
+ }
70
+ } else if ( isObject ( value ) ) {
71
+ for ( var key in value ) {
72
+ if ( value [ key ] ) res . push ( key )
73
+ }
73
74
}
74
75
return res
75
76
}
@@ -85,14 +86,12 @@ function stringToObject (value) {
85
86
* @param {Function } fn
86
87
*/
87
88
88
- function toggleClasses ( el , key , fn ) {
89
+ function apply ( el , key , fn ) {
89
90
key = key . trim ( )
90
-
91
91
if ( key . indexOf ( ' ' ) === - 1 ) {
92
92
fn ( el , key )
93
93
return
94
94
}
95
-
96
95
// The key contains one or more space characters.
97
96
// Since a class name doesn't accept such characters, we
98
97
// treat it as multiple classes.
0 commit comments