/* This json editor is open sourced with the intention to use the editor as a component in your own application. Not to just copy and monetize the editor as it is. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright (c) 2011-2012 Jos de Jong, http://jsoneditoronline.org @author Jos de Jong, @date 2012-08-07 */ if(!Array.prototype.indexOf)Array.prototype.indexOf=function(a){for(var b=0;b3)b.scrollTop+=a/3,c.animateTimeout=setTimeout(e,50)};e()}}; JSONEditor.Node=function(a){this.dom={};this.expanded=!1;a&&a instanceof Object?(this.setField(a.field,a.fieldEditable),this.setValue(a.value),a.search&&this.search(a.search)):(this.setField(),this.setValue())};JSONEditor.Node.prototype.setParent=function(a){this.parent=a};JSONEditor.Node.prototype.getParent=function(){return this.parent};JSONEditor.Node.prototype.getEditor=function(){return this.parent?this.parent.getEditor():void 0}; JSONEditor.Node.prototype.setField=function(a,b){this.field=a;this.fieldEditable=b==!0};JSONEditor.Node.prototype.getField=function(){if(this.field===void 0)this.field=this._getDomField();return this.field}; JSONEditor.Node.prototype.setValue=function(a){var b=this.childs;if(b)for(;b.length;)this.removeChild(b[0]);this.type=this._getType(a);if(this.type=="array"){this.childs=[];for(var c=0,b=a.length;cb+d&&(e=void 0);e&&e.parent&&e.parent.moveBefore(this,e)}else if(e=(d=this.expanded&& this.append?this.append.getDom():this.dom.tr)?d.nextSibling:void 0){for(var d=JSONEditor.getAbsoluteTop(e),b=void 0,e=e.nextSibling,g=JSONEditor.getAbsoluteTop(e),g=e?g-d:0;e&&f>c+g;)b=JSONEditor.getNodeFromTarget(e),e=e.nextSibling,g=JSONEditor.getAbsoluteTop(e),g=e?g-d:0;b&&b.parent&&b.parent.moveBefore(this,b)}JSONEditor.Events.preventDefault(a)}; JSONEditor.Node.prototype._onDragEnd=function(a){a=a||window.event;document.body.style.cursor=this.oldCursor;delete JSONEditor.freezeHighlight;delete this.oldCursor;this.setHighlight(!1);this.mousemove&&(JSONEditor.Events.removeEventListener(document,"mousemove",this.mousemove),delete this.mousemove);this.mouseup&&(JSONEditor.Events.removeEventListener(document,"mouseup",this.mouseup),delete this.mouseup);JSONEditor.Events.preventDefault(a)}; JSONEditor.Node.prototype._createDomDragArea=function(){if(this.parent){var a=document.createElement("button");a.className="jsoneditor-dragarea";return a}};JSONEditor.Node.prototype._createDomField=function(){return document.createElement("div")}; JSONEditor.Node.prototype.setHighlight=function(a){if(!JSONEditor.freezeHighlight&&this.dom.tr){this.dom.tr.className="jsoneditor-tr"+(a?" jsoneditor-tr-highlight":"");this.append&&this.append.setHighlight(a);var b=this.childs;if(b)for(var c=0,d=b.length;c0?a:0,f=c.length;a0?a:0;for(f=c.length;a/g,">").replace(/ /g,"  ").replace(/^ /," ").replace(/ $/," ");a=JSON.stringify(a);return a.substring(1,a.length-1)}; JSONEditor.Node.prototype._unescape=function(a){return JSON.parse('"'+a+'"').replace(/</g,"<").replace(/>/g,">").replace(/ /g," ")};JSONEditor.Node.prototype._stripHTML=function(a){return a.replace(/<(?:.|\n)*?>/gm,"")};JSONEditor.AppendNode=function(){this.dom={}};JSONEditor.AppendNode.prototype=new JSONEditor.Node; JSONEditor.AppendNode.prototype.getDom=function(){function a(a){var b=document.createElement("td");b.className=a||"";return b}if(this.dom.tr)return this.dom.tr;var b=document.createElement("tr");b.appendChild(a("jsoneditor-td"));b.node=this;var c=document.createElement("td");b.appendChild(c);c.className="jsoneditor-td";var d=document.createElement("button");d.className="jsoneditor-append";d.title="Append a field";this.dom.append=d;c.appendChild(d);b.appendChild(a("jsoneditor-td jsoneditor-td-edit")); b.appendChild(a("jsoneditor-td jsoneditor-td-edit"));b.appendChild(a("jsoneditor-td jsoneditor-td-edit"));this.dom.tr=b;this.dom.td=c;this.updateDom();return b};JSONEditor.AppendNode.prototype.updateDom=function(){var a=this.dom.td;if(a)a.style.paddingLeft=this.getLevel()*24+26+"px"}; JSONEditor.AppendNode.prototype.onEvent=function(a){var b=a.type;if((a.target||a.srcElement)==this.dom.append)switch(b){case "click":a=new JSONEditor.Node({field:"field",value:"value"});this.parent.appendChild(a);this.parent.setHighlight(!1);a.focus();break;case "mouseover":this.parent.setHighlight(!0);break;case "mouseout":this.parent.setHighlight(!1)}}; JSONEditor.prototype._createFrame=function(){this.container.innerHTML="";this.frame=document.createElement("div");this.frame.className="jsoneditor-frame";this.container.appendChild(this.frame);var a=this,b=function(b){var b=b||window.event,c=b.target||b.srcElement;if(a.options.enableSearch&&b.type=="keydown"){var d=b.which||b.keyCode;d==70&&b.ctrlKey?a.searchBox&&(a.searchBox.dom.search.focus(),a.searchBox.dom.search.select(),JSONEditor.Events.preventDefault(b)):d==114&&(b.shiftKey?a.searchBox.previous(): a.searchBox.next(),a.searchBox.focusActiveResult(),JSONEditor.Events.preventDefault(b))}if(c=JSONEditor.getNodeFromTarget(c))c.onEvent(b)};this.frame.onclick=function(a){b(a);JSONEditor.Events.preventDefault(a)};this.frame.onchange=b;this.frame.onkeydown=b;this.frame.onkeyup=b;this.frame.oncut=b;this.frame.onpaste=b;this.frame.onmousedown=b;this.frame.onmouseup=b;this.frame.onmouseover=b;this.frame.onmouseout=b;JSONEditor.Events.addEventListener(this.frame,"focus",b,!0);JSONEditor.Events.addEventListener(this.frame, "blur",b,!0);this.frame.onfocusin=b;this.frame.onfocusout=b;this.head=document.createElement("table");this.head.className="jsoneditor-menu";var c=document.createElement("tbody");this.head.appendChild(c);var d=document.createElement("tr");c.appendChild(d);this.tdMenu=c=document.createElement("td");c.className="jsoneditor-menu";d.appendChild(c);d=document.createElement("button");d.innerHTML="Expand All";d.title="Expand all fields";d.onclick=function(){a.expandAll()};c.appendChild(d);d=document.createElement("button"); d.innerHTML="Collapse All";d.title="Collapse all fields";d.onclick=function(){a.collapseAll()};c.appendChild(d);if(this.options.enableSearch)this.searchBox=new JSONEditor.SearchBox(this,c);this.frame.appendChild(this.head)}; JSONEditor.prototype._createTable=function(){var a=document.createElement("div");a.className="jsoneditor-content-outer";this.contentOuter=a;this.content=document.createElement("div");this.content.className="jsoneditor-content";a.appendChild(this.content);this.table=document.createElement("table");this.table.className="jsoneditor-table";this.content.appendChild(this.table);if(JSONEditor.getInternetExplorerVersion()==8)this.content.style.overflow="scroll";var b;this.colgroupContent=document.createElement("colgroup"); b=document.createElement("col");b.width="24px";this.colgroupContent.appendChild(b);b=document.createElement("col");this.colgroupContent.appendChild(b);b=document.createElement("col");b.width="24px";this.colgroupContent.appendChild(b);b=document.createElement("col");b.width="24px";this.colgroupContent.appendChild(b);b=document.createElement("col");b.width="24px";this.colgroupContent.appendChild(b);this.table.appendChild(this.colgroupContent);this.tbody=document.createElement("tbody");this.table.appendChild(this.tbody); this.frame.appendChild(a)};JSONEditor.getNodeFromTarget=function(a){for(;a;){if(a.node)return a.node;a=a.parentNode}}; JSONFormatter=function(a){if(!JSON)throw Error("Your browser does not support JSON. \n\nPlease install the newest version of your browser.\n(all modern browsers support JSON).");this.container=a;this.width=a.clientWidth;this.height=a.clientHeight;this.frame=document.createElement("div");this.frame.className="jsoneditor-frame";this.frame.onclick=function(a){JSONEditor.Events.preventDefault(a)};this.head=document.createElement("table");this.head.className="jsoneditor-menu";a=document.createElement("tbody"); this.head.appendChild(a);var b=document.createElement("tr");a.appendChild(b);a=document.createElement("td");a.className="jsoneditor-menu";b.appendChild(a);b=document.createElement("button");b.innerHTML="Format";b.title="Format JSON data, with proper indentation and line feeds";b.className="jsoneditor-button";a.appendChild(b);var c=document.createElement("button");c.innerHTML="Compact";c.title="Compact JSON data, remove all whitespaces";c.className="jsoneditor-button";a.appendChild(c);this.frame.appendChild(this.head); this.content=document.createElement("div");this.content.className="jsonformatter-content";this.frame.appendChild(this.content);this.textarea=document.createElement("textarea");this.textarea.className="jsonformatter-textarea";this.textarea.spellcheck=!1;this.content.appendChild(this.textarea);var d=this.textarea,f=this;b.onclick=function(){try{d.value=JSON.stringify(JSON.parse(d.value),null," ")}catch(a){f.onError(a)}};c.onclick=function(){try{d.value=JSON.stringify(JSON.parse(d.value))}catch(a){f.onError(a)}}; this.container.appendChild(this.frame)};JSONFormatter.prototype.onError=function(){};JSONFormatter.prototype._checkChange=function(){var a=this.textarea.value;if(a!=this.lastContent&&(this.lastContent=a,this.onChangeCallback))this.onChangeCallback()};JSONFormatter.prototype.set=function(a){this.textarea.value=JSON.stringify(a,null," ")};JSONFormatter.prototype.get=function(){return JSON.parse(this.textarea.value)}; JSONEditor.SearchBox=function(a,b){var c=this;this.editor=a;this.timeout=void 0;this.delay=200;this.lastText=void 0;this.dom={};this.dom.container=b;var d=document.createElement("table");this.dom.table=d;d.className="jsoneditor-search";b.appendChild(d);var f=document.createElement("tbody");this.dom.tbody=f;d.appendChild(f);d=document.createElement("tr");f.appendChild(d);f=document.createElement("td");f.className="jsoneditor-search";d.appendChild(f);var e=document.createElement("div");this.dom.results= e;e.className="jsoneditor-search-results";f.appendChild(e);f=document.createElement("td");f.className="jsoneditor-search";d.appendChild(f);d=document.createElement("div");this.dom.input=d;d.className="jsoneditor-search";d.title="Search fields and values";f.appendChild(d);f=document.createElement("table");f.className="jsoneditor-search-input";d.appendChild(f);e=document.createElement("tbody");f.appendChild(e);d=document.createElement("tr");e.appendChild(d);e=document.createElement("input");this.dom.search= e;e.className="jsoneditor-search";e.oninput=function(a){c.onDelayedSearch(a)};e.onchange=function(a){c.onSearch(a)};e.onkeyup=function(a){c.onKeyUp(a)};f=document.createElement("td");d.appendChild(f);f.appendChild(e);e=document.createElement("button");e.className="jsoneditor-search-refresh";e.onclick=function(a){c.onSearch(a,!0)};f=document.createElement("td");d.appendChild(f);f.appendChild(e)}; JSONEditor.SearchBox.prototype.next=function(){if(this.results!=void 0){var a=this.resultIndex!=void 0?this.resultIndex+1:0;a>this.results.length-1&&(a=0);this.setActiveResult(a)}};JSONEditor.SearchBox.prototype.previous=function(){if(this.results!=void 0){var a=this.results.length-1,b=this.resultIndex!=void 0?this.resultIndex-1:a;b<0&&(b=a);this.setActiveResult(b)}}; JSONEditor.SearchBox.prototype.setActiveResult=function(a){if(this.activeResult){var b=this.activeResult.node;this.activeResult.elem=="field"?delete b.searchFieldActive:delete b.searchValueActive;b.updateDom()}!this.results||!this.results[a]?this.activeResult=this.resultIndex=void 0:(this.resultIndex=a,a=this.results[this.resultIndex].node,this.results[this.resultIndex].elem=="field"?a.searchFieldActive=!0:a.searchValueActive=!0,this.activeResult=this.results[this.resultIndex],a.updateDom(),a.scrollTo())}; JSONEditor.SearchBox.prototype.focusActiveResult=function(){this.activeResult||this.next();this.activeResult&&this.activeResult.node.focus(this.activeResult.elem)};JSONEditor.SearchBox.prototype.clearDelay=function(){this.timeout!=void 0&&(clearTimeout(this.timeout),delete this.timeout)};JSONEditor.SearchBox.prototype.onDelayedSearch=function(){this.clearDelay();var a=this;this.timeout=setTimeout(function(b){a.onSearch(b)},this.delay)}; JSONEditor.SearchBox.prototype.onSearch=function(a,b){this.clearDelay();var c=this.dom.search.value,c=c.length>0?c:void 0;if(c!=this.lastText||b)if(this.lastText=c,this.results=editor.search(c),this.setActiveResult(void 0),c!=void 0)switch(c=this.results.length,c){case 0:this.dom.results.innerHTML="no results";break;case 1:this.dom.results.innerHTML="1 result";break;default:this.dom.results.innerHTML=c+" results"}else this.dom.results.innerHTML=""}; JSONEditor.SearchBox.prototype.onKeyUp=function(a){var a=a||window.event,b=a.which||a.keyCode;if(b==27)this.dom.search.value="",this.onSearch(a);else if(b==13)if(a.ctrlKey)this.onSearch(a,!0);else a.shiftKey?this.previous():this.next();else this.onDelayedSearch(a)};JSONEditor.Events={}; JSONEditor.Events.addEventListener=function(a,b,c,d){return a.addEventListener?(d===void 0&&(d=!1),b==="mousewheel"&&navigator.userAgent.indexOf("Firefox")>=0&&(b="DOMMouseScroll"),a.addEventListener(b,c,d),c):(d=function(){return c.call(a,window.event)},a.attachEvent("on"+b,d),d)}; JSONEditor.Events.removeEventListener=function(a,b,c,d){a.removeEventListener?(d===void 0&&(d=!1),b==="mousewheel"&&navigator.userAgent.indexOf("Firefox")>=0&&(b="DOMMouseScroll"),a.removeEventListener(b,c,d)):a.detachEvent("on"+b,c)};JSONEditor.Events.stopPropagation=function(a){if(!a)a=window.event;a.stopPropagation?a.stopPropagation():a.cancelBubble=!0};JSONEditor.Events.preventDefault=function(a){if(!a)a=window.event;a.preventDefault?a.preventDefault():a.returnValue=!1}; JSONEditor.getAbsoluteLeft=function(a){for(var b=0,c=document.body;a!=null&&a!=c;)b+=a.offsetLeft,b-=a.scrollLeft,a=a.offsetParent;return b};JSONEditor.getAbsoluteTop=function(a){for(var b=0,c=document.body;a!=null&&a!=c;)b+=a.offsetTop,b-=a.scrollTop,a=a.offsetParent;return b};JSONEditor.addClassName=function(a,b){var c=a.className;if(c.indexOf(b)==-1)c+=" "+b,a.className=c}; JSONEditor.removeClassName=function(a,b){var c=a.className;if(c.indexOf(b)!=-1)c=c.replace(b,""),c=c.replace(/ /g,""),a.className=c};JSONEditor.stripFormatting=function(a){for(var a=a.childNodes,b=0,c=a.length;b=0;e--){var g=f[e];g.specified==!0&&d.removeAttribute(g.name)}JSONEditor.stripFormatting(d)}}; JSONEditor.setEndOfContentEditable=function(a){var b;document.createRange?(b=document.createRange(),b.selectNodeContents(a),b.collapse(!1),a=window.getSelection(),a.removeAllRanges(),a.addRange(b)):document.selection&&(b=document.body.createTextRange(),b.moveToElementText(a),b.collapse(!1),b.select())};JSONEditor.getInternetExplorerVersion=function(){var a=-1;navigator.appName=="Microsoft Internet Explorer"&&/MSIE ([0-9]{1,}[.0-9]{0,})/.exec(navigator.userAgent)!=null&&(a=parseFloat(RegExp.$1));return a}; JSONEditor.ieVersion=JSONEditor.getInternetExplorerVersion();