diff --git a/src/ui/TreeView.js b/src/ui/TreeView.js
index 49df208ce9..9d830e2c3c 100644
--- a/src/ui/TreeView.js
+++ b/src/ui/TreeView.js
@@ -10,6 +10,8 @@ class TreeView {
}
this.beforeJsonNodeRendered = opts.beforeJsonNodeRendered || noop
this.beforeJsonValueRendered = opts.beforeJsonValueRendered || noop
+ this.extractData = opts.extractData || this.extractDataDefault
+ this.formatData = opts.formatData || this.formatDataDefault
this.view = null
this.cssLabel = ui.formatCss(opts.css || {}, style.label)
this.cssList = ui.formatCss(opts.css || {}, style.list)
@@ -34,36 +36,50 @@ class TreeView {
}
renderObject (item, key, expand) {
- var label
- if (item instanceof Array || item instanceof Object) {
- var properties = this.renderProperties(item, false)
- label = yo``
- var list = yo`
${properties}`
- list.style.display = expand ? 'block' : 'none'
+ var data = this.extractData(item, key)
+ var children = Object.keys(data.children).map((innerkey) => {
+ return this.renderObject(data.children[innerkey], innerkey, expand)
+ })
+ return this.formatData(key, data.self, children, expand)
+ }
+
+
+ renderProperties (json, expand) {
+ var children = Object.keys(json).map((innerkey) => {
+ return this.renderObject(json[innerkey], innerkey, expand)
+ })
+ return yo`- ${children}
`
+ }
+
+ formatDataDefault (key, self, children, expand) {
+ var label = yo``
+ var renderedChildren = ''
+ if (children.length) {
+ renderedChildren = yo``
+ renderedChildren.style.display = expand ? 'block' : 'none'
label.firstElementChild.className = expand ? 'fa fa-caret-down' : 'fa fa-caret-right'
label.onclick = function () {
this.firstElementChild.className = this.firstElementChild.className === 'fa fa-caret-right' ? 'fa fa-caret-down' : 'fa fa-caret-right'
var list = this.parentElement.querySelector('ul')
list.style.display = list.style.display === 'none' ? 'block' : 'none'
}
- if (this.beforeJsonNodeRendered) {
- this.beforeJsonNodeRendered(label, item, key)
- }
- return yo`- ${label}${list}
`
- } else {
- label = yo``
- if (this.beforeJsonValueRendered) {
- this.beforeJsonValueRendered(label, item, key)
- }
- return yo`- ${label}: ${item}
`
}
+ return yo`- ${label}${renderedChildren}
`
}
- renderProperties (json, expand) {
- var properties = Object.keys(json).map((innerkey) => {
- return this.renderObject(json[innerkey], innerkey, expand)
- })
- return properties
+ extractDataDefault (item, key) {
+ var ret = {}
+ if (item instanceof Array) {
+ ret.children = item
+ ret.self = 'Array'
+ } else if (item instanceof Object) {
+ ret.children = item
+ ret.self = 'Object'
+ } else {
+ ret.self = item
+ ret.children = []
+ }
+ return ret
}
}