This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
/
SerializedEditor.vue
112 lines (96 loc) · 2.39 KB
/
SerializedEditor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<template>
<div class="serialized-editor" v-if="jsonDataIsValid">
<div class="btn-group se-expand-collapse-btns" role="group" v-if="jsonData.type == 'a'">
<button class="btn btn-default se-expand-all" @click="expandAll">Expand All</button>
<button class="btn btn-default se-collapse-all" @click="collapseAll">Collapse All</button>
</div>
<array :item="jsonData" :expanded="true" v-if="jsonData.type == 'a'"></array>
<object-item :item="jsonData" :expanded="true" v-else-if="jsonData.type.toLowerCase() == 'o'"></object-item>
<edit-value :item="jsonData" v-else></edit-value>
</div>
</template>
<style>
.se-expand-collapse-btns {
margin-bottom: 20px;
}
</style>
<script>
import Vue from 'vue';
import Array from './components/Array.vue';
import ObjectItem from './components/ObjectItem.vue';
import EditValue from './components/EditValue.vue';
import { EventBus } from './util/EventBus';
export default {
props: {
jsonData: {
type: Object,
required: true
}
},
data() {
return {
allComponents: []
}
},
computed: {
jsonDataIsValid() {
return this.jsonData && Object.keys(this.jsonData).length !== 0;
}
},
created() {
EventBus.$on(['value-updated', 'array-item-added', 'array-item-removed'], () => {
Vue.nextTick(() => {
this.generateOutput();
});
});
},
mounted() {
this.generateOutput();
},
methods: {
generateOutput() {
this.allComponents = [];
this.findChildren(this);
var output = this.allComponents.join('');
this.$emit('output', output);
return output;
},
findChildren(component) {
if (component.$children) {
if (component.rawOutput) {
this.allComponents.push(component.rawOutput);
}
component.$children.forEach(child => {
// Don't include ObjectItem's name <edit-value> in the list of array values
if (child.isObject) {
return;
}
this.findChildren(child);
});
if (component.values) {
// Append closing bracket for arrays/objects
this.allComponents.push('}');
}
}
},
expandAll() {
EventBus.$emit('expand-all');
},
collapseAll() {
EventBus.$emit('collapse-all');
}
},
watch: {
jsonData(newValue) {
Vue.nextTick(() => {
this.generateOutput();
});
}
},
components: {
'array': Array,
'object-item': ObjectItem,
'edit-value': EditValue
}
}
</script>