blob: af1b700cee96a55da58dd54e7d317bcee1b95ee4 [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
The Android Open Source Project88b60792009-03-03 19:28:42 -080034window.onresize = resizeAll;
35
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070036function mobileSetup() {
37 $("body").css({'overflow':'auto'});
38 $("html").css({'overflow':'auto'});
39 $("#body-content").css({'position':'relative', 'top':'0'});
40 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
41 $("#side-nav").css({'padding':'0'});
42 $("#nav-tree").css({'overflow-y': 'auto'});
43}
44
The Android Open Source Project88b60792009-03-03 19:28:42 -080045function setToRoot(root) {
46 toRoot = root;
47 // note: toRoot also used by carousel.js
48}
49
50function restoreWidth(navWidth) {
51 var windowWidth = $(window).width() + "px";
52 content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar
53 width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"});
54 sidenav.css({width:navWidth});
55 resizePackagesNav.css({width:navWidth});
56 classesNav.css({width:navWidth});
57 $("#packages-nav").css({width:navWidth});
58}
59
60function restoreHeight(packageHeight) {
61 var windowHeight = ($(window).height() - HEADER_HEIGHT);
62 var swapperHeight = windowHeight - 13;
63 $("#swapper").css({height:swapperHeight + "px"});
64 sidenav.css({height:windowHeight + "px"});
65 content.css({height:windowHeight + "px"});
66 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
67 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
68 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
69 devdocNav.css({height:sidenav.css("height")});
70 $("#nav-tree").css({height:swapperHeight + "px"});
71}
72
Dirk Dougherty4405a232009-07-07 17:43:27 -070073function readCookie(cookie) {
74 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -080075 if (document.cookie) {
76 var index = document.cookie.indexOf(myCookie);
77 if (index != -1) {
78 var valStart = index + myCookie.length;
79 var valEnd = document.cookie.indexOf(";", valStart);
80 if (valEnd == -1) {
81 valEnd = document.cookie.length;
82 }
83 var val = document.cookie.substring(valStart, valEnd);
84 return val;
85 }
86 }
87 return 0;
88}
89
Dirk Dougherty4405a232009-07-07 17:43:27 -070090function writeCookie(cookie, val, section, expiration) {
The Android Open Source Project88b60792009-03-03 19:28:42 -080091 if (!val) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -070092 section = section == null ? "_" : "_"+section+"_";
93 if (expiration == null) {
94 var date = new Date();
95 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
96 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -080097 }
Dirk Dougherty4405a232009-07-07 17:43:27 -070098 document.cookie = cookie_namespace+section+cookie+"="+val+"; expires="+expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -080099}
100
101function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800102 $("#side-nav").css({position:"absolute",left:0});
103 content = $("#doc-content");
104 resizePackagesNav = $("#resize-packages-nav");
105 classesNav = $("#classes-nav");
106 sidenav = $("#side-nav");
107 devdocNav = $("#devdoc-nav");
108
109 if (location.href.indexOf("/reference/") != -1) {
110 var cookiePath = "reference_";
111 } else if (location.href.indexOf("/guide/") != -1) {
112 var cookiePath = "guide_";
113 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700114
115 if (!isMobile) {
116 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } });
117 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700118 var cookieWidth = readCookie(cookiePath+'width');
119 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700120 if (cookieWidth) {
121 restoreWidth(cookieWidth);
122 } else if ($(".side-nav-resizable").length) {
123 resizeWidth();
124 }
125 if (cookieHeight) {
126 restoreHeight(cookieHeight);
127 } else {
128 resizeHeight();
129 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800130 }
131
132 if (devdocNav.length) { // only dev guide and sdk
133 highlightNav(location.href);
134 }
135}
136
137function highlightNav(fullPageName) {
138 var lastSlashPos = fullPageName.lastIndexOf("/");
139 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
140 fullPageName.indexOf("/guide/") :
141 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
142 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
143 fullPageName = fullPageName + "index.html";
144 }
145 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
146 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
147 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
148 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) {
149// 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)
150 lastBackstep = pathPageName.lastIndexOf("/");
151 while (link.length == 0) {
152 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
153 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
154 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
155 if (lastBackstep == 0) break;
156 }
157 }
158 link.parent().addClass('selected');
159 if (link.parent().parent().is(':hidden')) {
160 toggle(link.parent().parent().parent(), false);
161 } else if (link.parent().parent().hasClass('toggle-list')) {
162 toggle(link.parent().parent(), false);
163 }
164}
165
166function resizeHeight() {
167 var windowHeight = ($(window).height() - HEADER_HEIGHT);
168 var swapperHeight = windowHeight - 13;
169 $("#swapper").css({height:swapperHeight + "px"});
170 sidenav.css({height:windowHeight + "px"});
171 content.css({height:windowHeight + "px"});
172 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
173 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
174 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
175 devdocNav.css({height:sidenav.css("height")});
176 $("#nav-tree").css({height:swapperHeight + "px"});
Dirk Dougherty4405a232009-07-07 17:43:27 -0700177
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700178 var basePath = getBaseUri(location.pathname);
179 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700180 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800181}
182
183function resizeWidth() {
184 var windowWidth = $(window).width() + "px";
185 if (sidenav.length) {
186 var sidenavWidth = sidenav.css("width");
187 } else {
188 var sidenavWidth = 0;
189 }
190 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
191 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
192 resizePackagesNav.css({width:sidenavWidth});
193 classesNav.css({width:sidenavWidth});
194 $("#packages-nav").css({width:sidenavWidth});
Dirk Dougherty4405a232009-07-07 17:43:27 -0700195
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700196 var basePath = getBaseUri(location.pathname);
197 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700198 writeCookie("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800199}
200
201function resizeAll() {
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700202 if (!isMobile) {
203 resizeHeight();
204 if ($(".side-nav-resizable").length) {
205 resizeWidth();
206 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800207 }
208}
209
Dirk Dougherty4405a232009-07-07 17:43:27 -0700210function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700211 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700212 if (intlUrl) {
213 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
214 base = base.substring(base.indexOf('/')+1, base.length);
215 //alert("intl, returning base url: /" + base);
216 return ("/" + base);
217 } else {
218 //alert("not intl, returning uri as found.");
219 return uri;
220 }
221}
222
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700223function requestAppendHL(uri) {
224//append "?hl=<lang> to an outgoing request (such as to blog)
225 var lang = getLangPref();
226 if (lang) {
227 var q = 'hl=' + lang;
228 uri += '?' + q;
229 window.location = uri;
230 return false;
231 } else {
232 return true;
233 }
234}
235
The Android Open Source Project88b60792009-03-03 19:28:42 -0800236function loadLast(cookiePath) {
237 var location = window.location.href;
238 if (location.indexOf("/"+cookiePath+"/") != -1) {
239 return true;
240 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700241 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800242 if (lastPage) {
243 window.location = lastPage;
244 return false;
245 }
246 return true;
247}
248
249$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700250 var path = getBaseUri(location.pathname);
251 if (path.indexOf("/reference/") != -1) {
252 writeCookie("lastpage", path, "reference", null);
253 } else if (path.indexOf("/guide/") != -1) {
254 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800255 }
256});
257
The Android Open Source Project88b60792009-03-03 19:28:42 -0800258function toggle(obj, slide) {
259 var ul = $("ul", obj);
260 var li = ul.parent();
261 if (li.hasClass("closed")) {
262 if (slide) {
263 ul.slideDown("fast");
264 } else {
265 ul.show();
266 }
267 li.removeClass("closed");
268 li.addClass("open");
269 $(".toggle-img", li).attr("title", "hide pages");
270 } else {
271 ul.slideUp("fast");
272 li.removeClass("open");
273 li.addClass("closed");
274 $(".toggle-img", li).attr("title", "show pages");
275 }
276}
277
The Android Open Source Project88b60792009-03-03 19:28:42 -0800278function buildToggleLists() {
279 $(".toggle-list").each(
280 function(i) {
281 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
282 $(this).addClass("closed");
283 });
284}
285
286function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700287 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800288 if (v != NAV_PREF_TREE) {
289 v = NAV_PREF_PANELS;
290 }
291 return v;
292}
293
294function chooseDefaultNav() {
295 nav_pref = getNavPref();
296 if (nav_pref == NAV_PREF_TREE) {
297 $("#nav-panels").toggle();
298 $("#panel-link").toggle();
299 $("#nav-tree").toggle();
300 $("#tree-link").toggle();
301 }
302}
303
304function swapNav() {
305 if (nav_pref == NAV_PREF_TREE) {
306 nav_pref = NAV_PREF_PANELS;
307 } else {
308 nav_pref = NAV_PREF_TREE;
309 init_navtree("nav-tree", toRoot, NAVTREE_DATA);
310 }
311 var date = new Date();
312 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700313 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800314
315 $("#nav-panels").toggle();
316 $("#panel-link").toggle();
317 $("#nav-tree").toggle();
318 $("#tree-link").toggle();
319
320 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
321 else {
322 scrollIntoView("packages-nav");
323 scrollIntoView("classes-nav");
324 }
325}
326
327function scrollIntoView(nav) {
328 var navObj = $("#"+nav);
329 if (navObj.is(':visible')) {
330 var selected = $(".selected", navObj);
331 if (selected.length == 0) return;
332 if (selected.is("div")) selected = selected.parent();
333
334 var scrolling = document.getElementById(nav);
335 var navHeight = navObj.height();
336 var offsetTop = selected.position().top;
337 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
338 if(offsetTop > navHeight - 92) {
339 scrolling.scrollTop = offsetTop - navHeight + 92;
340 }
341 }
342}
343
344function toggleAllInherited(linkObj, expand) {
345 var a = $(linkObj);
346 var table = $(a.parent().parent().parent());
347 var expandos = $(".jd-expando-trigger", table);
348 if ( (expand == null && a.text() == "[Expand]") || expand ) {
349 expandos.each(function(i) {
350 toggleInherited(this, true);
351 });
352 a.text("[Collapse]");
353 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
354 expandos.each(function(i) {
355 toggleInherited(this, false);
356 });
357 a.text("[Expand]");
358 }
359 return false;
360}
361
362function toggleAllSummaryInherited(linkObj) {
363 var a = $(linkObj);
364 var content = $(a.parent().parent().parent());
365 var toggles = $(".toggle-all", content);
366 if (a.text() == "[Expand All]") {
367 toggles.each(function(i) {
368 toggleAllInherited(this, true);
369 });
370 a.text("[Collapse All]");
371 } else {
372 toggles.each(function(i) {
373 toggleAllInherited(this, false);
374 });
375 a.text("[Expand All]");
376 }
377 return false;
378}
Dirk Dougherty4405a232009-07-07 17:43:27 -0700379
380
381function changeTabLang(lang) {
382 var nodes = $("#header-tabs").find("."+lang);
383 for (i=0; i < nodes.length; i++) { // for each node in this language
384 var node = $(nodes[i]);
385 node.siblings().css("display","none"); // hide all siblings
386 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
387 node.css("display","inline");
388 } else { //otherwise, show English instead
389 node.css("display","none");
390 node.siblings().filter(".en").css("display","inline");
391 }
392 }
393}
394
395function changeNavLang(lang) {
396 var nodes = $("#side-nav").find("."+lang);
397 for (i=0; i < nodes.length; i++) { // for each node in this language
398 var node = $(nodes[i]);
399 node.siblings().css("display","none"); // hide all siblings
400 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
401 node.css("display","inline");
402 } else { // otherwise, show English instead
403 node.css("display","none");
404 node.siblings().filter(".en").css("display","inline");
405 }
406 }
407}
408
409function changeDocLang(lang) {
410 changeTabLang(lang);
411 changeNavLang(lang);
412}
413
414function changeLangPref(lang, refresh) {
415 var date = new Date();
416 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
417 //alert("expires: " + expires)
418 writeCookie("pref_lang", lang, null, expires);
419 //changeDocLang(lang);
420 if (refresh) {
421 l = getBaseUri(location.pathname);
422 window.location = l;
423 }
424}
425
426function loadLangPref() {
427 var lang = readCookie("pref_lang");
428 if (lang != 0) {
429 $("#language").find("option[value='"+lang+"']").attr("selected",true);
430 }
431}
432
433function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700434 var lang = $("#language").find(":selected").attr("value");
435 if (!lang) {
436 lang = readCookie("pref_lang");
437 }
438 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700439}