UNPKG

json-tree-viewer

Version:

json formatter/viewer/pretty-printer (with jsonTree javascript-library)

1 lines 7.3 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var o in n)("object"==typeof exports?exports:e)[o]=n[o]}}(window,function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var s=t[o]={i:o,l:!1,exports:{}};return e[o].call(s.exports,s,s.exports,n),s.l=!0,s.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:o})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t){var n=function(){var e,t={getClass:function(e){return Object.prototype.toString.call(e)},getType:function(e){if(null===e)return"null";switch(typeof e){case"number":return"number";case"string":return"string";case"boolean":return"boolean"}switch(t.getClass(e)){case"[object Array]":return"array";case"[object Object]":return"object"}throw new Error("Bad type: "+t.getClass(e))},forEachNode:function(e,n){var o;switch(t.getType(e)){case"array":o=e.length-1,e.forEach(function(e,t){n(t,e,t===o)});break;case"object":var s=Object.keys(e).sort();o=s.length-1,s.forEach(function(t,s){n(t,e[t],s===o)})}},inherits:(e=function(){},function(t,n){e.prototype=n.prototype,t.prototype=new e,t.prototype.constructor=t}),isValidRoot:function(e){switch(t.getType(e)){case"object":case"array":return!0;default:return!1}},extend:function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])}};function n(e,o,s){var r=t.getType(o);if(r in n.CONSTRUCTORS)return new n.CONSTRUCTORS[r](e,o,s);throw new Error("Bad type: "+t.getClass(o))}function o(e,t,n){if(this.constructor===o)throw new Error("This is abstract class");var s=this,r=document.createElement("li");s.label=e,s.isComplex=!1,r.classList.add("jsontree_node"),r.innerHTML=function(e,t){return' <span class="jsontree_label-wrapper"> <span class="jsontree_label">"'+e+'"</span> : </span> <span class="jsontree_value-wrapper"> <span class="jsontree_value jsontree_value_'+s.type+'">'+t+"</span>"+(n?"":",")+"</span>"}(e,t),s.el=r,r.querySelector(".jsontree_label").addEventListener("click",function(e){if(!e.altKey)return e.shiftKey?(document.getSelection().removeAllRanges(),void alert(s.getJSONPath())):void 0;s.toggleMarked()},!1)}function s(e,t,n){this.type="boolean",o.call(this,e,t,n)}function r(e,t,n){this.type="number",o.call(this,e,t,n)}function i(e,t,n){this.type="string",o.call(this,e,'"'+t+'"',n)}function a(e,t,n){this.type="null",o.call(this,e,t,n)}function l(e,o,s){if(this.constructor===l)throw new Error("This is abstract class");var r,i,a,c=this,u=document.createElement("li"),d=[];c.label=e,c.isComplex=!0,u.classList.add("jsontree_node"),u.classList.add("jsontree_node_complex"),u.innerHTML=function(e,t){var n=s?"":",",o=' <div class="jsontree_value-wrapper"> <div class="jsontree_value jsontree_value_'+c.type+'"> <b>'+t[0]+'</b> <span class="jsontree_show-more">&hellip;</span> <ul class="jsontree_child-nodes"></ul> <b>'+t[1]+"</b></div>"+n+"</div>";return null!==e&&(o=' <span class="jsontree_label-wrapper"> <span class="jsontree_label"><span class="jsontree_expand-button"></span>"'+e+'"</span> : </span>'+o),o}(e,c.sym),r=u.querySelector(".jsontree_child-nodes"),null!==e?(i=u.querySelector(".jsontree_label"),a=u.querySelector(".jsontree_show-more"),i.addEventListener("click",function(e){if(!e.altKey)return e.shiftKey?(document.getSelection().removeAllRanges(),void alert(c.getJSONPath())):void c.toggle(e.ctrlKey||e.metaKey);c.toggleMarked()},!1),a.addEventListener("click",function(e){c.toggle(e.ctrlKey||e.metaKey)},!1),c.isRoot=!1):(c.isRoot=!0,c.parent=null,u.classList.add("jsontree_node_expanded")),c.el=u,c.childNodes=d,c.childNodesUl=r,t.forEachNode(o,function(e,t,o){c.addChild(new n(e,t,o))}),c.isEmpty=!Boolean(d.length),c.isEmpty&&u.classList.add("jsontree_node_empty")}function c(e,t,n){this.sym=["{","}"],this.type="object",l.call(this,e,t,n)}function u(e,t,n){this.sym=["[","]"],this.type="array",l.call(this,e,t,n)}function d(e,t){this.wrapper=document.createElement("ul"),this.wrapper.className="jsontree_tree clearfix",this.rootNode=null,this.sourceJSONObj=e,this.loadData(e),this.appendTo(t)}return n.CONSTRUCTORS={boolean:s,number:r,string:i,null:a,object:c,array:u},o.prototype={constructor:o,mark:function(){this.el.classList.add("jsontree_node_marked")},unmark:function(){this.el.classList.remove("jsontree_node_marked")},toggleMarked:function(){this.el.classList.toggle("jsontree_node_marked")},expandParent:function(e){this.parent&&(this.parent.expand(),this.parent.expandParent(e))},getJSONPath:function(e){return this.isRoot?"$":(t="array"===this.parent.type?"["+this.label+"]":e?"."+this.label:"['"+this.label+"']",this.parent.getJSONPath(e)+t);var t}},t.inherits(s,o),t.inherits(r,o),t.inherits(i,o),t.inherits(a,o),t.inherits(l,o),t.extend(l.prototype,{constructor:l,addChild:function(e){this.childNodes.push(e),this.childNodesUl.appendChild(e.el),e.parent=this},expand:function(e){this.isEmpty||(this.isRoot||this.el.classList.add("jsontree_node_expanded"),e&&this.childNodes.forEach(function(t,n){t.isComplex&&t.expand(e)}))},collapse:function(e){this.isEmpty||(this.isRoot||this.el.classList.remove("jsontree_node_expanded"),e&&this.childNodes.forEach(function(t,n){t.isComplex&&t.collapse(e)}))},toggle:function(e){if(!this.isEmpty&&(this.el.classList.toggle("jsontree_node_expanded"),e)){var t=this.el.classList.contains("jsontree_node_expanded");this.childNodes.forEach(function(n,o){n.isComplex&&n[t?"expand":"collapse"](e)})}},findChildren:function(e,t,n){this.isEmpty||this.childNodes.forEach(function(o,s){e(o)&&t(o),o.isComplex&&n&&o.findChildren(e,t,n)})}}),t.inherits(c,l),t.inherits(u,l),d.prototype={constructor:d,loadData:function(e){t.isValidRoot(e)?(this.sourceJSONObj=e,this.rootNode=new n(null,e,"last"),this.wrapper.innerHTML="",this.wrapper.appendChild(this.rootNode.el)):alert("The root should be an object or an array")},appendTo:function(e){e.appendChild(this.wrapper)},expand:function(e){this.rootNode.isComplex&&("function"==typeof e?this.rootNode.childNodes.forEach(function(t,n){t.isComplex&&e(t)&&t.expand()}):this.rootNode.expand("recursive"))},collapse:function(){"function"==typeof this.rootNode.collapse&&this.rootNode.collapse("recursive")},toSourceJSON:function(e){if(!e)return JSON.stringify(this.sourceJSONObj);var t=JSON.stringify(this.sourceJSONObj,null,"[%^$#$%^%]");return t=(t=t.split("\n").join("<br />")).split("[%^$#$%^%]").join("&nbsp;&nbsp;&nbsp;&nbsp;")},findAndHandle:function(e,t){this.rootNode.findChildren(e,t,"isRecursive")},unmarkAll:function(){this.rootNode.findChildren(function(e){return!0},function(e){e.unmark()},"isRecursive")}},{create:function(e,t){return new d(e,t)}}}();e.exports=n}])});