blob: 64311638a139ce5890a69ea9983cef732f88cb80 [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
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070027var agent = navigator['userAgent'];
Scott Main3f7d1df2009-11-02 15:31:04 -080028// If a mobile phone, set flag and do mobile setup
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070029if ((agent.indexOf("Mobile") != -1) ||
30 (agent.indexOf("BlackBerry") != -1) ||
31 (agent.indexOf("Mini") != -1)) {
32 isMobile = true;
33 addLoadEvent(mobileSetup);
Scott Main3f7d1df2009-11-02 15:31:04 -080034// If not a mobile browser, set the onresize event for IE6, and others
35} else if (agent.indexOf("MSIE 6.0") != -1) {
36 isIE6 = true;
37 addLoadEvent(function() {
38 window.onresize = resizeAll;
39 });
40} else {
41 addLoadEvent(function() {
42 window.onresize = resizeHeight;
43 });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070044}
45
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070046function mobileSetup() {
47 $("body").css({'overflow':'auto'});
48 $("html").css({'overflow':'auto'});
49 $("#body-content").css({'position':'relative', 'top':'0'});
50 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
51 $("#side-nav").css({'padding':'0'});
52 $("#nav-tree").css({'overflow-y': 'auto'});
53}
54
Scott Main25fda192009-08-04 11:26:30 -070055/* loads the lists.js file to the page.
56Loading this in the head was slowing page load time */
57addLoadEvent( function() {
58 var lists = document.createElement("script");
59 lists.setAttribute("type","text/javascript");
60 lists.setAttribute("src", toRoot+"reference/lists.js");
Scott Main1da82382009-09-17 10:54:05 -070061 document.getElementsByTagName("head")[0].appendChild(lists);
Scott Main25fda192009-08-04 11:26:30 -070062} );
63
The Android Open Source Project88b60792009-03-03 19:28:42 -080064function setToRoot(root) {
65 toRoot = root;
66 // note: toRoot also used by carousel.js
67}
68
69function restoreWidth(navWidth) {
70 var windowWidth = $(window).width() + "px";
Scott Main3f7d1df2009-11-02 15:31:04 -080071 content.css({marginLeft:parseInt(navWidth) + 6 + "px"}); //account for 6px-wide handle-bar
72
73 if (isIE6) {
74 content.css({width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); // necessary in order for scrollbars to be visible
75 }
76
The Android Open Source Project88b60792009-03-03 19:28:42 -080077 sidenav.css({width:navWidth});
78 resizePackagesNav.css({width:navWidth});
79 classesNav.css({width:navWidth});
80 $("#packages-nav").css({width:navWidth});
81}
82
83function restoreHeight(packageHeight) {
84 var windowHeight = ($(window).height() - HEADER_HEIGHT);
85 var swapperHeight = windowHeight - 13;
86 $("#swapper").css({height:swapperHeight + "px"});
87 sidenav.css({height:windowHeight + "px"});
88 content.css({height:windowHeight + "px"});
89 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
90 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
91 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
92 devdocNav.css({height:sidenav.css("height")});
93 $("#nav-tree").css({height:swapperHeight + "px"});
94}
95
Dirk Dougherty4405a232009-07-07 17:43:27 -070096function readCookie(cookie) {
97 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -080098 if (document.cookie) {
99 var index = document.cookie.indexOf(myCookie);
100 if (index != -1) {
101 var valStart = index + myCookie.length;
102 var valEnd = document.cookie.indexOf(";", valStart);
103 if (valEnd == -1) {
104 valEnd = document.cookie.length;
105 }
106 var val = document.cookie.substring(valStart, valEnd);
107 return val;
108 }
109 }
110 return 0;
111}
112
Dirk Dougherty4405a232009-07-07 17:43:27 -0700113function writeCookie(cookie, val, section, expiration) {
Scott Main9b5fdb92009-10-27 15:09:15 -0700114 if (val==undefined) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -0700115 section = section == null ? "_" : "_"+section+"_";
116 if (expiration == null) {
117 var date = new Date();
118 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
119 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800120 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800121 document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800122}
123
124function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800125 $("#side-nav").css({position:"absolute",left:0});
126 content = $("#doc-content");
127 resizePackagesNav = $("#resize-packages-nav");
128 classesNav = $("#classes-nav");
129 sidenav = $("#side-nav");
130 devdocNav = $("#devdoc-nav");
131
132 if (location.href.indexOf("/reference/") != -1) {
133 var cookiePath = "reference_";
134 } else if (location.href.indexOf("/guide/") != -1) {
135 var cookiePath = "guide_";
136 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700137
138 if (!isMobile) {
Scott Main3f7d1df2009-11-02 15:31:04 -0800139 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700140 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700141 var cookieWidth = readCookie(cookiePath+'width');
142 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700143 if (cookieWidth) {
144 restoreWidth(cookieWidth);
145 } else if ($(".side-nav-resizable").length) {
146 resizeWidth();
147 }
148 if (cookieHeight) {
149 restoreHeight(cookieHeight);
150 } else {
151 resizeHeight();
152 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800153 }
154
155 if (devdocNav.length) { // only dev guide and sdk
156 highlightNav(location.href);
157 }
158}
159
160function highlightNav(fullPageName) {
161 var lastSlashPos = fullPageName.lastIndexOf("/");
162 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
163 fullPageName.indexOf("/guide/") :
164 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
165 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
166 fullPageName = fullPageName + "index.html";
167 }
168 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
169 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
170 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Scott Main0d8872e2009-10-23 13:04:31 -0700171 if ((link.length == 0) && (fullPageName.indexOf("/guide/") != -1)) {
172// 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 -0800173 lastBackstep = pathPageName.lastIndexOf("/");
174 while (link.length == 0) {
175 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
176 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
177 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
178 if (lastBackstep == 0) break;
179 }
180 }
181 link.parent().addClass('selected');
182 if (link.parent().parent().is(':hidden')) {
183 toggle(link.parent().parent().parent(), false);
184 } else if (link.parent().parent().hasClass('toggle-list')) {
185 toggle(link.parent().parent(), false);
186 }
187}
188
Scott Main3f7d1df2009-11-02 15:31:04 -0800189/* Resize the height of the nav panels in the reference,
190 * and save the new size to a cookie */
191function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800192 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800193 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800194 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
195 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800196
197 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800198 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800199
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700200 var basePath = getBaseUri(location.pathname);
201 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700202 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800203}
204
Scott Main3f7d1df2009-11-02 15:31:04 -0800205/* Resize the height of the side-nav and doc-content divs,
206 * which creates the frame effect */
207function resizeHeight() {
208 // Get the window height and always resize the doc-content and side-nav divs
209 var windowHeight = ($(window).height() - HEADER_HEIGHT);
210 content.css({height:windowHeight + "px"});
211 sidenav.css({height:windowHeight + "px"});
212
213 var href = location.href;
214 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
215 if (href.indexOf("/reference/") != -1) {
216 var swapperHeight = windowHeight - 13;
217 $("#swapper").css({height:swapperHeight + "px"});
218 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
219 $("#nav-tree").css({height:swapperHeight + "px"});
220
221 // If in the dev guide docs, also resize the "devdoc-nav" div
222 } else if (href.indexOf("/guide/") != -1) {
223 $("#devdoc-nav").css({height:sidenav.css("height")});
224 }
225}
226
227/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
228 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800229function resizeWidth() {
230 var windowWidth = $(window).width() + "px";
231 if (sidenav.length) {
232 var sidenavWidth = sidenav.css("width");
233 } else {
234 var sidenavWidth = 0;
235 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800236 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
237
238 if (isIE6) {
239 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
240 }
241
The Android Open Source Project88b60792009-03-03 19:28:42 -0800242 resizePackagesNav.css({width:sidenavWidth});
243 classesNav.css({width:sidenavWidth});
244 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800245
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700246 var basePath = getBaseUri(location.pathname);
247 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700248 writeCookie("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800249}
250
Scott Main3f7d1df2009-11-02 15:31:04 -0800251/* For IE6 only,
252 * because it can't properly perform auto width for "doc-content" div,
253 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800254function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800255 resizeHeight();
256 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800257}
258
Dirk Dougherty4405a232009-07-07 17:43:27 -0700259function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700260 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700261 if (intlUrl) {
262 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
263 base = base.substring(base.indexOf('/')+1, base.length);
264 //alert("intl, returning base url: /" + base);
265 return ("/" + base);
266 } else {
267 //alert("not intl, returning uri as found.");
268 return uri;
269 }
270}
271
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700272function requestAppendHL(uri) {
273//append "?hl=<lang> to an outgoing request (such as to blog)
274 var lang = getLangPref();
275 if (lang) {
276 var q = 'hl=' + lang;
277 uri += '?' + q;
278 window.location = uri;
279 return false;
280 } else {
281 return true;
282 }
283}
284
The Android Open Source Project88b60792009-03-03 19:28:42 -0800285function loadLast(cookiePath) {
286 var location = window.location.href;
287 if (location.indexOf("/"+cookiePath+"/") != -1) {
288 return true;
289 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700290 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800291 if (lastPage) {
292 window.location = lastPage;
293 return false;
294 }
295 return true;
296}
297
298$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700299 var path = getBaseUri(location.pathname);
300 if (path.indexOf("/reference/") != -1) {
301 writeCookie("lastpage", path, "reference", null);
302 } else if (path.indexOf("/guide/") != -1) {
303 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800304 }
305});
306
The Android Open Source Project88b60792009-03-03 19:28:42 -0800307function toggle(obj, slide) {
308 var ul = $("ul", obj);
309 var li = ul.parent();
310 if (li.hasClass("closed")) {
311 if (slide) {
312 ul.slideDown("fast");
313 } else {
314 ul.show();
315 }
316 li.removeClass("closed");
317 li.addClass("open");
318 $(".toggle-img", li).attr("title", "hide pages");
319 } else {
320 ul.slideUp("fast");
321 li.removeClass("open");
322 li.addClass("closed");
323 $(".toggle-img", li).attr("title", "show pages");
324 }
325}
326
The Android Open Source Project88b60792009-03-03 19:28:42 -0800327function buildToggleLists() {
328 $(".toggle-list").each(
329 function(i) {
330 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
331 $(this).addClass("closed");
332 });
333}
334
335function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700336 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800337 if (v != NAV_PREF_TREE) {
338 v = NAV_PREF_PANELS;
339 }
340 return v;
341}
342
343function chooseDefaultNav() {
344 nav_pref = getNavPref();
345 if (nav_pref == NAV_PREF_TREE) {
346 $("#nav-panels").toggle();
347 $("#panel-link").toggle();
348 $("#nav-tree").toggle();
349 $("#tree-link").toggle();
350 }
351}
352
353function swapNav() {
354 if (nav_pref == NAV_PREF_TREE) {
355 nav_pref = NAV_PREF_PANELS;
356 } else {
357 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700358 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800359 }
360 var date = new Date();
361 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700362 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800363
364 $("#nav-panels").toggle();
365 $("#panel-link").toggle();
366 $("#nav-tree").toggle();
367 $("#tree-link").toggle();
368
369 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
370 else {
371 scrollIntoView("packages-nav");
372 scrollIntoView("classes-nav");
373 }
374}
375
376function scrollIntoView(nav) {
377 var navObj = $("#"+nav);
378 if (navObj.is(':visible')) {
379 var selected = $(".selected", navObj);
380 if (selected.length == 0) return;
381 if (selected.is("div")) selected = selected.parent();
382
383 var scrolling = document.getElementById(nav);
384 var navHeight = navObj.height();
385 var offsetTop = selected.position().top;
386 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
387 if(offsetTop > navHeight - 92) {
388 scrolling.scrollTop = offsetTop - navHeight + 92;
389 }
390 }
391}
392
393function toggleAllInherited(linkObj, expand) {
394 var a = $(linkObj);
395 var table = $(a.parent().parent().parent());
396 var expandos = $(".jd-expando-trigger", table);
397 if ( (expand == null && a.text() == "[Expand]") || expand ) {
398 expandos.each(function(i) {
399 toggleInherited(this, true);
400 });
401 a.text("[Collapse]");
402 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
403 expandos.each(function(i) {
404 toggleInherited(this, false);
405 });
406 a.text("[Expand]");
407 }
408 return false;
409}
410
411function toggleAllSummaryInherited(linkObj) {
412 var a = $(linkObj);
413 var content = $(a.parent().parent().parent());
414 var toggles = $(".toggle-all", content);
415 if (a.text() == "[Expand All]") {
416 toggles.each(function(i) {
417 toggleAllInherited(this, true);
418 });
419 a.text("[Collapse All]");
420 } else {
421 toggles.each(function(i) {
422 toggleAllInherited(this, false);
423 });
424 a.text("[Expand All]");
425 }
426 return false;
427}
Dirk Dougherty4405a232009-07-07 17:43:27 -0700428
429
430function changeTabLang(lang) {
431 var nodes = $("#header-tabs").find("."+lang);
432 for (i=0; i < nodes.length; i++) { // for each node in this language
433 var node = $(nodes[i]);
434 node.siblings().css("display","none"); // hide all siblings
435 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
436 node.css("display","inline");
437 } else { //otherwise, show English instead
438 node.css("display","none");
439 node.siblings().filter(".en").css("display","inline");
440 }
441 }
442}
443
444function changeNavLang(lang) {
445 var nodes = $("#side-nav").find("."+lang);
446 for (i=0; i < nodes.length; i++) { // for each node in this language
447 var node = $(nodes[i]);
448 node.siblings().css("display","none"); // hide all siblings
449 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
450 node.css("display","inline");
451 } else { // otherwise, show English instead
452 node.css("display","none");
453 node.siblings().filter(".en").css("display","inline");
454 }
455 }
456}
457
458function changeDocLang(lang) {
459 changeTabLang(lang);
460 changeNavLang(lang);
461}
462
463function changeLangPref(lang, refresh) {
464 var date = new Date();
465 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
466 //alert("expires: " + expires)
467 writeCookie("pref_lang", lang, null, expires);
468 //changeDocLang(lang);
469 if (refresh) {
470 l = getBaseUri(location.pathname);
471 window.location = l;
472 }
473}
474
475function loadLangPref() {
476 var lang = readCookie("pref_lang");
477 if (lang != 0) {
478 $("#language").find("option[value='"+lang+"']").attr("selected",true);
479 }
480}
481
482function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700483 var lang = $("#language").find(":selected").attr("value");
484 if (!lang) {
485 lang = readCookie("pref_lang");
486 }
487 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700488}
Scott Main462cc372009-10-23 16:19:37 -0700489
490
491function toggleContent(obj) {
492 var button = $(obj);
493 var div = $(obj.parentNode);
494 var toggleMe = $(".toggle-content-toggleme",div);
495 if (button.hasClass("show")) {
496 toggleMe.slideDown();
497 button.removeClass("show").addClass("hide");
498 } else {
499 toggleMe.slideUp();
500 button.removeClass("hide").addClass("show");
501 }
502 $("span", button).toggle();
503}