blob: b16ed0d8db5e86af1cfe8d69e36aab14fe451c80 [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
The Android Open Source Project88b60792009-03-03 19:28:42 -080013
14function addLoadEvent(newfun) {
15 var current = window.onload;
16 if (typeof window.onload != 'function') {
17 window.onload = newfun;
18 } else {
19 window.onload = function() {
20 current();
21 newfun();
22 }
23 }
24}
25
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070026var agent = navigator['userAgent'];
27if ((agent.indexOf("Mobile") != -1) ||
28 (agent.indexOf("BlackBerry") != -1) ||
29 (agent.indexOf("Mini") != -1)) {
30 isMobile = true;
31 addLoadEvent(mobileSetup);
32}
33
Scott Main25fda192009-08-04 11:26:30 -070034addLoadEvent(function() {
The Android Open Source Project88b60792009-03-03 19:28:42 -080035window.onresize = resizeAll;
Scott Main25fda192009-08-04 11:26:30 -070036});
The Android Open Source Project88b60792009-03-03 19:28:42 -080037
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070038function mobileSetup() {
39 $("body").css({'overflow':'auto'});
40 $("html").css({'overflow':'auto'});
41 $("#body-content").css({'position':'relative', 'top':'0'});
42 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
43 $("#side-nav").css({'padding':'0'});
44 $("#nav-tree").css({'overflow-y': 'auto'});
45}
46
Scott Main25fda192009-08-04 11:26:30 -070047/* loads the lists.js file to the page.
48Loading this in the head was slowing page load time */
49addLoadEvent( function() {
50 var lists = document.createElement("script");
51 lists.setAttribute("type","text/javascript");
52 lists.setAttribute("src", toRoot+"reference/lists.js");
53 $("head").append($(lists));
54} );
55
The Android Open Source Project88b60792009-03-03 19:28:42 -080056function setToRoot(root) {
57 toRoot = root;
58 // note: toRoot also used by carousel.js
59}
60
61function restoreWidth(navWidth) {
62 var windowWidth = $(window).width() + "px";
63 content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar
64 width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"});
65 sidenav.css({width:navWidth});
66 resizePackagesNav.css({width:navWidth});
67 classesNav.css({width:navWidth});
68 $("#packages-nav").css({width:navWidth});
69}
70
71function restoreHeight(packageHeight) {
72 var windowHeight = ($(window).height() - HEADER_HEIGHT);
73 var swapperHeight = windowHeight - 13;
74 $("#swapper").css({height:swapperHeight + "px"});
75 sidenav.css({height:windowHeight + "px"});
76 content.css({height:windowHeight + "px"});
77 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
78 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
79 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
80 devdocNav.css({height:sidenav.css("height")});
81 $("#nav-tree").css({height:swapperHeight + "px"});
82}
83
Dirk Dougherty4405a232009-07-07 17:43:27 -070084function readCookie(cookie) {
85 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -080086 if (document.cookie) {
87 var index = document.cookie.indexOf(myCookie);
88 if (index != -1) {
89 var valStart = index + myCookie.length;
90 var valEnd = document.cookie.indexOf(";", valStart);
91 if (valEnd == -1) {
92 valEnd = document.cookie.length;
93 }
94 var val = document.cookie.substring(valStart, valEnd);
95 return val;
96 }
97 }
98 return 0;
99}
100
Dirk Dougherty4405a232009-07-07 17:43:27 -0700101function writeCookie(cookie, val, section, expiration) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800102 if (!val) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -0700103 section = section == null ? "_" : "_"+section+"_";
104 if (expiration == null) {
105 var date = new Date();
106 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
107 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800108 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700109 document.cookie = cookie_namespace+section+cookie+"="+val+"; expires="+expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800110}
111
112function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800113 $("#side-nav").css({position:"absolute",left:0});
114 content = $("#doc-content");
115 resizePackagesNav = $("#resize-packages-nav");
116 classesNav = $("#classes-nav");
117 sidenav = $("#side-nav");
118 devdocNav = $("#devdoc-nav");
119
120 if (location.href.indexOf("/reference/") != -1) {
121 var cookiePath = "reference_";
122 } else if (location.href.indexOf("/guide/") != -1) {
123 var cookiePath = "guide_";
124 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700125
126 if (!isMobile) {
127 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } });
128 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700129 var cookieWidth = readCookie(cookiePath+'width');
130 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700131 if (cookieWidth) {
132 restoreWidth(cookieWidth);
133 } else if ($(".side-nav-resizable").length) {
134 resizeWidth();
135 }
136 if (cookieHeight) {
137 restoreHeight(cookieHeight);
138 } else {
139 resizeHeight();
140 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800141 }
142
143 if (devdocNav.length) { // only dev guide and sdk
144 highlightNav(location.href);
145 }
146}
147
148function highlightNav(fullPageName) {
149 var lastSlashPos = fullPageName.lastIndexOf("/");
150 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
151 fullPageName.indexOf("/guide/") :
152 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
153 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
154 fullPageName = fullPageName + "index.html";
155 }
156 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
157 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
158 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
159 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) {
160// 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 and sdk)
161 lastBackstep = pathPageName.lastIndexOf("/");
162 while (link.length == 0) {
163 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
164 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
165 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
166 if (lastBackstep == 0) break;
167 }
168 }
169 link.parent().addClass('selected');
170 if (link.parent().parent().is(':hidden')) {
171 toggle(link.parent().parent().parent(), false);
172 } else if (link.parent().parent().hasClass('toggle-list')) {
173 toggle(link.parent().parent(), false);
174 }
175}
176
177function resizeHeight() {
178 var windowHeight = ($(window).height() - HEADER_HEIGHT);
179 var swapperHeight = windowHeight - 13;
180 $("#swapper").css({height:swapperHeight + "px"});
181 sidenav.css({height:windowHeight + "px"});
182 content.css({height:windowHeight + "px"});
183 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
184 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
185 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
186 devdocNav.css({height:sidenav.css("height")});
187 $("#nav-tree").css({height:swapperHeight + "px"});
Dirk Dougherty4405a232009-07-07 17:43:27 -0700188
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700189 var basePath = getBaseUri(location.pathname);
190 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700191 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800192}
193
194function resizeWidth() {
195 var windowWidth = $(window).width() + "px";
196 if (sidenav.length) {
197 var sidenavWidth = sidenav.css("width");
198 } else {
199 var sidenavWidth = 0;
200 }
201 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
202 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
203 resizePackagesNav.css({width:sidenavWidth});
204 classesNav.css({width:sidenavWidth});
205 $("#packages-nav").css({width:sidenavWidth});
Dirk Dougherty4405a232009-07-07 17:43:27 -0700206
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("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800210}
211
212function resizeAll() {
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700213 if (!isMobile) {
214 resizeHeight();
215 if ($(".side-nav-resizable").length) {
216 resizeWidth();
217 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800218 }
219}
220
Dirk Dougherty4405a232009-07-07 17:43:27 -0700221function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700222 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700223 if (intlUrl) {
224 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
225 base = base.substring(base.indexOf('/')+1, base.length);
226 //alert("intl, returning base url: /" + base);
227 return ("/" + base);
228 } else {
229 //alert("not intl, returning uri as found.");
230 return uri;
231 }
232}
233
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700234function requestAppendHL(uri) {
235//append "?hl=<lang> to an outgoing request (such as to blog)
236 var lang = getLangPref();
237 if (lang) {
238 var q = 'hl=' + lang;
239 uri += '?' + q;
240 window.location = uri;
241 return false;
242 } else {
243 return true;
244 }
245}
246
The Android Open Source Project88b60792009-03-03 19:28:42 -0800247function loadLast(cookiePath) {
248 var location = window.location.href;
249 if (location.indexOf("/"+cookiePath+"/") != -1) {
250 return true;
251 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700252 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800253 if (lastPage) {
254 window.location = lastPage;
255 return false;
256 }
257 return true;
258}
259
260$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700261 var path = getBaseUri(location.pathname);
262 if (path.indexOf("/reference/") != -1) {
263 writeCookie("lastpage", path, "reference", null);
264 } else if (path.indexOf("/guide/") != -1) {
265 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800266 }
267});
268
The Android Open Source Project88b60792009-03-03 19:28:42 -0800269function toggle(obj, slide) {
270 var ul = $("ul", obj);
271 var li = ul.parent();
272 if (li.hasClass("closed")) {
273 if (slide) {
274 ul.slideDown("fast");
275 } else {
276 ul.show();
277 }
278 li.removeClass("closed");
279 li.addClass("open");
280 $(".toggle-img", li).attr("title", "hide pages");
281 } else {
282 ul.slideUp("fast");
283 li.removeClass("open");
284 li.addClass("closed");
285 $(".toggle-img", li).attr("title", "show pages");
286 }
287}
288
The Android Open Source Project88b60792009-03-03 19:28:42 -0800289function buildToggleLists() {
290 $(".toggle-list").each(
291 function(i) {
292 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
293 $(this).addClass("closed");
294 });
295}
296
297function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700298 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800299 if (v != NAV_PREF_TREE) {
300 v = NAV_PREF_PANELS;
301 }
302 return v;
303}
304
305function chooseDefaultNav() {
306 nav_pref = getNavPref();
307 if (nav_pref == NAV_PREF_TREE) {
308 $("#nav-panels").toggle();
309 $("#panel-link").toggle();
310 $("#nav-tree").toggle();
311 $("#tree-link").toggle();
312 }
313}
314
315function swapNav() {
316 if (nav_pref == NAV_PREF_TREE) {
317 nav_pref = NAV_PREF_PANELS;
318 } else {
319 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700320 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800321 }
322 var date = new Date();
323 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700324 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800325
326 $("#nav-panels").toggle();
327 $("#panel-link").toggle();
328 $("#nav-tree").toggle();
329 $("#tree-link").toggle();
330
331 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
332 else {
333 scrollIntoView("packages-nav");
334 scrollIntoView("classes-nav");
335 }
336}
337
338function scrollIntoView(nav) {
339 var navObj = $("#"+nav);
340 if (navObj.is(':visible')) {
341 var selected = $(".selected", navObj);
342 if (selected.length == 0) return;
343 if (selected.is("div")) selected = selected.parent();
344
345 var scrolling = document.getElementById(nav);
346 var navHeight = navObj.height();
347 var offsetTop = selected.position().top;
348 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
349 if(offsetTop > navHeight - 92) {
350 scrolling.scrollTop = offsetTop - navHeight + 92;
351 }
352 }
353}
354
355function toggleAllInherited(linkObj, expand) {
356 var a = $(linkObj);
357 var table = $(a.parent().parent().parent());
358 var expandos = $(".jd-expando-trigger", table);
359 if ( (expand == null && a.text() == "[Expand]") || expand ) {
360 expandos.each(function(i) {
361 toggleInherited(this, true);
362 });
363 a.text("[Collapse]");
364 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
365 expandos.each(function(i) {
366 toggleInherited(this, false);
367 });
368 a.text("[Expand]");
369 }
370 return false;
371}
372
373function toggleAllSummaryInherited(linkObj) {
374 var a = $(linkObj);
375 var content = $(a.parent().parent().parent());
376 var toggles = $(".toggle-all", content);
377 if (a.text() == "[Expand All]") {
378 toggles.each(function(i) {
379 toggleAllInherited(this, true);
380 });
381 a.text("[Collapse All]");
382 } else {
383 toggles.each(function(i) {
384 toggleAllInherited(this, false);
385 });
386 a.text("[Expand All]");
387 }
388 return false;
389}
Dirk Dougherty4405a232009-07-07 17:43:27 -0700390
391
392function changeTabLang(lang) {
393 var nodes = $("#header-tabs").find("."+lang);
394 for (i=0; i < nodes.length; i++) { // for each node in this language
395 var node = $(nodes[i]);
396 node.siblings().css("display","none"); // hide all siblings
397 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
398 node.css("display","inline");
399 } else { //otherwise, show English instead
400 node.css("display","none");
401 node.siblings().filter(".en").css("display","inline");
402 }
403 }
404}
405
406function changeNavLang(lang) {
407 var nodes = $("#side-nav").find("."+lang);
408 for (i=0; i < nodes.length; i++) { // for each node in this language
409 var node = $(nodes[i]);
410 node.siblings().css("display","none"); // hide all siblings
411 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
412 node.css("display","inline");
413 } else { // otherwise, show English instead
414 node.css("display","none");
415 node.siblings().filter(".en").css("display","inline");
416 }
417 }
418}
419
420function changeDocLang(lang) {
421 changeTabLang(lang);
422 changeNavLang(lang);
423}
424
425function changeLangPref(lang, refresh) {
426 var date = new Date();
427 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
428 //alert("expires: " + expires)
429 writeCookie("pref_lang", lang, null, expires);
430 //changeDocLang(lang);
431 if (refresh) {
432 l = getBaseUri(location.pathname);
433 window.location = l;
434 }
435}
436
437function loadLangPref() {
438 var lang = readCookie("pref_lang");
439 if (lang != 0) {
440 $("#language").find("option[value='"+lang+"']").attr("selected",true);
441 }
442}
443
444function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700445 var lang = $("#language").find(":selected").attr("value");
446 if (!lang) {
447 lang = readCookie("pref_lang");
448 }
449 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700450}