blob: 3080760f10fdd1a83c0f78accb45ab64af8bd74b [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001
Scott Main25fda192009-08-04 11:26:30 -07002/* API LEVEL TOGGLE */
3addLoadEvent(changeApiLevel);
4var API_LEVEL_COOKIE = "api_level";
5var minLevel = 1;
6
7function buildApiLevelToggle() {
8 var maxLevel = SINCE_DATA.length;
9 var userApiLevel = readCookie(API_LEVEL_COOKIE);
10
11 if (userApiLevel != 0) {
12 selectedLevel = userApiLevel;
13 } else {
14 selectedLevel = maxLevel;
15 }
16
17 minLevel = $("body").attr("class");
18 var select = $("#apiLevelControl").html("").change(changeApiLevel);
19 for (var i = maxLevel-1; i >= 0; i--) {
20 var option = $("<option />").attr("value",""+SINCE_DATA[i]).append(""+SINCE_DATA[i]);
21// if (SINCE_DATA[i] < minLevel) option.addClass("absent"); // always false for strings (codenames)
22 select.append(option);
23 }
24
25 // get the DOM element and use setAttribute cuz IE6 fails when using jquery .attr('selected',true)
26 var selectedLevelItem = $("#apiLevelControl option[value='"+selectedLevel+"']").get(0);
27 selectedLevelItem.setAttribute('selected',true);
28}
29
30function changeApiLevel() {
31 var selectedLevel = $("#apiLevelControl option:selected").val();
Scott Main69497272009-08-24 17:33:06 -070032 toggleVisisbleApis(selectedLevel, "body");
33
Scott Main25fda192009-08-04 11:26:30 -070034 var date = new Date();
35 date.setTime(date.getTime()+(50*365*24*60*60*1000)); // keep this for 50 years
36 writeCookie(API_LEVEL_COOKIE, selectedLevel, null, date);
37
38 if (selectedLevel < minLevel) {
39 var thing = ($("#jd-header").html().indexOf("package") != -1) ? "package" : "class";
40 $("#naMessage").show().html("<div><p><strong>This " + thing + " is not available with API Level " + selectedLevel + ".</strong></p>"
41 + "<p>To use this " + thing + ", your application must specify API Level " + minLevel + " or higher in its manifest "
42 + "and be compiled against a version of the Android library that supports an equal or higher API Level. To reveal this "
43 + "document, change the value of the API Level filter above.</p>"
44 + "<p><a href='" +toRoot+ "guide/appendix/api-levels.html'>What is the API Level?</a></p></div>");
45 } else {
46 $("#naMessage").hide();
47 }
48}
49
Scott Main69497272009-08-24 17:33:06 -070050function toggleVisisbleApis(selectedLevel, context) {
51 var apis = $(".api",context);
52 apis.each(function(i) {
53 var obj = $(this);
54 var className = obj.attr("class");
55 var apiLevelIndex = className.lastIndexOf("-")+1;
56 var apiLevelEndIndex = className.indexOf(" ", apiLevelIndex);
57 apiLevelEndIndex = apiLevelEndIndex != -1 ? apiLevelEndIndex : className.length;
58 var apiLevel = className.substring(apiLevelIndex, apiLevelEndIndex);
59 if (apiLevel > selectedLevel) obj.addClass("absent").attr("title","Requires API Level "+apiLevel+" or higher");
60 else obj.removeClass("absent").removeAttr("title");
61 });
62}
63
Scott Main25fda192009-08-04 11:26:30 -070064/* NAVTREE */
65
66function new_node(me, mom, text, link, children_data, api_level)
The Android Open Source Project88b60792009-03-03 19:28:42 -080067{
68 var node = new Object();
69 node.children = Array();
70 node.children_data = children_data;
71 node.depth = mom.depth + 1;
72
73 node.li = document.createElement("li");
74 mom.get_children_ul().appendChild(node.li);
75
76 node.label_div = document.createElement("div");
Scott Main25fda192009-08-04 11:26:30 -070077 node.label_div.className = "label";
78 if (api_level != null) {
79 $(node.label_div).addClass("api");
80 $(node.label_div).addClass("api-level-"+api_level);
81 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080082 node.li.appendChild(node.label_div);
83 node.label_div.style.paddingLeft = 10*node.depth + "px";
The Android Open Source Project88b60792009-03-03 19:28:42 -080084
85 if (children_data == null) {
86 // 12 is the width of the triangle and padding extra space
87 node.label_div.style.paddingLeft = ((10*node.depth)+12) + "px";
88 } else {
89 node.label_div.style.paddingLeft = 10*node.depth + "px";
90 node.expand_toggle = document.createElement("a");
91 node.expand_toggle.href = "javascript:void(0)";
92 node.expand_toggle.onclick = function() {
93 if (node.expanded) {
94 $(node.get_children_ul()).slideUp("fast");
95 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
96 node.expanded = false;
97 } else {
98 expand_node(me, node);
99 }
100 };
101 node.label_div.appendChild(node.expand_toggle);
102
103 node.plus_img = document.createElement("img");
104 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
105 node.plus_img.className = "plus";
106 node.plus_img.border = "0";
107 node.expand_toggle.appendChild(node.plus_img);
108
109 node.expanded = false;
110 }
111
112 var a = document.createElement("a");
113 node.label_div.appendChild(a);
114 node.label = document.createTextNode(text);
115 a.appendChild(node.label);
116 if (link) {
117 a.href = me.toroot + link;
118 } else {
119 if (children_data != null) {
120 a.className = "nolink";
121 a.href = "javascript:void(0)";
122 a.onclick = node.expand_toggle.onclick;
123 // This next line shouldn't be necessary. I'll buy a beer for the first
124 // person who figures out how to remove this line and have the link
125 // toggle shut on the first try. --joeo@android.com
126 node.expanded = false;
127 }
128 }
129
130
131 node.children_ul = null;
132 node.get_children_ul = function() {
133 if (!node.children_ul) {
134 node.children_ul = document.createElement("ul");
135 node.children_ul.className = "children_ul";
136 node.children_ul.style.display = "none";
137 node.li.appendChild(node.children_ul);
138 }
139 return node.children_ul;
140 };
141
142 return node;
143}
144
145function expand_node(me, node)
146{
147 if (node.children_data && !node.expanded) {
148 if (node.children_visited) {
149 $(node.get_children_ul()).slideDown("fast");
150 } else {
151 get_node(me, node);
Scott Main25fda192009-08-04 11:26:30 -0700152 if ($(node.label_div).hasClass("absent")) $(node.get_children_ul()).addClass("absent");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800153 $(node.get_children_ul()).slideDown("fast");
154 }
155 node.plus_img.src = me.toroot + "assets/images/triangle-opened-small.png";
156 node.expanded = true;
Scott Main69497272009-08-24 17:33:06 -0700157
158 // perform api level toggling because new nodes are new to the DOM
159 var selectedLevel = $("#apiLevelControl option:selected").val();
160 toggleVisisbleApis(selectedLevel, "#side-nav");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800161 }
162}
163
164function get_node(me, mom)
165{
166 mom.children_visited = true;
167 for (var i in mom.children_data) {
168 var node_data = mom.children_data[i];
169 mom.children[i] = new_node(me, mom, node_data[0], node_data[1],
Scott Main25fda192009-08-04 11:26:30 -0700170 node_data[2], node_data[3]);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800171 }
172}
173
174function this_page_relative(toroot)
175{
176 var full = document.location.pathname;
177 var file = "";
178 if (toroot.substr(0, 1) == "/") {
179 if (full.substr(0, toroot.length) == toroot) {
Scott Main25fda192009-08-04 11:26:30 -0700180 return full.substr(toroot.length);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800181 } else {
182 // the file isn't under toroot. Fail.
183 return null;
184 }
185 } else {
186 if (toroot != "./") {
187 toroot = "./" + toroot;
188 }
189 do {
190 if (toroot.substr(toroot.length-3, 3) == "../" || toroot == "./") {
191 var pos = full.lastIndexOf("/");
192 file = full.substr(pos) + file;
193 full = full.substr(0, pos);
194 toroot = toroot.substr(0, toroot.length-3);
195 }
196 } while (toroot != "" && toroot != "/");
197 return file.substr(1);
198 }
199}
200
201function find_page(url, data)
202{
203 var nodes = data;
204 var result = null;
205 for (var i in nodes) {
206 var d = nodes[i];
207 if (d[1] == url) {
208 return new Array(i);
209 }
210 else if (d[2] != null) {
211 result = find_page(url, d[2]);
212 if (result != null) {
213 return (new Array(i).concat(result));
214 }
215 }
216 }
217 return null;
218}
219
Scott Main5b53cd72009-06-04 11:10:17 -0700220function load_navtree_data(toroot) {
221 var navtreeData = document.createElement("script");
222 navtreeData.setAttribute("type","text/javascript");
223 navtreeData.setAttribute("src", toroot+"navtree_data.js");
224 $("head").append($(navtreeData));
Scott Main25fda192009-08-04 11:26:30 -0700225}
Scott Main5b53cd72009-06-04 11:10:17 -0700226
227function init_default_navtree(toroot) {
Scott Main5b53cd72009-06-04 11:10:17 -0700228 init_navtree("nav-tree", toroot, NAVTREE_DATA);
Scott Main69497272009-08-24 17:33:06 -0700229
230 // perform api level toggling because because the whole tree is new to the DOM
231 var selectedLevel = $("#apiLevelControl option:selected").val();
232 toggleVisisbleApis(selectedLevel, "#side-nav");
Scott Main5b53cd72009-06-04 11:10:17 -0700233}
234
The Android Open Source Project88b60792009-03-03 19:28:42 -0800235function init_navtree(navtree_id, toroot, root_nodes)
Scott Main25fda192009-08-04 11:26:30 -0700236{
The Android Open Source Project88b60792009-03-03 19:28:42 -0800237 var me = new Object();
238 me.toroot = toroot;
239 me.node = new Object();
240
241 me.node.li = document.getElementById(navtree_id);
242 me.node.children_data = root_nodes;
243 me.node.children = new Array();
244 me.node.children_ul = document.createElement("ul");
245 me.node.get_children_ul = function() { return me.node.children_ul; };
246 //me.node.children_ul.className = "children_ul";
247 me.node.li.appendChild(me.node.children_ul);
248 me.node.depth = 0;
249
250 get_node(me, me.node);
251
252 me.this_page = this_page_relative(toroot);
253 me.breadcrumbs = find_page(me.this_page, root_nodes);
254 if (me.breadcrumbs != null && me.breadcrumbs.length != 0) {
255 var mom = me.node;
256 for (var i in me.breadcrumbs) {
257 var j = me.breadcrumbs[i];
258 mom = mom.children[j];
259 expand_node(me, mom);
260 }
261 mom.label_div.className = mom.label_div.className + " selected";
262 addLoadEvent(function() {
263 scrollIntoView("nav-tree");
264 });
265 }
266}