e4c62057c1e267a64c2dc47a6dd0fd4f565a1887.svn-base 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function JsonTree(jsonData) {
  2. function isArray(o) {
  3. if (Array.isArray) {
  4. return Array.isArray(o);
  5. }
  6. if (Object.prototype.toString.call(o) === '[object Array]') {
  7. return true;
  8. }
  9. return false;
  10. }
  11. function htmlEncode(str) {
  12. var div = document.createElement("div");
  13. div.appendChild(document.createTextNode(str));
  14. return div.innerHTML;
  15. }
  16. //building the jsonData as dom elements
  17. this.getTree = function(domId) {
  18. if (this.display === true) {
  19. return;
  20. } else {
  21. this.display = true;
  22. }
  23. var html = '';
  24. var dataType = isArray(jsonData) ? 'arr' : '';
  25. var tree = document.createElement('div');
  26. tree.id = domId || "tree";
  27. tree.className = 'tree';
  28. //tree.id = "JsonViewer";
  29. tree.innerHTML = buildDom(jsonData, dataType);
  30. bindEvents(tree);
  31. return tree;
  32. }
  33. //building doms
  34. function buildDom(o, literal) {
  35. // null object
  36. var type = o === null
  37. ? 'null'
  38. : (isArray(o) ? 'array' : typeof o);
  39. var html = '';
  40. switch(type) {
  41. case 'array' :
  42. for (var i = 0, len = o.length; i < len; i++) {
  43. html += '<li class = \'tree-close\' title=\''
  44. + literal + '[' + i + ']\'><strong>'
  45. + i + '</strong>:'
  46. + buildDom(o[i], literal + '[' + i + ']')
  47. + ',</li>';
  48. }
  49. return '<span class="operator">+</span><div class="group">'
  50. + '[<ul class="' + type + '">'
  51. + html.replace(/,<\/li>$/, '<\/li>')
  52. + '</ul>]</div><div class="summary">Array['
  53. + len + ']</div>';
  54. break;
  55. case 'object':
  56. //sort obj
  57. //var keys = Object.keys(o);
  58. //keys.sort();
  59. for (var key in o) {
  60. //quote numeric property
  61. if (/^\d+$/.test(key)) {
  62. html += '<li class = \'tree-close\' title=\'' + literal
  63. + '["' + key + '"]\'><strong>"'
  64. + key + '"</strong>:'
  65. + buildDom(
  66. o[key], literal + '["' + key + '"]'
  67. )
  68. + ',</li>';
  69. } else {
  70. html += '<li class = \'tree-close\' title=\''
  71. + key
  72. + '\'><a href=\'#' + (literal == '' ? key : literal).charAt(0).toUpperCase()
  73. + (literal == '' ? key : literal).slice(1)
  74. + '\'><strong>' + key + '</strong></a>:'
  75. + buildDom(o[key], literal == '' ? key : literal)
  76. + ',</li>';
  77. }
  78. }
  79. //remove last comma
  80. return '<span class="operator">+</span><div class="group">'
  81. +'{<ul class="' + type + '">'
  82. + html.replace(/,<\/li>$/, '<\/li>')
  83. + '</ul>}</div><div class="summary">Object</div>';
  84. break;
  85. case 'string':
  86. return '<span class="value ' + type + '">"'
  87. + (/^https?\:(\/\/).*$/i.test(o)
  88. ? '<a href="' + o + '" target="_blank">' + o + '</a>'
  89. : htmlEncode(o) ) + '"</span>';
  90. break;
  91. default :
  92. return '<span class="value ' + type + '">' + o + '</span>';
  93. }
  94. }
  95. function bindEvents(tree) {
  96. tree.onclick = function(e) {
  97. e = e || window.event;
  98. var src = e.srcElement || e.target;
  99. if (src.className === 'operator') {
  100. if (src.parentNode.className == 'tree-close') {
  101. src.parentNode.className = 'open';
  102. src.innerHTML = '-';
  103. } else {
  104. src.parentNode.className = 'tree-close';
  105. src.innerHTML = '+';
  106. }
  107. }
  108. _resize();
  109. }
  110. }
  111. }
  112. var domConfig = document.getElementById('config');
  113. var domToc = document.getElementById('toc');
  114. domConfig.appendChild(new JsonTree(echartsConfig).getTree());
  115. function _resize() {
  116. var viewHeight = document.documentElement.clientHeight;
  117. var scrollHeight = document.documentElement.scrollTop
  118. || document.body.scrollTop;
  119. var offsetHeight = document.body.offsetHeight;
  120. var maxHeight;
  121. var footHole = offsetHeight - scrollHeight - viewHeight;
  122. if (footHole > 230) {
  123. // 未见footer,60 top、bottom, 40 per one
  124. maxHeight = viewHeight - 100 - 40 * 2;
  125. }
  126. else {
  127. // 见footer
  128. maxHeight = viewHeight - 350 - 40 * 2;
  129. }
  130. if (domConfig.scrollHeight > maxHeight) {
  131. domConfig.style.height = maxHeight + 'px';
  132. } else {
  133. domConfig.style.height = 'auto';
  134. }
  135. if (domToc.scrollHeight > maxHeight) {
  136. domToc.style.height = maxHeight + 'px';
  137. } else {
  138. domToc.style.height = 'auto';
  139. }
  140. }
  141. $(window).on('scroll', _resize);
  142. $(window).on('resize', _resize);
  143. _resize();