coffeescript-ui
Version:
Coffeescript User Interface System
140 lines (117 loc) • 3.01 kB
text/coffeescript
class CUI.ObjectDumperNode extends CUI.ListViewTreeNode
initOpts: ->
super()
key:
default: "root"
mandatory: true
data: {}
do_open:
mandatory: true
default: false
check: Boolean
parse_json:
mandatory: true
default: false
check: Boolean
readOpts: ->
super()
=
if == "root"
=
if not and
= true
setData: (data) ->
if and CUI.util.isString(data)
try
= JSON.parse(data)
if CUI.util.isString()
= data
catch e
= data
else
= data
@
getData: ->
getChildren: ->
isLeaf: ->
not .has_children
renderContent: ->
if or not
label = new CUI.Label
text: .text
text_node_func: =>
if not .cls == "String"
return .text
return CUI.Label.parseLinks(.text)
multiline: true
CUI.dom.setAttribute(label, "data-type", .cls)
new CUI.Label(
class: "cui-object-dumper-node-key"
text:
multiline: true
)
getClass: ->
cls = super()
cls + " cui-object-dumper-node-" + .cls.toLowerCase()
getInfoFromData: (data) ->
info = {}
if data == undefined
info.cls = "undefined"
info.text = "undefined"
else if data == null
info.cls = "null"
info.text = "null"
else if data == true
info.cls = "boolean"
info.text = "true"
else if data == false
info.cls = "boolean"
info.text = "false"
else if typeof(data) == "number" and isNaN(data)
info.cls = "NaN"
info.text = "NaN"
else if CUI.util.isNumber(data)
info.cls = "number"
info.text = ""+data
else if CUI.util.isString(data)
info.cls = "string"
info.text = data
else
info.cls = CUI.util.getObjectClass(data)
if CUI.util.isArray(data) or CUI.util.isPlainObject(data)
info.has_children = not CUI.util.isEmptyObject(data)
if CUI.util.isArray(data)
info.text = info.cls + " [" + data.length + "]"
if CUI.util.isPlainObject(data)
info.text = info.cls + " {" + Object.keys(data).length + "}"
else
info.has_children = true
info.text = info.cls
info
getNodesFromData: (data) ->
nodes = []
info =
if info.cls == "Array"
for idx in [0...data.length]
nodes.push(new CUI.ObjectDumperNode(key: idx, data: data[idx], do_open: , parse_json: ))
else
keys = []
for k of data
keys.push(k)
keys.sort (a, b) ->
a.localeCompare(b)
for k in keys
v = data[k]
nodes.push(new CUI.ObjectDumperNode(key: k, data: v, do_open: , parse_json: ))
nodes