The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame^] | 1 | |
| 2 | function new_node(me, mom, text, link, children_data) |
| 3 | { |
| 4 | var node = new Object(); |
| 5 | node.children = Array(); |
| 6 | node.children_data = children_data; |
| 7 | node.depth = mom.depth + 1; |
| 8 | |
| 9 | node.li = document.createElement("li"); |
| 10 | mom.get_children_ul().appendChild(node.li); |
| 11 | |
| 12 | node.label_div = document.createElement("div"); |
| 13 | node.li.appendChild(node.label_div); |
| 14 | node.label_div.style.paddingLeft = 10*node.depth + "px"; |
| 15 | node.label_div.className = "label"; |
| 16 | |
| 17 | if (children_data == null) { |
| 18 | // 12 is the width of the triangle and padding extra space |
| 19 | node.label_div.style.paddingLeft = ((10*node.depth)+12) + "px"; |
| 20 | } else { |
| 21 | node.label_div.style.paddingLeft = 10*node.depth + "px"; |
| 22 | node.expand_toggle = document.createElement("a"); |
| 23 | node.expand_toggle.href = "javascript:void(0)"; |
| 24 | node.expand_toggle.onclick = function() { |
| 25 | if (node.expanded) { |
| 26 | $(node.get_children_ul()).slideUp("fast"); |
| 27 | node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png"; |
| 28 | node.expanded = false; |
| 29 | } else { |
| 30 | expand_node(me, node); |
| 31 | } |
| 32 | }; |
| 33 | node.label_div.appendChild(node.expand_toggle); |
| 34 | |
| 35 | node.plus_img = document.createElement("img"); |
| 36 | node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png"; |
| 37 | node.plus_img.className = "plus"; |
| 38 | node.plus_img.border = "0"; |
| 39 | node.expand_toggle.appendChild(node.plus_img); |
| 40 | |
| 41 | node.expanded = false; |
| 42 | } |
| 43 | |
| 44 | var a = document.createElement("a"); |
| 45 | node.label_div.appendChild(a); |
| 46 | node.label = document.createTextNode(text); |
| 47 | a.appendChild(node.label); |
| 48 | if (link) { |
| 49 | a.href = me.toroot + link; |
| 50 | } else { |
| 51 | if (children_data != null) { |
| 52 | a.className = "nolink"; |
| 53 | a.href = "javascript:void(0)"; |
| 54 | a.onclick = node.expand_toggle.onclick; |
| 55 | // This next line shouldn't be necessary. I'll buy a beer for the first |
| 56 | // person who figures out how to remove this line and have the link |
| 57 | // toggle shut on the first try. --joeo@android.com |
| 58 | node.expanded = false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | node.children_ul = null; |
| 64 | node.get_children_ul = function() { |
| 65 | if (!node.children_ul) { |
| 66 | node.children_ul = document.createElement("ul"); |
| 67 | node.children_ul.className = "children_ul"; |
| 68 | node.children_ul.style.display = "none"; |
| 69 | node.li.appendChild(node.children_ul); |
| 70 | } |
| 71 | return node.children_ul; |
| 72 | }; |
| 73 | |
| 74 | return node; |
| 75 | } |
| 76 | |
| 77 | function expand_node(me, node) |
| 78 | { |
| 79 | if (node.children_data && !node.expanded) { |
| 80 | if (node.children_visited) { |
| 81 | $(node.get_children_ul()).slideDown("fast"); |
| 82 | } else { |
| 83 | get_node(me, node); |
| 84 | $(node.get_children_ul()).slideDown("fast"); |
| 85 | } |
| 86 | node.plus_img.src = me.toroot + "assets/images/triangle-opened-small.png"; |
| 87 | node.expanded = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function get_node(me, mom) |
| 92 | { |
| 93 | mom.children_visited = true; |
| 94 | for (var i in mom.children_data) { |
| 95 | var node_data = mom.children_data[i]; |
| 96 | mom.children[i] = new_node(me, mom, node_data[0], node_data[1], |
| 97 | node_data[2]); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | function this_page_relative(toroot) |
| 102 | { |
| 103 | var full = document.location.pathname; |
| 104 | var file = ""; |
| 105 | if (toroot.substr(0, 1) == "/") { |
| 106 | if (full.substr(0, toroot.length) == toroot) { |
| 107 | return full.substr(toroot.length); |
| 108 | } else { |
| 109 | // the file isn't under toroot. Fail. |
| 110 | return null; |
| 111 | } |
| 112 | } else { |
| 113 | if (toroot != "./") { |
| 114 | toroot = "./" + toroot; |
| 115 | } |
| 116 | do { |
| 117 | if (toroot.substr(toroot.length-3, 3) == "../" || toroot == "./") { |
| 118 | var pos = full.lastIndexOf("/"); |
| 119 | file = full.substr(pos) + file; |
| 120 | full = full.substr(0, pos); |
| 121 | toroot = toroot.substr(0, toroot.length-3); |
| 122 | } |
| 123 | } while (toroot != "" && toroot != "/"); |
| 124 | return file.substr(1); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | function find_page(url, data) |
| 129 | { |
| 130 | var nodes = data; |
| 131 | var result = null; |
| 132 | for (var i in nodes) { |
| 133 | var d = nodes[i]; |
| 134 | if (d[1] == url) { |
| 135 | return new Array(i); |
| 136 | } |
| 137 | else if (d[2] != null) { |
| 138 | result = find_page(url, d[2]); |
| 139 | if (result != null) { |
| 140 | return (new Array(i).concat(result)); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | function init_navtree(navtree_id, toroot, root_nodes) |
| 148 | { |
| 149 | var me = new Object(); |
| 150 | me.toroot = toroot; |
| 151 | me.node = new Object(); |
| 152 | |
| 153 | me.node.li = document.getElementById(navtree_id); |
| 154 | me.node.children_data = root_nodes; |
| 155 | me.node.children = new Array(); |
| 156 | me.node.children_ul = document.createElement("ul"); |
| 157 | me.node.get_children_ul = function() { return me.node.children_ul; }; |
| 158 | //me.node.children_ul.className = "children_ul"; |
| 159 | me.node.li.appendChild(me.node.children_ul); |
| 160 | me.node.depth = 0; |
| 161 | |
| 162 | get_node(me, me.node); |
| 163 | |
| 164 | me.this_page = this_page_relative(toroot); |
| 165 | me.breadcrumbs = find_page(me.this_page, root_nodes); |
| 166 | if (me.breadcrumbs != null && me.breadcrumbs.length != 0) { |
| 167 | var mom = me.node; |
| 168 | for (var i in me.breadcrumbs) { |
| 169 | var j = me.breadcrumbs[i]; |
| 170 | mom = mom.children[j]; |
| 171 | expand_node(me, mom); |
| 172 | } |
| 173 | mom.label_div.className = mom.label_div.className + " selected"; |
| 174 | addLoadEvent(function() { |
| 175 | scrollIntoView("nav-tree"); |
| 176 | }); |
| 177 | } |
| 178 | } |
| 179 | |