1
- <template >
2
- <div class =" jv-node" >
3
- <span class =" jv-toggle" :class =" {open: !!expand}" v-if =" keyName && isObject" @click.stop =" toggleNode" ></span >
4
- <span class =" jv-key" v-if =" keyName" >
5
- {{keyName}}:
6
- </span >
7
- <commponent
8
- :expand.sync =" expand"
9
- :is =" `Json${valueType}`"
10
- :json-value =" value"
11
- :key-name =" keyName"
12
- :sort =" sort"
13
- :depth =" depth" ></commponent >
14
- </div >
15
- </template >
16
-
17
1
<script >
18
2
import JsonString from ' ./types/json-string'
19
3
import JsonUndefined from ' ./types/json-undefined'
@@ -27,8 +11,14 @@ export default {
27
11
name: ' JsonBox' ,
28
12
inject: [' expandDepth' ],
29
13
props: {
30
- value: [Object , Array , String , Number , Boolean , Function ],
31
- keyName: String ,
14
+ value: {
15
+ type: [Object , Array , String , Number , Boolean , Function ],
16
+ default: null
17
+ },
18
+ keyName: {
19
+ type: String ,
20
+ default: ' '
21
+ },
32
22
sort: Boolean ,
33
23
depth: {
34
24
type: Number ,
@@ -40,53 +30,89 @@ export default {
40
30
expand: true
41
31
}
42
32
},
33
+ mounted () {
34
+ this .expand = this .depth >= this .expandDepth ? false : true
35
+ },
43
36
methods: {
44
- toggleNode () {
37
+ toggle () {
45
38
this .expand = ! this .expand
46
39
47
40
try {
48
- this .$el .dispatchEvent (new Event (" resized" ))
41
+ this .$el .dispatchEvent (new Event (' resized' ))
49
42
} catch (e) {
50
43
// handle IE not supporting Event constructor
51
- var evt = document .createEvent (" Event" )
52
- evt .initEvent (" resized" , true , false )
44
+ var evt = document .createEvent (' Event' )
45
+ evt .initEvent (' resized' , true , false )
53
46
this .$el .dispatchEvent (evt)
54
47
}
55
48
}
56
49
},
57
- mounted () {
58
- this .expand = this .depth >= this .expandDepth ? false : true
59
- },
60
- computed: {
61
- valueType () {
62
- if (this .value === null || this .value === undefined ) {
63
- return ' Undefined'
64
- } else if (Array .isArray (this .value )) {
65
- return ' Array'
66
- } else if (typeof this .value === ' object' ) {
67
- return ' Object'
68
- } else if (typeof this .value === ' number' ) {
69
- return ' Number'
70
- } else if (typeof this .value === ' string' ) {
71
- return ' String'
72
- } else if (typeof this .value === ' boolean' ) {
73
- return ' Boolean'
74
- } else if (typeof this .value === ' function' ) {
75
- return ' Function'
76
- }
77
- },
78
- isObject () {
79
- return this .valueType == ' Array' || this .valueType == ' Object' ; // eslint-disable-line
50
+ render (h ) {
51
+ let elements = []
52
+ let dataType
53
+
54
+ if (this .value === null || this .value === undefined ) {
55
+ dataType = JsonUndefined
56
+ } else if (Array .isArray (this .value )) {
57
+ dataType = JsonArray
58
+ } else if (typeof this .value === ' object' ) {
59
+ dataType = JsonObject
60
+ } else if (typeof this .value === ' number' ) {
61
+ dataType = JsonNumber
62
+ } else if (typeof this .value === ' string' ) {
63
+ dataType = JsonString
64
+ } else if (typeof this .value === ' boolean' ) {
65
+ dataType = JsonBoolean
66
+ } else if (typeof this .value === ' function' ) {
67
+ dataType = JsonFunction
80
68
}
81
- },
82
- components: {
83
- JsonString,
84
- JsonNumber,
85
- JsonBoolean,
86
- JsonObject,
87
- JsonArray,
88
- JsonFunction,
89
- JsonUndefined
69
+
70
+ if (this .keyName && (this .value && (Array .isArray (this .value ) || typeof this .value === ' object' ))) {
71
+ elements .push (h (' span' , {
72
+ class: {
73
+ ' jv-toggle' : true ,
74
+ open: !! this .expand
75
+ },
76
+ on: {
77
+ click: this .toggle
78
+ }
79
+ }))
80
+ }
81
+
82
+ if (this .keyName ) {
83
+ elements .push (h (' span' , {
84
+ class: {
85
+ ' jv-key' : true
86
+ },
87
+ domProps: {
88
+ innerHTML: ` ${ this .keyName } :`
89
+ }
90
+ }))
91
+ }
92
+
93
+ elements .push (h (dataType, {
94
+ class: {
95
+ ' jv-push' : true
96
+ },
97
+ props: {
98
+ jsonValue: this .value ,
99
+ keyName: this .keyName ,
100
+ sort: this .sort ,
101
+ depth: this .depth ,
102
+ expand: this .expand
103
+ },
104
+ on: {
105
+ ' update:expand ' : (event ) => {
106
+ this .expand = event
107
+ }
108
+ }
109
+ }))
110
+
111
+ return h (' div' , {
112
+ class: {
113
+ ' jv-node' : true
114
+ }
115
+ }, elements)
90
116
}
91
117
}
92
118
</script >
0 commit comments