jquery.json-editor.min.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (e['_type'] && (e['_type'].value === 'real' || e['_type'].value === 'int')) {
  37. e._value = e._value.toNumber();
  38. }
  39. var a = Object.keys(e).length;
  40. if (a > 0) {
  41. s += '{<ul class="json-dict">';
  42. for (var i in e)
  43. if (e.hasOwnProperty(i)) {
  44. s += "<li>";
  45. var c = r.withQuotes ? '<span class="json-string json-property">"' + i + '"</span>' : '<span class="json-property">' + i + "</span>";
  46. s += o(e[i]) ? '<a href class="json-toggle"></a>' + c : c, s += ": " + n(e[i], r), --a > 0 && (s += ","), s += "</li>"
  47. }
  48. s += "</ul>}"
  49. } else s += "{}"
  50. } else if ('symbol' == typeof e) {
  51. s += '<span class="json-string">"' + e.toString() + '"</span>';
  52. }
  53. return s
  54. }
  55. e.fn.jsonViewer = function(t, r) {
  56. return r = r || {}, this.each(function() {
  57. var s = n(t, r);
  58. 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() {
  59. var o = e(this).toggleClass("collapsed").siblings("ul.json-dict, ol.json-array");
  60. if (o.toggle(), o.is(":visible")) o.siblings(".json-placeholder").remove();
  61. else {
  62. var t = o.children("li").length,
  63. n = t + (t > 1 ? " items" : " item");
  64. o.after('<a href class="json-placeholder">' + n + "</a>")
  65. }
  66. return !1
  67. }), e(this).on("click", "a.json-placeholder", function() {
  68. return e(this).siblings("a.json-toggle").click(), !1
  69. }), 1 == r.collapsed && e(this).find("a.json-toggle").click()
  70. })
  71. }
  72. }(jQuery),
  73. function(e) {
  74. function o(e) {
  75. var o = {
  76. '"': '\\"',
  77. "\\": "\\\\",
  78. "\b": "\\b",
  79. "\f": "\\f",
  80. "\n": "\\n",
  81. "\r": "\\r",
  82. " ": "\\t"
  83. };
  84. return e.replace(/["\\\b\f\n\r\t]/g, function(e) {
  85. return o[e]
  86. })
  87. }
  88. function t(e) {
  89. if ("string" == typeof e) return o(e);
  90. if ("object" == typeof e)
  91. for (var n in e) e[n] = t(e[n]);
  92. else if (Array.isArray(e))
  93. for (var r = 0; r < e.length; r++) e[r] = t(e[r]);
  94. return e
  95. }
  96. function n(o, t, n) {
  97. n = n || {}, n.editable !== !1 && (n.editable = !0), this.$container = e(o), this.options = n, this.load(t)
  98. }
  99. n.prototype = {
  100. constructor: n,
  101. load: function(e) {
  102. this.$container.jsonViewer(t(e), {
  103. collapsed: this.options.defaultCollapsed,
  104. withQuotes: !0
  105. }).addClass("json-editor-blackbord").attr("contenteditable", !!this.options.editable)
  106. },
  107. get: function() {
  108. try {
  109. return this.$container.find(".collapsed").click(), JSON.parse(this.$container.text())
  110. } catch (e) {
  111. throw new Error(e)
  112. }
  113. }
  114. }, window.JsonEditor = n
  115. }(jQuery);