From 668c082e4cbb4ab89baec4756aa566eb3abb10b6 Mon Sep 17 00:00:00 2001 From: Mo Morsi Date: Fri, 19 Jun 2020 14:40:55 -0400 Subject: [PATCH] Add 'show_all', 'hide_all' methods on top level renderjson element When called, these will walk down the renderjson DOM structure and invoke 'show' and 'hide' on all the appropriate elements. Use like so: const rj = renderjson({ js : 'on }) document.getElementById("test").appendChild(rj); rj.show_all(); rj.hide_all(); // ^ wire these up to a button click, etc --- renderjson.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/renderjson.js b/renderjson.js index 9d41d2d..8f301b9 100644 --- a/renderjson.js +++ b/renderjson.js @@ -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){ + 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, @@ -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; }; @@ -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;