blob: 153f5e0fa67ec29bad33a9343cae384a88059190 [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
Scott Main3f7d1df2009-11-02 15:31:04 -080013var isIE6 = false; // true if IE6
The Android Open Source Project88b60792009-03-03 19:28:42 -080014
Scott Mainf8af8872009-12-02 20:59:40 -080015// TODO: use $(document).ready instead
The Android Open Source Project88b60792009-03-03 19:28:42 -080016function addLoadEvent(newfun) {
17 var current = window.onload;
18 if (typeof window.onload != 'function') {
19 window.onload = newfun;
20 } else {
21 window.onload = function() {
22 current();
23 newfun();
24 }
25 }
26}
27
Scott Maind8115ff2009-11-03 18:54:26 -080028var agent = navigator['userAgent'].toLowerCase();
Scott Main3f7d1df2009-11-02 15:31:04 -080029// If a mobile phone, set flag and do mobile setup
Roman Nurik78eff9f2010-03-01 18:35:13 -080030if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod
Scott Maind8115ff2009-11-03 18:54:26 -080031 (agent.indexOf("blackberry") != -1) ||
32 (agent.indexOf("webos") != -1) ||
Roman Nurik78eff9f2010-03-01 18:35:13 -080033 (agent.indexOf("mini") != -1)) { // opera mini browsers
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070034 isMobile = true;
35 addLoadEvent(mobileSetup);
Scott Main3f7d1df2009-11-02 15:31:04 -080036// If not a mobile browser, set the onresize event for IE6, and others
Scott Maind8115ff2009-11-03 18:54:26 -080037} else if (agent.indexOf("msie 6") != -1) {
Scott Main3f7d1df2009-11-02 15:31:04 -080038 isIE6 = true;
39 addLoadEvent(function() {
40 window.onresize = resizeAll;
41 });
42} else {
43 addLoadEvent(function() {
44 window.onresize = resizeHeight;
45 });
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070046}
47
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -070048function mobileSetup() {
49 $("body").css({'overflow':'auto'});
50 $("html").css({'overflow':'auto'});
51 $("#body-content").css({'position':'relative', 'top':'0'});
52 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'});
53 $("#side-nav").css({'padding':'0'});
54 $("#nav-tree").css({'overflow-y': 'auto'});
55}
56
Scott Main25fda192009-08-04 11:26:30 -070057/* loads the lists.js file to the page.
58Loading this in the head was slowing page load time */
59addLoadEvent( function() {
60 var lists = document.createElement("script");
61 lists.setAttribute("type","text/javascript");
62 lists.setAttribute("src", toRoot+"reference/lists.js");
Scott Main1da82382009-09-17 10:54:05 -070063 document.getElementsByTagName("head")[0].appendChild(lists);
Scott Main25fda192009-08-04 11:26:30 -070064} );
65
Scott Mainf8af8872009-12-02 20:59:40 -080066addLoadEvent( function() {
Scott Main945ed362010-01-15 17:30:38 -080067 $("pre:not(.no-pretty-print)").addClass("prettyprint");
Scott Mainf8af8872009-12-02 20:59:40 -080068 prettyPrint();
69} );
70
The Android Open Source Project88b60792009-03-03 19:28:42 -080071function setToRoot(root) {
72 toRoot = root;
73 // note: toRoot also used by carousel.js
74}
75
76function restoreWidth(navWidth) {
77 var windowWidth = $(window).width() + "px";
Scott Main3f7d1df2009-11-02 15:31:04 -080078 content.css({marginLeft:parseInt(navWidth) + 6 + "px"}); //account for 6px-wide handle-bar
79
80 if (isIE6) {
81 content.css({width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); // necessary in order for scrollbars to be visible
82 }
83
The Android Open Source Project88b60792009-03-03 19:28:42 -080084 sidenav.css({width:navWidth});
85 resizePackagesNav.css({width:navWidth});
86 classesNav.css({width:navWidth});
87 $("#packages-nav").css({width:navWidth});
88}
89
90function restoreHeight(packageHeight) {
91 var windowHeight = ($(window).height() - HEADER_HEIGHT);
92 var swapperHeight = windowHeight - 13;
93 $("#swapper").css({height:swapperHeight + "px"});
94 sidenav.css({height:windowHeight + "px"});
95 content.css({height:windowHeight + "px"});
96 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight});
97 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"});
98 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle
99 devdocNav.css({height:sidenav.css("height")});
100 $("#nav-tree").css({height:swapperHeight + "px"});
101}
102
Dirk Dougherty4405a232009-07-07 17:43:27 -0700103function readCookie(cookie) {
104 var myCookie = cookie_namespace+"_"+cookie+"=";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800105 if (document.cookie) {
106 var index = document.cookie.indexOf(myCookie);
107 if (index != -1) {
108 var valStart = index + myCookie.length;
109 var valEnd = document.cookie.indexOf(";", valStart);
110 if (valEnd == -1) {
111 valEnd = document.cookie.length;
112 }
113 var val = document.cookie.substring(valStart, valEnd);
114 return val;
115 }
116 }
117 return 0;
118}
119
Dirk Dougherty4405a232009-07-07 17:43:27 -0700120function writeCookie(cookie, val, section, expiration) {
Scott Main9b5fdb92009-10-27 15:09:15 -0700121 if (val==undefined) return;
Dirk Dougherty4405a232009-07-07 17:43:27 -0700122 section = section == null ? "_" : "_"+section+"_";
123 if (expiration == null) {
124 var date = new Date();
125 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
126 expiration = date.toGMTString();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800127 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800128 document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/";
Roman Nurik78eff9f2010-03-01 18:35:13 -0800129}
The Android Open Source Project88b60792009-03-03 19:28:42 -0800130
131function init() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800132 $("#side-nav").css({position:"absolute",left:0});
133 content = $("#doc-content");
134 resizePackagesNav = $("#resize-packages-nav");
135 classesNav = $("#classes-nav");
136 sidenav = $("#side-nav");
137 devdocNav = $("#devdoc-nav");
138
Scott Maina39d3bb2010-01-06 14:18:02 -0800139 var cookiePath = "";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800140 if (location.href.indexOf("/reference/") != -1) {
Scott Maina39d3bb2010-01-06 14:18:02 -0800141 cookiePath = "reference_";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800142 } else if (location.href.indexOf("/guide/") != -1) {
Scott Maina39d3bb2010-01-06 14:18:02 -0800143 cookiePath = "guide_";
Scott Mainfd85d422011-01-07 12:08:09 -0800144 } else if (location.href.indexOf("/sdk/") != -1) {
145 cookiePath = "sdk_";
Scott Main629b0f02011-07-08 16:17:02 -0700146 } else if ((location.href.indexOf("/resources/") != -1) ||
147 (location.href.indexOf("/training/") != -1)) {
Scott Maina39d3bb2010-01-06 14:18:02 -0800148 cookiePath = "resources_";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800149 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700150
151 if (!isMobile) {
Scott Main3f7d1df2009-11-02 15:31:04 -0800152 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } });
Scott Mainfd85d422011-01-07 12:08:09 -0800153 $("#side-nav").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700154 var cookieWidth = readCookie(cookiePath+'width');
155 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700156 if (cookieWidth) {
157 restoreWidth(cookieWidth);
Scott Mainfd85d422011-01-07 12:08:09 -0800158 } else if ($("#side-nav").length) {
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700159 resizeWidth();
160 }
161 if (cookieHeight) {
162 restoreHeight(cookieHeight);
163 } else {
164 resizeHeight();
165 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800166 }
167
Roman Nurik78eff9f2010-03-01 18:35:13 -0800168 if (devdocNav.length) { // only dev guide, resources, and sdk
169 tryPopulateResourcesNav();
170 highlightNav(location.href);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800171 }
172}
173
Roman Nurik78eff9f2010-03-01 18:35:13 -0800174function tryPopulateResourcesNav() {
175 var sampleList = $('#devdoc-nav-sample-list');
176 var articleList = $('#devdoc-nav-article-list');
177 var tutorialList = $('#devdoc-nav-tutorial-list');
178 var topicList = $('#devdoc-nav-topic-list');
179
180 if (!topicList.length || !ANDROID_TAGS || !ANDROID_RESOURCES)
181 return;
182
183 var topics = [];
184 for (var topic in ANDROID_TAGS['topic']) {
185 topics.push({name:topic,title:ANDROID_TAGS['topic'][topic]});
186 }
187 topics.sort(function(x,y){ return (x.title < y.title) ? -1 : 1; });
188 for (var i = 0; i < topics.length; i++) {
189 topicList.append(
190 $('<li>').append(
191 $('<a>')
192 .attr('href', toRoot + "resources/browser.html?tag=" + topics[i].name)
193 .append($('<span>')
194 .addClass('en')
195 .html(topics[i].title)
196 )
197 )
198 );
199 }
200
201 var _renderResourceList = function(tag, listNode) {
202 var resources = [];
203 var tags;
204 var resource;
205 var i, j;
206 for (i = 0; i < ANDROID_RESOURCES.length; i++) {
207 resource = ANDROID_RESOURCES[i];
208 tags = resource.tags || [];
209 var hasTag = false;
210 for (j = 0; j < tags.length; j++)
211 if (tags[j] == tag) {
212 hasTag = true;
213 break;
214 }
215 if (!hasTag)
216 continue;
217 resources.push(resource);
218 }
219 //resources.sort(function(x,y){ return (x.title.en < y.title.en) ? -1 : 1; });
220 for (i = 0; i < resources.length; i++) {
221 resource = resources[i];
222 var listItemNode = $('<li>').append(
223 $('<a>')
224 .attr('href', toRoot + "resources/" + resource.path)
225 .append($('<span>')
226 .addClass('en')
227 .html(resource.title.en)
228 )
229 );
230 tags = resource.tags || [];
231 for (j = 0; j < tags.length; j++) {
232 if (tags[j] == 'new') {
233 listItemNode.get(0).innerHTML += '&nbsp;<span class="new">new!</span>';
234 break;
235 }
236 }
237 listNode.append(listItemNode);
238 }
239 };
240
241 _renderResourceList('sample', sampleList);
242 _renderResourceList('article', articleList);
243 _renderResourceList('tutorial', tutorialList);
244}
245
The Android Open Source Project88b60792009-03-03 19:28:42 -0800246function highlightNav(fullPageName) {
247 var lastSlashPos = fullPageName.lastIndexOf("/");
Dirk Dougherty502c4982009-12-02 18:01:16 -0800248 var firstSlashPos;
249 if (fullPageName.indexOf("/guide/") != -1) {
Scott Mainfd85d422011-01-07 12:08:09 -0800250 firstSlashPos = fullPageName.indexOf("/guide/");
251 } else if (fullPageName.indexOf("/sdk/") != -1) {
252 firstSlashPos = fullPageName.indexOf("/sdk/");
253 } else if (fullPageName.indexOf("/resources/") != -1) {
254 firstSlashPos = fullPageName.indexOf("/resources/");
Scott Main629b0f02011-07-08 16:17:02 -0700255 } else if (fullPageName.indexOf("/training/") != -1) {
256 firstSlashPos = fullPageName.indexOf("/training/");
Scott Mainfd85d422011-01-07 12:08:09 -0800257 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800258 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
259 fullPageName = fullPageName + "index.html";
260 }
Scott Main76a62f22011-07-08 16:19:25 -0700261
262 // get the path and page name from the URL (such as 'guide/topics/graphics/index.html')
263 var htmlPos = fullPageName.indexOf(".html");
264 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); // +5 advances past ".html"
265 // find instances of the page name in the side nav
The Android Open Source Project88b60792009-03-03 19:28:42 -0800266 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Scott Main76a62f22011-07-08 16:19:25 -0700267 // if there's no match, then let's backstep through the directory until we find an index.html
268 // page that matches our ancestor directories (only for dev guide and resources)
269 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) ||
270 (fullPageName.indexOf("/resources/") != -1))) {
271 lastBackstep = pathPageName.lastIndexOf("/");
272 while (link.length == 0) {
273 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
274 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory +
275 1)+"index.html']");
276 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
277 if (lastBackstep == 0) break;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800278 }
279 }
Scott Mainee629d92010-02-05 14:30:08 -0800280
281 // add 'selected' to the <li> or <div> that wraps this <a>
The Android Open Source Project88b60792009-03-03 19:28:42 -0800282 link.parent().addClass('selected');
Scott Mainee629d92010-02-05 14:30:08 -0800283
284 // if we're in a toggleable root link (<li class=toggle-list><div><a>)
285 if (link.parent().parent().hasClass('toggle-list')) {
286 toggle(link.parent().parent(), false); // open our own list
287 // then also check if we're in a third-level nested list that's toggleable
288 if (link.parent().parent().parent().is(':hidden')) {
289 toggle(link.parent().parent().parent().parent(), false); // open the super parent list
290 }
291 }
292 // if we're in a normal nav link (<li><a>) and the parent <ul> is hidden
293 else if (link.parent().parent().is(':hidden')) {
294 toggle(link.parent().parent().parent(), false); // open the parent list
295 // then also check if the parent list is also nested in a hidden list
296 if (link.parent().parent().parent().parent().is(':hidden')) {
297 toggle(link.parent().parent().parent().parent().parent(), false); // open the super parent list
298 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800299 }
300}
301
Scott Main3f7d1df2009-11-02 15:31:04 -0800302/* Resize the height of the nav panels in the reference,
303 * and save the new size to a cookie */
304function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800305 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800306 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800307 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
308 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800309
310 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800311 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800312
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700313 var basePath = getBaseUri(location.pathname);
314 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700315 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800316}
317
Scott Main3f7d1df2009-11-02 15:31:04 -0800318/* Resize the height of the side-nav and doc-content divs,
319 * which creates the frame effect */
320function resizeHeight() {
Scott Main13b79b22010-01-12 13:37:00 -0800321 var docContent = $("#doc-content");
322
Scott Main3f7d1df2009-11-02 15:31:04 -0800323 // Get the window height and always resize the doc-content and side-nav divs
324 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main13b79b22010-01-12 13:37:00 -0800325 docContent.css({height:windowHeight + "px"});
326 $("#side-nav").css({height:windowHeight + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800327
328 var href = location.href;
329 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
330 if (href.indexOf("/reference/") != -1) {
331 var swapperHeight = windowHeight - 13;
332 $("#swapper").css({height:swapperHeight + "px"});
333 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
334 $("#nav-tree").css({height:swapperHeight + "px"});
335
Scott Mainfd85d422011-01-07 12:08:09 -0800336 // Also resize the "devdoc-nav" div
337 } else if ($("#devdoc-nav").length) {
Dirk Dougherty502c4982009-12-02 18:01:16 -0800338 $("#devdoc-nav").css({height:sidenav.css("height")});
Scott Main3f7d1df2009-11-02 15:31:04 -0800339 }
Scott Main13b79b22010-01-12 13:37:00 -0800340
341 // Hide the "Go to top" link if there's no vertical scroll
342 if ( parseInt($("#jd-content").css("height")) <= parseInt(docContent.css("height")) ) {
343 $("a[href='#top']").css({'display':'none'});
344 } else {
345 $("a[href='#top']").css({'display':'inline'});
346 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800347}
348
349/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
350 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800351function resizeWidth() {
352 var windowWidth = $(window).width() + "px";
Scott Mainfd85d422011-01-07 12:08:09 -0800353 var sidenav = $("#side-nav");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800354 if (sidenav.length) {
355 var sidenavWidth = sidenav.css("width");
356 } else {
357 var sidenavWidth = 0;
358 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800359 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
360
361 if (isIE6) {
362 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
363 }
364
The Android Open Source Project88b60792009-03-03 19:28:42 -0800365 resizePackagesNav.css({width:sidenavWidth});
366 classesNav.css({width:sidenavWidth});
367 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800368
Scott Mainfd85d422011-01-07 12:08:09 -0800369 if (sidenav.length) { // Must check if the nav exists because IE6 calls resizeWidth() from resizeAll() for all pages
Scott Mainb6da4802010-01-06 14:18:02 -0800370 var basePath = getBaseUri(location.pathname);
371 var section = basePath.substring(1,basePath.indexOf("/",1));
372 writeCookie("width", sidenavWidth, section, null);
373 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800374}
375
Scott Main3f7d1df2009-11-02 15:31:04 -0800376/* For IE6 only,
377 * because it can't properly perform auto width for "doc-content" div,
378 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800379function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800380 resizeHeight();
Scott Mainb6da4802010-01-06 14:18:02 -0800381 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800382}
383
Dirk Dougherty4405a232009-07-07 17:43:27 -0700384function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700385 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700386 if (intlUrl) {
387 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
388 base = base.substring(base.indexOf('/')+1, base.length);
389 //alert("intl, returning base url: /" + base);
390 return ("/" + base);
391 } else {
392 //alert("not intl, returning uri as found.");
393 return uri;
394 }
395}
396
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700397function requestAppendHL(uri) {
398//append "?hl=<lang> to an outgoing request (such as to blog)
399 var lang = getLangPref();
400 if (lang) {
401 var q = 'hl=' + lang;
402 uri += '?' + q;
403 window.location = uri;
404 return false;
405 } else {
406 return true;
407 }
408}
409
The Android Open Source Project88b60792009-03-03 19:28:42 -0800410function loadLast(cookiePath) {
411 var location = window.location.href;
412 if (location.indexOf("/"+cookiePath+"/") != -1) {
413 return true;
414 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700415 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800416 if (lastPage) {
417 window.location = lastPage;
418 return false;
419 }
420 return true;
421}
422
423$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700424 var path = getBaseUri(location.pathname);
425 if (path.indexOf("/reference/") != -1) {
426 writeCookie("lastpage", path, "reference", null);
427 } else if (path.indexOf("/guide/") != -1) {
428 writeCookie("lastpage", path, "guide", null);
Dirk Dougherty502c4982009-12-02 18:01:16 -0800429 } else if (path.indexOf("/resources/") != -1) {
430 writeCookie("lastpage", path, "resources", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800431 }
432});
433
The Android Open Source Project88b60792009-03-03 19:28:42 -0800434function toggle(obj, slide) {
Scott Mainee629d92010-02-05 14:30:08 -0800435 var ul = $("ul:first", obj);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800436 var li = ul.parent();
437 if (li.hasClass("closed")) {
438 if (slide) {
439 ul.slideDown("fast");
440 } else {
441 ul.show();
442 }
443 li.removeClass("closed");
444 li.addClass("open");
445 $(".toggle-img", li).attr("title", "hide pages");
446 } else {
447 ul.slideUp("fast");
448 li.removeClass("open");
449 li.addClass("closed");
450 $(".toggle-img", li).attr("title", "show pages");
451 }
452}
453
The Android Open Source Project88b60792009-03-03 19:28:42 -0800454function buildToggleLists() {
455 $(".toggle-list").each(
456 function(i) {
Scott Mainee629d92010-02-05 14:30:08 -0800457 $("div:first", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800458 $(this).addClass("closed");
459 });
460}
461
462function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700463 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800464 if (v != NAV_PREF_TREE) {
465 v = NAV_PREF_PANELS;
466 }
467 return v;
468}
469
470function chooseDefaultNav() {
471 nav_pref = getNavPref();
472 if (nav_pref == NAV_PREF_TREE) {
473 $("#nav-panels").toggle();
474 $("#panel-link").toggle();
475 $("#nav-tree").toggle();
476 $("#tree-link").toggle();
477 }
478}
479
480function swapNav() {
481 if (nav_pref == NAV_PREF_TREE) {
482 nav_pref = NAV_PREF_PANELS;
483 } else {
484 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700485 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800486 }
487 var date = new Date();
488 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700489 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800490
491 $("#nav-panels").toggle();
492 $("#panel-link").toggle();
493 $("#nav-tree").toggle();
494 $("#tree-link").toggle();
495
496 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
497 else {
498 scrollIntoView("packages-nav");
499 scrollIntoView("classes-nav");
500 }
501}
502
503function scrollIntoView(nav) {
504 var navObj = $("#"+nav);
505 if (navObj.is(':visible')) {
506 var selected = $(".selected", navObj);
507 if (selected.length == 0) return;
Scott Main3ab162e2011-03-04 12:23:00 -0800508 if (selected.is("div")) selected = selected.parent(); // when the selected item is a parent
The Android Open Source Project88b60792009-03-03 19:28:42 -0800509
510 var scrolling = document.getElementById(nav);
511 var navHeight = navObj.height();
512 var offsetTop = selected.position().top;
Scott Main3ab162e2011-03-04 12:23:00 -0800513
514 // handle nested items
515 if (selected.parent().parent().is(".toggle-list")) {
516 selected = selected.parent().parent();
517 // handle second level nested items
518 if (selected.parent().parent().is(".toggle-list")) {
519 selected = selected.parent().parent();
520 }
521 offsetTop += selected.position().top;
522 }
523
524 // 180px from the bottom of the list is the threshold
525 if(offsetTop > navHeight - 180) {
526 scrolling.scrollTop = offsetTop - navHeight + 180;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800527 }
528 }
529}
530
Dirk Dougherty4405a232009-07-07 17:43:27 -0700531function changeTabLang(lang) {
532 var nodes = $("#header-tabs").find("."+lang);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800533 for (i=0; i < nodes.length; i++) { // for each node in this language
Dirk Dougherty4405a232009-07-07 17:43:27 -0700534 var node = $(nodes[i]);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800535 node.siblings().css("display","none"); // hide all siblings
536 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
Dirk Dougherty4405a232009-07-07 17:43:27 -0700537 node.css("display","inline");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800538 } else { //otherwise, show English instead
Dirk Dougherty4405a232009-07-07 17:43:27 -0700539 node.css("display","none");
540 node.siblings().filter(".en").css("display","inline");
541 }
542 }
543}
544
545function changeNavLang(lang) {
546 var nodes = $("#side-nav").find("."+lang);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800547 for (i=0; i < nodes.length; i++) { // for each node in this language
Dirk Dougherty4405a232009-07-07 17:43:27 -0700548 var node = $(nodes[i]);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800549 node.siblings().css("display","none"); // hide all siblings
550 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
Dirk Dougherty4405a232009-07-07 17:43:27 -0700551 node.css("display","inline");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800552 } else { // otherwise, show English instead
Dirk Dougherty4405a232009-07-07 17:43:27 -0700553 node.css("display","none");
554 node.siblings().filter(".en").css("display","inline");
555 }
556 }
557}
558
559function changeDocLang(lang) {
560 changeTabLang(lang);
561 changeNavLang(lang);
562}
563
564function changeLangPref(lang, refresh) {
565 var date = new Date();
566 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
567 //alert("expires: " + expires)
568 writeCookie("pref_lang", lang, null, expires);
569 //changeDocLang(lang);
570 if (refresh) {
571 l = getBaseUri(location.pathname);
572 window.location = l;
573 }
574}
575
576function loadLangPref() {
577 var lang = readCookie("pref_lang");
578 if (lang != 0) {
579 $("#language").find("option[value='"+lang+"']").attr("selected",true);
580 }
581}
582
583function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700584 var lang = $("#language").find(":selected").attr("value");
585 if (!lang) {
586 lang = readCookie("pref_lang");
587 }
588 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700589}
Scott Main462cc372009-10-23 16:19:37 -0700590
591
Scott Maine5781192011-01-12 14:33:58 -0800592/* Used to hide and reveal supplemental content, such as long code samples.
593 See the companion CSS in android-developer-docs.css */
Scott Main462cc372009-10-23 16:19:37 -0700594function toggleContent(obj) {
Scott Maine5781192011-01-12 14:33:58 -0800595 var div = $(obj.parentNode.parentNode);
Scott Main462cc372009-10-23 16:19:37 -0700596 var toggleMe = $(".toggle-content-toggleme",div);
Scott Maine5781192011-01-12 14:33:58 -0800597 if (div.hasClass("closed")) { // if it's closed, open it
Scott Main462cc372009-10-23 16:19:37 -0700598 toggleMe.slideDown();
Scott Main0a53dc42011-01-12 16:50:00 -0800599 $(".toggle-content-text", obj).toggle();
Scott Maine5781192011-01-12 14:33:58 -0800600 div.removeClass("closed").addClass("open");
601 $(".toggle-content-img", div).attr("title", "hide").attr("src", toRoot + "assets/images/triangle-opened.png");
602 } else { // if it's open, close it
Scott Main0a53dc42011-01-12 16:50:00 -0800603 toggleMe.slideUp('fast', function() { // Wait until the animation is done before closing arrow
604 $(".toggle-content-text", obj).toggle();
605 div.removeClass("open").addClass("closed");
606 $(".toggle-content-img", div).attr("title", "show").attr("src", toRoot + "assets/images/triangle-closed.png");
607 });
Scott Main462cc372009-10-23 16:19:37 -0700608 }
Scott Maine5781192011-01-12 14:33:58 -0800609 return false;
Scott Main462cc372009-10-23 16:19:37 -0700610}