2
0
mirror of https://github.com/tenrok/vue-json-viewer.git synced 2026-06-20 20:00:37 +03:00

Support for incremental update components

This commit is contained in:
陈峰
2019-06-12 19:37:52 +08:00
parent 6e7c4eb113
commit 6b09e02ac8
4 changed files with 74 additions and 9 deletions
+30 -4
View File
@@ -3,6 +3,11 @@ import JsonBox from '../json-box'
export default {
name: 'JsonArray',
data() {
return {
value: []
}
},
props: {
jsonValue: {
type: Array,
@@ -21,7 +26,7 @@ export default {
},
computed: {
ordered () {
let value = this.jsonValue
let value = this.value
if (!this.sort) {
return value
@@ -30,7 +35,27 @@ export default {
return value.sort()
}
},
watch: {
jsonValue(newVal) {
this.setValue(newVal);
}
},
mounted() {
this.setValue(this.jsonValue);
},
methods: {
setValue(vals, index = 0) {
if (index === 0) {
this.value = [];
}
setTimeout(() => {
this.value.push(vals[index]);
if (vals[index + 1]) {
this.setValue(vals, index + 1);
}
}, 0);
},
toggle() {
this.$emit('update:expand', !this.expand)
@@ -42,7 +67,8 @@ export default {
evt.initEvent('resized', true, false)
this.$el.dispatchEvent(evt)
}
}
},
},
render (h) {
let elements = []
@@ -84,7 +110,7 @@ export default {
}))
})
if (!this.expand && this.jsonValue.length) {
if (!this.expand && this.value.length) {
elements.push(h('span', {
style: {
display: undefined
@@ -96,7 +122,7 @@ export default {
click: this.toggle
},
attrs: {
title: `click to reveal ${this.jsonValue.length} hidden items`
title: `click to reveal ${this.value.length} hidden items`
},
domProps: {
innerHTML: '...'
+25 -5
View File
@@ -3,6 +3,11 @@ import JsonBox from '../json-box'
export default {
name: 'JsonObject',
data() {
return {
value: {}
}
},
props: {
jsonValue: {
type: Object,
@@ -22,20 +27,35 @@ export default {
computed: {
ordered () {
if (!this.sort) {
return this.jsonValue
return this.value
}
const ordered = {}
Object.keys(this.jsonValue).forEach(key => {
ordered[key] = this.jsonValue[key]
Object.keys(this.value).sort().forEach(key => {
ordered[key] = this.value[key]
})
return ordered
}
},
watch: {
jsonValue(newVal) {
this.setValue(newVal);
}
},
mounted() {
this.setValue(this.jsonValue);
},
methods: {
setValue(val) {
setTimeout(() => {
this.value = val;
}, 0);
},
toggle() {
this.$emit('update:expand', !this.expand)
this.dispatchEvent();
},
dispatchEvent() {
try {
this.$el.dispatchEvent(new Event('resized'))
} catch (e) {
@@ -90,7 +110,7 @@ export default {
}
}
if (!this.expand && Object.keys(this.jsonValue).length) {
if (!this.expand && Object.keys(this.value).length) {
elements.push(h('span', {
style: {
display: this.expand ? 'none' : undefined