blob: 94391fdc0ec4f8f8f48f8ddfa0fdd93dba71f323 [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_";
Dirk Dougherty502c4982009-12-02 18:01:16 -0800146 } else if (location.href.indexOf("/resources/") != -1) {
Scott Maina39d3bb2010-01-06 14:18:02 -0800147 cookiePath = "resources_";
The Android Open Source Project88b60792009-03-03 19:28:42 -0800148 }
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700149
150 if (!isMobile) {
Scott Main3f7d1df2009-11-02 15:31:04 -0800151 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } });
Scott Mainfd85d422011-01-07 12:08:09 -0800152 $("#side-nav").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } });
Dirk Dougherty4405a232009-07-07 17:43:27 -0700153 var cookieWidth = readCookie(cookiePath+'width');
154 var cookieHeight = readCookie(cookiePath+'height');
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700155 if (cookieWidth) {
156 restoreWidth(cookieWidth);
Scott Mainfd85d422011-01-07 12:08:09 -0800157 } else if ($("#side-nav").length) {
The Android Open Source Projectfdd3a102009-03-11 12:11:54 -0700158 resizeWidth();
159 }
160 if (cookieHeight) {
161 restoreHeight(cookieHeight);
162 } else {
163 resizeHeight();
164 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800165 }
166
Roman Nurik78eff9f2010-03-01 18:35:13 -0800167 if (devdocNav.length) { // only dev guide, resources, and sdk
168 tryPopulateResourcesNav();
169 highlightNav(location.href);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800170 }
171}
172
Roman Nurik78eff9f2010-03-01 18:35:13 -0800173function tryPopulateResourcesNav() {
174 var sampleList = $('#devdoc-nav-sample-list');
175 var articleList = $('#devdoc-nav-article-list');
176 var tutorialList = $('#devdoc-nav-tutorial-list');
177 var topicList = $('#devdoc-nav-topic-list');
178
179 if (!topicList.length || !ANDROID_TAGS || !ANDROID_RESOURCES)
180 return;
181
182 var topics = [];
183 for (var topic in ANDROID_TAGS['topic']) {
184 topics.push({name:topic,title:ANDROID_TAGS['topic'][topic]});
185 }
186 topics.sort(function(x,y){ return (x.title < y.title) ? -1 : 1; });
187 for (var i = 0; i < topics.length; i++) {
188 topicList.append(
189 $('<li>').append(
190 $('<a>')
191 .attr('href', toRoot + "resources/browser.html?tag=" + topics[i].name)
192 .append($('<span>')
193 .addClass('en')
194 .html(topics[i].title)
195 )
196 )
197 );
198 }
199
200 var _renderResourceList = function(tag, listNode) {
201 var resources = [];
202 var tags;
203 var resource;
204 var i, j;
205 for (i = 0; i < ANDROID_RESOURCES.length; i++) {
206 resource = ANDROID_RESOURCES[i];
207 tags = resource.tags || [];
208 var hasTag = false;
209 for (j = 0; j < tags.length; j++)
210 if (tags[j] == tag) {
211 hasTag = true;
212 break;
213 }
214 if (!hasTag)
215 continue;
216 resources.push(resource);
217 }
218 //resources.sort(function(x,y){ return (x.title.en < y.title.en) ? -1 : 1; });
219 for (i = 0; i < resources.length; i++) {
220 resource = resources[i];
221 var listItemNode = $('<li>').append(
222 $('<a>')
223 .attr('href', toRoot + "resources/" + resource.path)
224 .append($('<span>')
225 .addClass('en')
226 .html(resource.title.en)
227 )
228 );
229 tags = resource.tags || [];
230 for (j = 0; j < tags.length; j++) {
231 if (tags[j] == 'new') {
232 listItemNode.get(0).innerHTML += '&nbsp;<span class="new">new!</span>';
233 break;
234 }
235 }
236 listNode.append(listItemNode);
237 }
238 };
239
240 _renderResourceList('sample', sampleList);
241 _renderResourceList('article', articleList);
242 _renderResourceList('tutorial', tutorialList);
243}
244
The Android Open Source Project88b60792009-03-03 19:28:42 -0800245function highlightNav(fullPageName) {
246 var lastSlashPos = fullPageName.lastIndexOf("/");
Dirk Dougherty502c4982009-12-02 18:01:16 -0800247 var firstSlashPos;
248 if (fullPageName.indexOf("/guide/") != -1) {
Scott Mainfd85d422011-01-07 12:08:09 -0800249 firstSlashPos = fullPageName.indexOf("/guide/");
250 } else if (fullPageName.indexOf("/sdk/") != -1) {
251 firstSlashPos = fullPageName.indexOf("/sdk/");
252 } else if (fullPageName.indexOf("/resources/") != -1) {
253 firstSlashPos = fullPageName.indexOf("/resources/");
254 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800255 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
256 fullPageName = fullPageName + "index.html";
257 }
Roman Nurik78eff9f2010-03-01 18:35:13 -0800258 // First check if the exact URL, with query string and all, is in the navigation menu
259 var pathPageName = fullPageName.substr(firstSlashPos);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800260 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800261 if (link.length == 0) {
262 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length);
263 pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); // +5 advances past ".html"
264 link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
265 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/resources/") != -1))) {
266 // if there's no match, then let's backstep through the directory until we find an index.html page
267 // that matches our ancestor directories (only for dev guide and resources)
268 lastBackstep = pathPageName.lastIndexOf("/");
269 while (link.length == 0) {
270 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
271 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']");
272 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
273 if (lastBackstep == 0) break;
274 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800275 }
276 }
Scott Mainee629d92010-02-05 14:30:08 -0800277
278 // add 'selected' to the <li> or <div> that wraps this <a>
The Android Open Source Project88b60792009-03-03 19:28:42 -0800279 link.parent().addClass('selected');
Scott Mainee629d92010-02-05 14:30:08 -0800280
281 // if we're in a toggleable root link (<li class=toggle-list><div><a>)
282 if (link.parent().parent().hasClass('toggle-list')) {
283 toggle(link.parent().parent(), false); // open our own list
284 // then also check if we're in a third-level nested list that's toggleable
285 if (link.parent().parent().parent().is(':hidden')) {
286 toggle(link.parent().parent().parent().parent(), false); // open the super parent list
287 }
288 }
289 // if we're in a normal nav link (<li><a>) and the parent <ul> is hidden
290 else if (link.parent().parent().is(':hidden')) {
291 toggle(link.parent().parent().parent(), false); // open the parent list
292 // then also check if the parent list is also nested in a hidden list
293 if (link.parent().parent().parent().parent().is(':hidden')) {
294 toggle(link.parent().parent().parent().parent().parent(), false); // open the super parent list
295 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800296 }
297}
298
Scott Main3f7d1df2009-11-02 15:31:04 -0800299/* Resize the height of the nav panels in the reference,
300 * and save the new size to a cookie */
301function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800302 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800303 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800304 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
305 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800306
307 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800308 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800309
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700310 var basePath = getBaseUri(location.pathname);
311 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700312 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800313}
314
Scott Main3f7d1df2009-11-02 15:31:04 -0800315/* Resize the height of the side-nav and doc-content divs,
316 * which creates the frame effect */
317function resizeHeight() {
Scott Main13b79b22010-01-12 13:37:00 -0800318 var docContent = $("#doc-content");
319
Scott Main3f7d1df2009-11-02 15:31:04 -0800320 // Get the window height and always resize the doc-content and side-nav divs
321 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main13b79b22010-01-12 13:37:00 -0800322 docContent.css({height:windowHeight + "px"});
323 $("#side-nav").css({height:windowHeight + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800324
325 var href = location.href;
326 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
327 if (href.indexOf("/reference/") != -1) {
328 var swapperHeight = windowHeight - 13;
329 $("#swapper").css({height:swapperHeight + "px"});
330 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
331 $("#nav-tree").css({height:swapperHeight + "px"});
332
Scott Mainfd85d422011-01-07 12:08:09 -0800333 // Also resize the "devdoc-nav" div
334 } else if ($("#devdoc-nav").length) {
Dirk Dougherty502c4982009-12-02 18:01:16 -0800335 $("#devdoc-nav").css({height:sidenav.css("height")});
Scott Main3f7d1df2009-11-02 15:31:04 -0800336 }
Scott Main13b79b22010-01-12 13:37:00 -0800337
338 // Hide the "Go to top" link if there's no vertical scroll
339 if ( parseInt($("#jd-content").css("height")) <= parseInt(docContent.css("height")) ) {
340 $("a[href='#top']").css({'display':'none'});
341 } else {
342 $("a[href='#top']").css({'display':'inline'});
343 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800344}
345
346/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
347 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800348function resizeWidth() {
349 var windowWidth = $(window).width() + "px";
Scott Mainfd85d422011-01-07 12:08:09 -0800350 var sidenav = $("#side-nav");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800351 if (sidenav.length) {
352 var sidenavWidth = sidenav.css("width");
353 } else {
354 var sidenavWidth = 0;
355 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800356 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
357
358 if (isIE6) {
359 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
360 }
361
The Android Open Source Project88b60792009-03-03 19:28:42 -0800362 resizePackagesNav.css({width:sidenavWidth});
363 classesNav.css({width:sidenavWidth});
364 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800365
Scott Mainfd85d422011-01-07 12:08:09 -0800366 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 -0800367 var basePath = getBaseUri(location.pathname);
368 var section = basePath.substring(1,basePath.indexOf("/",1));
369 writeCookie("width", sidenavWidth, section, null);
370 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800371}
372
Scott Main3f7d1df2009-11-02 15:31:04 -0800373/* For IE6 only,
374 * because it can't properly perform auto width for "doc-content" div,
375 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800376function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800377 resizeHeight();
Scott Mainb6da4802010-01-06 14:18:02 -0800378 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800379}
380
Dirk Dougherty4405a232009-07-07 17:43:27 -0700381function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700382 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700383 if (intlUrl) {
384 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
385 base = base.substring(base.indexOf('/')+1, base.length);
386 //alert("intl, returning base url: /" + base);
387 return ("/" + base);
388 } else {
389 //alert("not intl, returning uri as found.");
390 return uri;
391 }
392}
393
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700394function requestAppendHL(uri) {
395//append "?hl=<lang> to an outgoing request (such as to blog)
396 var lang = getLangPref();
397 if (lang) {
398 var q = 'hl=' + lang;
399 uri += '?' + q;
400 window.location = uri;
401 return false;
402 } else {
403 return true;
404 }
405}
406
The Android Open Source Project88b60792009-03-03 19:28:42 -0800407function loadLast(cookiePath) {
408 var location = window.location.href;
409 if (location.indexOf("/"+cookiePath+"/") != -1) {
410 return true;
411 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700412 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800413 if (lastPage) {
414 window.location = lastPage;
415 return false;
416 }
417 return true;
418}
419
420$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700421 var path = getBaseUri(location.pathname);
422 if (path.indexOf("/reference/") != -1) {
423 writeCookie("lastpage", path, "reference", null);
424 } else if (path.indexOf("/guide/") != -1) {
425 writeCookie("lastpage", path, "guide", null);
Dirk Dougherty502c4982009-12-02 18:01:16 -0800426 } else if (path.indexOf("/resources/") != -1) {
427 writeCookie("lastpage", path, "resources", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800428 }
429});
430
The Android Open Source Project88b60792009-03-03 19:28:42 -0800431function toggle(obj, slide) {
Scott Mainee629d92010-02-05 14:30:08 -0800432 var ul = $("ul:first", obj);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800433 var li = ul.parent();
434 if (li.hasClass("closed")) {
435 if (slide) {
436 ul.slideDown("fast");
437 } else {
438 ul.show();
439 }
440 li.removeClass("closed");
441 li.addClass("open");
442 $(".toggle-img", li).attr("title", "hide pages");
443 } else {
444 ul.slideUp("fast");
445 li.removeClass("open");
446 li.addClass("closed");
447 $(".toggle-img", li).attr("title", "show pages");
448 }
449}
450
The Android Open Source Project88b60792009-03-03 19:28:42 -0800451function buildToggleLists() {
452 $(".toggle-list").each(
453 function(i) {
Scott Mainee629d92010-02-05 14:30:08 -0800454 $("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 -0800455 $(this).addClass("closed");
456 });
457}
458
459function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700460 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800461 if (v != NAV_PREF_TREE) {
462 v = NAV_PREF_PANELS;
463 }
464 return v;
465}
466
467function chooseDefaultNav() {
468 nav_pref = getNavPref();
469 if (nav_pref == NAV_PREF_TREE) {
470 $("#nav-panels").toggle();
471 $("#panel-link").toggle();
472 $("#nav-tree").toggle();
473 $("#tree-link").toggle();
474 }
475}
476
477function swapNav() {
478 if (nav_pref == NAV_PREF_TREE) {
479 nav_pref = NAV_PREF_PANELS;
480 } else {
481 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700482 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800483 }
484 var date = new Date();
485 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700486 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800487
488 $("#nav-panels").toggle();
489 $("#panel-link").toggle();
490 $("#nav-tree").toggle();
491 $("#tree-link").toggle();
492
493 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
494 else {
495 scrollIntoView("packages-nav");
496 scrollIntoView("classes-nav");
497 }
498}
499
500function scrollIntoView(nav) {
501 var navObj = $("#"+nav);
502 if (navObj.is(':visible')) {
503 var selected = $(".selected", navObj);
504 if (selected.length == 0) return;
505 if (selected.is("div")) selected = selected.parent();
506
507 var scrolling = document.getElementById(nav);
508 var navHeight = navObj.height();
509 var offsetTop = selected.position().top;
510 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top;
511 if(offsetTop > navHeight - 92) {
512 scrolling.scrollTop = offsetTop - navHeight + 92;
513 }
514 }
515}
516
Dirk Dougherty4405a232009-07-07 17:43:27 -0700517function changeTabLang(lang) {
518 var nodes = $("#header-tabs").find("."+lang);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800519 for (i=0; i < nodes.length; i++) { // for each node in this language
Dirk Dougherty4405a232009-07-07 17:43:27 -0700520 var node = $(nodes[i]);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800521 node.siblings().css("display","none"); // hide all siblings
522 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
Dirk Dougherty4405a232009-07-07 17:43:27 -0700523 node.css("display","inline");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800524 } else { //otherwise, show English instead
Dirk Dougherty4405a232009-07-07 17:43:27 -0700525 node.css("display","none");
526 node.siblings().filter(".en").css("display","inline");
527 }
528 }
529}
530
531function changeNavLang(lang) {
532 var nodes = $("#side-nav").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 changeDocLang(lang) {
546 changeTabLang(lang);
547 changeNavLang(lang);
548}
549
550function changeLangPref(lang, refresh) {
551 var date = new Date();
552 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
553 //alert("expires: " + expires)
554 writeCookie("pref_lang", lang, null, expires);
555 //changeDocLang(lang);
556 if (refresh) {
557 l = getBaseUri(location.pathname);
558 window.location = l;
559 }
560}
561
562function loadLangPref() {
563 var lang = readCookie("pref_lang");
564 if (lang != 0) {
565 $("#language").find("option[value='"+lang+"']").attr("selected",true);
566 }
567}
568
569function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700570 var lang = $("#language").find(":selected").attr("value");
571 if (!lang) {
572 lang = readCookie("pref_lang");
573 }
574 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700575}
Scott Main462cc372009-10-23 16:19:37 -0700576
577
Scott Maine5781192011-01-12 14:33:58 -0800578/* Used to hide and reveal supplemental content, such as long code samples.
579 See the companion CSS in android-developer-docs.css */
Scott Main462cc372009-10-23 16:19:37 -0700580function toggleContent(obj) {
Scott Maine5781192011-01-12 14:33:58 -0800581 var div = $(obj.parentNode.parentNode);
Scott Main462cc372009-10-23 16:19:37 -0700582 var toggleMe = $(".toggle-content-toggleme",div);
Scott Maine5781192011-01-12 14:33:58 -0800583 if (div.hasClass("closed")) { // if it's closed, open it
Scott Main462cc372009-10-23 16:19:37 -0700584 toggleMe.slideDown();
Scott Maine5781192011-01-12 14:33:58 -0800585 div.removeClass("closed").addClass("open");
586 $(".toggle-content-img", div).attr("title", "hide").attr("src", toRoot + "assets/images/triangle-opened.png");
587 } else { // if it's open, close it
Scott Main462cc372009-10-23 16:19:37 -0700588 toggleMe.slideUp();
Scott Maine5781192011-01-12 14:33:58 -0800589 div.removeClass("open").addClass("closed");
590 $(".toggle-content-img", div).attr("title", "show").attr("src", toRoot + "assets/images/triangle-closed.png");
Scott Main462cc372009-10-23 16:19:37 -0700591 }
Scott Maine5781192011-01-12 14:33:58 -0800592 $(".toggle-content-text", obj).toggle();
593 return false;
Scott Main462cc372009-10-23 16:19:37 -0700594}