blob: 8843f9a0a26ebf52574f56a3bb83665463eecaf9 [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
15function addLoadEvent(newfun) {
16 var current = window.onload;
17 if (typeof window.onload != 'function') {
18 window.onload = newfun;
19 } else {
20 window.onload = function() {
21 current();
22 newfun();
23 }
24 }
25}
26
Scott Maind8115ff2009-11-03 18:54:26 -080027var agent = navigator['userAgent'].toLowerCase();
Scott Main3f7d1df2009-11-02 15:31:04 -080028// If a mobile phone, set flag and do mobile setup
Scott Maind8115ff2009-11-03 18:54:26 -080029if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod
30 (agent.indexOf("blackberry") != -1) ||
31 (agent.indexOf("webos") != -1) ||
32 (agent.indexOf("mini") != -1)) { // opera mini browsers
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070033 isMobile = true;
34 addLoadEvent(mobileSetup);
Scott Main3f7d1df2009-11-02 15:31:04 -080035// If not a mobile browser, set the onresize event for IE6, and others
Scott Maind8115ff2009-11-03 18:54:26 -080036} else if (agent.indexOf("msie 6") != -1) {
Scott Main3f7d1df2009-11-02 15:31:04 -080037 isIE6 = true;
38 addLoadEvent(function() {
39 window.onresize = resizeAll;
40 });
41} else {
42 addLoadEvent(function() {
43 window.onresize = resizeHeight;
44 });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070045}
46
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070047function mobileSetup() {
48 $("body").css({'overflow':'auto'});
49 $("html").css({'overflow':'auto'});
50 $("#body-content").css({'position':'relative', 'top':'0'});
51 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
52 $("#side-nav").css({'padding':'0'});
53 $("#nav-tree").css({'overflow-y': 'auto'});
54}
55
Scott Main25fda192009-08-04 11:26:30 -070056/* loads the lists.js file to the page.
57Loading this in the head was slowing page load time */
58addLoadEvent( function() {
59 var lists = document.createElement("script");
60 lists.setAttribute("type","text/javascript");
61 lists.setAttribute("src", toRoot+"reference/lists.js");
Scott Main1da82382009-09-17 10:54:05 -070062 document.getElementsByTagName("head")[0].appendChild(lists);
Scott Main25fda192009-08-04 11:26:30 -070063} );
64
The Android Open Source Project88b60792009-03-03 19:28:42 -080065function setToRoot(root) {
66 toRoot = root;
67 // note: toRoot also used by carousel.js
68}
69
70function restoreWidth(navWidth) {
71 var windowWidth = $(window).width() + "px";
Scott Main3f7d1df2009-11-02 15:31:04 -080072 content.css({marginLeft:parseInt(navWidth) + 6 + "px"}); //account for 6px-wide handle-bar
73
74 if (isIE6) {
75 content.css({width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); // necessary in order for scrollbars to be visible
76 }
77
The Android Open Source Project88b60792009-03-03 19:28:42 -080078 sidenav.css({width:navWidth});
79 resizePackagesNav.css({width:navWidth});
80 classesNav.css({width:navWidth});
81 $("#packages-nav").css({width:navWidth});
82}
83
84function restoreHeight(packageHeight) {
85 var windowHeight = ($(window).height() - HEADER_HEIGHT);
86 var swapperHeight = windowHeight - 13;
87 $("#swapper").css({height:swapperHeight + "px"});
88 sidenav.css({height:windowHeight + "px"});
89 content.css({height:windowHeight + "px"});
90 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
91 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
92 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
93 devdocNav.css({height:sidenav.css("height")});
94 $("#nav-tree").css({height:swapperHeight + "px"});
95}
96
Dirk Dougherty4405a232009-07-07 17:43:27 -070097function readCookie(cookie) {
98 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -080099 if (document.cookie) {
100 var index = document.cookie.indexOf(myCookie);
101 if (index != -1) {
102 var valStart = index + myCookie.length;
103 var valEnd = document.cookie.indexOf(";", valStart);
104 if (valEnd == -1) {
105 valEnd = document.cookie.length;
106 }
107 var val = document.cookie.substring(valStart, valEnd);
108 return val;
109 }
110 }
111 return 0;
112}
113
Dirk Dougherty4405a232009-07-07 17:43:27 -0700114function writeCookie(cookie, val, section, expiration) {
Scott Main9b5fdb92009-10-27 15:09:15 -0700115 if (val==undefined) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -0700116 section = section == null ? "_" : "_"+section+"_";
117 if (expiration == null) {
118 var date = new Date();
119 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
120 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800121 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800122 document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800123}
124
125function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800126 $("#side-nav").css({position:"absolute",left:0});
127 content = $("#doc-content");
128 resizePackagesNav = $("#resize-packages-nav");
129 classesNav = $("#classes-nav");
130 sidenav = $("#side-nav");
131 devdocNav = $("#devdoc-nav");
132
133 if (location.href.indexOf("/reference/") != -1) {
134 var cookiePath = "reference_";
135 } else if (location.href.indexOf("/guide/") != -1) {
136 var cookiePath = "guide_";
137 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700138
139 if (!isMobile) {
Scott Main3f7d1df2009-11-02 15:31:04 -0800140 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700141 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700142 var cookieWidth = readCookie(cookiePath+'width');
143 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700144 if (cookieWidth) {
145 restoreWidth(cookieWidth);
146 } else if ($(".side-nav-resizable").length) {
147 resizeWidth();
148 }
149 if (cookieHeight) {
150 restoreHeight(cookieHeight);
151 } else {
152 resizeHeight();
153 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800154 }
155
156 if (devdocNav.length) { // only dev guide and sdk
157 highlightNav(location.href);
158 }
159}
160
161function highlightNav(fullPageName) {
162 var lastSlashPos = fullPageName.lastIndexOf("/");
163 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
164 fullPageName.indexOf("/guide/") :
165 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
166 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
167 fullPageName = fullPageName + "index.html";
168 }
169 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
170 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
171 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Scott Main0d8872e2009-10-23 13:04:31 -0700172 if ((link.length == 0) && (fullPageName.indexOf("/guide/") != -1)) {
173// 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 -0800174 lastBackstep = pathPageName.lastIndexOf("/");
175 while (link.length == 0) {
176 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
177 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
178 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
179 if (lastBackstep == 0) break;
180 }
181 }
182 link.parent().addClass('selected');
183 if (link.parent().parent().is(':hidden')) {
184 toggle(link.parent().parent().parent(), false);
185 } else if (link.parent().parent().hasClass('toggle-list')) {
186 toggle(link.parent().parent(), false);
187 }
188}
189
Scott Main3f7d1df2009-11-02 15:31:04 -0800190/* Resize the height of the nav panels in the reference,
191 * and save the new size to a cookie */
192function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800193 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800194 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800195 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
196 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800197
198 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800199 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800200
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700201 var basePath = getBaseUri(location.pathname);
202 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700203 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800204}
205
Scott Main3f7d1df2009-11-02 15:31:04 -0800206/* Resize the height of the side-nav and doc-content divs,
207 * which creates the frame effect */
208function resizeHeight() {
209 // Get the window height and always resize the doc-content and side-nav divs
210 var windowHeight = ($(window).height() - HEADER_HEIGHT);
211 content.css({height:windowHeight + "px"});
212 sidenav.css({height:windowHeight + "px"});
213
214 var href = location.href;
215 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
216 if (href.indexOf("/reference/") != -1) {
217 var swapperHeight = windowHeight - 13;
218 $("#swapper").css({height:swapperHeight + "px"});
219 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
220 $("#nav-tree").css({height:swapperHeight + "px"});
221
222 // If in the dev guide docs, also resize the "devdoc-nav" div
223 } else if (href.indexOf("/guide/") != -1) {
224 $("#devdoc-nav").css({height:sidenav.css("height")});
225 }
226}
227
228/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
229 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800230function resizeWidth() {
231 var windowWidth = $(window).width() + "px";
232 if (sidenav.length) {
233 var sidenavWidth = sidenav.css("width");
234 } else {
235 var sidenavWidth = 0;
236 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800237 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
238
239 if (isIE6) {
240 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
241 }
242
The Android Open Source Project88b60792009-03-03 19:28:42 -0800243 resizePackagesNav.css({width:sidenavWidth});
244 classesNav.css({width:sidenavWidth});
245 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800246
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700247 var basePath = getBaseUri(location.pathname);
248 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700249 writeCookie("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800250}
251
Scott Main3f7d1df2009-11-02 15:31:04 -0800252/* For IE6 only,
253 * because it can't properly perform auto width for "doc-content" div,
254 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800255function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800256 resizeHeight();
257 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800258}
259
Dirk Dougherty4405a232009-07-07 17:43:27 -0700260function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700261 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700262 if (intlUrl) {
263 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
264 base = base.substring(base.indexOf('/')+1, base.length);
265 //alert("intl, returning base url: /" + base);
266 return ("/" + base);
267 } else {
268 //alert("not intl, returning uri as found.");
269 return uri;
270 }
271}
272
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700273function requestAppendHL(uri) {
274//append "?hl=<lang> to an outgoing request (such as to blog)
275 var lang = getLangPref();
276 if (lang) {
277 var q = 'hl=' + lang;
278 uri += '?' + q;
279 window.location = uri;
280 return false;
281 } else {
282 return true;
283 }
284}
285
The Android Open Source Project88b60792009-03-03 19:28:42 -0800286function loadLast(cookiePath) {
287 var location = window.location.href;
288 if (location.indexOf("/"+cookiePath+"/") != -1) {
289 return true;
290 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700291 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800292 if (lastPage) {
293 window.location = lastPage;
294 return false;
295 }
296 return true;
297}
298
299$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700300 var path = getBaseUri(location.pathname);
301 if (path.indexOf("/reference/") != -1) {
302 writeCookie("lastpage", path, "reference", null);
303 } else if (path.indexOf("/guide/") != -1) {
304 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800305 }
306});
307
The Android Open Source Project88b60792009-03-03 19:28:42 -0800308function toggle(obj, slide) {
309 var ul = $("ul", obj);
310 var li = ul.parent();
311 if (li.hasClass("closed")) {
312 if (slide) {
313 ul.slideDown("fast");
314 } else {
315 ul.show();
316 }
317 li.removeClass("closed");
318 li.addClass("open");
319 $(".toggle-img", li).attr("title", "hide pages");
320 } else {
321 ul.slideUp("fast");
322 li.removeClass("open");
323 li.addClass("closed");
324 $(".toggle-img", li).attr("title", "show pages");
325 }
326}
327
The Android Open Source Project88b60792009-03-03 19:28:42 -0800328function buildToggleLists() {
329 $(".toggle-list").each(
330 function(i) {
331 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
332 $(this).addClass("closed");
333 });
334}
335
336function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700337 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800338 if (v != NAV_PREF_TREE) {
339 v = NAV_PREF_PANELS;
340 }
341 return v;
342}
343
344function chooseDefaultNav() {
345 nav_pref = getNavPref();
346 if (nav_pref == NAV_PREF_TREE) {
347 $("#nav-panels").toggle();
348 $("#panel-link").toggle();
349 $("#nav-tree").toggle();
350 $("#tree-link").toggle();
351 }
352}
353
354function swapNav() {
355 if (nav_pref == NAV_PREF_TREE) {
356 nav_pref = NAV_PREF_PANELS;
357 } else {
358 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700359 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800360 }
361 var date = new Date();
362 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700363 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800364
365 $("#nav-panels").toggle();
366 $("#panel-link").toggle();
367 $("#nav-tree").toggle();
368 $("#tree-link").toggle();
369
370 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
371 else {
372 scrollIntoView("packages-nav");
373 scrollIntoView("classes-nav");
374 }
375}
376
377function scrollIntoView(nav) {
378 var navObj = $("#"+nav);
379 if (navObj.is(':visible')) {
380 var selected = $(".selected", navObj);
381 if (selected.length == 0) return;
382 if (selected.is("div")) selected = selected.parent();
383
384 var scrolling = document.getElementById(nav);
385 var navHeight = navObj.height();
386 var offsetTop = selected.position().top;
387 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
388 if(offsetTop > navHeight - 92) {
389 scrolling.scrollTop = offsetTop - navHeight + 92;
390 }
391 }
392}
393
394function toggleAllInherited(linkObj, expand) {
395 var a = $(linkObj);
396 var table = $(a.parent().parent().parent());
397 var expandos = $(".jd-expando-trigger", table);
398 if ( (expand == null && a.text() == "[Expand]") || expand ) {
399 expandos.each(function(i) {
400 toggleInherited(this, true);
401 });
402 a.text("[Collapse]");
403 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
404 expandos.each(function(i) {
405 toggleInherited(this, false);
406 });
407 a.text("[Expand]");
408 }
409 return false;
410}
411
412function toggleAllSummaryInherited(linkObj) {
413 var a = $(linkObj);
414 var content = $(a.parent().parent().parent());
415 var toggles = $(".toggle-all", content);
416 if (a.text() == "[Expand All]") {
417 toggles.each(function(i) {
418 toggleAllInherited(this, true);
419 });
420 a.text("[Collapse All]");
421 } else {
422 toggles.each(function(i) {
423 toggleAllInherited(this, false);
424 });
425 a.text("[Expand All]");
426 }
427 return false;
428}
Dirk Dougherty4405a232009-07-07 17:43:27 -0700429
430
431function changeTabLang(lang) {
432 var nodes = $("#header-tabs").find("."+lang);
433 for (i=0; i < nodes.length; i++) { // for each node in this language
434 var node = $(nodes[i]);
435 node.siblings().css("display","none"); // hide all siblings
436 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
437 node.css("display","inline");
438 } else { //otherwise, show English instead
439 node.css("display","none");
440 node.siblings().filter(".en").css("display","inline");
441 }
442 }
443}
444
445function changeNavLang(lang) {
446 var nodes = $("#side-nav").find("."+lang);
447 for (i=0; i < nodes.length; i++) { // for each node in this language
448 var node = $(nodes[i]);
449 node.siblings().css("display","none"); // hide all siblings
450 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
451 node.css("display","inline");
452 } else { // otherwise, show English instead
453 node.css("display","none");
454 node.siblings().filter(".en").css("display","inline");
455 }
456 }
457}
458
459function changeDocLang(lang) {
460 changeTabLang(lang);
461 changeNavLang(lang);
462}
463
464function changeLangPref(lang, refresh) {
465 var date = new Date();
466 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
467 //alert("expires: " + expires)
468 writeCookie("pref_lang", lang, null, expires);
469 //changeDocLang(lang);
470 if (refresh) {
471 l = getBaseUri(location.pathname);
472 window.location = l;
473 }
474}
475
476function loadLangPref() {
477 var lang = readCookie("pref_lang");
478 if (lang != 0) {
479 $("#language").find("option[value='"+lang+"']").attr("selected",true);
480 }
481}
482
483function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700484 var lang = $("#language").find(":selected").attr("value");
485 if (!lang) {
486 lang = readCookie("pref_lang");
487 }
488 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700489}
Scott Main462cc372009-10-23 16:19:37 -0700490
491
492function toggleContent(obj) {
493 var button = $(obj);
494 var div = $(obj.parentNode);
495 var toggleMe = $(".toggle-content-toggleme",div);
496 if (button.hasClass("show")) {
497 toggleMe.slideDown();
498 button.removeClass("show").addClass("hide");
499 } else {
500 toggleMe.slideUp();
501 button.removeClass("hide").addClass("show");
502 }
503 $("span", button).toggle();
504}