-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject-preview.vue
98 lines (93 loc) · 2.59 KB
/
object-preview.vue
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
<template>
<span
><object-value v-if="type === Type.simple" :object="object" /><span
v-if="type === Type.array"
><span class="object-preview-desc">{{
object.length === 0 ? '' : `(${object.length})\xa0`
}}</span
><span class="object-preview"
>[<span v-for="(item, index) of previewArray" :key="index"
><span v-if="index != 0">, </span><object-value :object="item" /><span
v-if="index === arrayMaxProperties - 1"
>, </span
><span v-if="index === arrayMaxProperties - 1">…</span></span
>]</span
> </span
><span v-if="type === Type.other"
><span class="object-preview-desc">{{ desc }}</span
><span class="object-preview"
>{<span v-for="(item, index) of previewObjectItems" :key="index"
><span v-if="index != 0">, </span
><span class="object-name">{{ item.key || `""` }}</span
>: <object-value :object="item.val" /><span
v-if="index === objectMaxProperties - 1"
>…</span
></span
>}</span
>
</span>
</span>
</template>
<script>
import objectValue from './object-value.vue'
const Type = {
simple: 'simple',
array: 'array',
other: 'other',
}
export default {
name: 'object-preview',
components: { objectValue },
// `provide` from top component `object-inspector`
inject: ['objectMaxProperties', 'arrayMaxProperties'],
props: ['data'],
data() {
return {
Type,
}
},
computed: {
object() {
return this.data
},
type() {
if (
typeof this.object !== 'object' ||
this.object === null ||
this.object instanceof Date ||
this.object instanceof RegExp
) {
return Type.simple
}
if (Array.isArray(this.object)) {
return Type.array
}
return Type.other
},
desc() {
const objectContructorName = this.object.constructor
? this.object.constructor.name
: 'Object'
return objectContructorName === 'Object' ? '' : `${objectContructorName} `
},
previewArray() {
return this.object.slice(0, this.arrayMaxProperties)
},
previewObjectItems() {
const keys = Object.keys(this.object)
if (keys.length > this.objectMaxProperties) {
return keys.slice(0, this.objectMaxProperties).map((k) => {
return { key: k, val: this.object[k] }
})
} else {
return Object.keys(this.object).map((k) => {
return {
key: k,
val: this.object[k],
}
})
}
},
},
}
</script>