mirror of
https://github.com/tenrok/vue-json-viewer.git
synced 2026-06-23 20:40:38 +03:00
Support for incremental update components
This commit is contained in:
@@ -37,6 +37,7 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import JsonBox from './json-box'
|
import JsonBox from './json-box'
|
||||||
import Clipboard from 'clipboard'
|
import Clipboard from 'clipboard'
|
||||||
|
import {debounce} from './utils';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'JsonViewer',
|
name: 'JsonViewer',
|
||||||
@@ -92,6 +93,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted: function () {
|
mounted: function () {
|
||||||
|
this.debounceResized = debounce(this.debResized.bind(this), 200);
|
||||||
if (this.boxed && this.$refs.jsonBox) {
|
if (this.boxed && this.$refs.jsonBox) {
|
||||||
this.onResized()
|
this.onResized()
|
||||||
this.$refs.jsonBox.$el.addEventListener("resized", this.onResized, true)
|
this.$refs.jsonBox.$el.addEventListener("resized", this.onResized, true)
|
||||||
@@ -109,6 +111,9 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
onResized () {
|
onResized () {
|
||||||
|
this.debounceResized();
|
||||||
|
},
|
||||||
|
debResized() {
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
if (this.$refs.jsonBox.$el.clientHeight >= 250) {
|
if (this.$refs.jsonBox.$el.clientHeight >= 250) {
|
||||||
this.expandableCode = true
|
this.expandableCode = true
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ import JsonBox from '../json-box'
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'JsonArray',
|
name: 'JsonArray',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
value: []
|
||||||
|
}
|
||||||
|
},
|
||||||
props: {
|
props: {
|
||||||
jsonValue: {
|
jsonValue: {
|
||||||
type: Array,
|
type: Array,
|
||||||
@@ -21,7 +26,7 @@ export default {
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
ordered () {
|
ordered () {
|
||||||
let value = this.jsonValue
|
let value = this.value
|
||||||
|
|
||||||
if (!this.sort) {
|
if (!this.sort) {
|
||||||
return value
|
return value
|
||||||
@@ -30,7 +35,27 @@ export default {
|
|||||||
return value.sort()
|
return value.sort()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
jsonValue(newVal) {
|
||||||
|
this.setValue(newVal);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this.setValue(this.jsonValue);
|
||||||
|
},
|
||||||
methods: {
|
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() {
|
toggle() {
|
||||||
this.$emit('update:expand', !this.expand)
|
this.$emit('update:expand', !this.expand)
|
||||||
|
|
||||||
@@ -42,7 +67,8 @@ export default {
|
|||||||
evt.initEvent('resized', true, false)
|
evt.initEvent('resized', true, false)
|
||||||
this.$el.dispatchEvent(evt)
|
this.$el.dispatchEvent(evt)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
},
|
},
|
||||||
render (h) {
|
render (h) {
|
||||||
let elements = []
|
let elements = []
|
||||||
@@ -84,7 +110,7 @@ export default {
|
|||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!this.expand && this.jsonValue.length) {
|
if (!this.expand && this.value.length) {
|
||||||
elements.push(h('span', {
|
elements.push(h('span', {
|
||||||
style: {
|
style: {
|
||||||
display: undefined
|
display: undefined
|
||||||
@@ -96,7 +122,7 @@ export default {
|
|||||||
click: this.toggle
|
click: this.toggle
|
||||||
},
|
},
|
||||||
attrs: {
|
attrs: {
|
||||||
title: `click to reveal ${this.jsonValue.length} hidden items`
|
title: `click to reveal ${this.value.length} hidden items`
|
||||||
},
|
},
|
||||||
domProps: {
|
domProps: {
|
||||||
innerHTML: '...'
|
innerHTML: '...'
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ import JsonBox from '../json-box'
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'JsonObject',
|
name: 'JsonObject',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
value: {}
|
||||||
|
}
|
||||||
|
},
|
||||||
props: {
|
props: {
|
||||||
jsonValue: {
|
jsonValue: {
|
||||||
type: Object,
|
type: Object,
|
||||||
@@ -22,20 +27,35 @@ export default {
|
|||||||
computed: {
|
computed: {
|
||||||
ordered () {
|
ordered () {
|
||||||
if (!this.sort) {
|
if (!this.sort) {
|
||||||
return this.jsonValue
|
return this.value
|
||||||
}
|
}
|
||||||
|
|
||||||
const ordered = {}
|
const ordered = {}
|
||||||
Object.keys(this.jsonValue).forEach(key => {
|
Object.keys(this.value).sort().forEach(key => {
|
||||||
ordered[key] = this.jsonValue[key]
|
ordered[key] = this.value[key]
|
||||||
})
|
})
|
||||||
return ordered
|
return ordered
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
jsonValue(newVal) {
|
||||||
|
this.setValue(newVal);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this.setValue(this.jsonValue);
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
setValue(val) {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.value = val;
|
||||||
|
}, 0);
|
||||||
|
},
|
||||||
toggle() {
|
toggle() {
|
||||||
this.$emit('update:expand', !this.expand)
|
this.$emit('update:expand', !this.expand)
|
||||||
|
this.dispatchEvent();
|
||||||
|
},
|
||||||
|
dispatchEvent() {
|
||||||
try {
|
try {
|
||||||
this.$el.dispatchEvent(new Event('resized'))
|
this.$el.dispatchEvent(new Event('resized'))
|
||||||
} catch (e) {
|
} 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', {
|
elements.push(h('span', {
|
||||||
style: {
|
style: {
|
||||||
display: this.expand ? 'none' : undefined
|
display: this.expand ? 'none' : undefined
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
export const debounce = function(func, wait) {
|
||||||
|
let startTime = Date.now();
|
||||||
|
let timer;
|
||||||
|
|
||||||
|
return (...args) => {
|
||||||
|
if (Date.now() - startTime < wait && timer) {
|
||||||
|
clearTimeout(timer);
|
||||||
|
}
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
func(...args);
|
||||||
|
}, wait);
|
||||||
|
startTime = Date.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user