blob: 5561cc906c627cdde42305088ff314f5c3e40366 [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);
Scott Main9b5fdb92009-10-27 15:09:15 -07004
5var API_LEVEL_ENABLED_COOKIE = "api_level_enabled";
Scott Main25fda192009-08-04 11:26:30 -07006var API_LEVEL_COOKIE = "api_level";
7var minLevel = 1;
Scott Main70e60662011-07-27 18:20:34 -07008var maxLevel = 1;
Scott Main25fda192009-08-04 11:26:30 -07009
Scott Main9b5fdb92009-10-27 15:09:15 -070010function toggleApiLevelSelector(checkbox) {
11 var date = new Date();
12 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
13 var expiration = date.toGMTString();
14 if (checkbox.checked) {
15 $("#apiLevelSelector").removeAttr("disabled");
16 $("#api-level-toggle label").removeClass("disabled");
17 writeCookie(API_LEVEL_ENABLED_COOKIE, 1, null, expiration);
18 } else {
19 $("#apiLevelSelector").attr("disabled","disabled");
20 $("#api-level-toggle label").addClass("disabled");
21 writeCookie(API_LEVEL_ENABLED_COOKIE, 0, null, expiration);
22 }
23 changeApiLevel();
24}
25
26function buildApiLevelSelector() {
Scott Main70e60662011-07-27 18:20:34 -070027 maxLevel = SINCE_DATA.length;
Scott Main9b5fdb92009-10-27 15:09:15 -070028 var userApiLevelEnabled = readCookie(API_LEVEL_ENABLED_COOKIE);
Scott Main70e60662011-07-27 18:20:34 -070029 var userApiLevel = parseInt(readCookie(API_LEVEL_COOKIE));
Scott Main64b879a2009-11-02 18:05:41 -080030 userApiLevel = userApiLevel == 0 ? maxLevel : userApiLevel; // If there's no cookie (zero), use the max by default
Scott Main9b5fdb92009-10-27 15:09:15 -070031
32 if (userApiLevelEnabled == 0) {
33 $("#apiLevelSelector").attr("disabled","disabled");
34 } else {
35 $("#apiLevelCheckbox").attr("checked","checked");
36 $("#api-level-toggle label").removeClass("disabled");
37 }
Scott Main70e60662011-07-27 18:20:34 -070038
39 minLevel = parseInt($("body").attr("class"));
40 // Handle provisional api levels; the provisional level will always be the highest possible level
41 if (isNaN(minLevel)) {
42 minLevel = maxLevel;
43 }
Scott Main9b5fdb92009-10-27 15:09:15 -070044 var select = $("#apiLevelSelector").html("").change(changeApiLevel);
45 for (var i = maxLevel-1; i >= 0; i--) {
46 var option = $("<option />").attr("value",""+SINCE_DATA[i]).append(""+SINCE_DATA[i]);
47 // if (SINCE_DATA[i] < minLevel) option.addClass("absent"); // always false for strings (codenames)
Scott Main7a0090b2010-04-16 09:00:29 -070048 select.append(option);
Scott Main9b5fdb92009-10-27 15:09:15 -070049 }
Scott Main70e60662011-07-27 18:20:34 -070050
Scott Main25fda192009-08-04 11:26:30 -070051 // get the DOM element and use setAttribute cuz IE6 fails when using jquery .attr('selected',true)
Scott Main7a0090b2010-04-16 09:00:29 -070052 var selectedLevelItem = $("#apiLevelSelector option[value='"+userApiLevel+"']").get(0);
53 selectedLevelItem.setAttribute('selected',true);
Scott Main25fda192009-08-04 11:26:30 -070054}
55
56function changeApiLevel() {
Scott Main70e60662011-07-27 18:20:34 -070057 maxLevel = SINCE_DATA.length;
Scott Main9b5fdb92009-10-27 15:09:15 -070058 var userApiLevelEnabled = readCookie(API_LEVEL_ENABLED_COOKIE);
Scott Main7a0090b2010-04-16 09:00:29 -070059 var selectedLevel = maxLevel;
Scott Main70e60662011-07-27 18:20:34 -070060
Scott Main9b5fdb92009-10-27 15:09:15 -070061 if (userApiLevelEnabled == 0) {
62 toggleVisisbleApis(selectedLevel, "body");
63 } else {
Scott Main11057f52011-02-04 15:13:04 -080064 selectedLevel = parseInt($("#apiLevelSelector option:selected").val());
Scott Main9b5fdb92009-10-27 15:09:15 -070065 toggleVisisbleApis(selectedLevel, "body");
Scott Main70e60662011-07-27 18:20:34 -070066
Scott Main9b5fdb92009-10-27 15:09:15 -070067 var date = new Date();
68 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
69 var expiration = date.toGMTString();
70 writeCookie(API_LEVEL_COOKIE, selectedLevel, null, expiration);
71 }
Scott Main70e60662011-07-27 18:20:34 -070072
Scott Main9b5fdb92009-10-27 15:09:15 -070073 if (selectedLevel < minLevel) {
74 var thing = ($("#jd-header").html().indexOf("package") != -1) ? "package" : "class";
75 $("#naMessage").show().html("<div><p><strong>This " + thing + " is not available with API Level " + selectedLevel + ".</strong></p>"
Scott Main70e60662011-07-27 18:20:34 -070076 + "<p>To use this " + thing + ", your application must specify API Level \"" + $("body").attr("class") + "\" or higher in its manifest "
Scott Main25fda192009-08-04 11:26:30 -070077 + "and be compiled against a version of the Android library that supports an equal or higher API Level. To reveal this "
78 + "document, change the value of the API Level filter above.</p>"
79 + "<p><a href='" +toRoot+ "guide/appendix/api-levels.html'>What is the API Level?</a></p></div>");
Scott Main9b5fdb92009-10-27 15:09:15 -070080 } else {
Scott Main25fda192009-08-04 11:26:30 -070081 $("#naMessage").hide();
82 }
83}
84
Scott Main69497272009-08-24 17:33:06 -070085function toggleVisisbleApis(selectedLevel, context) {
Scott Main7a0090b2010-04-16 09:00:29 -070086 var apis = $(".api",context);
87 apis.each(function(i) {
88 var obj = $(this);
89 var className = obj.attr("class");
90 var apiLevelIndex = className.lastIndexOf("-")+1;
91 var apiLevelEndIndex = className.indexOf(" ", apiLevelIndex);
92 apiLevelEndIndex = apiLevelEndIndex != -1 ? apiLevelEndIndex : className.length;
93 var apiLevel = className.substring(apiLevelIndex, apiLevelEndIndex);
Scott Main70e60662011-07-27 18:20:34 -070094 if (apiLevel.length == 0) { // for odd cases when the since data is actually missing, just bail
95 return;
96 }
97 apiLevel = parseInt(apiLevel);
98
99 // Handle provisional api levels; if this item's level is the provisional one, set it to the max
100 var selectedLevelNum = parseInt(selectedLevel)
101 var apiLevelNum = parseInt(apiLevel);
102 if (isNaN(apiLevelNum)) {
103 apiLevelNum = maxLevel;
104 }
105
106 // Grey things out that aren't available and give a tooltip title
107 if (apiLevelNum > selectedLevelNum) obj.addClass("absent").attr("title","Requires API Level \""
108 + apiLevel + "\" or higher");
Scott Main7a0090b2010-04-16 09:00:29 -0700109 else obj.removeClass("absent").removeAttr("title");
110 });
Scott Main69497272009-08-24 17:33:06 -0700111}
112
Scott Main25fda192009-08-04 11:26:30 -0700113/* NAVTREE */
114
115function new_node(me, mom, text, link, children_data, api_level)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800116{
117 var node = new Object();
118 node.children = Array();
119 node.children_data = children_data;
120 node.depth = mom.depth + 1;
121
122 node.li = document.createElement("li");
123 mom.get_children_ul().appendChild(node.li);
124
125 node.label_div = document.createElement("div");
Scott Main25fda192009-08-04 11:26:30 -0700126 node.label_div.className = "label";
127 if (api_level != null) {
128 $(node.label_div).addClass("api");
129 $(node.label_div).addClass("api-level-"+api_level);
130 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800131 node.li.appendChild(node.label_div);
132 node.label_div.style.paddingLeft = 10*node.depth + "px";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800133
134 if (children_data == null) {
135 // 12 is the width of the triangle and padding extra space
136 node.label_div.style.paddingLeft = ((10*node.depth)+12) + "px";
137 } else {
138 node.label_div.style.paddingLeft = 10*node.depth + "px";
139 node.expand_toggle = document.createElement("a");
140 node.expand_toggle.href = "javascript:void(0)";
141 node.expand_toggle.onclick = function() {
142 if (node.expanded) {
143 $(node.get_children_ul()).slideUp("fast");
144 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
145 node.expanded = false;
146 } else {
147 expand_node(me, node);
148 }
149 };
150 node.label_div.appendChild(node.expand_toggle);
151
152 node.plus_img = document.createElement("img");
153 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
154 node.plus_img.className = "plus";
155 node.plus_img.border = "0";
156 node.expand_toggle.appendChild(node.plus_img);
157
158 node.expanded = false;
159 }
160
161 var a = document.createElement("a");
162 node.label_div.appendChild(a);
163 node.label = document.createTextNode(text);
164 a.appendChild(node.label);
165 if (link) {
166 a.href = me.toroot + link;
167 } else {
168 if (children_data != null) {
169 a.className = "nolink";
170 a.href = "javascript:void(0)";
171 a.onclick = node.expand_toggle.onclick;
172 // This next line shouldn't be necessary. I'll buy a beer for the first
173 // person who figures out how to remove this line and have the link
174 // toggle shut on the first try. --joeo@android.com
175 node.expanded = false;
176 }
177 }
178
179
180 node.children_ul = null;
181 node.get_children_ul = function() {
182 if (!node.children_ul) {
183 node.children_ul = document.createElement("ul");
184 node.children_ul.className = "children_ul";
185 node.children_ul.style.display = "none";
186 node.li.appendChild(node.children_ul);
187 }
188 return node.children_ul;
189 };
190
191 return node;
192}
193
194function expand_node(me, node)
195{
196 if (node.children_data && !node.expanded) {
197 if (node.children_visited) {
198 $(node.get_children_ul()).slideDown("fast");
199 } else {
200 get_node(me, node);
Scott Main25fda192009-08-04 11:26:30 -0700201 if ($(node.label_div).hasClass("absent")) $(node.get_children_ul()).addClass("absent");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800202 $(node.get_children_ul()).slideDown("fast");
203 }
204 node.plus_img.src = me.toroot + "assets/images/triangle-opened-small.png";
205 node.expanded = true;
Scott Main70e60662011-07-27 18:20:34 -0700206
Scott Main7a0090b2010-04-16 09:00:29 -0700207 // perform api level toggling because new nodes are new to the DOM
208 var selectedLevel = $("#apiLevelSelector option:selected").val();
Scott Main69497272009-08-24 17:33:06 -0700209 toggleVisisbleApis(selectedLevel, "#side-nav");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800210 }
211}
212
213function get_node(me, mom)
214{
215 mom.children_visited = true;
216 for (var i in mom.children_data) {
217 var node_data = mom.children_data[i];
218 mom.children[i] = new_node(me, mom, node_data[0], node_data[1],
Scott Main25fda192009-08-04 11:26:30 -0700219 node_data[2], node_data[3]);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800220 }
221}
222
223function this_page_relative(toroot)
224{
225 var full = document.location.pathname;
226 var file = "";
227 if (toroot.substr(0, 1) == "/") {
228 if (full.substr(0, toroot.length) == toroot) {
Scott Main25fda192009-08-04 11:26:30 -0700229 return full.substr(toroot.length);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800230 } else {
231 // the file isn't under toroot. Fail.
232 return null;
233 }
234 } else {
235 if (toroot != "./") {
236 toroot = "./" + toroot;
237 }
238 do {
239 if (toroot.substr(toroot.length-3, 3) == "../" || toroot == "./") {
240 var pos = full.lastIndexOf("/");
241 file = full.substr(pos) + file;
242 full = full.substr(0, pos);
243 toroot = toroot.substr(0, toroot.length-3);
244 }
245 } while (toroot != "" && toroot != "/");
246 return file.substr(1);
247 }
248}
249
250function find_page(url, data)
251{
252 var nodes = data;
253 var result = null;
254 for (var i in nodes) {
255 var d = nodes[i];
256 if (d[1] == url) {
257 return new Array(i);
258 }
259 else if (d[2] != null) {
260 result = find_page(url, d[2]);
261 if (result != null) {
262 return (new Array(i).concat(result));
263 }
264 }
265 }
266 return null;
267}
268
Scott Main5b53cd72009-06-04 11:10:17 -0700269function load_navtree_data(toroot) {
270 var navtreeData = document.createElement("script");
271 navtreeData.setAttribute("type","text/javascript");
272 navtreeData.setAttribute("src", toroot+"navtree_data.js");
273 $("head").append($(navtreeData));
Scott Main25fda192009-08-04 11:26:30 -0700274}
Scott Main5b53cd72009-06-04 11:10:17 -0700275
276function init_default_navtree(toroot) {
Scott Main5b53cd72009-06-04 11:10:17 -0700277 init_navtree("nav-tree", toroot, NAVTREE_DATA);
Scott Main69497272009-08-24 17:33:06 -0700278
Scott Main7a0090b2010-04-16 09:00:29 -0700279 // perform api level toggling because because the whole tree is new to the DOM
280 var selectedLevel = $("#apiLevelSelector option:selected").val();
Scott Main69497272009-08-24 17:33:06 -0700281 toggleVisisbleApis(selectedLevel, "#side-nav");
Scott Main5b53cd72009-06-04 11:10:17 -0700282}
283
The Android Open Source Project88b60792009-03-03 19:28:42 -0800284function init_navtree(navtree_id, toroot, root_nodes)
Scott Main25fda192009-08-04 11:26:30 -0700285{
The Android Open Source Project88b60792009-03-03 19:28:42 -0800286 var me = new Object();
287 me.toroot = toroot;
288 me.node = new Object();
289
290 me.node.li = document.getElementById(navtree_id);
291 me.node.children_data = root_nodes;
292 me.node.children = new Array();
293 me.node.children_ul = document.createElement("ul");
294 me.node.get_children_ul = function() { return me.node.children_ul; };
295 //me.node.children_ul.className = "children_ul";
296 me.node.li.appendChild(me.node.children_ul);
297 me.node.depth = 0;
298
299 get_node(me, me.node);
300
301 me.this_page = this_page_relative(toroot);
302 me.breadcrumbs = find_page(me.this_page, root_nodes);
303 if (me.breadcrumbs != null && me.breadcrumbs.length != 0) {
304 var mom = me.node;
305 for (var i in me.breadcrumbs) {
306 var j = me.breadcrumbs[i];
307 mom = mom.children[j];
308 expand_node(me, mom);
309 }
310 mom.label_div.className = mom.label_div.className + " selected";
311 addLoadEvent(function() {
312 scrollIntoView("nav-tree");
313 });
314 }
315}
Scott Main7a0090b2010-04-16 09:00:29 -0700316
317/* TOGGLE INHERITED MEMBERS */
318
319/* Toggle an inherited class (arrow toggle)
320 * @param linkObj The link that was clicked.
321 * @param expand 'true' to ensure it's expanded. 'false' to ensure it's closed.
322 * 'null' to simply toggle.
323 */
324function toggleInherited(linkObj, expand) {
325 var base = linkObj.getAttribute("id");
326 var list = document.getElementById(base + "-list");
327 var summary = document.getElementById(base + "-summary");
328 var trigger = document.getElementById(base + "-trigger");
329 var a = $(linkObj);
330 if ( (expand == null && a.hasClass("closed")) || expand ) {
331 list.style.display = "none";
332 summary.style.display = "block";
333 trigger.src = toRoot + "assets/images/triangle-opened.png";
334 a.removeClass("closed");
335 a.addClass("opened");
336 } else if ( (expand == null && a.hasClass("opened")) || (expand == false) ) {
337 list.style.display = "block";
338 summary.style.display = "none";
339 trigger.src = toRoot + "assets/images/triangle-closed.png";
340 a.removeClass("opened");
341 a.addClass("closed");
342 }
343 return false;
344}
345
346/* Toggle all inherited classes in a single table (e.g. all inherited methods)
347 * @param linkObj The link that was clicked.
348 * @param expand 'true' to ensure it's expanded. 'false' to ensure it's closed.
349 * 'null' to simply toggle.
350 */
351function toggleAllInherited(linkObj, expand) {
352 var a = $(linkObj);
353 var table = $(a.parent().parent().parent()); // ugly way to get table/tbody
354 var expandos = $(".jd-expando-trigger", table);
355 if ( (expand == null && a.text() == "[Expand]") || expand ) {
356 expandos.each(function(i) {
357 toggleInherited(this, true);
358 });
359 a.text("[Collapse]");
360 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
361 expandos.each(function(i) {
362 toggleInherited(this, false);
363 });
364 a.text("[Expand]");
365 }
366 return false;
367}
368
369/* Toggle all inherited members in the class (link in the class title)
370 */
371function toggleAllClassInherited() {
372 var a = $("#toggleAllClassInherited"); // get toggle link from class title
373 var toggles = $(".toggle-all", $("#doc-content"));
374 if (a.text() == "[Expand All]") {
375 toggles.each(function(i) {
376 toggleAllInherited(this, true);
377 });
378 a.text("[Collapse All]");
379 } else {
380 toggles.each(function(i) {
381 toggleAllInherited(this, false);
382 });
383 a.text("[Expand All]");
384 }
385 return false;
386}
387
388/* Expand all inherited members in the class. Used when initiating page search */
389function ensureAllInheritedExpanded() {
390 var toggles = $(".toggle-all", $("#doc-content"));
391 toggles.each(function(i) {
392 toggleAllInherited(this, true);
393 });
394 $("#toggleAllClassInherited").text("[Collapse All]");
395}
396
397
398/* HANDLE KEY EVENTS
399 * - Listen for Ctrl+F (Cmd on Mac) and expand all inherited members (to aid page search)
400 */
401var agent = navigator['userAgent'].toLowerCase();
402var mac = agent.indexOf("macintosh") != -1;
403
404$(document).keydown( function(e) {
405var control = mac ? e.metaKey && !e.ctrlKey : e.ctrlKey; // get ctrl key
406 if (control && e.which == 70) { // 70 is "F"
407 ensureAllInheritedExpanded();
408 }
409});