blob: 160e4926dc8b388cc0ec05261a8b07e8bd574c92 [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;
Scott Main1043da92009-05-20 17:16:35 -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 Main5b53cd72009-06-04 11:10:17 -070034/* loads the lists.js file to the page.
35Loading this in the head was slowing page load time */
36addLoadEvent( function() {
37 var lists = document.createElement("script");
38 lists.setAttribute("type","text/javascript");
39 lists.setAttribute("src", toRoot+"reference/lists.js");
40 $("head").append($(lists));
41} );
42
The Android Open Source Project88b60792009-03-03 19:28:42 -080043window.onresize = resizeAll;
44
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070045function mobileSetup() {
46 $("body").css({'overflow':'auto'});
47 $("html").css({'overflow':'auto'});
48 $("#body-content").css({'position':'relative', 'top':'0'});
49 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
50 $("#side-nav").css({'padding':'0'});
51 $("#nav-tree").css({'overflow-y': 'auto'});
52}
53
The Android Open Source Project88b60792009-03-03 19:28:42 -080054function setToRoot(root) {
55 toRoot = root;
56 // note: toRoot also used by carousel.js
57}
58
59function restoreWidth(navWidth) {
60 var windowWidth = $(window).width() + "px";
61 content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar
62 width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"});
63 sidenav.css({width:navWidth});
64 resizePackagesNav.css({width:navWidth});
65 classesNav.css({width:navWidth});
66 $("#packages-nav").css({width:navWidth});
67}
68
69function restoreHeight(packageHeight) {
70 var windowHeight = ($(window).height() - HEADER_HEIGHT);
71 var swapperHeight = windowHeight - 13;
72 $("#swapper").css({height:swapperHeight + "px"});
73 sidenav.css({height:windowHeight + "px"});
74 content.css({height:windowHeight + "px"});
75 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
76 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
77 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
78 devdocNav.css({height:sidenav.css("height")});
79 $("#nav-tree").css({height:swapperHeight + "px"});
80}
81
Scott Main1043da92009-05-20 17:16:35 -070082function readCookie(cookie) {
83 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -080084 if (document.cookie) {
85 var index = document.cookie.indexOf(myCookie);
86 if (index != -1) {
87 var valStart = index + myCookie.length;
88 var valEnd = document.cookie.indexOf(";", valStart);
89 if (valEnd == -1) {
90 valEnd = document.cookie.length;
91 }
92 var val = document.cookie.substring(valStart, valEnd);
93 return val;
94 }
95 }
96 return 0;
97}
98
Scott Main1043da92009-05-20 17:16:35 -070099function writeCookie(cookie, val, section, expiration) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800100 if (!val) return;
Scott Main1043da92009-05-20 17:16:35 -0700101 section = section == null ? "_" : "_"+section+"_";
102 if (expiration == null) {
103 var date = new Date();
104 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
105 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800106 }
Scott Main1043da92009-05-20 17:16:35 -0700107 document.cookie = cookie_namespace+section+cookie+"="+val+"; expires="+expiration+"; path=/";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800108}
109
110function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800111 $("#side-nav").css({position:"absolute",left:0});
112 content = $("#doc-content");
113 resizePackagesNav = $("#resize-packages-nav");
114 classesNav = $("#classes-nav");
115 sidenav = $("#side-nav");
116 devdocNav = $("#devdoc-nav");
117
118 if (location.href.indexOf("/reference/") != -1) {
119 var cookiePath = "reference_";
120 } else if (location.href.indexOf("/guide/") != -1) {
121 var cookiePath = "guide_";
122 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700123
124 if (!isMobile) {
125 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } });
126 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Scott Main1043da92009-05-20 17:16:35 -0700127 var cookieWidth = readCookie(cookiePath+'width');
128 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700129 if (cookieWidth) {
130 restoreWidth(cookieWidth);
131 } else if ($(".side-nav-resizable").length) {
132 resizeWidth();
133 }
134 if (cookieHeight) {
135 restoreHeight(cookieHeight);
136 } else {
137 resizeHeight();
138 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800139 }
140
141 if (devdocNav.length) { // only dev guide and sdk
142 highlightNav(location.href);
143 }
144}
145
146function highlightNav(fullPageName) {
147 var lastSlashPos = fullPageName.lastIndexOf("/");
148 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
149 fullPageName.indexOf("/guide/") :
150 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
151 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
152 fullPageName = fullPageName + "index.html";
153 }
154 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
155 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
156 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
157 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) {
158// 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)
159 lastBackstep = pathPageName.lastIndexOf("/");
160 while (link.length == 0) {
161 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
162 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
163 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
164 if (lastBackstep == 0) break;
165 }
166 }
167 link.parent().addClass('selected');
168 if (link.parent().parent().is(':hidden')) {
169 toggle(link.parent().parent().parent(), false);
170 } else if (link.parent().parent().hasClass('toggle-list')) {
171 toggle(link.parent().parent(), false);
172 }
173}
174
175function resizeHeight() {
176 var windowHeight = ($(window).height() - HEADER_HEIGHT);
177 var swapperHeight = windowHeight - 13;
178 $("#swapper").css({height:swapperHeight + "px"});
179 sidenav.css({height:windowHeight + "px"});
180 content.css({height:windowHeight + "px"});
181 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
182 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
183 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
184 devdocNav.css({height:sidenav.css("height")});
185 $("#nav-tree").css({height:swapperHeight + "px"});
Scott Main1043da92009-05-20 17:16:35 -0700186
187 var section = location.pathname.substring(1,location.pathname.indexOf("/",1));
188 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800189}
190
191function resizeWidth() {
192 var windowWidth = $(window).width() + "px";
193 if (sidenav.length) {
194 var sidenavWidth = sidenav.css("width");
195 } else {
196 var sidenavWidth = 0;
197 }
198 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
199 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
200 resizePackagesNav.css({width:sidenavWidth});
201 classesNav.css({width:sidenavWidth});
202 $("#packages-nav").css({width:sidenavWidth});
Scott Main1043da92009-05-20 17:16:35 -0700203
204 var section = location.pathname.substring(1,location.pathname.indexOf("/",1));
205 writeCookie("width", sidenavWidth, section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800206}
207
208function resizeAll() {
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700209 if (!isMobile) {
210 resizeHeight();
211 if ($(".side-nav-resizable").length) {
212 resizeWidth();
213 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800214 }
215}
216
217function loadLast(cookiePath) {
218 var location = window.location.href;
219 if (location.indexOf("/"+cookiePath+"/") != -1) {
220 return true;
221 }
Scott Main1043da92009-05-20 17:16:35 -0700222 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800223 if (lastPage) {
224 window.location = lastPage;
225 return false;
226 }
227 return true;
228}
229
230$(window).unload(function(){
Scott Main1043da92009-05-20 17:16:35 -0700231 var path = location.pathname;
232 if (path.indexOf("/reference/") != -1) {
233 writeCookie("lastpage", path, "reference", null);
234 } else if (path.indexOf("/guide/") != -1) {
235 writeCookie("lastpage", path, "guide", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800236 }
237});
238
239
240
241function toggle(obj, slide) {
242 var ul = $("ul", obj);
243 var li = ul.parent();
244 if (li.hasClass("closed")) {
245 if (slide) {
246 ul.slideDown("fast");
247 } else {
248 ul.show();
249 }
250 li.removeClass("closed");
251 li.addClass("open");
252 $(".toggle-img", li).attr("title", "hide pages");
253 } else {
254 ul.slideUp("fast");
255 li.removeClass("open");
256 li.addClass("closed");
257 $(".toggle-img", li).attr("title", "show pages");
258 }
259}
260
261
262
263function buildToggleLists() {
264 $(".toggle-list").each(
265 function(i) {
266 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
267 $(this).addClass("closed");
268 });
269}
270
271function getNavPref() {
Scott Main1043da92009-05-20 17:16:35 -0700272 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800273 if (v != NAV_PREF_TREE) {
274 v = NAV_PREF_PANELS;
275 }
276 return v;
277}
278
279function chooseDefaultNav() {
280 nav_pref = getNavPref();
281 if (nav_pref == NAV_PREF_TREE) {
282 $("#nav-panels").toggle();
283 $("#panel-link").toggle();
284 $("#nav-tree").toggle();
285 $("#tree-link").toggle();
286 }
287}
288
289function swapNav() {
290 if (nav_pref == NAV_PREF_TREE) {
291 nav_pref = NAV_PREF_PANELS;
292 } else {
293 nav_pref = NAV_PREF_TREE;
Scott Main5b53cd72009-06-04 11:10:17 -0700294 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800295 }
296 var date = new Date();
297 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Scott Main1043da92009-05-20 17:16:35 -0700298 writeCookie("nav", nav_pref, null, date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800299
300 $("#nav-panels").toggle();
301 $("#panel-link").toggle();
302 $("#nav-tree").toggle();
303 $("#tree-link").toggle();
304
305 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
306 else {
307 scrollIntoView("packages-nav");
308 scrollIntoView("classes-nav");
309 }
310}
311
312function scrollIntoView(nav) {
313 var navObj = $("#"+nav);
314 if (navObj.is(':visible')) {
315 var selected = $(".selected", navObj);
316 if (selected.length == 0) return;
317 if (selected.is("div")) selected = selected.parent();
318
319 var scrolling = document.getElementById(nav);
320 var navHeight = navObj.height();
321 var offsetTop = selected.position().top;
322 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
323 if(offsetTop > navHeight - 92) {
324 scrolling.scrollTop = offsetTop - navHeight + 92;
325 }
326 }
327}
328
329function toggleAllInherited(linkObj, expand) {
330 var a = $(linkObj);
331 var table = $(a.parent().parent().parent());
332 var expandos = $(".jd-expando-trigger", table);
333 if ( (expand == null && a.text() == "[Expand]") || expand ) {
334 expandos.each(function(i) {
335 toggleInherited(this, true);
336 });
337 a.text("[Collapse]");
338 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
339 expandos.each(function(i) {
340 toggleInherited(this, false);
341 });
342 a.text("[Expand]");
343 }
344 return false;
345}
346
347function toggleAllSummaryInherited(linkObj) {
348 var a = $(linkObj);
349 var content = $(a.parent().parent().parent());
350 var toggles = $(".toggle-all", content);
351 if (a.text() == "[Expand All]") {
352 toggles.each(function(i) {
353 toggleAllInherited(this, true);
354 });
355 a.text("[Collapse All]");
356 } else {
357 toggles.each(function(i) {
358 toggleAllInherited(this, false);
359 });
360 a.text("[Expand All]");
361 }
362 return false;
363}
Scott Main1043da92009-05-20 17:16:35 -0700364
365
366function changeTabLang(lang) {
367 var nodes = $("#header-tabs").find("."+lang);
368 for (i=0; i < nodes.length; i++) { // for each node in this language
369 var node = $(nodes[i]);
370 node.siblings().css("display","none"); // hide all siblings
371 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
372 node.css("display","inline");
373 } else { //otherwise, show English instead
374 node.css("display","none");
375 node.siblings().filter(".en").css("display","inline");
376 }
377 }
378}
379
380function changeNavLang(lang) {
381 var nodes = $("#side-nav").find("."+lang);
382 for (i=0; i < nodes.length; i++) { // for each node in this language
383 var node = $(nodes[i]);
384 node.siblings().css("display","none"); // hide all siblings
385 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
386 node.css("display","inline");
387 } else { // otherwise, show English instead
388 node.css("display","none");
389 node.siblings().filter(".en").css("display","inline");
390 }
391 }
392}
393
394function changeDocLang(lang) {
395 changeTabLang(lang);
396 changeNavLang(lang);
397}
398
399function changeLangPref(lang) {
400 var date = new Date();
401 date.setTime(date.getTime()+(50*365*24*60*60*1000)); // keep this for 50 years
402 writeCookie("pref_lang", lang, null, date);
403
404 changeDocLang(lang);
405}
406
407function loadLangPref() {
408 var lang = readCookie("pref_lang");
409 if (lang != 0) {
410 $("#language").find("option[value='"+lang+"']").attr("selected",true);
411 }
412}
413
414function getLangPref() {
415 return $("#language").find(":selected").attr("value");
416}
417