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