34 lines
63 KiB
JavaScript
34 lines
63 KiB
JavaScript
/*!
|
|
* jsoneditor.js
|
|
*
|
|
* @brief
|
|
* JSONEditor is a web-based tool to view, edit, and format JSON.
|
|
* It shows data a clear, editable treeview.
|
|
*
|
|
* Supported browsers: Chrome, Firefox, Safari, Opera, Internet Explorer 8+
|
|
*
|
|
* @license
|
|
* 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-2013 Jos de Jong, http://jsoneditoronline.org
|
|
*
|
|
* @author Jos de Jong, <wjosdejong@gmail.com>
|
|
* @version 2.2.0-SNAPSHOT
|
|
* @date 2013-05-03
|
|
*/
|
|
(function(){function e(t,i,n){if(!(this instanceof e))throw new Error('JSONEditor constructor called without "new".');arguments.length&&this._create(t,i,n)}function t(e,i,n){if(!(this instanceof t))throw new Error('TreeEditor constructor called without "new".');this._create(e,i,n)}function i(e,t,n){if(!(this instanceof i))throw new Error('TextEditor constructor called without "new".');this._create(e,t,n)}function n(e,t){this.editor=e,this.dom={},this.expanded=!1,t&&t instanceof Object?(this.setField(t.field,t.fieldEditable),this.setValue(t.value,t.type)):(this.setField(""),this.setValue(null))}function o(e){this.editor=e,this.dom={}}function s(e,t){function i(e,t,o){o.forEach(function(o){if(o.type=="separator"){var s=document.createElement("div");s.className="separator",a=document.createElement("li"),a.appendChild(s),e.appendChild(a)}else{var r={},a=document.createElement("li");e.appendChild(a);var l=document.createElement("button");if(l.className=o.className,r.button=l,o.title&&(l.title=o.title),o.click&&(l.onclick=function(){n.hide(),o.click()}),a.appendChild(l),o.submenu){var d=document.createElement("div");d.className="icon",l.appendChild(d),l.appendChild(document.createTextNode(o.text));var h;if(o.click){l.className+=" default";var c=document.createElement("button");r.buttonExpand=c,c.className="expand",c.innerHTML='<div class="expand"></div>',a.appendChild(c),o.submenuTitle&&(c.title=o.submenuTitle),h=c}else{var u=document.createElement("div");u.className="expand",l.appendChild(u),h=l}h.onclick=function(){n._onExpandItem(r),h.focus()};var p=[];r.subItems=p;var m=document.createElement("ul");r.ul=m,m.className="menu",m.style.height="0",a.appendChild(m),i(m,p,o.submenu)}else l.innerHTML='<div class="icon"></div>'+o.text;t.push(r)}})}this.dom={};var n=this,o=this.dom;this.anchor=void 0,this.items=e,this.eventListeners={},this.selection=void 0,this.visibleSubmenu=void 0,this.onClose=t?t.close:void 0;var s=document.createElement("div");s.className="jsoneditor-contextmenu",o.menu=s;var r=document.createElement("ul");r.className="menu",s.appendChild(r),o.list=r,o.items=[];var a=document.createElement("button");o.focusButton=a;var l=document.createElement("li");l.style.overflow="hidden",l.style.height="0",l.appendChild(a),r.appendChild(l),i(r,this.dom.items,e),this.maxHeight=0,e.forEach(function(t){var i=(e.length+(t.submenu?t.submenu.length:0))*24;n.maxHeight=Math.max(n.maxHeight,i)})}function r(e){this.editor=e,this.clear(),this.actions={editField:{undo:function(e){e.node.updateField(e.oldValue)},redo:function(e){e.node.updateField(e.newValue)}},editValue:{undo:function(e){e.node.updateValue(e.oldValue)},redo:function(e){e.node.updateValue(e.newValue)}},appendNode:{undo:function(e){e.parent.removeChild(e.node)},redo:function(e){e.parent.appendChild(e.node)}},insertBeforeNode:{undo:function(e){e.parent.removeChild(e.node)},redo:function(e){e.parent.insertBefore(e.node,e.beforeNode)}},insertAfterNode:{undo:function(e){e.parent.removeChild(e.node)},redo:function(e){e.parent.insertAfter(e.node,e.afterNode)}},removeNode:{undo:function(e){var t=e.parent,i=t.childs[e.index]||t.append;t.insertBefore(e.node,i)},redo:function(e){e.parent.removeChild(e.node)}},duplicateNode:{undo:function(e){e.parent.removeChild(e.clone)},redo:function(e){e.parent.insertAfter(e.clone,e.node)}},changeType:{undo:function(e){e.node.changeType(e.oldType)},redo:function(e){e.node.changeType(e.newType)}},moveNode:{undo:function(e){e.startParent.moveTo(e.node,e.startIndex)},redo:function(e){e.endParent.moveTo(e.node,e.endIndex)}},sort:{undo:function(e){var t=e.node;t.hideChilds(),t.sort=e.oldSort,t.childs=e.oldChilds,t.showChilds()},redo:function(e){var t=e.node;t.hideChilds(),t.sort=e.newSort,t.childs=e.newChilds,t.showChilds()}}}}function a(e,t){var i=this;this.editor=e,this.timeout=void 0,this.delay=200,this.lastText=void 0,this.dom={},this.dom.container=t;var n=document.createElement("table");this.dom.table=n,n.className="search",t.appendChild(n);var o=document.createElement("tbody");this.dom.tbody=o,n.appendChild(o);var s=document.createElement("tr");o.appendChild(s);var r=document.createElement("td");s.appendChild(r);var a=document.createElement("div");this.dom.results=a,a.className="results",r.appendChild(a),r=document.createElement("td"),s.appendChild(r);var l=document.createElement("div");this.dom.input=l,l.className="frame",l.title="Search fields and values",r.appendChild(l);var d=document.createElement("table");l.appendChild(d);var h=document.createElement("tbody");d.appendChild(h),s=document.createElement("tr"),h.appendChild(s);var c=document.createElement("button");c.className="refresh",r=document.createElement("td"),r.appendChild(c),s.appendChild(r);var u=document.createElement("input");this.dom.search=u,u.oninput=function(e){i._onDelayedSearch(e)},u.onchange=function(e){i._onSearch(e)},u.onkeydown=function(e){i._onKeyDown(e)},u.onkeyup=function(e){i._onKeyUp(e)},c.onclick=function(){u.select()},r=document.createElement("td"),r.appendChild(u),s.appendChild(r);var p=document.createElement("button");p.title="Next result (Enter)",p.className="next",p.onclick=function(){i.next()},r=document.createElement("td"),r.appendChild(p),s.appendChild(r);var m=document.createElement("button");m.title="Previous result (Shift+Enter)",m.className="previous",m.onclick=function(){i.previous()},r=document.createElement("td"),r.appendChild(m),s.appendChild(r)}function l(){this.locked=!1}e.modes={},e.prototype._create=function(e,t,i){this.container=e,this.options=t||{},this.json=i||{};var n=this.options.mode||"tree";this.setMode(n)},e.prototype._delete=function(){},e.prototype.set=function(e){this.json=e},e.prototype.get=function(){return this.json},e.prototype.setText=function(e){this.json=util.parse(e)},e.prototype.getText=function(){return JSON.stringify(this.json)},e.prototype.setName=function(e){this.options||(this.options={}),this.options.name=e},e.prototype.getName=function(){return this.options&&this.options.name},e.prototype.setMode=function(t){var i,n,o=this.container,s=util.extend({},this.options);s.mode=t;var r=e.modes[t];if(!r)throw new Error('Unknown mode "'+s.mode+'"');r.data=="text"?(n=this.getName(),i=this.getText(),this._delete(),util.clear(this),util.extend(this,r.editor.prototype),this._create(o,s),this.setName(n),this.setText(i)):(n=this.getName(),i=this.get(),this._delete(),util.clear(this),util.extend(this,r.editor.prototype),this._create(o,s),this.setName(n),this.set(i)),typeof r.load=="function"&&r.load.call(this)},t.prototype._create=function(e,t,i){if("undefined"==typeof JSON)throw new Error("Your browser does not support JSON. \n\nPlease install the newest version of your browser.\n(all modern browsers support JSON).");if(!e)throw new Error("No container element provided.");this.container=e,this.dom={},this.highlighter=new l,this.selection=void 0,this._setOptions(t),this.options.history&&!this.mode.view&&(this.history=new r(this)),this._createFrame(),this._createTable(),this.set(i||{})},t.prototype._delete=function(){this.frame&&this.container&&this.frame.parentNode==this.container&&this.container.removeChild(this.frame)},t.prototype._setOptions=function(e){if(this.options={search:!0,history:!0,mode:"tree",name:void 0},e){for(var t in e)e.hasOwnProperty(t)&&(this.options[t]=e[t]);e.enableSearch&&(this.options.search=e.enableSearch,console.log('WARNING: Option "enableSearch" is deprecated. Use "search" instead.')),e.enableHistory&&(this.options.history=e.enableHistory,console.log('WARNING: Option "enableHistory" is deprecated. Use "history" instead.')),e.mode=="editor"&&(this.options.mode="tree",console.log('WARNING: Mode "editor" is deprecated. Use "tree" instead.')),e.mode=="viewer"&&(this.options.mode="view",console.log('WARNING: Mode "viewer" is deprecated. Use "view" instead.'))}this.mode={edit:this.options.mode!="view"&&this.options.mode!="form",view:this.options.mode=="view",form:this.options.mode=="form"}},t.focusNode=void 0,t.prototype.set=function(e,t){if(t&&(console.log('Warning: second parameter "name" is deprecated. Use setName(name) instead.'),this.options.name=t),e instanceof Function||void 0===e)this.clear();else{this.content.removeChild(this.table);var i={field:this.options.name,value:e},o=new n(this,i);this._setRoot(o);var s=!1;this.node.expand(s),this.content.appendChild(this.table)}this.history&&this.history.clear()},t.prototype.get=function(){return t.focusNode&&t.focusNode.blur(),this.node?this.node.getValue():void 0},t.prototype.getText=function(){return JSON.stringify(this.get())},t.prototype.setText=function(e){this.set(util.parse(e))},t.prototype.setName=function(e){this.options.name=e,this.node&&this.node.updateField(this.options.name)},t.prototype.getName=function(){return this.options.name},t.prototype.clear=function(){this.node&&(this.node.collapse(),this.tbody.removeChild(this.node.getDom()),delete this.node)},t.prototype._setRoot=function(e){this.clear(),this.node=e,this.tbody.appendChild(e.getDom())},t.prototype.search=function(e){var t;return this.node?(this.content.removeChild(this.table),t=this.node.search(e),this.content.appendChild(this.table)):t=[],t},t.prototype.expandAll=function(){this.node&&(this.content.removeChild(this.table),this.node.expand(),this.content.appendChild(this.table))},t.prototype.collapseAll=function(){this.node&&(this.content.removeChild(this.table),this.node.collapse(),this.content.appendChild(this.table))},t.prototype._onAction=function(e,t){if(this.history&&this.history.add(e,t),this.options.change)try{this.options.change()}catch(i){console.log("Error in change callback: ",i)}},t.prototype.startAutoScroll=function(e){var t=this,i=this.content,n=util.getAbsoluteTop(i),o=i.clientHeight,s=n+o,r=24,a=50;this.autoScrollStep=n+r>e&&i.scrollTop>0?(n+r-e)/3:e>s-r&&o+i.scrollTop<i.scrollHeight?(s-r-e)/3:void 0,this.autoScrollStep?this.autoScrollTimer||(this.autoScrollTimer=setInterval(function(){t.autoScrollStep?i.scrollTop-=t.autoScrollStep:t.stopAutoScroll()},a)):this.stopAutoScroll()},t.prototype.stopAutoScroll=function(){this.autoScrollTimer&&(clearTimeout(this.autoScrollTimer),delete this.autoScrollTimer),this.autoScrollStep&&delete this.autoScrollStep},t.prototype.setSelection=function(e){e&&("scrollTop"in e&&this.content&&(this.content.scrollTop=e.scrollTop),e.range&&util.setSelectionOffset(e.range),e.dom&&e.dom.focus())},t.prototype.getSelection=function(){return{dom:t.domFocus,scrollTop:this.content?this.content.scrollTop:0,range:util.getSelectionOffset()}},t.prototype.scrollTo=function(e,t){var i=this.content;if(i){var n=this;n.animateTimeout&&(clearTimeout(n.animateTimeout),delete n.animateTimeout),n.animateCallback&&(n.animateCallback(!1),delete n.animateCallback);var o=i.clientHeight,s=i.scrollHeight-o,r=Math.min(Math.max(e-o/4,0),s),a=function(){var e=i.scrollTop,o=r-e;Math.abs(o)>3?(i.scrollTop+=o/3,n.animateCallback=t,n.animateTimeout=setTimeout(a,50)):(t&&t(!0),i.scrollTop=r,delete n.animateTimeout,delete n.animateCallback)};a()}else t&&t(!1)},t.prototype._createFrame=function(){this.frame=document.createElement("div"),this.frame.className="jsoneditor",this.container.appendChild(this.frame);var e=this,t=function(t){e._onEvent(t)};this.frame.onclick=function(e){e=e||window.event;var i=e.target||e.srcElement;t(e),i.nodeName=="BUTTON"&&util.preventDefault(e)},this.frame.oninput=t,this.frame.onchange=t,this.frame.onkeydown=t,this.frame.onkeyup=t,this.frame.oncut=t,this.frame.onpaste=t,this.frame.onmousedown=t,this.frame.onmouseup=t,this.frame.onmouseover=t,this.frame.onmouseout=t,util.addEventListener(this.frame,"focus",t,!0),util.addEventListener(this.frame,"blur",t,!0),this.frame.onfocusin=t,this.frame.onfocusout=t,this.menu=document.createElement("div"),this.menu.className="menu",this.frame.appendChild(this.menu);var i=document.createElement("button");i.className="expand-all",i.title="Expand all fields",i.onclick=function(){e.expandAll()},this.menu.appendChild(i);var n=document.createElement("button");if(n.title="Collapse all fields",n.className="collapse-all",n.onclick=function(){e.collapseAll()},this.menu.appendChild(n),this.history){var o=document.createElement("span");o.innerHTML=" ",this.menu.appendChild(o);var s=document.createElement("button");s.className="undo",s.title="Undo last action (Ctrl+Z)",s.onclick=function(){e._onUndo()},this.menu.appendChild(s),this.dom.undo=s;var r=document.createElement("button");r.className="redo",r.title="Redo (Ctrl+Shift+Z)",r.onclick=function(){e._onRedo()},this.menu.appendChild(r),this.dom.redo=r,this.history.onChange=function(){s.disabled=!e.history.canUndo(),r.disabled=!e.history.canRedo()},this.history.onChange()}this.options.search&&(this.searchBox=new a(this,this.menu))},t.prototype._onUndo=function(){this.history&&(this.history.undo(),this.options.change&&this.options.change())},t.prototype._onRedo=function(){this.history&&(this.history.redo(),this.options.change&&this.options.change())},t.prototype._onEvent=function(e){e=e||window.event;var i=e.target||e.srcElement;e.type=="keydown"&&this._onKeyDown(e),e.type=="focus"&&(t.domFocus=i);var o=n.getNodeFromTarget(i);o&&o.onEvent(e)},t.prototype._onKeyDown=function(e){var i=e.which||e.keyCode,n=e.ctrlKey,o=e.shiftKey,s=!1;if(9==i&&setTimeout(function(){util.selectContentEditable(t.domFocus)},0),this.searchBox)if(n&&70==i)this.searchBox.dom.search.focus(),this.searchBox.dom.search.select(),s=!0;else if(114==i||n&&71==i){var r=!0;o?this.searchBox.previous(r):this.searchBox.next(r),s=!0}this.history&&(n&&!o&&90==i?(this._onUndo(),s=!0):n&&o&&90==i&&(this._onRedo(),s=!0)),s&&(util.preventDefault(e),util.stopPropagation(e))},t.prototype._createTable=function(){var e=document.createElement("div");e.className="outer",this.contentOuter=e,this.content=document.createElement("div"),this.content.className="content",e.appendChild(this.content),this.table=document.createElement("table"),this.table.className="content",this.content.appendChild(this.table);var t=util.getInternetExplorerVersion();8==t&&(this.content.style.overflow="scroll");var i;this.colgroupContent=document.createElement("colgroup"),this.mode.edit&&(i=document.createElement("col"),i.width="24px",this.colgroupContent.appendChild(i)),i=document.createElement("col"),i.width="24px",this.colgroupContent.appendChild(i),i=document.createElement("col"),this.colgroupContent.appendChild(i),this.table.appendChild(this.colgroupContent),this.tbody=document.createElement("tbody"),this.table.appendChild(this.tbody),this.frame.appendChild(e)},e.modes.tree={editor:t,data:"json"},e.modes.view={editor:t,data:"json"},e.modes.form={editor:t,data:"json"},e.modes.editor={editor:t,data:"json"},e.modes.viewer={editor:t,data:"json"},i.prototype._create=function(e,t,i){if("undefined"==typeof JSON)throw new Error("Your browser does not support JSON. \n\nPlease install the newest version of your browser.\n(all modern browsers support JSON).");t=t||{},t.indentation&&(this.indentation=Number(t.indentation)),this.mode=t.mode=="code"?"code":"text",this.mode=="code"&&("undefined"==typeof ace&&(this.mode="text",console.log("WARNING: Cannot load code editor, Ace library not loaded. Falling back to plain text editor")),util.getInternetExplorerVersion()==8&&(this.mode="text",console.log("WARNING: Cannot load code editor, Ace is not supported on IE8. Falling back to plain text editor")));var n=this;this.container=e,this.editor=void 0,this.textarea=void 0,this.indentation=4,this.width=e.clientWidth,this.height=e.clientHeight,this.frame=document.createElement("div"),this.frame.className="jsoneditor",this.frame.onclick=function(e){util.preventDefault(e)},this.menu=document.createElement("div"),this.menu.className="menu",this.frame.appendChild(this.menu);var o=document.createElement("button");o.className="format",o.title="Format JSON data, with proper indentation and line feeds",this.menu.appendChild(o),o.onclick=function(){n.format()};var s=document.createElement("button");if(s.className="compact",s.title="Compact JSON data, remove all whitespaces",this.menu.appendChild(s),s.onclick=function(){n.compact()},this.content=document.createElement("div"),this.content.className="outer",this.frame.appendChild(this.content),this.container.appendChild(this.frame),this.mode=="code"){this.editorDom=document.createElement("div"),this.editorDom.style.height="100%",this.editorDom.style.width="100%",this.content.appendChild(this.editorDom);var r=ace.edit(this.editorDom);r.setTheme("ace/theme/jso"),r.setShowPrintMargin(!1),r.setFontSize(13),r.getSession().setMode("ace/mode/json"),r.getSession().setUseSoftTabs(!0),r.getSession().setUseWrapMode(!0),this.editor=r;var a=document.createElement("a");a.appendChild(document.createTextNode("powered by ace")),a.href="http://ace.ajax.org",a.target="_blank",a.className="poweredBy",a.onclick=function(){window.open(a.href,a.target)},this.menu.appendChild(a),t.change&&r.on("change",function(){t.change()})}else{var l=document.createElement("textarea");l.className="content",l.spellcheck=!1,this.content.appendChild(l),this.textarea=l,t.change&&(this.textarea.oninput===null?this.textarea.oninput=function(){t.change()}:this.textarea.onchange=function(){t.change()})}"string"==typeof i?this.setText(i):this.set(i)},i.prototype._delete=function(){this.frame&&this.container&&this.frame.parentNode==this.container&&this.container.removeChild(this.frame)},i.prototype.onError=function(){},i.prototype.compact=function(){try{var e=util.parse(this.getText());this.setText(JSON.stringify(e))}catch(t){this.onError(t)}},i.prototype.format=function(){try{var e=util.parse(this.getText());this.setText(JSON.stringify(e,null,this.indentation))}catch(t){this.onError(t)}},i.prototype.focus=function(){this.textarea&&this.textarea.focus(),this.editor&&this.editor.focus()},i.prototype.resize=function(){if(this.editor){var e=!1;this.editor.resize(e)}},i.prototype.set=function(e){this.setText(JSON.stringify(e,null,this.indentation))},i.prototype.get=function(){return util.parse(this.getText())},i.prototype.getText=function(){return this.textarea?this.textarea.value:this.editor?this.editor.getValue():""},i.prototype.setText=function(e){this.textarea&&(this.textarea.value=e),this.editor&&this.editor.setValue(e,-1)},e.modes.text={editor:i,data:"text",load:i.prototype.format},e.modes.code={editor:i,data:"text",load:i.prototype.format},n.prototype.setParent=function(e){this.parent=e},n.prototype.setField=function(e,t){this.field=e,this.fieldEditable=1==t},n.prototype.getField=function(){return this.field===void 0&&this._getDomField(),this.field},n.prototype.setValue=function(e,t){var i,o,s=this.childs;if(s)for(;s.length;)this.removeChild(s[0]);if(this.type=this._getType(e),t&&t!=this.type){if("string"!=t||this.type!="auto")throw new Error('Type mismatch: cannot cast value of type "'+this.type+' to the specified type "'+t+'"');this.type=t}if(this.type=="array"){this.childs=[];for(var r=0,a=e.length;a>r;r++)i=e[r],void 0===i||i instanceof Function||(o=new n(this.editor,{value:i}),this.appendChild(o));this.value=""}else if(this.type=="object"){this.childs=[];for(var l in e)e.hasOwnProperty(l)&&(i=e[l],void 0===i||i instanceof Function||(o=new n(this.editor,{field:l,value:i}),this.appendChild(o)));this.value=""}else this.childs=void 0,this.value=e},n.prototype.getValue=function(){if(this.type=="array"){var e=[];return this.childs.forEach(function(t){e.push(t.getValue())}),e}if(this.type=="object"){var t={};return this.childs.forEach(function(e){t[e.getField()]=e.getValue()}),t}return this.value===void 0&&this._getDomValue(),this.value},n.prototype.getLevel=function(){return this.parent?this.parent.getLevel()+1:0},n.prototype.clone=function(){var e=new n(this.editor);if(e.type=this.type,e.field=this.field,e.fieldInnerText=this.fieldInnerText,e.fieldEditable=this.fieldEditable,e.value=this.value,e.valueInnerText=this.valueInnerText,e.expanded=this.expanded,this.childs){var t=[];this.childs.forEach(function(i){var n=i.clone();n.setParent(e),t.push(n)}),e.childs=t}else e.childs=void 0;return e},n.prototype.expand=function(e){this.childs&&(this.expanded=!0,this.dom.expand&&(this.dom.expand.className="expanded"),this.showChilds(),0!=e&&this.childs.forEach(function(t){t.expand(e)}))},n.prototype.collapse=function(e){this.childs&&(this.hideChilds(),0!=e&&this.childs.forEach(function(t){t.collapse(e)}),this.dom.expand&&(this.dom.expand.className="collapsed"),this.expanded=!1)},n.prototype.showChilds=function(){var e=this.childs;if(e&&this.expanded){var t=this.dom.tr,i=t?t.parentNode:void 0;if(i){var n=this.getAppend(),o=t.nextSibling;o?i.insertBefore(n,o):i.appendChild(n),this.childs.forEach(function(e){i.insertBefore(e.getDom(),n),e.showChilds()})}}},n.prototype.hide=function(){var e=this.dom.tr,t=e?e.parentNode:void 0;t&&t.removeChild(e),this.hideChilds()},n.prototype.hideChilds=function(){var e=this.childs;if(e&&this.expanded){var t=this.getAppend();t.parentNode&&t.parentNode.removeChild(t),this.childs.forEach(function(e){e.hide()})}},n.prototype.appendChild=function(e){if(this._hasChilds()){if(e.setParent(this),e.fieldEditable=this.type=="object",this.type=="array"&&(e.index=this.childs.length),this.childs.push(e),this.expanded){var t=e.getDom(),i=this.getAppend(),n=i?i.parentNode:void 0;i&&n&&n.insertBefore(t,i),e.showChilds()}this.updateDom({updateIndexes:!0}),e.updateDom({recurse:!0})}},n.prototype.moveBefore=function(e,t){if(this._hasChilds()){var i=this.dom.tr?this.dom.tr.parentNode:void 0;if(i){var n=document.createElement("tr");n.style.height=i.clientHeight+"px",i.appendChild(n)}e.parent&&e.parent.removeChild(e),t instanceof o?this.appendChild(e):this.insertBefore(e,t),i&&i.removeChild(n)}},n.prototype.moveTo=function(e,t){if(e.parent==this){var i=this.childs.indexOf(e);t>i&&t++}var n=this.childs[t]||this.append;this.moveBefore(e,n)},n.prototype.insertBefore=function(e,t){if(this._hasChilds()){if(t==this.append)e.setParent(this),e.fieldEditable=this.type=="object",this.childs.push(e);else{var i=this.childs.indexOf(t);if(-1==i)throw new Error("Node not found");e.setParent(this),e.fieldEditable=this.type=="object",this.childs.splice(i,0,e)}if(this.expanded){var n=e.getDom(),o=t.getDom(),s=o?o.parentNode:void 0;o&&s&&s.insertBefore(n,o),e.showChilds()}this.updateDom({updateIndexes:!0}),e.updateDom({recurse:!0})}},n.prototype.insertAfter=function(e,t){if(this._hasChilds()){var i=this.childs.indexOf(t),n=this.childs[i+1];n?this.insertBefore(e,n):this.appendChild(e)}},n.prototype.search=function(e){var t,i=[],n=e?e.toLowerCase():void 0;if(delete this.searchField,delete this.searchValue,this.field!=void 0){var o=String(this.field).toLowerCase();t=o.indexOf(n),-1!=t&&(this.searchField=!0,i.push({node:this,elem:"field"})),this._updateDomField()}if(this._hasChilds()){if(this.childs){var s=[];this.childs.forEach(function(t){s=s.concat(t.search(e))}),i=i.concat(s)}if(void 0!=n){var r=!1;s.length==0?this.collapse(r):this.expand(r)}}else{if(this.value!=void 0){var a=String(this.value).toLowerCase();t=a.indexOf(n),-1!=t&&(this.searchValue=!0,i.push({node:this,elem:"value"}))}this._updateDomValue()}return i},n.prototype.scrollTo=function(e){if(!this.dom.tr||!this.dom.tr.parentNode)for(var t=this.parent,i=!1;t;)t.expand(i),t=t.parent;this.dom.tr&&this.dom.tr.parentNode&&this.editor.scrollTo(this.dom.tr.offsetTop,e)},n.focusElement=void 0,n.prototype.focus=function(e){if(n.focusElement=e,this.dom.tr&&this.dom.tr.parentNode){var t=this.dom;switch(e){case"drag":t.drag?t.drag.focus():t.menu.focus();break;case"menu":t.menu.focus();break;case"expand":this._hasChilds()?t.expand.focus():t.field&&this.fieldEditable?(t.field.focus(),util.selectContentEditable(t.field)):t.value&&!this._hasChilds()?(t.value.focus(),util.selectContentEditable(t.value)):t.menu.focus();break;case"field":t.field&&this.fieldEditable?(t.field.focus(),util.selectContentEditable(t.field)):t.value&&!this._hasChilds()?(t.value.focus(),util.selectContentEditable(t.value)):this._hasChilds()?t.expand.focus():t.menu.focus();break;case"value":default:t.value&&!this._hasChilds()?(t.value.focus(),util.selectContentEditable(t.value)):t.field&&this.fieldEditable?(t.field.focus(),util.selectContentEditable(t.field)):this._hasChilds()?t.expand.focus():t.menu.focus()}}},n.select=function(e){setTimeout(function(){util.selectContentEditable(e)},0)},n.prototype.blur=function(){this._getDomValue(!1),this._getDomField(!1)},n.prototype._duplicate=function(e){var t=e.clone();return this.insertAfter(t,e),t},n.prototype.containsNode=function(e){if(this==e)return!0;var t=this.childs;if(t)for(var i=0,n=t.length;n>i;i++)if(t[i].containsNode(e))return!0;return!1},n.prototype._move=function(e,t){if(e!=t){if(e.containsNode(this))throw new Error("Cannot move a field into a child of itself");e.parent&&e.parent.removeChild(e);var i=e.clone();e.clearDom(),t?this.insertBefore(i,t):this.appendChild(i)}},n.prototype.removeChild=function(e){if(this.childs){var t=this.childs.indexOf(e);if(-1!=t){e.hide(),delete e.searchField,delete e.searchValue;var i=this.childs.splice(t,1)[0];return this.updateDom({updateIndexes:!0}),i}}return void 0},n.prototype._remove=function(e){this.removeChild(e)},n.prototype.changeType=function(e){var t=this.type;if(t!=e){if("string"!=e&&"auto"!=e||"string"!=t&&"auto"!=t){var i,n=this.dom.tr?this.dom.tr.parentNode:void 0;i=this.expanded?this.getAppend():this.getDom();var o=i&&i.parentNode?i.nextSibling:void 0;this.hide(),this.clearDom(),this.type=e,"object"==e?(this.childs||(this.childs=[]),this.childs.forEach(function(e){e.clearDom(),delete e.index,e.fieldEditable=!0,e.field==void 0&&(e.field="")}),("string"==t||"auto"==t)&&(this.expanded=!0)):"array"==e?(this.childs||(this.childs=[]),this.childs.forEach(function(e,t){e.clearDom(),e.fieldEditable=!1,e.index=t}),("string"==t||"auto"==t)&&(this.expanded=!0)):this.expanded=!1,n&&(o?n.insertBefore(this.getDom(),o):n.appendChild(this.getDom())),this.showChilds()}else this.type=e;("auto"==e||"string"==e)&&(this.value="string"==e?String(this.value):this._stringCast(String(this.value)),this.focus()),this.updateDom({updateIndexes:!0})}},n.prototype._getDomValue=function(e){if(this.dom.value&&this.type!="array"&&this.type!="object"&&(this.valueInnerText=util.getInnerText(this.dom.value)),this.valueInnerText!=void 0)try{var t;if(this.type=="string")t=this._unescapeHTML(this.valueInnerText);else{var i=this._unescapeHTML(this.valueInnerText);t=this._stringCast(i)}if(t!==this.value){var n=this.value;this.value=t,this.editor._onAction("editValue",{node:this,oldValue:n,newValue:t,oldSelection:this.editor.selection,newSelection:this.editor.getSelection()})}}catch(o){if(this.value=void 0,1!=e)throw o}},n.prototype._updateDomValue=function(){var e=this.dom.value;if(e){var t=this.value,i=this.type=="auto"?typeof t:this.type,n="string"==i&&util.isUrl(t),o="";o=n&&!this.editor.mode.edit?"":"string"==i?"green":"number"==i?"red":"boolean"==i?"orange":this._hasChilds()?"":null===t?"#004ED0":"black",e.style.color=o;var s=String(this.value)==""&&this.type!="array"&&this.type!="object";if(s?util.addClassName(e,"empty"):util.removeClassName(e,"empty"),n?util.addClassName(e,"url"):util.removeClassName(e,"url"),"array"==i||"object"==i){var r=this.childs?this.childs.length:0;e.title=this.type+" containing "+r+" items"}else"string"==i&&util.isUrl(t)?this.editor.mode.edit&&(e.title="Ctrl+Click or Ctrl+Enter to open url in new window"):e.title="";this.searchValueActive?util.addClassName(e,"highlight-active"):util.removeClassName(e,"highlight-active"),this.searchValue?util.addClassName(e,"highlight"):util.removeClassName(e,"highlight"),util.stripFormatting(e)}},n.prototype._updateDomField=function(){var e=this.dom.field;if(e){var t=String(this.field)==""&&this.parent.type!="array";t?util.addClassName(e,"empty"):util.removeClassName(e,"empty"),this.searchFieldActive?util.addClassName(e,"highlight-active"):util.removeClassName(e,"highlight-active"),this.searchField?util.addClassName(e,"highlight"):util.removeClassName(e,"highlight"),util.stripFormatting(e)}},n.prototype._getDomField=function(e){if(this.dom.field&&this.fieldEditable&&(this.fieldInnerText=util.getInnerText(this.dom.field)),this.fieldInnerText!=void 0)try{var t=this._unescapeHTML(this.fieldInnerText);if(t!==this.field){var i=this.field;this.field=t,this.editor._onAction("editField",{node:this,oldValue:i,newValue:t,oldSelection:this.editor.selection,newSelection:this.editor.getSelection()})}}catch(n){if(this.field=void 0,1!=e)throw n}},n.prototype.clearDom=function(){this.dom={}},n.prototype.getDom=function(){var e=this.dom;if(e.tr)return e.tr;if(e.tr=document.createElement("tr"),e.tr.node=this,this.editor.mode.edit){var t=document.createElement("td");if(this.parent){var i=document.createElement("button");e.drag=i,i.className="dragarea",i.title="Drag to move this field (Alt+Shift+Arrows)",t.appendChild(i)}e.tr.appendChild(t);var n=document.createElement("td"),o=document.createElement("button");e.menu=o,o.className="contextmenu",o.title="Click to open the actions menu (Ctrl+M)",n.appendChild(e.menu),e.tr.appendChild(n)}var s=document.createElement("td");return e.tr.appendChild(s),e.tree=this._createDomTree(),s.appendChild(e.tree),this.updateDom({updateIndexes:!0}),e.tr},n.prototype._onDragStart=function(e){e=e||window.event;var t=this;this.mousemove||(this.mousemove=util.addEventListener(document,"mousemove",function(e){t._onDrag(e)})),this.mouseup||(this.mouseup=util.addEventListener(document,"mouseup",function(e){t._onDragEnd(e)})),this.editor.highlighter.lock(),this.drag={oldCursor:document.body.style.cursor,startParent:this.parent,startIndex:this.parent.childs.indexOf(this),mouseX:util.getMouseX(e),level:this.getLevel()},document.body.style.cursor="move",util.preventDefault(e)},n.prototype._onDrag=function(e){e=e||window.event;var t,i,s,r,a,l,d,h,c,u,p,m,f,v,g=util.getMouseY(e),y=util.getMouseX(e),x=!1;if(t=this.dom.tr,c=util.getAbsoluteTop(t),m=t.offsetHeight,c>g){i=t;do i=i.previousSibling,d=n.getNodeFromTarget(i),u=i?util.getAbsoluteTop(i):0;while(i&&u>g);d&&!d.parent&&(d=void 0),d||(l=t.parentNode.firstChild,i=l?l.nextSibling:void 0,d=n.getNodeFromTarget(i),d==this&&(d=void 0)),d&&(i=d.dom.tr,u=i?util.getAbsoluteTop(i):0,g>u+m&&(d=void 0)),d&&(d.parent.moveBefore(this,d),x=!0)}else if(a=this.expanded&&this.append?this.append.getDom():this.dom.tr,r=a?a.nextSibling:void 0){p=util.getAbsoluteTop(r),s=r;do h=n.getNodeFromTarget(s),s&&(f=s.nextSibling?util.getAbsoluteTop(s.nextSibling):0,v=s?f-p:0,h.parent.childs.length==1&&h.parent.childs[0]==this&&(c+=23)),s=s.nextSibling;while(s&&g>c+v);if(h&&h.parent){var b=y-this.drag.mouseX,C=Math.round(b/24/2),N=this.drag.level+C,E=h.getLevel();for(i=h.dom.tr.previousSibling;N>E&&i;){if(d=n.getNodeFromTarget(i),d==this||d._isChildOf(this));else{if(!(d instanceof o))break;var _=d.parent.childs;if(!(_.length>1||_.length==1&&_[0]!=this))break;h=n.getNodeFromTarget(i),E=h.getLevel()}i=i.previousSibling}a.nextSibling!=h.dom.tr&&(h.parent.moveBefore(this,h),x=!0)}}x&&(this.drag.mouseX=y,this.drag.level=this.getLevel()),this.editor.startAutoScroll(g),util.preventDefault(e)},n.prototype._onDragEnd=function(e){e=e||window.event;var t={node:this,startParent:this.drag.startParent,startIndex:this.drag.startIndex,endParent:this.parent,endIndex:this.parent.childs.indexOf(this)};(t.startParent!=t.endParent||t.startIndex!=t.endIndex)&&this.editor._onAction("moveNode",t),document.body.style.cursor=this.drag.oldCursor,this.editor.highlighter.unlock(),delete this.drag,this.mousemove&&(util.removeEventListener(document,"mousemove",this.mousemove),delete this.mousemove),this.mouseup&&(util.removeEventListener(document,"mouseup",this.mouseup),delete this.mouseup),this.editor.stopAutoScroll(),util.preventDefault(e)},n.prototype._isChildOf=function(e){for(var t=this.parent;t;){if(t==e)return!0;t=t.parent}return!1},n.prototype._createDomField=function(){return document.createElement("div")},n.prototype.setHighlight=function(e){this.dom.tr&&(this.dom.tr.className=e?"highlight":"",this.append&&this.append.setHighlight(e),this.childs&&this.childs.forEach(function(t){t.setHighlight(e)
|
|
}))},n.prototype.updateValue=function(e){this.value=e,this.updateDom()},n.prototype.updateField=function(e){this.field=e,this.updateDom()},n.prototype.updateDom=function(e){var t=this.dom.tree;t&&(t.style.marginLeft=this.getLevel()*24+"px");var i=this.dom.field;if(i){this.fieldEditable==1?(i.contentEditable=this.editor.mode.edit,i.spellcheck=!1,i.className="field"):i.className="readonly";var n;n=this.index!=void 0?this.index:this.field!=void 0?this.field:this._hasChilds()?this.type:"",i.innerHTML=this._escapeHTML(n)}var o=this.dom.value;if(o){var s=this.childs?this.childs.length:0;o.innerHTML=this.type=="array"?"["+s+"]":this.type=="object"?"{"+s+"}":this._escapeHTML(this.value)}this._updateDomField(),this._updateDomValue(),e&&e.updateIndexes==1&&this._updateDomIndexes(),e&&e.recurse==1&&this.childs&&this.childs.forEach(function(t){t.updateDom(e)}),this.append&&this.append.updateDom()},n.prototype._updateDomIndexes=function(){var e=this.dom.value,t=this.childs;e&&t&&(this.type=="array"?t.forEach(function(e,t){e.index=t;var i=e.dom.field;i&&(i.innerHTML=t)}):this.type=="object"&&t.forEach(function(e){e.index!=void 0&&(delete e.index,e.field==void 0&&(e.field=""))}))},n.prototype._createDomValue=function(){var e;return this.type=="array"?(e=document.createElement("div"),e.className="readonly",e.innerHTML="[...]"):this.type=="object"?(e=document.createElement("div"),e.className="readonly",e.innerHTML="{...}"):!this.editor.mode.edit&&util.isUrl(this.value)?(e=document.createElement("a"),e.className="value",e.href=this.value,e.target="_blank",e.innerHTML=this._escapeHTML(this.value)):(e=document.createElement("div"),e.contentEditable=!this.editor.mode.view,e.spellcheck=!1,e.className="value",e.innerHTML=this._escapeHTML(this.value)),e},n.prototype._createDomExpandButton=function(){var e=document.createElement("button");return this._hasChilds()?(e.className=this.expanded?"expanded":"collapsed",e.title="Click to expand/collapse this field (Ctrl+E). \nCtrl+Click to expand/collapse including all childs."):(e.className="invisible",e.title=""),e},n.prototype._createDomTree=function(){var e=this.dom,t=document.createElement("table"),i=document.createElement("tbody");t.style.borderCollapse="collapse",t.appendChild(i);var n=document.createElement("tr");i.appendChild(n);var o=document.createElement("td");o.className="tree",n.appendChild(o),e.expand=this._createDomExpandButton(),o.appendChild(e.expand),e.tdExpand=o;var s=document.createElement("td");s.className="tree",n.appendChild(s),e.field=this._createDomField(),s.appendChild(e.field),e.tdField=s;var r=document.createElement("td");r.className="tree",n.appendChild(r),this.type!="object"&&this.type!="array"&&(r.appendChild(document.createTextNode(":")),r.className="separator"),e.tdSeparator=r;var a=document.createElement("td");return a.className="tree",n.appendChild(a),e.value=this._createDomValue(),a.appendChild(e.value),e.tdValue=a,t},n.prototype.onEvent=function(e){var t,i=e.type,n=e.target||e.srcElement,o=this.dom,s=this,r=this._hasChilds();if((n==o.drag||n==o.menu)&&("mouseover"==i?this.editor.highlighter.highlight(this):"mouseout"==i&&this.editor.highlighter.unhighlight()),"mousedown"==i&&n==o.drag&&this._onDragStart(e),"click"==i&&n==o.menu){var a=s.editor.highlighter;a.highlight(s),a.lock(),util.addClassName(o.menu,"selected"),this.showContextMenu(o.menu,function(){util.removeClassName(o.menu,"selected"),a.unlock(),a.unhighlight()})}if("click"==i&&n==o.expand&&r){var l=e.ctrlKey;this._onExpand(l)}var d=o.value;if(n==d)switch(i){case"focus":t=this;break;case"blur":case"change":this._getDomValue(!0),this._updateDomValue(),this.value&&(d.innerHTML=this._escapeHTML(this.value));break;case"input":this._getDomValue(!0),this._updateDomValue();break;case"keydown":case"mousedown":this.editor.selection=this.editor.getSelection();break;case"click":e.ctrlKey&&this.editor.mode.edit&&util.isUrl(this.value)&&window.open(this.value,"_blank");break;case"keyup":this._getDomValue(!0),this._updateDomValue();break;case"cut":case"paste":setTimeout(function(){s._getDomValue(!0),s._updateDomValue()},1)}var h=o.field;if(n==h)switch(i){case"focus":t=this;break;case"blur":case"change":this._getDomField(!0),this._updateDomField(),this.field&&(h.innerHTML=this._escapeHTML(this.field));break;case"input":this._getDomField(!0),this._updateDomField();break;case"keydown":case"mousedown":this.editor.selection=this.editor.getSelection();break;case"keyup":this._getDomField(!0),this._updateDomField();break;case"cut":case"paste":setTimeout(function(){s._getDomField(!0),s._updateDomField()},1)}var c=o.tree;if(n==c.parentNode)switch(i){case"click":var u=e.offsetX!=void 0?e.offsetX<(this.getLevel()+1)*24:util.getMouseX(e)<util.getAbsoluteLeft(o.tdSeparator);u||r?h&&(util.setEndOfContentEditable(h),h.focus()):d&&(util.setEndOfContentEditable(d),d.focus())}if(n==o.tdExpand&&!r||n==o.tdField||n==o.tdSeparator)switch(i){case"click":h&&(util.setEndOfContentEditable(h),h.focus())}"keydown"==i&&this.onKeyDown(e)},n.prototype.onKeyDown=function(e){var t,i,s,r,a=e.which||e.keyCode,l=e.target||e.srcElement,d=e.ctrlKey,h=e.shiftKey,c=e.altKey,u=!1;if(13==a){if(l==this.dom.value)(!this.editor.mode.edit||e.ctrlKey)&&util.isUrl(this.value)&&(window.open(this.value,"_blank"),u=!0);else if(l==this.dom.expand){var p=this._hasChilds();if(p){var m=e.ctrlKey;this._onExpand(m),l.focus(),u=!0}}}else if(68==a)d&&(this._onDuplicate(),u=!0);else if(69==a)d&&(this._onExpand(h),l.focus(),u=!0);else if(77==a)d&&(this.showContextMenu(l),u=!0);else if(46==a)d&&(this._onRemove(),u=!0);else if(45==a)d&&!h?(this._onInsertBefore(),u=!0):d&&h&&(this._onInsertAfter(),u=!0);else if(35==a){if(c){var f=this._lastNode();f&&f.focus(n.focusElement||this._getElementName(l)),u=!0}}else if(36==a){if(c){var v=this._firstNode();v&&v.focus(n.focusElement||this._getElementName(l)),u=!0}}else if(37==a){if(c&&!h){var g=this._previousElement(l);g&&this.focus(this._getElementName(g)),u=!0}else if(c&&h){if(this.expanded){var y=this.getAppend();s=y?y.nextSibling:void 0}else{var x=this.getDom();s=x.nextSibling}s&&(i=n.getNodeFromTarget(s),r=s.nextSibling,N=n.getNodeFromTarget(r),i&&i instanceof o&&this.parent.childs.length!=1&&N&&N.parent&&(N.parent.moveBefore(this,N),this.focus(n.focusElement||this._getElementName(l))))}}else if(38==a)c&&!h?(t=this._previousNode(),t&&t.focus(n.focusElement||this._getElementName(l)),u=!0):c&&h&&(t=this._previousNode(),t&&t.parent&&(t.parent.moveBefore(this,t),this.focus(n.focusElement||this._getElementName(l))),u=!0);else if(39==a){if(c&&!h){var b=this._nextElement(l);b&&this.focus(this._getElementName(b)),u=!0}else if(c&&h){x=this.getDom();var C=x.previousSibling;C&&(t=n.getNodeFromTarget(C),t&&t.parent&&t instanceof o&&!t.isVisible()&&(t.parent.moveBefore(this,t),this.focus(n.focusElement||this._getElementName(l))))}}else if(40==a)if(c&&!h)i=this._nextNode(),i&&i.focus(n.focusElement||this._getElementName(l)),u=!0;else if(c&&h){i=this.expanded?this.append?this.append._nextNode():void 0:this._nextNode(),s=i?i.getDom():void 0,r=this.parent.childs.length==1?s:s?s.nextSibling:void 0;var N=n.getNodeFromTarget(r);N&&N.parent&&(N.parent.moveBefore(this,N),this.focus(n.focusElement||this._getElementName(l))),u=!0}u&&(util.preventDefault(e),util.stopPropagation(e))},n.prototype._onExpand=function(e){if(e){var t=this.dom.tr.parentNode,i=t.parentNode,n=i.scrollTop;i.removeChild(t)}this.expanded?this.collapse(e):this.expand(e),e&&(i.appendChild(t),i.scrollTop=n)},n.prototype._onRemove=function(){this.editor.highlighter.unhighlight();var e=this.parent.childs,t=e.indexOf(this),i=this.editor.getSelection();e[t+1]?e[t+1].focus():e[t-1]?e[t-1].focus():this.parent.focus();var n=this.editor.getSelection();this.parent._remove(this),this.editor._onAction("removeNode",{node:this,parent:this.parent,index:t,oldSelection:i,newSelection:n})},n.prototype._onDuplicate=function(){var e=this.editor.getSelection(),t=this.parent._duplicate(this);t.focus();var i=this.editor.getSelection();this.editor._onAction("duplicateNode",{node:this,clone:t,parent:this.parent,oldSelection:e,newSelection:i})},n.prototype._onInsertBefore=function(e,t,i){var o=this.editor.getSelection(),s=new n(this.editor,{field:void 0!=e?e:"",value:void 0!=t?t:"",type:i});s.expand(!0),this.parent.insertBefore(s,this),this.editor.highlighter.unhighlight(),s.focus("field");var r=this.editor.getSelection();this.editor._onAction("insertBeforeNode",{node:s,beforeNode:this,parent:this.parent,oldSelection:o,newSelection:r})},n.prototype._onInsertAfter=function(e,t,i){var o=this.editor.getSelection(),s=new n(this.editor,{field:void 0!=e?e:"",value:void 0!=t?t:"",type:i});s.expand(!0),this.parent.insertAfter(s,this),this.editor.highlighter.unhighlight(),s.focus("field");var r=this.editor.getSelection();this.editor._onAction("insertAfterNode",{node:s,afterNode:this,parent:this.parent,oldSelection:o,newSelection:r})},n.prototype._onAppend=function(e,t,i){var o=this.editor.getSelection(),s=new n(this.editor,{field:void 0!=e?e:"",value:void 0!=t?t:"",type:i});s.expand(!0),this.parent.appendChild(s),this.editor.highlighter.unhighlight(),s.focus("field");var r=this.editor.getSelection();this.editor._onAction("appendNode",{node:s,parent:this.parent,oldSelection:o,newSelection:r})},n.prototype._onChangeType=function(e){var t=this.type;if(e!=t){var i=this.editor.getSelection();this.changeType(e);var n=this.editor.getSelection();this.editor._onAction("changeType",{node:this,oldType:t,newType:e,oldSelection:i,newSelection:n})}},n.prototype._onSort=function(e){if(this._hasChilds()){var t="desc"==e?-1:1,i=this.type=="array"?"value":"field";this.hideChilds();var n=this.childs,o=this.sort;this.childs=this.childs.concat(),this.childs.sort(function(e,n){return e[i]>n[i]?t:e[i]<n[i]?-t:0}),this.sort=1==t?"asc":"desc",this.editor._onAction("sort",{node:this,oldChilds:n,oldSort:o,newChilds:this.childs,newSort:this.sort}),this.showChilds()}},n.prototype.getAppend=function(){return this.append||(this.append=new o(this.editor),this.append.setParent(this)),this.append.getDom()},n.getNodeFromTarget=function(e){for(;e;){if(e.node)return e.node;e=e.parentNode}return void 0},n.prototype._previousNode=function(){var e=null,t=this.getDom();if(t&&t.parentNode){var i=t;do i=i.previousSibling,e=n.getNodeFromTarget(i);while(i&&e instanceof o&&!e.isVisible())}return e},n.prototype._nextNode=function(){var e=null,t=this.getDom();if(t&&t.parentNode){var i=t;do i=i.nextSibling,e=n.getNodeFromTarget(i);while(i&&e instanceof o&&!e.isVisible())}return e},n.prototype._firstNode=function(){var e=null,t=this.getDom();if(t&&t.parentNode){var i=t.parentNode.firstChild;e=n.getNodeFromTarget(i)}return e},n.prototype._lastNode=function(){var e=null,t=this.getDom();if(t&&t.parentNode){var i=t.parentNode.lastChild;for(e=n.getNodeFromTarget(i);i&&e instanceof o&&!e.isVisible();)i=i.previousSibling,e=n.getNodeFromTarget(i)}return e},n.prototype._previousElement=function(e){var t=this.dom;switch(e){case t.value:if(this.fieldEditable)return t.field;case t.field:if(this._hasChilds())return t.expand;case t.expand:return t.menu;case t.menu:if(t.drag)return t.drag;default:return null}},n.prototype._nextElement=function(e){var t=this.dom;switch(e){case t.drag:return t.menu;case t.menu:if(this._hasChilds())return t.expand;case t.expand:if(this.fieldEditable)return t.field;case t.field:if(!this._hasChilds())return t.value;default:return null}},n.prototype._getElementName=function(e){var t=this.dom;for(var i in t)if(t.hasOwnProperty(i)&&t[i]==e)return i;return null},n.prototype._hasChilds=function(){return this.type=="array"||this.type=="object"},n.TYPE_TITLES={auto:'Field type "auto". The field type is automatically determined from the value and can be a string, number, boolean, or null.',object:'Field type "object". An object contains an unordered set of key/value pairs.',array:'Field type "array". An array contains an ordered collection of values.',string:'Field type "string". Field type is not determined from the value, but always returned as string.'},n.prototype.showContextMenu=function(e,t){var i=this,o=n.TYPE_TITLES,r=[];if(r.push({text:"Type",title:"Change the type of this field",className:"type-"+this.type,submenu:[{text:"Auto",className:"type-auto"+(this.type=="auto"?" selected":""),title:o.auto,click:function(){i._onChangeType("auto")}},{text:"Array",className:"type-array"+(this.type=="array"?" selected":""),title:o.array,click:function(){i._onChangeType("array")}},{text:"Object",className:"type-object"+(this.type=="object"?" selected":""),title:o.object,click:function(){i._onChangeType("object")}},{text:"String",className:"type-string"+(this.type=="string"?" selected":""),title:o.string,click:function(){i._onChangeType("string")}}]}),this._hasChilds()){var a=this.sort=="asc"?"desc":"asc";r.push({text:"Sort",title:"Sort the childs of this "+this.type,className:"sort-"+a,click:function(){i._onSort(a)},submenu:[{text:"Ascending",className:"sort-asc",title:"Sort the childs of this "+this.type+" in ascending order",click:function(){i._onSort("asc")}},{text:"Descending",className:"sort-desc",title:"Sort the childs of this "+this.type+" in descending order",click:function(){i._onSort("desc")}}]})}if(this.parent&&this.parent._hasChilds()){r.push({type:"separator"});var l=i.parent.childs;i==l[l.length-1]&&r.push({text:"Append",title:"Append a new field with type 'auto' after this field (Ctrl+Shift+Ins)",submenuTitle:"Select the type of the field to be appended",className:"append",click:function(){i._onAppend("","","auto")},submenu:[{text:"Auto",className:"type-auto",title:o.auto,click:function(){i._onAppend("","","auto")}},{text:"Array",className:"type-array",title:o.array,click:function(){i._onAppend("",[])}},{text:"Object",className:"type-object",title:o.object,click:function(){i._onAppend("",{})}},{text:"String",className:"type-string",title:o.string,click:function(){i._onAppend("","","string")}}]}),r.push({text:"Insert",title:"Insert a new field with type 'auto' before this field (Ctrl+Ins)",submenuTitle:"Select the type of the field to be inserted",className:"insert",click:function(){i._onInsertBefore("","","auto")},submenu:[{text:"Auto",className:"type-auto",title:o.auto,click:function(){i._onInsertBefore("","","auto")}},{text:"Array",className:"type-array",title:o.array,click:function(){i._onInsertBefore("",[])}},{text:"Object",className:"type-object",title:o.object,click:function(){i._onInsertBefore("",{})}},{text:"String",className:"type-string",title:o.string,click:function(){i._onInsertBefore("","","string")}}]}),r.push({text:"Duplicate",title:"Duplicate this field (Ctrl+D)",className:"duplicate",click:function(){i._onDuplicate()}}),r.push({text:"Remove",title:"Remove this field (Ctrl+Del)",className:"remove",click:function(){i._onRemove()}})}var d=new s(r,{close:t});d.show(e)},n.prototype._getType=function(e){return e instanceof Array?"array":e instanceof Object?"object":"string"==typeof e&&typeof this._stringCast(e)!="string"?"string":"auto"},n.prototype._stringCast=function(e){var t=e.toLowerCase(),i=Number(e),n=parseFloat(e);return""==e?"":"null"==t?null:"true"==t?!0:"false"==t?!1:isNaN(i)||isNaN(n)?e:i},n.prototype._escapeHTML=function(e){var t=String(e).replace(/</g,"<").replace(/>/g,">").replace(/ /g," ").replace(/^ /," ").replace(/ $/," "),i=JSON.stringify(t);return i.substring(1,i.length-1)},n.prototype._unescapeHTML=function(e){var t='"'+this._escapeJSON(e)+'"',i=util.parse(t);return i.replace(/</g,"<").replace(/>/g,">").replace(/ /g," ")},n.prototype._escapeJSON=function(e){for(var t="",i=0,n=e.length;n>i;){var o=e.charAt(i);"\n"==o?t+="\\n":"\\"==o?(t+=o,i++,o=e.charAt(i),'"\\/bfnrtu'.indexOf(o)==-1&&(t+="\\"),t+=o):t+='"'==o?'\\"':o,i++}return t},o.prototype=new n,o.prototype.getDom=function(){var e=this.dom;if(e.tr)return e.tr;var t=document.createElement("tr");if(t.node=this,e.tr=t,this.editor.mode.edit){e.tdDrag=document.createElement("td");var i=document.createElement("td");e.tdMenu=i;var n=document.createElement("button");n.className="contextmenu",n.title="Click to open the actions menu (Ctrl+M)",e.menu=n,i.appendChild(e.menu)}var o=document.createElement("td"),s=document.createElement("div");return s.innerHTML="(empty)",s.className="readonly",o.appendChild(s),e.td=o,e.text=s,this.updateDom(),t},o.prototype.updateDom=function(){var e=this.dom,t=e.td;t&&(t.style.paddingLeft=this.getLevel()*24+26+"px");var i=e.text;i&&(i.innerHTML="(empty "+this.parent.type+")");var n=e.tr;this.isVisible()?e.tr.firstChild||(e.tdDrag&&n.appendChild(e.tdDrag),e.tdMenu&&n.appendChild(e.tdMenu),n.appendChild(t)):e.tr.firstChild&&(e.tdDrag&&n.removeChild(e.tdDrag),e.tdMenu&&n.removeChild(e.tdMenu),n.removeChild(t))},o.prototype.isVisible=function(){return this.parent.childs.length==0},o.prototype.showContextMenu=function(e,t){var i=this,o=n.TYPE_TITLES,r=[{text:"Append",title:"Append a new field with type 'auto' (Ctrl+Shift+Ins)",submenuTitle:"Select the type of the field to be appended",className:"insert",click:function(){i._onAppend("","","auto")},submenu:[{text:"Auto",className:"type-auto",title:o.auto,click:function(){i._onAppend("","","auto")}},{text:"Array",className:"type-array",title:o.array,click:function(){i._onAppend("",[])}},{text:"Object",className:"type-object",title:o.object,click:function(){i._onAppend("",{})}},{text:"String",className:"type-string",title:o.string,click:function(){i._onAppend("","","string")}}]}],a=new s(r,{close:t});a.show(e)},o.prototype.onEvent=function(e){var t=e.type,i=e.target||e.srcElement,n=this.dom,o=n.menu;if(i==o&&("mouseover"==t?this.editor.highlighter.highlight(this.parent):"mouseout"==t&&this.editor.highlighter.unhighlight()),"click"==t&&i==n.menu){var s=this.editor.highlighter;s.highlight(this.parent),s.lock(),util.addClassName(n.menu,"selected"),this.showContextMenu(n.menu,function(){util.removeClassName(n.menu,"selected"),s.unlock(),s.unhighlight()})}"keydown"==t&&this.onKeyDown(e)},s.prototype._getVisibleButtons=function(){var e=[],t=this;return this.dom.items.forEach(function(i){e.push(i.button),i.buttonExpand&&e.push(i.buttonExpand),i.subItems&&i==t.expandedItem&&i.subItems.forEach(function(t){e.push(t.button),t.buttonExpand&&e.push(t.buttonExpand)})}),e},s.visibleMenu=void 0,s.prototype.show=function(e){this.hide();var t=util.getWindowHeight(),i=e.offsetHeight,n=this.maxHeight,o=util.getAbsoluteLeft(e),r=util.getAbsoluteTop(e);t>r+i+n?(this.dom.menu.style.left=o+"px",this.dom.menu.style.top=r+i+"px",this.dom.menu.style.bottom=""):(this.dom.menu.style.left=o+"px",this.dom.menu.style.top="",this.dom.menu.style.bottom=t-r+"px"),document.body.appendChild(this.dom.menu);var a=this,l=this.dom.list;this.eventListeners.mousedown=util.addEventListener(document,"mousedown",function(e){e=e||window.event;var t=e.target||e.srcElement;t==l||a._isChildOf(t,l)||(a.hide(),util.stopPropagation(e),util.preventDefault(e))}),this.eventListeners.mousewheel=util.addEventListener(document,"mousewheel",function(){util.stopPropagation(event),util.preventDefault(event)}),this.eventListeners.keydown=util.addEventListener(document,"keydown",function(e){a._onKeyDown(e)}),this.selection=util.getSelection(),this.anchor=e,setTimeout(function(){a.dom.focusButton.focus()},0),s.visibleMenu&&s.visibleMenu.hide(),s.visibleMenu=this},s.prototype.hide=function(){this.dom.menu.parentNode&&(this.dom.menu.parentNode.removeChild(this.dom.menu),this.onClose&&this.onClose());for(var e in this.eventListeners)if(this.eventListeners.hasOwnProperty(e)){var t=this.eventListeners[e];t&&util.removeEventListener(document,e,t),delete this.eventListeners[e]}s.visibleMenu==this&&(s.visibleMenu=void 0)},s.prototype._onExpandItem=function(e){var t=this,i=e==this.expandedItem,n=this.expandedItem;if(n&&(n.ul.style.height="0",n.ul.style.padding="",setTimeout(function(){t.expandedItem!=n&&(n.ul.style.display="",util.removeClassName(n.ul.parentNode,"selected"))},300),this.expandedItem=void 0),!i){var o=e.ul;o.style.display="block",o.clientHeight,setTimeout(function(){t.expandedItem==e&&(o.style.height=o.childNodes.length*24+"px",o.style.padding="5px 10px")},0),util.addClassName(o.parentNode,"selected"),this.expandedItem=e}},s.prototype._onKeyDown=function(e){e=e||window.event;var t,i,n,o,s=e.target||e.srcElement,r=e.which||e.keyCode,a=!1;27==r?(this.selection&&util.setSelection(this.selection),this.anchor&&this.anchor.focus(),this.hide(),a=!0):9==r?e.shiftKey?(t=this._getVisibleButtons(),i=t.indexOf(s),0==i&&(t[t.length-1].focus(),a=!0)):(t=this._getVisibleButtons(),i=t.indexOf(s),i==t.length-1&&(t[0].focus(),a=!0)):37==r?(s.className=="expand"&&(t=this._getVisibleButtons(),i=t.indexOf(s),n=t[i-1],n&&n.focus()),a=!0):38==r?(t=this._getVisibleButtons(),i=t.indexOf(s),n=t[i-1],n&&n.className=="expand"&&(n=t[i-2]),n||(n=t[t.length-1]),n&&n.focus(),a=!0):39==r?(t=this._getVisibleButtons(),i=t.indexOf(s),o=t[i+1],o&&o.className=="expand"&&o.focus(),a=!0):40==r&&(t=this._getVisibleButtons(),i=t.indexOf(s),o=t[i+1],o&&o.className=="expand"&&(o=t[i+2]),o||(o=t[0]),o&&(o.focus(),a=!0),a=!0),a&&(util.stopPropagation(e),util.preventDefault(e))},s.prototype._isChildOf=function(e,t){for(var i=e.parentNode;i;){if(i==t)return!0;i=i.parentNode}return!1},r.prototype.onChange=function(){},r.prototype.add=function(e,t){this.index++,this.history[this.index]={action:e,params:t,timestamp:new Date},this.index<this.history.length-1&&this.history.splice(this.index+1,this.history.length-this.index-1),this.onChange()},r.prototype.clear=function(){this.history=[],this.index=-1,this.onChange()},r.prototype.canUndo=function(){return this.index>=0},r.prototype.canRedo=function(){return this.index<this.history.length-1},r.prototype.undo=function(){if(this.canUndo()){var e=this.history[this.index];if(e){var t=this.actions[e.action];t&&t.undo?(t.undo(e.params),e.params.oldSelection&&this.editor.setSelection(e.params.oldSelection)):console.log('Error: unknown action "'+e.action+'"')}this.index--,this.onChange()}},r.prototype.redo=function(){if(this.canRedo()){this.index++;var e=this.history[this.index];if(e){var t=this.actions[e.action];t&&t.redo?(t.redo(e.params),e.params.newSelection&&this.editor.setSelection(e.params.newSelection)):console.log('Error: unknown action "'+e.action+'"')}this.onChange()}},a.prototype.next=function(e){if(this.results!=void 0){var t=this.resultIndex!=void 0?this.resultIndex+1:0;t>this.results.length-1&&(t=0),this._setActiveResult(t,e)}},a.prototype.previous=function(e){if(this.results!=void 0){var t=this.results.length-1,i=this.resultIndex!=void 0?this.resultIndex-1:t;0>i&&(i=t),this._setActiveResult(i,e)}},a.prototype._setActiveResult=function(e,t){if(this.activeResult){var i=this.activeResult.node,n=this.activeResult.elem;"field"==n?delete i.searchFieldActive:delete i.searchValueActive,i.updateDom()}if(!this.results||!this.results[e])return this.resultIndex=void 0,this.activeResult=void 0,void 0;this.resultIndex=e;var o=this.results[this.resultIndex].node,s=this.results[this.resultIndex].elem;"field"==s?o.searchFieldActive=!0:o.searchValueActive=!0,this.activeResult=this.results[this.resultIndex],o.updateDom(),o.scrollTo(function(){t&&o.focus(s)})},a.prototype._clearDelay=function(){this.timeout!=void 0&&(clearTimeout(this.timeout),delete this.timeout)},a.prototype._onDelayedSearch=function(){this._clearDelay();var e=this;this.timeout=setTimeout(function(t){e._onSearch(t)},this.delay)},a.prototype._onSearch=function(e,t){this._clearDelay();var i=this.dom.search.value,n=i.length>0?i:void 0;if(n!=this.lastText||t)if(this.lastText=n,this.results=this.editor.search(n),this._setActiveResult(void 0),void 0!=n){var o=this.results.length;switch(o){case 0:this.dom.results.innerHTML="no results";break;case 1:this.dom.results.innerHTML="1 result";break;default:this.dom.results.innerHTML=o+" results"}}else this.dom.results.innerHTML=""},a.prototype._onKeyDown=function(e){e=e||window.event;var t=e.which||e.keyCode;27==t?(this.dom.search.value="",this._onSearch(e),util.preventDefault(e),util.stopPropagation(e)):13==t&&(e.ctrlKey?this._onSearch(e,!0):e.shiftKey?this.previous():this.next(),util.preventDefault(e),util.stopPropagation(e))},a.prototype._onKeyUp=function(e){e=e||window.event;var t=e.which||e.keyCode;27!=t&&13!=t&&this._onDelayedSearch(e)},l.prototype.highlight=function(e){this.locked||(this.node!=e&&(this.node&&this.node.setHighlight(!1),this.node=e,this.node.setHighlight(!0)),this._cancelUnhighlight())},l.prototype.unhighlight=function(){if(!this.locked){var e=this;this.node&&(this._cancelUnhighlight(),this.unhighlightTimer=setTimeout(function(){e.node.setHighlight(!1),e.node=void 0,e.unhighlightTimer=void 0},0))}},l.prototype._cancelUnhighlight=function(){this.unhighlightTimer&&(clearTimeout(this.unhighlightTimer),this.unhighlightTimer=void 0)},l.prototype.lock=function(){this.locked=!0},l.prototype.unlock=function(){this.locked=!1},util={},Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var t=0;t<this.length;t++)if(this[t]==e)return t;return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(e,t){for(var i=0,n=this.length;n>i;++i)e.call(t||this,this[i],i,this)}),"undefined"==typeof console&&(console={log:function(){}}),util.parse=function(e){try{return JSON.parse(e)}catch(t){var i=util.validate(e)||t;throw new Error(i)}},util.validate=function(e){var t=void 0;try{"undefined"!=typeof jsonlint?jsonlint.parse(e):JSON.parse(e)}catch(i){t='<pre class="error">'+i.toString()+"</pre>","undefined"!=typeof jsonlint&&(t+='<a class="error" href="http://zaach.github.com/jsonlint/" target="_blank">validated by jsonlint</a>')}return t},util.extend=function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e[i]=t[i]);return e},util.clear=function(e){for(var t in e)e.hasOwnProperty(t)&&delete e[t];return e};var d=/^https?:\/\/\S+$/;util.isUrl=function(e){return("string"==typeof e||e instanceof String)&&d.test(e)},util.getAbsoluteLeft=function(e){for(var t=e.offsetLeft,i=document.body,n=e.offsetParent;null!=n&&e!=i;)t+=n.offsetLeft,t-=n.scrollLeft,n=n.offsetParent;return t},util.getAbsoluteTop=function(e){for(var t=e.offsetTop,i=document.body,n=e.offsetParent;null!=n&&n!=i;)t+=n.offsetTop,t-=n.scrollTop,n=n.offsetParent;return t},util.getMouseY=function(e){var t;return t="pageY"in e?e.pageY:e.clientY+document.documentElement.scrollTop},util.getMouseX=function(e){var t;return t="pageX"in e?e.pageX:e.clientX+document.documentElement.scrollLeft},util.getWindowHeight=function(){return"innerHeight"in window?window.innerHeight:Math.max(document.body.clientHeight,document.documentElement.clientHeight)},util.addClassName=function(e,t){var i=e.className.split(" ");i.indexOf(t)==-1&&(i.push(t),e.className=i.join(" "))},util.removeClassName=function(e,t){var i=e.className.split(" "),n=i.indexOf(t);-1!=n&&(i.splice(n,1),e.className=i.join(" "))},util.stripFormatting=function(e){for(var t=e.childNodes,i=0,n=t.length;n>i;i++){var o=t[i];o.style&&o.removeAttribute("style");var s=o.attributes;if(s)for(var r=s.length-1;r>=0;r--){var a=s[r];a.specified==1&&o.removeAttribute(a.name)}util.stripFormatting(o)}},util.setEndOfContentEditable=function(e){var t,i;document.createRange?(t=document.createRange(),t.selectNodeContents(e),t.collapse(!1),i=window.getSelection(),i.removeAllRanges(),i.addRange(t)):document.selection&&(t=document.body.createTextRange(),t.moveToElementText(e),t.collapse(!1),t.select())},util.selectContentEditable=function(e){if(e&&e.nodeName=="DIV"){var t,i;window.getSelection&&document.createRange?(i=document.createRange(),i.selectNodeContents(e),t=window.getSelection(),t.removeAllRanges(),t.addRange(i)):document.body.createTextRange&&(i=document.body.createTextRange(),i.moveToElementText(e),i.select())}},util.getSelection=function(){if(window.getSelection){var e=window.getSelection();if(e.getRangeAt&&e.rangeCount)return e.getRangeAt(0)}else if(document.selection&&document.selection.createRange)return document.selection.createRange();return null},util.setSelection=function(e){if(e)if(window.getSelection){var t=window.getSelection();t.removeAllRanges(),t.addRange(e)}else document.selection&&e.select&&e.select()},util.getSelectionOffset=function(){var e=util.getSelection();return e&&"startOffset"in e&&"endOffset"in e&&e.startContainer&&e.startContainer==e.endContainer?{startOffset:e.startOffset,endOffset:e.endOffset,container:e.startContainer.parentNode}:null},util.setSelectionOffset=function(e){if(document.createRange&&window.getSelection){var t=window.getSelection();if(t){var i=document.createRange();i.setStart(e.container.firstChild,e.startOffset),i.setEnd(e.container.firstChild,e.endOffset),util.setSelection(i)}}},util.getInnerText=function(e,t){var i=void 0==t;if(i&&(t={text:"",flush:function(){var e=this.text;return this.text="",e},set:function(e){this.text=e}}),e.nodeValue)return t.flush()+e.nodeValue;if(e.hasChildNodes()){for(var n=e.childNodes,o="",s=0,r=n.length;r>s;s++){var a=n[s];if(a.nodeName=="DIV"||a.nodeName=="P"){var l=n[s-1],d=l?l.nodeName:void 0;d&&"DIV"!=d&&"P"!=d&&"BR"!=d&&(o+="\n",t.flush()),o+=util.getInnerText(a,t),t.set("\n")}else a.nodeName=="BR"?(o+=t.flush(),t.set("\n")):o+=util.getInnerText(a,t)}return o}return e.nodeName=="P"&&util.getInternetExplorerVersion()!=-1?t.flush():""},util.getInternetExplorerVersion=function(){if(-1==h){var e=-1;if(navigator.appName=="Microsoft Internet Explorer"){var t=navigator.userAgent,i=new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");i.exec(t)!=null&&(e=parseFloat(RegExp.$1))}h=e}return h};var h=-1;util.addEventListener=function(e,t,i,n){if(e.addEventListener)return void 0===n&&(n=!1),"mousewheel"===t&&navigator.userAgent.indexOf("Firefox")>=0&&(t="DOMMouseScroll"),e.addEventListener(t,i,n),i;var o=function(){return i.call(e,window.event)};return e.attachEvent("on"+t,o),o},util.removeEventListener=function(e,t,i,n){e.removeEventListener?(void 0===n&&(n=!1),"mousewheel"===t&&navigator.userAgent.indexOf("Firefox")>=0&&(t="DOMMouseScroll"),e.removeEventListener(t,i,n)):e.detachEvent("on"+t,i)},util.stopPropagation=function(e){e||(e=window.event),e.stopPropagation?e.stopPropagation():e.cancelBubble=!0},util.preventDefault=function(e){e||(e=window.event),e.preventDefault?e.preventDefault():e.returnValue=!1};var c={JSONEditor:e,JSONFormatter:function(){throw new Error('JSONFormatter is deprecated. Use JSONEditor with mode "text" or "code" instead')},util:util},u=function(){var e=document.getElementsByTagName("script"),t=e[e.length-1].src.split("?")[0],i=t.substring(0,t.length-2)+"css",n=document.createElement("link");n.type="text/css",n.rel="stylesheet",n.href=i,document.getElementsByTagName("head")[0].appendChild(n)};"undefined"!=typeof module&&"undefined"!=typeof exports&&(u(),module.exports=exports=c),"undefined"!=typeof require&&"undefined"!=typeof define?define(function(){return u(),c}):window.jsoneditor=c})(); |