1 line
19 KiB
JavaScript
1 line
19 KiB
JavaScript
|
|
function collapse(e){var t,e=e.target,n=e.parentNode.getElementsByClassName("collapsible");n.length&&("none"===(n=n[0]).style.display?(t=n.parentNode.getElementsByClassName("ellipsis")[0],n.parentNode.removeChild(t),n.style.display=""):(n.style.display="none",(t=document.createElement("span")).className="ellipsis",t.innerHTML=" … ",n.parentNode.insertBefore(t,n)),e.innerHTML="-"===e.innerHTML?"+":"-")}function addCollapser(e){var t;"LI"===e.nodeName&&((t=document.createElement("div")).className="collapser",t.innerHTML="-",t.addEventListener("click",collapse,!1),e.insertBefore(t,e.firstChild))}function jsonView(e,t){if(this.debug=!1,-1!==e.indexOf("#"))this.idType="id",this.id=e.replace("#","");else{if(-1===e.indexOf("."))return void(this.debug&&console.log("Can't find that element"));this.idType="class",this.id=e.replace(".","")}if(this.data=document.getElementById(this.id).innerHTML,-1!==t.indexOf("#"))this.targetType="id",this.target=t.replace("#","");else{if(-1===e.indexOf("."))return void(this.debug&&console.log("Can't find the target element"));this.targetType="class",this.target=t.replace(".","")}/^\<pre.*\>(.*)\<\/pre\>$/.test(this.data)&&(this.debug&&console.log("JSONView: data is wrapped in <pre>...</pre>, stripping HTML..."),this.data=this.data.replace(/<(?:.|\s)*?>/g,""));var e=/^[\s\u200B\uFEFF]*([\w$\[\]\.]+)[\s\u200B\uFEFF]*\([\s\u200B\uFEFF]*([\[{][\s\S]*[\]}])[\s\u200B\uFEFF]*\);?[\s\u200B\uFEFF]*$/,t=/^\s*([\[\{].*[\}\]])\s*$/.test(this.data),n=e.test(this.data);if(this.debug&&console.log("JSONView: is_json="+t+" is_jsonp="+n),t||n){function a(){}this.debug&&console.log("JSONView: sexytime!"),a.prototype={htmlEncode:function(e){return null!=e?e.toString().replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">"):""},decorateWithSpan:function(e,t){return'<span class="'+t+'">'+this.htmlEncode(e)+"</span>"},valueToHTML:function(e){var t=typeof e,n="";return null===e?n+=this.decorateWithSpan("null","null"):e&&e.constructor===Array?n+=this.arrayToHTML(e):"object"==t?n+=this.objectToHTML(e):"number"==t?n+=this.decorateWithSpan(e,"num"):"string"==t?/^(http|https):\/\/[^\s]+$/.test(e)?n+='<a href="'+e+'">'+this.htmlEncode(e)+"</a>":n+=this.decorateWithSpan('"'+e+'"',"string"):"boolean"==t&&(n+=this.decorateWithSpan(e,"bool")),n},arrayToHTML:function(e){var t,n='[<ul class="array collapsible">',a=!1;for(t in e)a=!0,n=(n+="<li>")+this.valueToHTML(e[t])+"</li>";return n+="</ul>]",n=a?n:"[ ]"},objectToHTML:function(e){var t,n='{<ul class="obj collapsible">',a=!1;for(t in e)a=!0,n=(n=(n+="<li>")+'<span class="prop">'+this.htmlEncode(t)+"</span>: ")+this.valueToHTML(e[t])+"</li>";return n+="</ul>}",n=a?n:"{ }"},jsonToHTML:function(e,t,n){var a="";return t?a=a+'<div class="callback">'+t+' (</div><div id="json">':a+='<div id="json">',a=a+this.valueToHTML(e)+"</div>",t&&(a+='<div class="callback">)</div>'),this.toHTML(a,n)},errorPage:function(e,t,n){var a='<div id="error">Error parsing JSON: '+e.message+"</div>",a=(a+="<h1>"+e.stack+":</h1>")+('<div id="json">'+this.htmlEncode(t)+"</div>");return this.toHTML(a,n+" - Error")},toHTML:function(e){return e}},this.jsonFormatter=new a;var t="",n="",r="",e=e.exec(this.data),n=e&&3===e.length?(this.debug&&console.log("THIS IS JSONp"),r=e[1],e[2]):(this.debug&&console.log("Vanilla JSON"),this.data);this.debug&&console.log(n);try{var i=JSON.parse(n);if(!i)throw"There was no object!";t=this.jsonFormatter.jsonToHTML(i,r)}catch(e){this.debug&&console.log(e),t=this.jsonFormatter.errorPage(e,this.data)}for(var o,e="<style type=\"text/css\">.jsonViewOutput .prop{font-weight:700;}.jsonViewOutput .null{color:red;}.jsonViewOutput .string{color:green;}.jsonViewOutput .collapser{position:absolute;left:-1em;cursor:pointer;}.jsonViewOutput li{position:relative;}.jsonViewOutput li:after{content:',';}.jsonViewOutput li:last-child:after{content:'';}.jsonViewOutput #error{-moz-border-radius:8px;border:1px solid #970000;background-color:#F7E8E8;margin:.5em;padding:.5em;}.jsonViewOutput .errormessage{font-family:monospace;}.jsonViewOutput #json{font-family:monosp
|