1 |
|
2 |
|
3 | import { isDef, isObject } from 'shared/util'
|
4 |
|
5 | export function genClassForVnode (vnode: VNodeWithData): string {
|
6 | let data = vnode.data
|
7 | let parentNode = vnode
|
8 | let childNode = vnode
|
9 | while (isDef(childNode.componentInstance)) {
|
10 | childNode = childNode.componentInstance._vnode
|
11 | if (childNode && childNode.data) {
|
12 | data = mergeClassData(childNode.data, data)
|
13 | }
|
14 | }
|
15 | while (isDef(parentNode = parentNode.parent)) {
|
16 | if (parentNode && parentNode.data) {
|
17 | data = mergeClassData(data, parentNode.data)
|
18 | }
|
19 | }
|
20 | return renderClass(data.staticClass, data.class)
|
21 | }
|
22 |
|
23 | function mergeClassData (child: VNodeData, parent: VNodeData): {
|
24 | staticClass: string,
|
25 | class: any
|
26 | } {
|
27 | return {
|
28 | staticClass: concat(child.staticClass, parent.staticClass),
|
29 | class: isDef(child.class)
|
30 | ? [child.class, parent.class]
|
31 | : parent.class
|
32 | }
|
33 | }
|
34 |
|
35 | export function renderClass (
|
36 | staticClass: ?string,
|
37 | dynamicClass: any
|
38 | ): string {
|
39 | if (isDef(staticClass) || isDef(dynamicClass)) {
|
40 | return concat(staticClass, stringifyClass(dynamicClass))
|
41 | }
|
42 |
|
43 | return ''
|
44 | }
|
45 |
|
46 | export function concat (a: ?string, b: ?string): string {
|
47 | return a ? b ? (a + ' ' + b) : a : (b || '')
|
48 | }
|
49 |
|
50 | export function stringifyClass (value: any): string {
|
51 | if (Array.isArray(value)) {
|
52 | return stringifyArray(value)
|
53 | }
|
54 | if (isObject(value)) {
|
55 | return stringifyObject(value)
|
56 | }
|
57 | if (typeof value === 'string') {
|
58 | return value
|
59 | }
|
60 |
|
61 | return ''
|
62 | }
|
63 |
|
64 | function stringifyArray (value: Array<any>): string {
|
65 | let res = ''
|
66 | let stringified
|
67 | for (let i = 0, l = value.length; i < l; i++) {
|
68 | if (isDef(stringified = stringifyClass(value[i])) && stringified !== '') {
|
69 | if (res) res += ' '
|
70 | res += stringified
|
71 | }
|
72 | }
|
73 | return res
|
74 | }
|
75 |
|
76 | function stringifyObject (value: Object): string {
|
77 | let res = ''
|
78 | for (const key in value) {
|
79 | if (value[key]) {
|
80 | if (res) res += ' '
|
81 | res += key
|
82 | }
|
83 | }
|
84 | return res
|
85 | }
|