forked from DavidDurman/FlexiJsonEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.jsoneditor.min.js
More file actions
8 lines (5 loc) · 3.7 KB
/
Copy pathjquery.jsoneditor.min.js
File metadata and controls
8 lines (5 loc) · 3.7 KB
1
2
3
4
5
6
7
8
// Simple yet flexible JSON editor plugin.
// Turns any element into a stylable interactive JSON editor.
// Copyright (c) 2013 David Durman
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
!function(t){function n(n,e,r,a,i,o,l){var c={target:n,onchange:r,onpropertyclick:a,original:e,propertyElement:i,valueElement:o,divChar:l};d(c,e,c.target),t(c.target).on("blur focus",".property, .value",function(){t(this).toggleClass("editing")})}function e(t){return"[object Object]"==Object.prototype.toString.call(t)}function r(t){return"[object Array]"==Object.prototype.toString.call(t)}function a(t){return"[object Boolean]"==Object.prototype.toString.call(t)}function i(t){return"[object Number]"==Object.prototype.toString.call(t)}function o(t){return"[object String]"==Object.prototype.toString.call(t)}function l(t,n,e){var r=2==arguments.length;if(n.indexOf(S)>-1){for(var a=t,i=0,o=n.split(S),l=o.length;l-1>i;i++)a=a[o[i]];r?delete a[o[l-1]]:a[o[l-1]]=e}else r?delete t[n]:t[n]=e;return t}function c(t,n,e){n=n.split(S);for(var r=0;r<n.length;)if(void 0==(t=t[n[r++]]))return e;return t}function u(t){window.console&&console.error(t)}function p(t){var n;try{n=JSON.parse(t)}catch(e){n=null,u("JSON parse failed.")}return n}function s(t){var n;try{n=JSON.stringify(t)}catch(e){n="null",u("JSON stringify failed.")}return n}function h(n){if(0==n.children(".expander").length){var e=t("<span>",{"class":"expander"});e.bind("click",function(){var n=t(this).parent();n.toggleClass("expanded")}),n.prepend(e)}}function f(n,e){var r=t("<div>",{"class":"item appender"}),a=t("<button></button>",{"class":"property"});return a.text("Add New Value"),r.append(a),n.append(r),a.click(e),r}function v(t){if(r(t))return t.push(null),!0;if(e(t)){for(var n=1,a="newKey";t.hasOwnProperty(a);)a="newKey"+n,n++;return t[a]=null,!0}return!1}function d(n,a,i,o){o=o||"",i.children(".item").remove(),S=n.divChar||".";for(var l in a)if(a.hasOwnProperty(l)){var c=t("<div>",{"class":"item","data-path":o}),u=t(n.propertyElement||"<input>",{"class":"property"}),g=t(n.valueElement||"<input>",{"class":"value"});(e(a[l])||r(a[l]))&&h(c),c.append(u).append(g),i.append(c),u.val(l).attr("title",l);var O=s(a[l]);g.val(O).attr("title",O),j(c,a[l]),u.change(b(n)),g.change(m(n)),u.click(y(n)),(e(a[l])||r(a[l]))&&d(n,a[l],c,(o?o+S:"")+l)}(e(a)||r(a))&&f(i,function(){v(a),d(n,a,i,o),n.onchange(p(s(n.original)))})}function g(n,e){t(n).parentsUntil(e.target).each(function(){var n=t(this).data("path");n=(n?n+S:n)+t(this).children(".property").val();var r=s(c(e.original,n,null));t(this).children(".value").val(r).attr("title",r)})}function y(n){return function(){var e=t(this).parent().data("path"),r=t(this).attr("title"),a=e?e.split(S).concat([r]).join("']['"):r;n.onpropertyclick("['"+a+"']")}}function b(n){return function(){var e=t(this).parent().data("path"),r=p(t(this).next().val()),a=t(this).val(),i=t(this).attr("title");t(this).attr("title",a),l(n.original,(e?e+S:"")+i),a&&l(n.original,(e?e+S:"")+a,r),g(this,n),a||t(this).parent().remove(),n.onchange(p(s(n.original)))}}function m(n){return function(){var a=t(this).prev().val(),i=p(t(this).val()||"null"),o=t(this).parent(),c=o.data("path");l(n.original,(c?c+S:"")+a,i),!e(i)&&!r(i)||t.isEmptyObject(i)?o.find(".expander, .item").remove():(d(n,i,o,(c?c+S:"")+a),h(o)),j(o,i),g(this,n),n.onchange(p(s(n.original)))}}function j(t,n){var l="null";e(n)?l="object":r(n)?l="array":a(n)?l="boolean":o(n)?l="string":i(n)&&(l="number"),t.removeClass(O),t.addClass(l)}t.fn.jsonEditor=function(e,r){r=r||{},e=p(s(e));var a=function(){},i=r.change||a,o=r.propertyclick||a;return this.each(function(){n(t(this),e,i,o,r.propertyElement,r.valueElement,r.divChar)})};var O="object array boolean number string null",S="."}(jQuery);