btree.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!doctype html>
  2. <title>CodeMirror: B-Tree visualization</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <script src="../lib/codemirror.js"></script>
  7. <style type="text/css">
  8. .lineblock { display: inline-block; margin: 1px; height: 5px; }
  9. .CodeMirror {border: 1px solid #aaa; height: 400px}
  10. </style>
  11. <div id=nav>
  12. <a href="http://codemirror.net"><img id=logo src="../doc/logo.png"></a>
  13. <ul>
  14. <li><a href="../index.html">Home</a>
  15. <li><a href="../doc/manual.html">Manual</a>
  16. <li><a href="https://github.com/marijnh/codemirror">Code</a>
  17. </ul>
  18. <ul>
  19. <li><a class=active href="#">B-Tree visualization</a>
  20. </ul>
  21. </div>
  22. <article>
  23. <h2>B-Tree visualization</h2>
  24. <form><textarea id="code" name="code">type here, see a summary of the document b-tree below</textarea></form>
  25. </div>
  26. <div style="display: inline-block; height: 402px; overflow-y: auto" id="output"></div>
  27. </div>
  28. <script id="me">
  29. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  30. lineNumbers: true,
  31. lineWrapping: true
  32. });
  33. var updateTimeout;
  34. editor.on("change", function(cm) {
  35. clearTimeout(updateTimeout);
  36. updateTimeout = setTimeout(updateVisual, 200);
  37. });
  38. updateVisual();
  39. function updateVisual() {
  40. var out = document.getElementById("output");
  41. out.innerHTML = "";
  42. function drawTree(out, node) {
  43. if (node.lines) {
  44. out.appendChild(document.createElement("div")).innerHTML =
  45. "<b>leaf</b>: " + node.lines.length + " lines, " + Math.round(node.height) + " px";
  46. var lines = out.appendChild(document.createElement("div"));
  47. lines.style.lineHeight = "6px"; lines.style.marginLeft = "10px";
  48. for (var i = 0; i < node.lines.length; ++i) {
  49. var line = node.lines[i], lineElt = lines.appendChild(document.createElement("div"));
  50. lineElt.className = "lineblock";
  51. var gray = Math.min(line.text.length * 3, 230), col = gray.toString(16);
  52. if (col.length == 1) col = "0" + col;
  53. lineElt.style.background = "#" + col + col + col;
  54. lineElt.style.width = Math.max(Math.round(line.height / 3), 1) + "px";
  55. }
  56. } else {
  57. out.appendChild(document.createElement("div")).innerHTML =
  58. "<b>node</b>: " + node.size + " lines, " + Math.round(node.height) + " px";
  59. var sub = out.appendChild(document.createElement("div"));
  60. sub.style.paddingLeft = "20px";
  61. for (var i = 0; i < node.children.length; ++i)
  62. drawTree(sub, node.children[i]);
  63. }
  64. }
  65. drawTree(out, editor.getDoc());
  66. }
  67. function fillEditor() {
  68. var sc = document.getElementById("me");
  69. var doc = (sc.textContent || sc.innerText || sc.innerHTML).replace(/^\s*/, "") + "\n";
  70. doc += doc; doc += doc; doc += doc; doc += doc; doc += doc; doc += doc;
  71. editor.setValue(doc);
  72. }
  73. </script>
  74. <p><button onclick="fillEditor()">Add a lot of content</button></p>
  75. </article>