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

Add ability to expand full branch

This commit is contained in:
Maksim Lanin
2021-12-17 16:43:16 +03:00
parent 683909e61e
commit 6c1da0769a
4 changed files with 74 additions and 24 deletions
+26 -4
View File
@@ -26,19 +26,30 @@ export default {
default: 0 default: 0
}, },
previewMode: Boolean, previewMode: Boolean,
forceExpand: Boolean,
}, },
data() { data() {
return { return {
expand: true expand: true,
forceExpandMe: this.forceExpand,
} }
}, },
mounted() { mounted() {
this.expand = this.previewMode || (this.depth >= this.expandDepth ? false : true) this.expand = this.previewMode || (this.depth >= this.expandDepth ? false : true) || this.forceExpandMe
}, },
methods: { methods: {
toggle() { toggle() {
this.expand = !this.expand this.expand = !this.expand
this.dispatchEvent()
},
toggleAll() {
this.expand = !this.expand
this.forceExpandMe = this.expand
this.dispatchEvent()
},
dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
} catch (e) { } catch (e) {
@@ -64,7 +75,7 @@ export default {
let elements = [] let elements = []
let dataType let dataType
if (this.value === null || this.value === undefined) { if (this.value === null || this.value === undefined) {
dataType = JsonUndefined dataType = JsonUndefined
} else if (Array.isArray(this.value)) { } else if (Array.isArray(this.value)) {
dataType = JsonArray dataType = JsonArray
@@ -90,7 +101,13 @@ export default {
open: !!this.expand open: !!this.expand
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -117,10 +134,15 @@ export default {
depth: this.depth, depth: this.depth,
expand: this.expand, expand: this.expand,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpandMe
}, },
on: { on: {
'update:expand': value => { 'update:expand': value => {
this.expand = value this.expand = value
},
'update:expandAll': value => {
this.expand = value
this.forceExpandMe = this.expand
} }
} }
})) }))
+4 -1
View File
@@ -1,5 +1,8 @@
<template> <template>
<div ref="viewer" :class="jvClass"> <div
ref="viewer"
:class="jvClass"
>
<div <div
v-if="copyable" v-if="copyable"
:class="`jv-tooltip ${copyText.align || 'right'}`" :class="`jv-tooltip ${copyText.align || 'right'}`"
+24 -11
View File
@@ -18,6 +18,7 @@ export default {
}, },
sort: Boolean, sort: Boolean,
expand: Boolean, expand: Boolean,
forceExpand: Boolean,
previewMode: Boolean, previewMode: Boolean,
}, },
data() { data() {
@@ -47,7 +48,13 @@ export default {
}, },
toggle() { toggle() {
this.$emit('update:expand', !this.expand) this.$emit('update:expand', !this.expand)
this.dispatchEvent();
},
toggleAll() {
this.$emit('update:expandAll', !this.expand)
this.dispatchEvent();
},
dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
} catch (e) { } catch (e) {
@@ -56,8 +63,7 @@ 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 = []
@@ -69,7 +75,13 @@ export default {
'open': !!this.expand, 'open': !!this.expand,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -87,15 +99,13 @@ export default {
this.value.forEach((value, key) => { this.value.forEach((value, key) => {
elements.push(h(JsonBox, { elements.push(h(JsonBox, {
key, key,
style: {
display: this.expand ? undefined : 'none'
},
props: { props: {
sort: this.sort, sort: this.sort,
keyName: `${key}`, keyName: `${key}`,
depth: this.depth + 1, depth: this.depth + 1,
value, value,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpand,
} }
})) }))
}) })
@@ -103,14 +113,17 @@ export default {
if (!this.expand && this.value.length) { if (!this.expand && this.value.length) {
elements.push(h('span', { elements.push(h('span', {
style: {
display: undefined
},
class: { class: {
'jv-ellipsis': true, 'jv-ellipsis': true,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
}, },
attrs: { attrs: {
title: `click to reveal ${this.value.length} hidden items` title: `click to reveal ${this.value.length} hidden items`
+20 -8
View File
@@ -17,6 +17,7 @@ export default {
default: 0 default: 0
}, },
expand: Boolean, expand: Boolean,
forceExpand: Boolean,
sort: Boolean, sort: Boolean,
previewMode: Boolean, previewMode: Boolean,
}, },
@@ -56,6 +57,10 @@ export default {
this.$emit('update:expand', !this.expand) this.$emit('update:expand', !this.expand)
this.dispatchEvent(); this.dispatchEvent();
}, },
toggleAll() {
this.$emit('update:expandAll', !this.expand)
this.dispatchEvent();
},
dispatchEvent() { dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
@@ -77,7 +82,13 @@ export default {
'open': !!this.expand, 'open': !!this.expand,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -99,15 +110,13 @@ export default {
elements.push(h(JsonBox, { elements.push(h(JsonBox, {
key, key,
style: {
display: !this.expand ? 'none' : undefined
},
props: { props: {
sort: this.sort, sort: this.sort,
keyName: key, keyName: key,
depth: this.depth + 1, depth: this.depth + 1,
value, value,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpand,
} }
})) }))
} }
@@ -116,14 +125,17 @@ export default {
if (!this.expand && Object.keys(this.value).length) { if (!this.expand && Object.keys(this.value).length) {
elements.push(h('span', { elements.push(h('span', {
style: {
display: this.expand ? 'none' : undefined
},
class: { class: {
'jv-ellipsis': true, 'jv-ellipsis': true,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
}, },
attrs: { attrs: {
title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})` title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})`