-
Notifications
You must be signed in to change notification settings - Fork 139
/
jquery.jsoneditor.min.js
14 lines (11 loc) · 3.54 KB
/
jquery.jsoneditor.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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(e){function v(b,a,c,d,f,h){b={target:b,onchange:c,onpropertyclick:d,original:a,propertyElement:f,valueElement:h};r(b,a,b.target);e(b.target).on("blur focus",".property, .value",function(){e(this).toggleClass("editing")})}function l(b){return"[object Object]"==Object.prototype.toString.call(b)}function m(b){return"[object Array]"==Object.prototype.toString.call(b)}function n(b,a,c){var d=2==arguments.length;if(-1<a.indexOf(".")){for(var f=b,e=0,g=a.split("."),k=g.length;e<k-1;e++)f=f[g[e]];
d?delete f[g[k-1]]:f[g[k-1]]=c}else d?delete b[a]:b[a]=c;return b}function p(b){var a;try{a=JSON.parse(b)}catch(c){a=null,window.console&&console.error("JSON parse failed.")}return a}function q(b){var a;try{a=JSON.stringify(b)}catch(c){a="null",window.console&&console.error("JSON stringify failed.")}return a}function s(b){if(0==b.children(".expander").length){var a=e("<span>",{"class":"expander"});a.bind("click",function(){e(this).parent().toggleClass("expanded")});b.prepend(a)}}function w(b,a){var c=
e("<div>",{"class":"item appender"}),d=e("<button></button>",{"class":"property"});d.text("Add New Value");c.append(d);b.append(c);d.click(a);return c}function r(b,a,c,d){d=d||"";c.children(".item").remove();for(var f in a)if(a.hasOwnProperty(f)){var h=e("<div>",{"class":"item","data-path":d}),g=e(b.propertyElement||"<input>",{"class":"property"}),k=e(b.valueElement||"<input>",{"class":"value"});(l(a[f])||m(a[f]))&&s(h);h.append(g).append(k);c.append(h);g.val(f).attr("title",f);var n=q(a[f]);k.val(n).attr("title",
n);t(h,a[f]);g.change(x(b));k.change(y(b));g.click(z(b));(l(a[f])||m(a[f]))&&r(b,a[f],h,(d?d+".":"")+f)}(l(a)||m(a))&&w(c,function(){if(m(a))a.push(null);else if(l(a)){for(var e=1,f="newKey";a.hasOwnProperty(f);)f="newKey"+e,e++;a[f]=null}r(b,a,c,d);b.onchange(p(q(b.original)))})}function u(b,a){e(b).parentsUntil(a.target).each(function(){var b=e(this).data("path"),b=(b?b+".":b)+e(this).children(".property").val(),d;a:{d=a.original;for(var b=b.split("."),f=0;f<b.length;)if(void 0==(d=d[b[f++]])){d=
null;break a}}d=q(d);e(this).children(".value").val(d).attr("title",d)})}function z(b){return function(){var a=e(this).parent().data("path"),c=e(this).attr("title"),a=a?a.split(".").concat([c]).join("']['"):c;b.onpropertyclick("['"+a+"']")}}function x(b){return function(){var a=e(this).parent().data("path"),c=p(e(this).next().val()),d=e(this).val(),f=e(this).attr("title");e(this).attr("title",d);n(b.original,(a?a+".":"")+f);d&&n(b.original,(a?a+".":"")+d,c);u(this,b);d||e(this).parent().remove();
b.onchange(p(q(b.original)))}}function y(b){return function(){var a=e(this).prev().val(),c=p(e(this).val()||"null"),d=e(this).parent(),f=d.data("path");n(b.original,(f?f+".":"")+a,c);!l(c)&&!m(c)||e.isEmptyObject(c)?d.find(".expander, .item").remove():(r(b,c,d,(f?f+".":"")+a),s(d));t(d,c);u(this,b);b.onchange(p(q(b.original)))}}function t(b,a){var c="null";l(a)?c="object":m(a)?c="array":"[object Boolean]"==Object.prototype.toString.call(a)?c="boolean":"[object String]"==Object.prototype.toString.call(a)?
c="string":"[object Number]"==Object.prototype.toString.call(a)&&(c="number");b.removeClass(A);b.addClass(c)}e.fn.jsonEditor=function(b,a){a=a||{};b=p(q(b));var c=function(){},d=a.change||c,f=a.propertyclick||c;return this.each(function(){v(e(this),b,d,f,a.propertyElement,a.valueElement)})};var A="object array boolean number string null"})(jQuery);