blob: 7b26efdbf10de4edb50ff37a3c4a9c78c2443396 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001var resizePackagesNav;
2var classesNav;
3var devdocNav;
4var sidenav;
5var content;
6var HEADER_HEIGHT = 117;
Dirk Dougherty4405a232009-07-07 17:43:27 -07007var cookie_namespace = 'android_developer';
The Android Open Source Project88b60792009-03-03 19:28:42 -08008var NAV_PREF_TREE = "tree";
9var NAV_PREF_PANELS = "panels";
10var nav_pref;
11var toRoot;
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070012var isMobile = false; // true if mobile, so we can adjust some layout
Scott Main3f7d1df2009-11-02 15:31:04 -080013var isIE6 = false; // true if IE6
The Android Open Source Project88b60792009-03-03 19:28:42 -080014
Scott Mainf8af8872009-12-02 20:59:40 -080015// TODO: use $(document).ready instead
The Android Open Source Project88b60792009-03-03 19:28:42 -080016function addLoadEvent(newfun) {
17 var current = window.onload;
18 if (typeof window.onload != 'function') {
19 window.onload = newfun;
20 } else {
21 window.onload = function() {
22 current();
23 newfun();
24 }
25 }
26}
27
Scott Maind8115ff2009-11-03 18:54:26 -080028var agent = navigator['userAgent'].toLowerCase();
Scott Main3f7d1df2009-11-02 15:31:04 -080029// If a mobile phone, set flag and do mobile setup
Scott Maind8115ff2009-11-03 18:54:26 -080030if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod
31 (agent.indexOf("blackberry") != -1) ||
32 (agent.indexOf("webos") != -1) ||
33 (agent.indexOf("mini") != -1)) { // opera mini browsers
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070034 isMobile = true;
35 addLoadEvent(mobileSetup);
Scott Main3f7d1df2009-11-02 15:31:04 -080036// If not a mobile browser, set the onresize event for IE6, and others
Scott Maind8115ff2009-11-03 18:54:26 -080037} else if (agent.indexOf("msie 6") != -1) {
Scott Main3f7d1df2009-11-02 15:31:04 -080038 isIE6 = true;
39 addLoadEvent(function() {
40 window.onresize = resizeAll;
41 });
42} else {
43 addLoadEvent(function() {
44 window.onresize = resizeHeight;
45 });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070046}
47
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070048function mobileSetup() {
49 $("body").css({'overflow':'auto'});
50 $("html").css({'overflow':'auto'});
51 $("#body-content").css({'position':'relative', 'top':'0'});
52 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
53 $("#side-nav").css({'padding':'0'});
54 $("#nav-tree").css({'overflow-y': 'auto'});
55}
56
Scott Main25fda192009-08-04 11:26:30 -070057/* loads the lists.js file to the page.
58Loading this in the head was slowing page load time */
59addLoadEvent( function() {
60 var lists = document.createElement("script");
61 lists.setAttribute("type","text/javascript");
62 lists.setAttribute("src", toRoot+"reference/lists.js");
Scott Main1da82382009-09-17 10:54:05 -070063 document.getElementsByTagName("head")[0].appendChild(lists);
Scott Main25fda192009-08-04 11:26:30 -070064} );
65
Scott Mainf8af8872009-12-02 20:59:40 -080066addLoadEvent( function() {
67 $("pre").addClass("prettyprint");
68 prettyPrint();
69} );
70
The Android Open Source Project88b60792009-03-03 19:28:42 -080071function setToRoot(root) {
72 toRoot = root;
73 // note: toRoot also used by carousel.js
74}
75
76function restoreWidth(navWidth) {
77 var windowWidth = $(window).width() + "px";
Scott Main3f7d1df2009-11-02 15:31:04 -080078 content.css({marginLeft:parseInt(navWidth) + 6 + "px"}); //account for 6px-wide handle-bar
79
80 if (isIE6) {
81 content.css({width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); // necessary in order for scrollbars to be visible
82 }
83
The Android Open Source Project88b60792009-03-03 19:28:42 -080084 sidenav.css({width:navWidth});
85 resizePackagesNav.css({width:navWidth});
86 classesNav.css({width:navWidth});
87 $("#packages-nav").css({width:navWidth});
88}
89
90function restoreHeight(packageHeight) {
91 var windowHeight = ($(window).height() - HEADER_HEIGHT);
92 var swapperHeight = windowHeight - 13;
93 $("#swapper").css({height:swapperHeight + "px"});
94 sidenav.css({height:windowHeight + "px"});
95 content.css({height:windowHeight + "px"});
96 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
97 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
98 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
99 devdocNav.css({height:sidenav.css("height")});
100 $("#nav-tree").css({height:swapperHeight + "px"});
101}
102
Dirk Dougherty4405a232009-07-07 17:43:27 -0700103function readCookie(cookie) {
104 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800105 if (document.cookie) {
106 var index = document.cookie.indexOf(myCookie);
107 if (index != -1) {
108 var valStart = index + myCookie.length;
109 var valEnd = document.cookie.indexOf(";", valStart);
110 if (valEnd == -1) {
111 valEnd = document.cookie.length;
112 }
113 var val = document.cookie.substring(valStart, valEnd);
114 return val;
115 }
116 }
117 return 0;
118}
119
Dirk Dougherty4405a232009-07-07 17:43:27 -0700120function writeCookie(cookie, val, section, expiration) {
Scott Main9b5fdb92009-10-27 15:09:15 -0700121 if (val==undefined) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -0700122 section = section == null ? "_" : "_"+section+"_";
123 if (expiration == null) {
124 var date = new Date();
125 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
126 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800127 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800128 document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800129}
130
131function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800132 $("#side-nav").css({position:"absolute",left:0});
133 content = $("#doc-content");
134 resizePackagesNav = $("#resize-packages-nav");
135 classesNav = $("#classes-nav");
136 sidenav = $("#side-nav");
137 devdocNav = $("#devdoc-nav");
138
139 if (location.href.indexOf("/reference/") != -1) {
140 var cookiePath = "reference_";
141 } else if (location.href.indexOf("/guide/") != -1) {
142 var cookiePath = "guide_";
143 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700144
145 if (!isMobile) {
Scott Main3f7d1df2009-11-02 15:31:04 -0800146 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700147 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700148 var cookieWidth = readCookie(cookiePath+'width');
149 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700150 if (cookieWidth) {
151 restoreWidth(cookieWidth);
152 } else if ($(".side-nav-resizable").length) {
153 resizeWidth();
154 }
155 if (cookieHeight) {
156 restoreHeight(cookieHeight);
157 } else {
158 resizeHeight();
159 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800160 }
161
162 if (devdocNav.length) { // only dev guide and sdk
163 highlightNav(location.href);
164 }
165}
166
167function highlightNav(fullPageName) {
168 var lastSlashPos = fullPageName.lastIndexOf("/");
169 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
170 fullPageName.indexOf("/guide/") :
171 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
172 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
173 fullPageName = fullPageName + "index.html";
174 }
175 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
176 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
177 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Scott Main0d8872e2009-10-23 13:04:31 -0700178 if ((link.length == 0) && (fullPageName.indexOf("/guide/") != -1)) {
179// if there's no match, then let's backstep through the directory until we find an index.html page that matches our ancestor directories (only for dev guide)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800180 lastBackstep = pathPageName.lastIndexOf("/");
181 while (link.length == 0) {
182 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
183 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
184 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
185 if (lastBackstep == 0) break;
186 }
187 }
188 link.parent().addClass('selected');
189 if (link.parent().parent().is(':hidden')) {
190 toggle(link.parent().parent().parent(), false);
191 } else if (link.parent().parent().hasClass('toggle-list')) {
192 toggle(link.parent().parent(), false);
193 }
194}
195
Scott Main3f7d1df2009-11-02 15:31:04 -0800196/* Resize the height of the nav panels in the reference,
197 * and save the new size to a cookie */
198function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800199 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800200 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800201 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
202 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800203
204 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800205 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800206
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700207 var basePath = getBaseUri(location.pathname);
208 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700209 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800210}
211
Scott Main3f7d1df2009-11-02 15:31:04 -0800212/* Resize the height of the side-nav and doc-content divs,
213 * which creates the frame effect */
214function resizeHeight() {
215 // Get the window height and always resize the doc-content and side-nav divs
216 var windowHeight = ($(window).height() - HEADER_HEIGHT);
217 content.css({height:windowHeight + "px"});
218 sidenav.css({height:windowHeight + "px"});
219
220 var href = location.href;
221 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
222 if (href.indexOf("/reference/") != -1) {
223 var swapperHeight = windowHeight - 13;
224 $("#swapper").css({height:swapperHeight + "px"});
225 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
226 $("#nav-tree").css({height:swapperHeight + "px"});
227
228 // If in the dev guide docs, also resize the "devdoc-nav" div
229 } else if (href.indexOf("/guide/") != -1) {
230 $("#devdoc-nav").css({height:sidenav.css("height")});
231 }
232}
233
234/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
235 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800236function resizeWidth() {
237 var windowWidth = $(window).width() + "px";
238 if (sidenav.length) {
239 var sidenavWidth = sidenav.css("width");
240 } else {
241 var sidenavWidth = 0;
242 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800243 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
244
245 if (isIE6) {
246 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
247 }
248
The Android Open Source Project88b60792009-03-03 19:28:42 -0800249 resizePackagesNav.css({width:sidenavWidth});
250 classesNav.css({width:sidenavWidth});
251 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800252
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700253 var basePath = getBaseUri(location.pathname);
254 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700255 writeCookie("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800256}
257
Scott Main3f7d1df2009-11-02 15:31:04 -0800258/* For IE6 only,
259 * because it can't properly perform auto width for "doc-content" div,
260 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800261function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800262 resizeHeight();
263 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800264}
265
Dirk Dougherty4405a232009-07-07 17:43:27 -0700266function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700267 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700268 if (intlUrl) {
269 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
270 base = base.substring(base.indexOf('/')+1, base.length);
271 //alert("intl, returning base url: /" + base);
272 return ("/" + base);
273 } else {
274 //alert("not intl, returning uri as found.");
275 return uri;
276 }
277}
278
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700279function requestAppendHL(uri) {
280//append "?hl=<lang> to an outgoing request (such as to blog)
281 var lang = getLangPref();
282 if (lang) {
283 var q = 'hl=' + lang;
284 uri += '?' + q;
285 window.location = uri;
286 return false;
287 } else {
288 return true;
289 }
290}
291
The Android Open Source Project88b60792009-03-03 19:28:42 -0800292function loadLast(cookiePath) {
293 var location = window.location.href;
294 if (location.indexOf("/"+cookiePath+"/") != -1) {
295 return true;
296 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700297 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800298 if (lastPage) {
299 window.location = lastPage;
300 return false;
301 }
302 return true;
303}
304
305$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700306 var path = getBaseUri(location.pathname);
307 if (path.indexOf("/reference/") != -1) {
308 writeCookie("lastpage", path, "reference", null);
309 } else if (path.indexOf("/guide/") != -1) {
310 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800311 }
312});
313
The Android Open Source Project88b60792009-03-03 19:28:42 -0800314function toggle(obj, slide) {
315 var ul = $("ul", obj);
316 var li = ul.parent();
317 if (li.hasClass("closed")) {
318 if (slide) {
319 ul.slideDown("fast");
320 } else {
321 ul.show();
322 }
323 li.removeClass("closed");
324 li.addClass("open");
325 $(".toggle-img", li).attr("title", "hide pages");
326 } else {
327 ul.slideUp("fast");
328 li.removeClass("open");
329 li.addClass("closed");
330 $(".toggle-img", li).attr("title", "show pages");
331 }
332}
333
The Android Open Source Project88b60792009-03-03 19:28:42 -0800334function buildToggleLists() {
335 $(".toggle-list").each(
336 function(i) {
337 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
338 $(this).addClass("closed");
339 });
340}
341
342function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700343 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800344 if (v != NAV_PREF_TREE) {
345 v = NAV_PREF_PANELS;
346 }
347 return v;
348}
349
350function chooseDefaultNav() {
351 nav_pref = getNavPref();
352 if (nav_pref == NAV_PREF_TREE) {
353 $("#nav-panels").toggle();
354 $("#panel-link").toggle();
355 $("#nav-tree").toggle();
356 $("#tree-link").toggle();
357 }
358}
359
360function swapNav() {
361 if (nav_pref == NAV_PREF_TREE) {
362 nav_pref = NAV_PREF_PANELS;
363 } else {
364 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700365 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800366 }
367 var date = new Date();
368 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700369 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800370
371 $("#nav-panels").toggle();
372 $("#panel-link").toggle();
373 $("#nav-tree").toggle();
374 $("#tree-link").toggle();
375
376 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
377 else {
378 scrollIntoView("packages-nav");
379 scrollIntoView("classes-nav");
380 }
381}
382
383function scrollIntoView(nav) {
384 var navObj = $("#"+nav);
385 if (navObj.is(':visible')) {
386 var selected = $(".selected", navObj);
387 if (selected.length == 0) return;
388 if (selected.is("div")) selected = selected.parent();
389
390 var scrolling = document.getElementById(nav);
391 var navHeight = navObj.height();
392 var offsetTop = selected.position().top;
393 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
394 if(offsetTop > navHeight - 92) {
395 scrolling.scrollTop = offsetTop - navHeight + 92;
396 }
397 }
398}
399
400function toggleAllInherited(linkObj, expand) {
401 var a = $(linkObj);
402 var table = $(a.parent().parent().parent());
403 var expandos = $(".jd-expando-trigger", table);
404 if ( (expand == null && a.text() == "[Expand]") || expand ) {
405 expandos.each(function(i) {
406 toggleInherited(this, true);
407 });
408 a.text("[Collapse]");
409 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
410 expandos.each(function(i) {
411 toggleInherited(this, false);
412 });
413 a.text("[Expand]");
414 }
415 return false;
416}
417
418function toggleAllSummaryInherited(linkObj) {
419 var a = $(linkObj);
420 var content = $(a.parent().parent().parent());
421 var toggles = $(".toggle-all", content);
422 if (a.text() == "[Expand All]") {
423 toggles.each(function(i) {
424 toggleAllInherited(this, true);
425 });
426 a.text("[Collapse All]");
427 } else {
428 toggles.each(function(i) {
429 toggleAllInherited(this, false);
430 });
431 a.text("[Expand All]");
432 }
433 return false;
434}
Dirk Dougherty4405a232009-07-07 17:43:27 -0700435
436
437function changeTabLang(lang) {
438 var nodes = $("#header-tabs").find("."+lang);
439 for (i=0; i < nodes.length; i++) { // for each node in this language
440 var node = $(nodes[i]);
441 node.siblings().css("display","none"); // hide all siblings
442 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
443 node.css("display","inline");
444 } else { //otherwise, show English instead
445 node.css("display","none");
446 node.siblings().filter(".en").css("display","inline");
447 }
448 }
449}
450
451function changeNavLang(lang) {
452 var nodes = $("#side-nav").find("."+lang);
453 for (i=0; i < nodes.length; i++) { // for each node in this language
454 var node = $(nodes[i]);
455 node.siblings().css("display","none"); // hide all siblings
456 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
457 node.css("display","inline");
458 } else { // otherwise, show English instead
459 node.css("display","none");
460 node.siblings().filter(".en").css("display","inline");
461 }
462 }
463}
464
465function changeDocLang(lang) {
466 changeTabLang(lang);
467 changeNavLang(lang);
468}
469
470function changeLangPref(lang, refresh) {
471 var date = new Date();
472 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
473 //alert("expires: " + expires)
474 writeCookie("pref_lang", lang, null, expires);
475 //changeDocLang(lang);
476 if (refresh) {
477 l = getBaseUri(location.pathname);
478 window.location = l;
479 }
480}
481
482function loadLangPref() {
483 var lang = readCookie("pref_lang");
484 if (lang != 0) {
485 $("#language").find("option[value='"+lang+"']").attr("selected",true);
486 }
487}
488
489function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700490 var lang = $("#language").find(":selected").attr("value");
491 if (!lang) {
492 lang = readCookie("pref_lang");
493 }
494 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700495}
Scott Main462cc372009-10-23 16:19:37 -0700496
497
498function toggleContent(obj) {
499 var button = $(obj);
500 var div = $(obj.parentNode);
501 var toggleMe = $(".toggle-content-toggleme",div);
502 if (button.hasClass("show")) {
503 toggleMe.slideDown();
504 button.removeClass("show").addClass("hide");
505 } else {
506 toggleMe.slideUp();
507 button.removeClass("hide").addClass("show");
508 }
509 $("span", button).toggle();
510}