JsonView.min.js 2.8 KB

1
  1. MWF.widget=MWF.widget||{};MWF.widget.JsonView=new Class({Implements:[Options,Events],Extends:MWF.widget.Common,options:{title:"JsonView",style:"default",width:400,height:400,top:0,left:0,isMove:true,isClose:true,isMax:true,isExpand:true,isResize:true,target:null},initialize:function(t,e){this.setOptions(e);this.json=t;this.jsonString=JSON.encode(t);this.node=new Element("div")},load:function(){if(this.fireEvent("queryLoad")){MWF.require("MWF.widget.Panel",function(){this.jsonObjectNode=this.createJsonObjectNode();this.jsonStringNode=this.createJsonStringNode();MWF.require("MWF.widget.Tab",function(){this.tab=new MWF.widget.Tab(this.node,{style:"moduleList"});this.tab.load();this.objectTabPage=this.tab.addTab(this.jsonObjectNode,"JSON",false);this.stringTabPage=this.tab.addTab(this.jsonStringNode,"Text",false);this.objectTabPage.showTab()}.bind(this));this.options["onResize"]=function(){this.setPanelSize()}.bind(this);this.jsonPanel=new MWF.widget.Panel(this.node,this.options);this.jsonPanel.load();this.jsonPanel.show();this.setPanelSize()}.bind(this));this.fireEvent("postLoad")}},setPanelSize:function(){var t=this.jsonPanel.content.getSize();var e=this.jsonPanel.content.getStyle("padding-top").toFloat();var i=this.jsonPanel.content.getStyle("padding-bottom").toFloat();var n=this.tab.tabNodeContainer.getSize();var o=this.tab.tabNodeContainer.getStyle("margin-top").toFloat();var s=this.tab.tabNodeContainer.getStyle("margin-bottom").toFloat();var a=this.stringTabPage.contentNodeArea.getStyle("margin-top").toFloat();var r=this.stringTabPage.contentNodeArea.getStyle("margin-bottom").toFloat();var h=t.y-e-i-n.y-o-s-a-r-5;if(h<10)h=10;this.jsonStringNode.setStyle("height",h);this.jsonObjectNode.setStyle("height",h-20)},createJsonObjectNode:function(){this.jsonObjectNode=new Element("div",{styles:{overflow:"hideen","margin-top":"0px",height:"auto"}});this.loadObjectTree();return this.jsonObjectNode},loadObjectTree:function(){if(this.objectTree){this.objectTree.node.destroy();this.objectTree=null}MWF.require("MWF.widget.Tree",function(){this.objectTree=new MWF.widget.Tree(this.jsonObjectNode,{style:"jsonview"});this.objectTree.load();this.parseJsonObject(this.objectTree,"JSON",this.json,true)}.bind(this))},parseJsonObject:function(t,e,n,o){var s={expand:o,title:"",text:"",action:"",icon:""};switch(typeOf(n)){case"object":s.text=e;s.icon="object.png";var a=t.appendChild(s);for(i in n){this.parseJsonObject(a,i,n[i],false)}break;case"array":s.text=e;s.icon="array.png";var a=t.appendChild(s);n.each(function(t,e){this.parseJsonObject(a,"["+e+"]",t,false)}.bind(this));break;default:s.text=e+" : "+n;s.icon="string.png";var a=t.appendChild(s)}},createJsonStringNode:function(){var t=new Element("textarea",{readonly:false,styles:{width:"99%",overflow:"auto",border:"0px"}});t.set("text",JSON.format(this.json));return t}});