blob: a84d5a639921a7cc958961750ca74bcb7d6139c9 [file] [log] [blame]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001var resizePackagesNav;
2var classesNav;
3var devdocNav;
4var sidenav;
5var content;
The Android Open Source Project475fa122009-02-10 15:43:57 -08006var HEADER_HEIGHT = 117;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08007var cookie_style = 'android_developer';
8var NAV_PREF_TREE = "tree";
9var NAV_PREF_PANELS = "panels";
10var nav_pref;
11var toRoot;
12
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070013
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 Projectb6c1cf62008-10-21 07:00:00 -070026window.onresize = resizeAll;
27
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080028function setToRoot(root) {
29 toRoot = root;
The Android Open Source Project475fa122009-02-10 15:43:57 -080030 // note: toRoot also used by carousel.js
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080031}
32
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070033function restoreWidth(navWidth) {
34 var windowWidth = $(window).width() + "px";
The Android Open Source Project475fa122009-02-10 15:43:57 -080035 content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar
36 width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070037 sidenav.css({width:navWidth});
38 resizePackagesNav.css({width:navWidth});
39 classesNav.css({width:navWidth});
40 $("#packages-nav").css({width:navWidth});
41}
42
43function restoreHeight(packageHeight) {
44 var windowHeight = ($(window).height() - HEADER_HEIGHT);
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080045 var swapperHeight = windowHeight - 13;
46 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070047 sidenav.css({height:windowHeight + "px"});
48 content.css({height:windowHeight + "px"});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080049 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
50 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070051 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
52 devdocNav.css({height:sidenav.css("height")});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080053 $("#nav-tree").css({height:swapperHeight + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070054}
55
56function getCookie(cookie) {
57 var myCookie = cookie_style+"_"+cookie+"=";
58 if (document.cookie) {
59 var index = document.cookie.indexOf(myCookie);
60 if (index != -1) {
61 var valStart = index + myCookie.length;
62 var valEnd = document.cookie.indexOf(";", valStart);
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080063 if (valEnd == -1) {
64 valEnd = document.cookie.length;
65 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070066 var val = document.cookie.substring(valStart, valEnd);
67 return val;
68 }
69 }
70 return 0;
71}
72
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080073function writeCookie(cookie, val, path, expiration) {
The Android Open Source Project475fa122009-02-10 15:43:57 -080074 if (!val) return;
75 var date = new Date();
76 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
77 expiration = expiration ? expiration : date.toGMTString();
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080078 if (location.href.indexOf("/reference/") != -1) {
The Android Open Source Project475fa122009-02-10 15:43:57 -080079 document.cookie = cookie_style+'_reference_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080080 } else if (location.href.indexOf("/guide/") != -1) {
The Android Open Source Project475fa122009-02-10 15:43:57 -080081 document.cookie = cookie_style+'_guide_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070082 }
83}
84
The Android Open Source Project475fa122009-02-10 15:43:57 -080085function init() {
86 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } });
87 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
88
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070089 $("#side-nav").css({position:"absolute",left:0});
90 content = $("#doc-content");
91 resizePackagesNav = $("#resize-packages-nav");
92 classesNav = $("#classes-nav");
93 sidenav = $("#side-nav");
94 devdocNav = $("#devdoc-nav");
95
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080096 if (location.href.indexOf("/reference/") != -1) {
97 var cookiePath = "reference_";
98 } else if (location.href.indexOf("/guide/") != -1) {
99 var cookiePath = "guide_";
100 }
101 var cookieWidth = getCookie(cookiePath+'width');
102 var cookieHeight = getCookie(cookiePath+'height');
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700103 if (cookieWidth) {
104 restoreWidth(cookieWidth);
The Android Open Source Projecta9b22492009-03-02 22:54:15 -0800105 } else if ($(".side-nav-resizable").length) {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700106 resizeWidth();
107 }
108 if (cookieHeight) {
109 restoreHeight(cookieHeight);
110 } else {
111 resizeHeight();
112 }
113
The Android Open Source Project475fa122009-02-10 15:43:57 -0800114 if (devdocNav.length) { // only dev guide and sdk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115 highlightNav(location.href);
116 }
117}
118
119function highlightNav(fullPageName) {
120 var lastSlashPos = fullPageName.lastIndexOf("/");
The Android Open Source Project475fa122009-02-10 15:43:57 -0800121 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
122 fullPageName.indexOf("/guide/") :
123 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800124 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700125 fullPageName = fullPageName + "index.html";
126 }
127 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800128 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
129 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
The Android Open Source Projecta9b22492009-03-02 22:54:15 -0800130 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) {
131// 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)
The Android Open Source Project475fa122009-02-10 15:43:57 -0800132 lastBackstep = pathPageName.lastIndexOf("/");
133 while (link.length == 0) {
134 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
135 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
136 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
137 if (lastBackstep == 0) break;
138 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700139 }
140 link.parent().addClass('selected');
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800141 if (link.parent().parent().is(':hidden')) {
142 toggle(link.parent().parent().parent(), false);
143 } else if (link.parent().parent().hasClass('toggle-list')) {
144 toggle(link.parent().parent(), false);
145 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700146}
147
148function resizeHeight() {
149 var windowHeight = ($(window).height() - HEADER_HEIGHT);
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800150 var swapperHeight = windowHeight - 13;
151 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700152 sidenav.css({height:windowHeight + "px"});
153 content.css({height:windowHeight + "px"});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800154 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
155 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700156 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
157 devdocNav.css({height:sidenav.css("height")});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800158 $("#nav-tree").css({height:swapperHeight + "px"});
The Android Open Source Project475fa122009-02-10 15:43:57 -0800159 writeCookie("height", resizePackagesNav.css("height"), "", null);
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700160}
161
162function resizeWidth() {
163 var windowWidth = $(window).width() + "px";
164 if (sidenav.length) {
165 var sidenavWidth = sidenav.css("width");
166 } else {
167 var sidenavWidth = 0;
168 }
The Android Open Source Project475fa122009-02-10 15:43:57 -0800169 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
170 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700171 resizePackagesNav.css({width:sidenavWidth});
172 classesNav.css({width:sidenavWidth});
173 $("#packages-nav").css({width:sidenavWidth});
The Android Open Source Project475fa122009-02-10 15:43:57 -0800174 writeCookie("width", sidenavWidth, "", null);
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700175}
176
177function resizeAll() {
178 resizeHeight();
The Android Open Source Projecta9b22492009-03-02 22:54:15 -0800179 if ($(".side-nav-resizable").length) {
180 resizeWidth();
181 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700182}
183
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800184function loadLast(cookiePath) {
The Android Open Source Project475fa122009-02-10 15:43:57 -0800185 var location = window.location.href;
186 if (location.indexOf("/"+cookiePath+"/") != -1) {
187 return true;
188 }
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800189 var lastPage = getCookie(cookiePath + "_lastpage");
190 if (lastPage) {
191 window.location = lastPage;
192 return false;
193 }
194 return true;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195}
196
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800197$(window).unload(function(){
198 var href = location.href;
199 if (href.indexOf("/reference/") != -1) {
200 writeCookie("lastpage", href, "", null);
201 } else if (href.indexOf("/guide/") != -1) {
202 writeCookie("lastpage", href, "", null);
203 }
204});
205
206
207
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800208function toggle(obj, slide) {
209 var ul = $("ul", obj);
210 var li = ul.parent();
211 if (li.hasClass("closed")) {
212 if (slide) {
213 ul.slideDown("fast");
214 } else {
215 ul.show();
216 }
217 li.removeClass("closed");
218 li.addClass("open");
219 $(".toggle-img", li).attr("title", "hide pages");
220 } else {
221 ul.slideUp("fast");
222 li.removeClass("open");
223 li.addClass("closed");
224 $(".toggle-img", li).attr("title", "show pages");
225 }
226}
227
228
229
230function buildToggleLists() {
231 $(".toggle-list").each(
232 function(i) {
233 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
234 $(this).addClass("closed");
235 });
236}
237
238function getNavPref() {
239 var v = getCookie('reference_nav');
240 if (v != NAV_PREF_TREE) {
241 v = NAV_PREF_PANELS;
242 }
243 return v;
244}
245
246function chooseDefaultNav() {
247 nav_pref = getNavPref();
248 if (nav_pref == NAV_PREF_TREE) {
249 $("#nav-panels").toggle();
250 $("#panel-link").toggle();
251 $("#nav-tree").toggle();
252 $("#tree-link").toggle();
253 }
254}
255
256function swapNav() {
257 if (nav_pref == NAV_PREF_TREE) {
258 nav_pref = NAV_PREF_PANELS;
259 } else {
260 nav_pref = NAV_PREF_TREE;
261 init_navtree("nav-tree", toRoot, NAVTREE_DATA);
262 }
263 var date = new Date();
264 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
The Android Open Source Project475fa122009-02-10 15:43:57 -0800265 writeCookie("nav", nav_pref, "", date.toGMTString());
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800266
267 $("#nav-panels").toggle();
268 $("#panel-link").toggle();
269 $("#nav-tree").toggle();
270 $("#tree-link").toggle();
271
272 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
273 else {
274 scrollIntoView("packages-nav");
275 scrollIntoView("classes-nav");
276 }
277}
278
279function scrollIntoView(nav) {
280 var navObj = $("#"+nav);
281 if (navObj.is(':visible')) {
282 var selected = $(".selected", navObj);
283 if (selected.length == 0) return;
The Android Open Source Project475fa122009-02-10 15:43:57 -0800284 if (selected.is("div")) selected = selected.parent();
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800285
286 var scrolling = document.getElementById(nav);
287 var navHeight = navObj.height();
The Android Open Source Project475fa122009-02-10 15:43:57 -0800288 var offsetTop = selected.position().top;
289 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
290 if(offsetTop > navHeight - 92) {
291 scrolling.scrollTop = offsetTop - navHeight + 92;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800292 }
293 }
294}
295
The Android Open Source Project475fa122009-02-10 15:43:57 -0800296function toggleAllInherited(linkObj, expand) {
297 var a = $(linkObj);
298 var table = $(a.parent().parent().parent());
299 var expandos = $(".jd-expando-trigger", table);
300 if ( (expand == null && a.text() == "[Expand]") || expand ) {
301 expandos.each(function(i) {
302 toggleInherited(this, true);
303 });
304 a.text("[Collapse]");
305 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
306 expandos.each(function(i) {
307 toggleInherited(this, false);
308 });
309 a.text("[Expand]");
310 }
311 return false;
312}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800313
The Android Open Source Project475fa122009-02-10 15:43:57 -0800314function toggleAllSummaryInherited(linkObj) {
315 var a = $(linkObj);
316 var content = $(a.parent().parent().parent());
317 var toggles = $(".toggle-all", content);
318 if (a.text() == "[Expand All]") {
319 toggles.each(function(i) {
320 toggleAllInherited(this, true);
321 });
322 a.text("[Collapse All]");
323 } else {
324 toggles.each(function(i) {
325 toggleAllInherited(this, false);
326 });
327 a.text("[Expand All]");
328 }
329 return false;
330}