diff --git a/index.js b/index.js index 4259cb7..807e9c0 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,20 @@ function formatContainer(container) { container.className = 'container'; return container; } +/** + * Formats Root UL element - can be overwritten in options + * @param: UL element + * @return: formatted UL element + */ +function formatRootUl(ul) { + ul.className += ' ul-root-item'; + return ul; +} + /** * Formats UL element - can be overwritten in options * @param: UL element - * @return: formatted LI element + * @return: formatted UL element */ function formatUl(ul) { ul.className = 'ul-item'; @@ -60,7 +70,8 @@ var _options = { formatUl: formatUl, formatLi: formatLi, formatProperty: formatProperty, - formatValue: formatValue + formatValue: formatValue, + formatRootUl:formatRootUl }; function JSON2HTMLList(json, options) { @@ -137,8 +148,13 @@ function JSON2HTMLList(json, options) { } } - + walk(json, container); + + if(container.children.length > 0){ + container.children[0] = _options.formatRootUl(container.children[0]) + } + return container; diff --git a/sample.html b/sample.html index 2891ed0..940b6f1 100644 --- a/sample.html +++ b/sample.html @@ -32,6 +32,17 @@

Output

(function() { + var options = { + formatUl: function(ul) { + ul.className = 'my-ul-class'; + return ul; + }, + formatRootUl:function(ul){ + ul.className = ul.className+" ul-root" + return ul; + } + } + var myJSON = { "glossary": { "title": "example glossary", @@ -57,7 +68,7 @@

Output

document.getElementById('input').appendChild(document.createTextNode(JSON.stringify(myJSON, null, 4))); -document.getElementById('output').appendChild(JSON2HTMLList(myJSON)); +document.getElementById('output').appendChild(JSON2HTMLList(myJSON, options)); })();