Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'show_all', 'hide_all' methods on top level renderjson element #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions renderjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,26 @@ var module, window, define, renderjson=(function() {
a.onclick = function(e) { callback(); if (e) e.stopPropagation(); return false; };
return a; };

var walkDOM = function(node, func){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting function, and use case. But not all json object graphs are tree. We explicitly, turn the tree back into a graph (for complex DTO object graphs and json schema), before rendering them.
https://github.com/andifreed/renderjson/blob/50a85654206671137e397d9d92996a94f4e9b06a/resolvejson.js

But there may be other graphs passed to this renderer, you can make it impervious to such infinite recursion by adding a array|stack parameter and checking that the object hasn't been seen yet before descending. Besure to put before descending and pop after returning.

func(node);
node = node.firstChild;
while(node) {
walkDOM(node,func);
node = node.nextSibling;
}
}

function _renderjson(json, indent, dont_indent, show_level, options) {
var my_indent = dont_indent ? "" : indent;

var disclosure = function(open, placeholder, close, type, builder) {
var content;
var empty = span(type);
var hide = function() { content.style.display="none";
empty.style.display="inline"; }
var show = function() { if (!content) append(empty.parentNode,
content = prepend(builder(),
A(options.hide, "disclosure",
function() { content.style.display="none";
empty.style.display="inline"; } )));
A(options.hide, "disclosure", hide)));
content.style.display="inline";
empty.style.display="none"; };
append(empty,
Expand All @@ -119,6 +128,8 @@ var module, window, define, renderjson=(function() {
var el = append(span(), text(my_indent.slice(0,-1)), empty);
if (show_level > 0 && type != "string")
show();
el.show = show;
el.hide = hide;
return el;
};

Expand Down Expand Up @@ -177,6 +188,19 @@ var module, window, define, renderjson=(function() {
options.replacer = typeof(options.replacer) == "function" ? options.replacer : function(k,v) { return v; };
var pre = append(document.createElement("pre"), _renderjson(json, "", false, options.show_to_level, options));
pre.className = "renderjson";

pre.show_all = function(){
walkDOM(pre, function(node){
if(node.show) node.show()
})
}

pre.hide_all = function(){
walkDOM(pre, function(node){
if(node.hide) node.hide()
})
}

return pre;
}
renderjson.set_icons = function(show, hide) { renderjson.options.show = show;
Expand Down