blob: cd9c0b3619cdcbc0008ef3ab28e1af67b031239a [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);
105 } else {
106 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 Project475fa122009-02-10 15:43:57 -0800130 if ((link.length == 0) && (fullPageName.indexOf("/guide/") != -1)) { // 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)
131 lastBackstep = pathPageName.lastIndexOf("/");
132 while (link.length == 0) {
133 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
134 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
135 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
136 if (lastBackstep == 0) break;
137 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138 }
139 link.parent().addClass('selected');
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140 if (link.parent().parent().is(':hidden')) {
141 toggle(link.parent().parent().parent(), false);
142 } else if (link.parent().parent().hasClass('toggle-list')) {
143 toggle(link.parent().parent(), false);
144 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700145}
146
147function resizeHeight() {
148 var windowHeight = ($(window).height() - HEADER_HEIGHT);
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800149 var swapperHeight = windowHeight - 13;
150 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700151 sidenav.css({height:windowHeight + "px"});
152 content.css({height:windowHeight + "px"});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800153 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
154 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700155 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
156 devdocNav.css({height:sidenav.css("height")});
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800157 $("#nav-tree").css({height:swapperHeight + "px"});
The Android Open Source Project475fa122009-02-10 15:43:57 -0800158 writeCookie("height", resizePackagesNav.css("height"), "", null);
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700159}
160
161function resizeWidth() {
162 var windowWidth = $(window).width() + "px";
163 if (sidenav.length) {
164 var sidenavWidth = sidenav.css("width");
165 } else {
166 var sidenavWidth = 0;
167 }
The Android Open Source Project475fa122009-02-10 15:43:57 -0800168 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
169 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700170 resizePackagesNav.css({width:sidenavWidth});
171 classesNav.css({width:sidenavWidth});
172 $("#packages-nav").css({width:sidenavWidth});
The Android Open Source Project475fa122009-02-10 15:43:57 -0800173 writeCookie("width", sidenavWidth, "", null);
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700174}
175
176function resizeAll() {
177 resizeHeight();
178 resizeWidth();
179}
180
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800181function loadLast(cookiePath) {
The Android Open Source Project475fa122009-02-10 15:43:57 -0800182 var location = window.location.href;
183 if (location.indexOf("/"+cookiePath+"/") != -1) {
184 return true;
185 }
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800186 var lastPage = getCookie(cookiePath + "_lastpage");
187 if (lastPage) {
188 window.location = lastPage;
189 return false;
190 }
191 return true;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700192}
193
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800194$(window).unload(function(){
195 var href = location.href;
196 if (href.indexOf("/reference/") != -1) {
197 writeCookie("lastpage", href, "", null);
198 } else if (href.indexOf("/guide/") != -1) {
199 writeCookie("lastpage", href, "", null);
200 }
201});
202
203
204
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800205function toggle(obj, slide) {
206 var ul = $("ul", obj);
207 var li = ul.parent();
208 if (li.hasClass("closed")) {
209 if (slide) {
210 ul.slideDown("fast");
211 } else {
212 ul.show();
213 }
214 li.removeClass("closed");
215 li.addClass("open");
216 $(".toggle-img", li).attr("title", "hide pages");
217 } else {
218 ul.slideUp("fast");
219 li.removeClass("open");
220 li.addClass("closed");
221 $(".toggle-img", li).attr("title", "show pages");
222 }
223}
224
225
226
227function buildToggleLists() {
228 $(".toggle-list").each(
229 function(i) {
230 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
231 $(this).addClass("closed");
232 });
233}
234
235function getNavPref() {
236 var v = getCookie('reference_nav');
237 if (v != NAV_PREF_TREE) {
238 v = NAV_PREF_PANELS;
239 }
240 return v;
241}
242
243function chooseDefaultNav() {
244 nav_pref = getNavPref();
245 if (nav_pref == NAV_PREF_TREE) {
246 $("#nav-panels").toggle();
247 $("#panel-link").toggle();
248 $("#nav-tree").toggle();
249 $("#tree-link").toggle();
250 }
251}
252
253function swapNav() {
254 if (nav_pref == NAV_PREF_TREE) {
255 nav_pref = NAV_PREF_PANELS;
256 } else {
257 nav_pref = NAV_PREF_TREE;
258 init_navtree("nav-tree", toRoot, NAVTREE_DATA);
259 }
260 var date = new Date();
261 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 -0800262 writeCookie("nav", nav_pref, "", date.toGMTString());
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800263
264 $("#nav-panels").toggle();
265 $("#panel-link").toggle();
266 $("#nav-tree").toggle();
267 $("#tree-link").toggle();
268
269 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
270 else {
271 scrollIntoView("packages-nav");
272 scrollIntoView("classes-nav");
273 }
274}
275
276function scrollIntoView(nav) {
277 var navObj = $("#"+nav);
278 if (navObj.is(':visible')) {
279 var selected = $(".selected", navObj);
280 if (selected.length == 0) return;
The Android Open Source Project475fa122009-02-10 15:43:57 -0800281 if (selected.is("div")) selected = selected.parent();
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800282
283 var scrolling = document.getElementById(nav);
284 var navHeight = navObj.height();
The Android Open Source Project475fa122009-02-10 15:43:57 -0800285 var offsetTop = selected.position().top;
286 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
287 if(offsetTop > navHeight - 92) {
288 scrolling.scrollTop = offsetTop - navHeight + 92;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800289 }
290 }
291}
292
The Android Open Source Project475fa122009-02-10 15:43:57 -0800293function toggleAllInherited(linkObj, expand) {
294 var a = $(linkObj);
295 var table = $(a.parent().parent().parent());
296 var expandos = $(".jd-expando-trigger", table);
297 if ( (expand == null && a.text() == "[Expand]") || expand ) {
298 expandos.each(function(i) {
299 toggleInherited(this, true);
300 });
301 a.text("[Collapse]");
302 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
303 expandos.each(function(i) {
304 toggleInherited(this, false);
305 });
306 a.text("[Expand]");
307 }
308 return false;
309}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800310
The Android Open Source Project475fa122009-02-10 15:43:57 -0800311function toggleAllSummaryInherited(linkObj) {
312 var a = $(linkObj);
313 var content = $(a.parent().parent().parent());
314 var toggles = $(".toggle-all", content);
315 if (a.text() == "[Expand All]") {
316 toggles.each(function(i) {
317 toggleAllInherited(this, true);
318 });
319 a.text("[Collapse All]");
320 } else {
321 toggles.each(function(i) {
322 toggleAllInherited(this, false);
323 });
324 a.text("[Expand All]");
325 }
326 return false;
327}