/* jsonEditor 1.02 copyright 2007-2009 Thomas Frank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ JSONeditor={ start:function(treeDivName,formDivName,json,showExamples, path){ if(this.examples.length<6){ var e=this.treeBuilder.JSONstring.make(this) eval("this.examples[5]={JSONeditor:"+e+"}") } this.treeDivName=treeDivName var t=this.treeBuilder, $=t.$ treeBuilder=t t.images.path=path+t.images.path; var s=$(treeDivName).style var f=$(formDivName) var fs=f.style f.innerHTML=this.formHTML if(!showExamples){$('jExamples').style.display="none"} fs.fontSize=s.fontSize="11px" fs.fontFamily=s.fontFamily="Verdana,Arial,Helvetica,sans-serif" var e=f.getElementsByTagName("*") for(var i=0;i
Load an example: 

\nLabel:


\nValue:


\nData type:     \n\n \n

\n\n\n

\n \n \n \n \n \n

\nAdd children first/siblings before\n
\nPaste as child on objects & arrays\n

\n", examples:[{}, {employee:{gid:102, companyID:121, defaultActionID:444,names:{firstName:"Stive", middleInitial:"Jr",lastName:"Martin"},address:{city:"Albany",state:"NY",zipCode:"14410-585",addreess:"41 State Street"},job:{departmentID:102,jobTitleID:100,hireDate:"1/02/2000",terminationDate:"1/12/2007"},contact:{phoneHome:"12-123-2133", beeper:"5656",email1:"info@soft-amis.com",fax:"21-321-23223",phoneMobile:"32-434-3433",phoneOffice:"82-900-8993"},login:{employeeID:"eID102",password:"password",superUser:true,lastLoginDate:"1/12/2007",text:"text", regexp:/^mmm/, date: new Date() },comment:{PCDATA:"comment"},roles:[{role:102},{role:103}]}}, {"widget": {"debug": true,"window": {"title": "Sample Konfabulator Widget","name": "main_window","width": 500,"height": 500},"Pairs": [ {"src": "Images/Sun.png","name": "sun1"},{"hOffset": 250,"vOffset": 200},null,{"alignment": "center"}],"text": {"a very long item label here": "Click Here","size": 36,"style": "null","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onmouseover": function(){alert("Hello World");},"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"}}}, {"members": [{"href": "1","entity": {"category": [{"term": "weblog", "label": "Weblog stuff"}],"updated": "2007-05-02T23:32:03Z","title": "This is the second post","author": {"uri": "http://dealmeida.net/","email": "roberto@dealmeida.net","name": "Rob de Almeida"},"summary": "Testing search","content": {"content": "This is my second post, to test the search.","type": "text"},"id": "1"}},{"href": "0","entity": {"category": [{"term": "weblog", "label": "Weblog stuff"},{"term": "json", "label": "JSON"}],"updated": "2007-05-02T23:25:59Z","title": "This is the second version of the first post","author": {"uri": "http://dealmeida.net/","email": "roberto@dealmeida.net","name": "Rob de Almeida"},"summary": "This is my first post here, after some modifications","content": {"content": "This is my first post, testing the jsonstore WSGI microapp PUT.","type": "html"},"id": "0"}}],"next": null}, {"menu": {"header": "SVG Viewer","items": [{"id": "Open"},{"id": "OpenNew", "label": "Open New", "thing": "thing"},{"id": "ZoomIn", "label": "Zoom In"},{"id": "ZoomOut", "label": "Zoom Out"},{"id": "OriginalView", "label": "Original View"},null,{"id": "Quality"},{"id": "Pause"},{"id": "Mute"},null,{"id": "Find", "label": "Find..."},{"id": "FindAgain", "label": "Find Again"},{"id": "Copy"},{"id": "CopyAgain", "label": "Copy Again"},{"id": "CopySVG", "label": "Copy SVG"},{"id": "ViewSVG", "label": "View SVG"}]}} ] } /* treeBuilder v 1.00 + a lot of json stuff added... copyright 2007 Thomas Frank */ JSONeditor.treeBuilder={ stateMem:{}, images:{ folderNode:'', folderNodeOpen:'', folderNodeLast:'', folderNodeOpenLast:'', docNode:'', docNodeLast:'', folder:'', folderOpen:'', doc:'', vertLine:'', folderNodeFirst:'', folderNodeOpenFirst:'', folderNodeLastFirst:'', folderNodeOpenLastFirst:'', path:'treeBuilderImages/', nodeWidth:16 }, $:function(x){return document.getElementById(x)}, preParse:function(x){ var x=x.innerHTML.split("\n"); var d=[]; for(var i=0;i0;i--){o[i]=o[i-1]} o[0]={} n=l+"[0]" } } this.JSONbuild(this.baseDiv,this.json) for(var i in this.stateMem){this.openAndClose(i,true)} this.jsonResponder(n) }, jsonAddSibling:function(f,label){ var before=f.jbefore.checked var l=f.orgjlabel.value var r=Math.random() eval('var temp=this.'+l) eval('this.'+l+"=r") var s=this.JSONstring.make(this.json) s=s.split(r+",") if(s.length<2){s=s[0].split(r)} var lp=this.jsonParent(l) eval('var o=this.'+lp) var t=(o && this.isArray(o))?"array":typeof o if(t=="object"){ var nl=label||prompt("Label (without path):","") if(!nl){return} if(nl/1==nl){nl="$"+nl} nl=this.jsonAlreadyExists(o,nl) var n=nl.replace(/\w/g,'')===""?"."+nl:'["'+nl+'"]' s=s.join('null,"'+nl+'":{},') lp+=n } if(t=="array"){ s=s.join('null,{},') var k=l.split("[") k[k.length-1]=(k[k.length-1].split("]").join("")/1+1)+"]" lp=k.join("[") } s=s.split("},}").join("}}") // replace with something better soon eval('this.json='+s) eval('this.'+l+'=temp') if(before){lp=this.jsonSwitchPlace(this.jsonParent(l),l,lp)} this.JSONbuild(this.baseDiv,this.json) for(var i in this.stateMem){this.openAndClose(i,true)} this.jsonResponder(lp) }, jSaveFirst:function(f,a){ var l=f.orgjlabel.value eval("var orgj=this."+l) orgj=this.JSONstring.make(orgj) var v=f.jvalue.value v=f.jtype.value=="string"?this.JSONstring.make(v):v v=v.split("\r").join("") if(orgj!=v || f.orgjlabel.value!=f.jlabel.value || this.jTypeChanged){ var k=confirm("Save before "+a+"?") if(k){this.jsonChange(f)} } }, jsonRename:function(f){ this.jSaveFirst(f,"renaming") var orgl=l=f.orgjlabel.value l=this.jsonChild(l) var nl=prompt("Label (without path):",l) if(!nl){return} this.jsonResponder(orgl) var nl=nl.replace(/\w/g,'')===""?"."+nl:'["'+nl+'"]' f.jlabel.value=this.jsonParent(orgl)+nl this.jsonChange(f,false,true) }, jsonSwitchPlace:function(p,el1,el2){ var orgel1=el1, orgel2=el2 eval("var o=this."+p) if(this.isArray(o)){ eval("var t=this."+el1) eval("this."+el1+"=this."+el2) eval("this."+el2+"=t") return orgel1 } el1=this.jsonChild(el1) el2=this.jsonChild(el2) var o2={} for(var i in o){ if(i==el1){o2[el2]=o[el2];o2[el1]=o[el1];continue} if(i==el2){continue} o2[i]=o[i] } eval("this."+p+"=o2") return orgel2 }, jsonCut:function(f){ this.jSaveFirst(f,"cutting") this.jsonCopy(f,true) this.jsonChange(f,true) this.setJsonMessage('Cut to clipboard!') }, jsonCopy:function(f,r){ if(!r){this.jSaveFirst(f,"copying")} var l=f.orgjlabel.value eval("var v=this."+l) v=this.JSONstring.make(v) var l=this.jsonChild(l) this.jClipboard={label:l,jvalue:v} this.jsonResponder(f.jlabel.value) if(!r){this.setJsonMessage('Copied to clipboard!')} }, jsonPaste:function(f,r){ var t=f.jtype.value var sibling=t!="object" && t!="array" if(!f.jPasteAsChild.checked){sibling=true} if(f.orgjlabel.value=="json"){sibling=false} if(sibling){this.jsonAddSibling(f,this.jClipboard.label)} else {this.jsonAddChild(f,this.jClipboard.label)} var l=f.orgjlabel.value eval("this."+l+"="+this.jClipboard.jvalue) this.jsonResponder(l) this.jsonChange(f) if(!r){this.setJsonMessage('Pasted!')} }, setJsonMessage:function(x){ this.$('jformMessage').innerHTML=x setTimeout("treeBuilder.$('jformMessage').innerHTML=''",1500) }, changeJsonDataType:function(x,f){ this.jTypeChanged=true var v=f.jvalue.value var orgv=v; v=x=='object'?'{"label":"'+v+'"}':v v=x=='array'?'["'+v+'"]':v if(!orgv){ v=x=='object'?'{}':v v=x=='array'?'[]':v } v=x=='function'?'function(){'+v+'}':v v=x=='string'?v:v v=x=='number'?v/1:v v=x=='boolean'?!!v:v v=x=='null'?'null':v v=x=='undefined'?'undefined':v f.jvalue.value=v }, jsonChange:function(f,remove,rename){ try { var l=f.jlabel.value var orgl=f.orgjlabel.value||"json.not1r2e3a4l" eval("var cur=this."+l) if(l!=orgl && cur!==undefined){ var c=confirm(l+"\n\nalready contains other data. Overwrite?") if(!c){return false} } var v=f.jvalue.value.split("\r").join("") if(f.jtype.value=="string"){ v=this.JSONstring.make(v) } if(l=="json"){ eval("v="+v) this.JSONbuild(this.baseDiv,v) for(var i in this.stateMem){this.openAndClose(i,true)} this.setJsonMessage('Saved!') return false } eval("var json="+this.JSONstring.make(this.json)) var randi=Math.random() eval(orgl+'='+randi) var paname=this.jsonParent(orgl) var samepa=this.jsonParent(orgl)==this.jsonParent(l) eval("var pa="+paname) if(this.isArray(pa)){ eval(paname+'=[];var newpa='+paname) for(var i=0;ilastlevel){levelmem.push(n);n=d[i-1].id}; if(d[i].level && d[i].level>lastlevel+1){return 'Trying to jump levels!'}; if(d[i].level && d[i].level=0)?j+'First':j; img.src=im.path+(im[k]?im[k]:k+'.gif'); img.style.display="none"; img.style.verticalAlign="middle"; img.id=m.id+"_"+j; if(j.indexOf('folderNode')==0){ img.onclick=function(e){ treeBuilder.openAndClose(this); if (!e){e=window.event}; e.cancelBubble = true; if(e.stopPropagation){e.stopPropagation()}; } }; if(m.firstChild){m.insertBefore(img,m.childNodes[co]); co++} else {m.appendChild(img)}; } }; m.insertBefore(m.t,m.childNodes[co]); m.inited=true }; var lastChild=m.childNodes[m.childNodes.length-1]; var isParent=(lastChild.tagName+"").toLowerCase()=="div"; var isLast=!m.nextSibling; var isOpen=isParent && lastChild.style.display!='none'; $(m.id+"_folder").style.display=!isOpen && isParent?'':'none'; $(m.id+"_folderOpen").style.display=isOpen && isParent?'':'none'; $(m.id+"_doc").style.display=isParent?'none':''; $(m.id+"_docNode").style.display=isParent || isLast?'none':''; $(m.id+"_docNodeLast").style.display=isParent || !isLast?'none':''; $(m.id+"_folderNode").style.display=isOpen || !isParent || isLast?'none':''; $(m.id+"_folderNodeLast").style.display=isOpen || !isParent || !isLast?'none':''; $(m.id+"_folderNodeOpen").style.display=!isOpen || !isParent || isLast?'none':''; $(m.id+"_folderNodeOpenLast").style.display=!isOpen || !isParent || !isLast?'none':''; var p=m.parentNode.nextSibling; if(p && p.id){ var sp=p;insideBase=false; while(sp){if(sp==$(this.treeBaseDiv)){insideBase=true};sp=sp.parentNode} if(!insideBase){return} var bg=im.path+(im.vertLine?im.vertLine:'vertLine.gif'); m.style.backgroundImage='url('+bg+')'; m.style.backgroundRepeat='repeat-y' }; }, openAndClose:function(x,remem){ var o, div=remem?this.$(x):x.parentNode; if(!div){return} if(remem){o=this.stateMem[div.id]} else {o=x.id.indexOf('Open')<0} if(remem=="open"){o=true} this.stateMem[div.id]=o var c=div.childNodes; for(var i=0;i 0) out.push(',\n'); this.toJsonStringArray(arg[i], out); this.path.pop(); } out.push(']'); return out; } else if (typeof arg.toString != 'undefined') { out.push('{'); var first = true; for (var i in arg) { if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue}; this.path.push(i); var curr = out.length; if (!first) out.push(this.compactOutput?',':',\n'); this.toJsonStringArray(i, out); out.push(':'); this.toJsonStringArray(arg[i], out); if (out[out.length - 1] == u) out.splice(curr, out.length - curr); else first = false; this.path.pop(); } out.push('}'); return out; } return out; } out.push('null'); return out; case 'unknown': case 'undefined': case 'function': try {eval('var a='+arg)} catch(e){arg='function(){alert("Could not convert the real function to JSON, due to a browser bug only found in Safari. Let us hope it will get fixed in future versions of Safari!")}'} out.push(this.includeFunctions?arg:u); return out; case 'string': if(this.restore && arg.indexOf("JSONcircRef:")==0){ this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj.")); }; out.push('"'); var a=['\n','\\n','\r','\\r','"','\\"']; arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])}; out.push(arg); out.push('"'); return out; default: out.push(String(arg)); return out; } } }