blob: a84d5a639921a7cc958961750ca74bcb7d6139c9 [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;
7var cookie_style = 'android_developer';
8var NAV_PREF_TREE = "tree";
9var NAV_PREF_PANELS = "panels";
10var nav_pref;
11var toRoot;
12
13
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
26window.onresize = resizeAll;
27
28function setToRoot(root) {
29 toRoot = root;
30 // note: toRoot also used by carousel.js
31}
32
33function restoreWidth(navWidth) {
34 var windowWidth = $(window).width() + "px";
35 content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar
36 width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"});
37 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);
45 var swapperHeight = windowHeight - 13;
46 $("#swapper").css({height:swapperHeight + "px"});
47 sidenav.css({height:windowHeight + "px"});
48 content.css({height:windowHeight + "px"});
49 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
50 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
51 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
52 devdocNav.css({height:sidenav.css("height")});
53 $("#nav-tree").css({height:swapperHeight + "px"});
54}
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);
63 if (valEnd == -1) {
64 valEnd = document.cookie.length;
65 }
66 var val = document.cookie.substring(valStart, valEnd);
67 return val;
68 }
69 }
70 return 0;
71}
72
73function writeCookie(cookie, val, path, expiration) {
74 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();
78 if (location.href.indexOf("/reference/") != -1) {
79 document.cookie = cookie_style+'_reference_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path;
80 } else if (location.href.indexOf("/guide/") != -1) {
81 document.cookie = cookie_style+'_guide_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path;
82 }
83}
84
85function 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
89 $("#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
96 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');
103 if (cookieWidth) {
104 restoreWidth(cookieWidth);
105 } else if ($(".side-nav-resizable").length) {
106 resizeWidth();
107 }
108 if (cookieHeight) {
109 restoreHeight(cookieHeight);
110 } else {
111 resizeHeight();
112 }
113
114 if (devdocNav.length) { // only dev guide and sdk
115 highlightNav(location.href);
116 }
117}
118
119function highlightNav(fullPageName) {
120 var lastSlashPos = fullPageName.lastIndexOf("/");
121 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ?
122 fullPageName.indexOf("/guide/") :
123 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk
124 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
125 fullPageName = fullPageName + "index.html";
126 }
127 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
128 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5);
129 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
130 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)
132 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 }
139 }
140 link.parent().addClass('selected');
141 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 }
146}
147
148function resizeHeight() {
149 var windowHeight = ($(window).height() - HEADER_HEIGHT);
150 var swapperHeight = windowHeight - 13;
151 $("#swapper").css({height:swapperHeight + "px"});
152 sidenav.css({height:windowHeight + "px"});
153 content.css({height:windowHeight + "px"});
154 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
155 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
156 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
157 devdocNav.css({height:sidenav.css("height")});
158 $("#nav-tree").css({height:swapperHeight + "px"});
159 writeCookie("height", resizePackagesNav.css("height"), "", null);
160}
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 }
169 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar
170 width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"});
171 resizePackagesNav.css({width:sidenavWidth});
172 classesNav.css({width:sidenavWidth});
173 $("#packages-nav").css({width:sidenavWidth});
174 writeCookie("width", sidenavWidth, "", null);
175}
176
177function resizeAll() {
178 resizeHeight();
179 if ($(".side-nav-resizable").length) {
180 resizeWidth();
181 }
182}
183
184function loadLast(cookiePath) {
185 var location = window.location.href;
186 if (location.indexOf("/"+cookiePath+"/") != -1) {
187 return true;
188 }
189 var lastPage = getCookie(cookiePath + "_lastpage");
190 if (lastPage) {
191 window.location = lastPage;
192 return false;
193 }
194 return true;
195}
196
197$(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
208function 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
265 writeCookie("nav", nav_pref, "", date.toGMTString());
266
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;
284 if (selected.is("div")) selected = selected.parent();
285
286 var scrolling = document.getElementById(nav);
287 var navHeight = navObj.height();
288 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;
292 }
293 }
294}
295
296function 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}
313
314function 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}