jquery.json-editor.min.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ! function() {
  2. var e = '/* Syntax highlighting for JSON objects */ .json-editor-blackbord { background: #1c2833; color: #fff; font-size: 13px; font-family: Menlo,Monaco,Consolas,"Courier New",monospace; } @media screen and (min-width: 1600px) { .json-editor-blackbord { font-size: 14px; } } ul.json-dict, ol.json-array { list-style-type: none; margin: 0 0 0 1px; border-left: 1px dotted #525252; padding-left: 2em; } .json-string { /*color: #0B7500;*/ /*color: #BCCB86;*/ color: #0ad161; } .json-literal { /*color: #1A01CC;*/ /*font-weight: bold;*/ color: #ff8c00; } .json-url { color: #1e90ff; } .json-property { color: #4fdee5; line-height: 160%; font-weight: 500; } /* Toggle button */ a.json-toggle { position: relative; color: inherit; text-decoration: none; cursor: pointer; } a.json-toggle:focus { outline: none; } a.json-toggle:before { color: #aaa; content: "\\25BC"; /* down arrow */ position: absolute; display: inline-block; width: 1em; left: -1em; } a.json-toggle.collapsed:before { transform: rotate(-90deg); /* Use rotated down arrow, prevents right arrow appearing smaller than down arrow in some browsers */ -ms-transform: rotate(-90deg); -webkit-transform: rotate(-90deg); } /* Collapsable placeholder links */ a.json-placeholder { color: #aaa; padding: 0 1em; text-decoration: none; cursor: pointer; } a.json-placeholder:hover { text-decoration: underline; }',
  3. o = function(e) {
  4. var o = document.getElementsByTagName("head")[0],
  5. t = document.createElement("style");
  6. if (o.appendChild(t), t.styleSheet) t.styleSheet.disabled || (t.styleSheet.cssText = e);
  7. else try {
  8. t.innerHTML = e
  9. } catch (n) {
  10. t.innerText = e
  11. }
  12. };
  13. o(e)
  14. }(),
  15. function(e) {
  16. function o(e) {
  17. return e instanceof Object && Object.keys(e).length > 0
  18. }
  19. function t(e) {
  20. var o = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  21. return o.test(e)
  22. }
  23. function n(e, r) {
  24. var s = "";
  25. if ("string" == typeof e) e = e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"), s += t(e) ? '<a href="' + e + '" class="json-string json-url">"' + e + '"</a>' : '<span class="json-string">"' + e + '"</span>';
  26. else if ("number" == typeof e) s += '<span class="json-literal json-literal-number">' + e + "</span>";
  27. else if ("boolean" == typeof e) s += '<span class="json-literal json-literal-boolean">' + e + "</span>";
  28. else if (null === e) s += '<span class="json-literal json-literal-null">null</span>';
  29. else if (e instanceof Array)
  30. if (e.length > 0) {
  31. s += '[<ol class="json-array">';
  32. for (var l = 0; l < e.length; ++l) s += "<li>", o(e[l]) && (s += '<a href class="json-toggle"></a>'), s += n(e[l], r), l < e.length - 1 && (s += ","), s += "</li>";
  33. s += "</ol>]"
  34. } else s += "[]";
  35. else if ("object" == typeof e) {
  36. var a = Object.keys(e).length;
  37. if (a > 0) {
  38. s += '{<ul class="json-dict">';
  39. for (var i in e)
  40. if (e.hasOwnProperty(i)) {
  41. s += "<li>";
  42. var c = r.withQuotes ? '<span class="json-string json-property">"' + i + '"</span>' : '<span class="json-property">' + i + "</span>";
  43. s += o(e[i]) ? '<a href class="json-toggle"></a>' + c : c, s += ": " + n(e[i], r), --a > 0 && (s += ","), s += "</li>"
  44. }
  45. s += "</ul>}"
  46. } else s += "{}"
  47. } else if ('symbol' == typeof e) {
  48. s += '<span class="json-string">"' + e.toString() + '"</span>';
  49. }
  50. return s
  51. }
  52. e.fn.jsonViewer = function(t, r) {
  53. return r = r || {}, this.each(function() {
  54. var s = n(t, r);
  55. o(t) && (s = '<a href class="json-toggle"></a>' + s), e(this).html(s), e(this).off("click"), e(this).on("click", "a.json-toggle", function() {
  56. var o = e(this).toggleClass("collapsed").siblings("ul.json-dict, ol.json-array");
  57. if (o.toggle(), o.is(":visible")) o.siblings(".json-placeholder").remove();
  58. else {
  59. var t = o.children("li").length,
  60. n = t + (t > 1 ? " items" : " item");
  61. o.after('<a href class="json-placeholder">' + n + "</a>")
  62. }
  63. return !1
  64. }), e(this).on("click", "a.json-placeholder", function() {
  65. return e(this).siblings("a.json-toggle").click(), !1
  66. }), 1 == r.collapsed && e(this).find("a.json-toggle").click()
  67. })
  68. }
  69. }(jQuery),
  70. function(e) {
  71. function o(e) {
  72. var o = {
  73. '"': '\\"',
  74. "\\": "\\\\",
  75. "\b": "\\b",
  76. "\f": "\\f",
  77. "\n": "\\n",
  78. "\r": "\\r",
  79. " ": "\\t"
  80. };
  81. return e.replace(/["\\\b\f\n\r\t]/g, function(e) {
  82. return o[e]
  83. })
  84. }
  85. function t(e) {
  86. if ("string" == typeof e) return o(e);
  87. if ("object" == typeof e)
  88. for (var n in e) e[n] = t(e[n]);
  89. else if (Array.isArray(e))
  90. for (var r = 0; r < e.length; r++) e[r] = t(e[r]);
  91. return e
  92. }
  93. function n(o, t, n) {
  94. n = n || {}, n.editable !== !1 && (n.editable = !0), this.$container = e(o), this.options = n, this.load(t)
  95. }
  96. n.prototype = {
  97. constructor: n,
  98. load: function(e) {
  99. this.$container.jsonViewer(t(e), {
  100. collapsed: this.options.defaultCollapsed,
  101. withQuotes: !0
  102. }).addClass("json-editor-blackbord").attr("contenteditable", !!this.options.editable)
  103. },
  104. get: function() {
  105. try {
  106. return this.$container.find(".collapsed").click(), JSON.parse(this.$container.text())
  107. } catch (e) {
  108. throw new Error(e)
  109. }
  110. }
  111. }, window.JsonEditor = n
  112. }(jQuery);