blob: c88f726daa99060cca38d91c5379b69e5ec16114 [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;
Scott Main99113ab2011-09-21 20:51:54 -0700235 } else if (tags[j] == 'updated') {
236 listItemNode.get(0).innerHTML += '&nbsp;<span class="new">updated!</span>';
237 break;
Roman Nurik78eff9f2010-03-01 18:35:13 -0800238 }
239 }
240 listNode.append(listItemNode);
241 }
242 };
243
244 _renderResourceList('sample', sampleList);
245 _renderResourceList('article', articleList);
246 _renderResourceList('tutorial', tutorialList);
247}
248
The Android Open Source Project88b60792009-03-03 19:28:42 -0800249function highlightNav(fullPageName) {
250 var lastSlashPos = fullPageName.lastIndexOf("/");
Dirk Dougherty502c4982009-12-02 18:01:16 -0800251 var firstSlashPos;
252 if (fullPageName.indexOf("/guide/") != -1) {
Scott Mainfd85d422011-01-07 12:08:09 -0800253 firstSlashPos = fullPageName.indexOf("/guide/");
254 } else if (fullPageName.indexOf("/sdk/") != -1) {
255 firstSlashPos = fullPageName.indexOf("/sdk/");
256 } else if (fullPageName.indexOf("/resources/") != -1) {
257 firstSlashPos = fullPageName.indexOf("/resources/");
Scott Main629b0f02011-07-08 16:17:02 -0700258 } else if (fullPageName.indexOf("/training/") != -1) {
259 firstSlashPos = fullPageName.indexOf("/training/");
Scott Mainfd85d422011-01-07 12:08:09 -0800260 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800261 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html')
262 fullPageName = fullPageName + "index.html";
263 }
Scott Main76a62f22011-07-08 16:19:25 -0700264
265 // get the path and page name from the URL (such as 'guide/topics/graphics/index.html')
266 var htmlPos = fullPageName.indexOf(".html");
267 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); // +5 advances past ".html"
268 // find instances of the page name in the side nav
The Android Open Source Project88b60792009-03-03 19:28:42 -0800269 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']");
Scott Main76a62f22011-07-08 16:19:25 -0700270 // if there's no match, then let's backstep through the directory until we find an index.html
271 // page that matches our ancestor directories (only for dev guide and resources)
272 if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) ||
273 (fullPageName.indexOf("/resources/") != -1))) {
274 lastBackstep = pathPageName.lastIndexOf("/");
275 while (link.length == 0) {
276 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep);
277 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory +
278 1)+"index.html']");
279 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1);
280 if (lastBackstep == 0) break;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800281 }
282 }
Scott Mainee629d92010-02-05 14:30:08 -0800283
284 // add 'selected' to the <li> or <div> that wraps this <a>
The Android Open Source Project88b60792009-03-03 19:28:42 -0800285 link.parent().addClass('selected');
Scott Mainee629d92010-02-05 14:30:08 -0800286
287 // if we're in a toggleable root link (<li class=toggle-list><div><a>)
288 if (link.parent().parent().hasClass('toggle-list')) {
289 toggle(link.parent().parent(), false); // open our own list
290 // then also check if we're in a third-level nested list that's toggleable
291 if (link.parent().parent().parent().is(':hidden')) {
292 toggle(link.parent().parent().parent().parent(), false); // open the super parent list
293 }
294 }
295 // if we're in a normal nav link (<li><a>) and the parent <ul> is hidden
296 else if (link.parent().parent().is(':hidden')) {
297 toggle(link.parent().parent().parent(), false); // open the parent list
298 // then also check if the parent list is also nested in a hidden list
299 if (link.parent().parent().parent().parent().is(':hidden')) {
300 toggle(link.parent().parent().parent().parent().parent(), false); // open the super parent list
301 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800302 }
303}
304
Scott Main3f7d1df2009-11-02 15:31:04 -0800305/* Resize the height of the nav panels in the reference,
306 * and save the new size to a cookie */
307function resizePackagesHeight() {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800308 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main3f7d1df2009-11-02 15:31:04 -0800309 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom
The Android Open Source Project88b60792009-03-03 19:28:42 -0800310 resizePackagesNav.css({maxHeight:swapperHeight + "px"});
311 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800312
313 $("#swapper").css({height:swapperHeight + "px"});
The Android Open Source Project88b60792009-03-03 19:28:42 -0800314 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle
Scott Main3f7d1df2009-11-02 15:31:04 -0800315
Dirk Doughertyc13d0452009-07-08 14:39:31 -0700316 var basePath = getBaseUri(location.pathname);
317 var section = basePath.substring(1,basePath.indexOf("/",1));
Dirk Dougherty4405a232009-07-07 17:43:27 -0700318 writeCookie("height", resizePackagesNav.css("height"), section, null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800319}
320
Scott Main3f7d1df2009-11-02 15:31:04 -0800321/* Resize the height of the side-nav and doc-content divs,
322 * which creates the frame effect */
323function resizeHeight() {
Scott Main13b79b22010-01-12 13:37:00 -0800324 var docContent = $("#doc-content");
325
Scott Main3f7d1df2009-11-02 15:31:04 -0800326 // Get the window height and always resize the doc-content and side-nav divs
327 var windowHeight = ($(window).height() - HEADER_HEIGHT);
Scott Main13b79b22010-01-12 13:37:00 -0800328 docContent.css({height:windowHeight + "px"});
329 $("#side-nav").css({height:windowHeight + "px"});
Scott Main3f7d1df2009-11-02 15:31:04 -0800330
331 var href = location.href;
332 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs
333 if (href.indexOf("/reference/") != -1) {
334 var swapperHeight = windowHeight - 13;
335 $("#swapper").css({height:swapperHeight + "px"});
336 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"});
337 $("#nav-tree").css({height:swapperHeight + "px"});
338
Scott Mainfd85d422011-01-07 12:08:09 -0800339 // Also resize the "devdoc-nav" div
340 } else if ($("#devdoc-nav").length) {
Dirk Dougherty502c4982009-12-02 18:01:16 -0800341 $("#devdoc-nav").css({height:sidenav.css("height")});
Scott Main3f7d1df2009-11-02 15:31:04 -0800342 }
Scott Main13b79b22010-01-12 13:37:00 -0800343
344 // Hide the "Go to top" link if there's no vertical scroll
345 if ( parseInt($("#jd-content").css("height")) <= parseInt(docContent.css("height")) ) {
346 $("a[href='#top']").css({'display':'none'});
347 } else {
348 $("a[href='#top']").css({'display':'inline'});
349 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800350}
351
352/* Resize the width of the "side-nav" and the left margin of the "doc-content" div,
353 * which creates the resizable side bar */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800354function resizeWidth() {
355 var windowWidth = $(window).width() + "px";
Scott Mainfd85d422011-01-07 12:08:09 -0800356 var sidenav = $("#side-nav");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800357 if (sidenav.length) {
358 var sidenavWidth = sidenav.css("width");
359 } else {
360 var sidenavWidth = 0;
361 }
Scott Main3f7d1df2009-11-02 15:31:04 -0800362 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar
363
364 if (isIE6) {
365 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible
366 }
367
The Android Open Source Project88b60792009-03-03 19:28:42 -0800368 resizePackagesNav.css({width:sidenavWidth});
369 classesNav.css({width:sidenavWidth});
370 $("#packages-nav").css({width:sidenavWidth});
Scott Main3f7d1df2009-11-02 15:31:04 -0800371
Scott Mainfd85d422011-01-07 12:08:09 -0800372 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 -0800373 var basePath = getBaseUri(location.pathname);
374 var section = basePath.substring(1,basePath.indexOf("/",1));
375 writeCookie("width", sidenavWidth, section, null);
376 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800377}
378
Scott Main3f7d1df2009-11-02 15:31:04 -0800379/* For IE6 only,
380 * because it can't properly perform auto width for "doc-content" div,
381 * avoiding this for all browsers provides better performance */
The Android Open Source Project88b60792009-03-03 19:28:42 -0800382function resizeAll() {
Scott Main3f7d1df2009-11-02 15:31:04 -0800383 resizeHeight();
Scott Mainb6da4802010-01-06 14:18:02 -0800384 resizeWidth();
The Android Open Source Project88b60792009-03-03 19:28:42 -0800385}
386
Dirk Dougherty4405a232009-07-07 17:43:27 -0700387function getBaseUri(uri) {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700388 var intlUrl = (uri.substring(0,6) == "/intl/");
Dirk Dougherty4405a232009-07-07 17:43:27 -0700389 if (intlUrl) {
390 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
391 base = base.substring(base.indexOf('/')+1, base.length);
392 //alert("intl, returning base url: /" + base);
393 return ("/" + base);
394 } else {
395 //alert("not intl, returning uri as found.");
396 return uri;
397 }
398}
399
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700400function requestAppendHL(uri) {
401//append "?hl=<lang> to an outgoing request (such as to blog)
402 var lang = getLangPref();
403 if (lang) {
404 var q = 'hl=' + lang;
405 uri += '?' + q;
406 window.location = uri;
407 return false;
408 } else {
409 return true;
410 }
411}
412
The Android Open Source Project88b60792009-03-03 19:28:42 -0800413function loadLast(cookiePath) {
414 var location = window.location.href;
415 if (location.indexOf("/"+cookiePath+"/") != -1) {
416 return true;
417 }
Dirk Dougherty4405a232009-07-07 17:43:27 -0700418 var lastPage = readCookie(cookiePath + "_lastpage");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800419 if (lastPage) {
420 window.location = lastPage;
421 return false;
422 }
423 return true;
424}
425
426$(window).unload(function(){
Dirk Dougherty4405a232009-07-07 17:43:27 -0700427 var path = getBaseUri(location.pathname);
Scott Main0f429eb2011-12-15 19:12:17 -0800428 if (path.indexOf("/reference/") != -1) {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700429 writeCookie("lastpage", path, "reference", null);
430 } else if (path.indexOf("/guide/") != -1) {
431 writeCookie("lastpage", path, "guide", null);
Scott Main0f429eb2011-12-15 19:12:17 -0800432 } else if ((path.indexOf("/resources/") != -1) || (path.indexOf("/training/") != -1)) {
Dirk Dougherty502c4982009-12-02 18:01:16 -0800433 writeCookie("lastpage", path, "resources", null);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800434 }
435});
436
The Android Open Source Project88b60792009-03-03 19:28:42 -0800437function toggle(obj, slide) {
Scott Mainee629d92010-02-05 14:30:08 -0800438 var ul = $("ul:first", obj);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800439 var li = ul.parent();
440 if (li.hasClass("closed")) {
441 if (slide) {
442 ul.slideDown("fast");
443 } else {
444 ul.show();
445 }
446 li.removeClass("closed");
447 li.addClass("open");
448 $(".toggle-img", li).attr("title", "hide pages");
449 } else {
450 ul.slideUp("fast");
451 li.removeClass("open");
452 li.addClass("closed");
453 $(".toggle-img", li).attr("title", "show pages");
454 }
455}
456
The Android Open Source Project88b60792009-03-03 19:28:42 -0800457function buildToggleLists() {
458 $(".toggle-list").each(
459 function(i) {
Scott Mainee629d92010-02-05 14:30:08 -0800460 $("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 -0800461 $(this).addClass("closed");
462 });
463}
464
465function getNavPref() {
Dirk Dougherty4405a232009-07-07 17:43:27 -0700466 var v = readCookie('reference_nav');
The Android Open Source Project88b60792009-03-03 19:28:42 -0800467 if (v != NAV_PREF_TREE) {
468 v = NAV_PREF_PANELS;
469 }
470 return v;
471}
472
473function chooseDefaultNav() {
474 nav_pref = getNavPref();
475 if (nav_pref == NAV_PREF_TREE) {
476 $("#nav-panels").toggle();
477 $("#panel-link").toggle();
478 $("#nav-tree").toggle();
479 $("#tree-link").toggle();
480 }
481}
482
483function swapNav() {
484 if (nav_pref == NAV_PREF_TREE) {
485 nav_pref = NAV_PREF_PANELS;
486 } else {
487 nav_pref = NAV_PREF_TREE;
Dirk Dougherty3b1ee372009-07-29 12:25:44 -0700488 init_default_navtree(toRoot);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800489 }
490 var date = new Date();
491 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
Dirk Dougherty4405a232009-07-07 17:43:27 -0700492 writeCookie("nav", nav_pref, "reference", date.toGMTString());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800493
494 $("#nav-panels").toggle();
495 $("#panel-link").toggle();
496 $("#nav-tree").toggle();
497 $("#tree-link").toggle();
498
499 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree");
500 else {
501 scrollIntoView("packages-nav");
502 scrollIntoView("classes-nav");
503 }
504}
505
506function scrollIntoView(nav) {
507 var navObj = $("#"+nav);
508 if (navObj.is(':visible')) {
509 var selected = $(".selected", navObj);
510 if (selected.length == 0) return;
Scott Main3ab162e2011-03-04 12:23:00 -0800511 if (selected.is("div")) selected = selected.parent(); // when the selected item is a parent
The Android Open Source Project88b60792009-03-03 19:28:42 -0800512
513 var scrolling = document.getElementById(nav);
514 var navHeight = navObj.height();
515 var offsetTop = selected.position().top;
Scott Main3ab162e2011-03-04 12:23:00 -0800516
517 // handle nested items
518 if (selected.parent().parent().is(".toggle-list")) {
519 selected = selected.parent().parent();
520 // handle second level nested items
521 if (selected.parent().parent().is(".toggle-list")) {
522 selected = selected.parent().parent();
523 }
524 offsetTop += selected.position().top;
525 }
526
527 // 180px from the bottom of the list is the threshold
528 if(offsetTop > navHeight - 180) {
529 scrolling.scrollTop = offsetTop - navHeight + 180;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800530 }
531 }
532}
533
Dirk Dougherty4405a232009-07-07 17:43:27 -0700534function changeTabLang(lang) {
535 var nodes = $("#header-tabs").find("."+lang);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800536 for (i=0; i < nodes.length; i++) { // for each node in this language
Dirk Dougherty4405a232009-07-07 17:43:27 -0700537 var node = $(nodes[i]);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800538 node.siblings().css("display","none"); // hide all siblings
539 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it
Dirk Dougherty4405a232009-07-07 17:43:27 -0700540 node.css("display","inline");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800541 } else { //otherwise, show English instead
Dirk Dougherty4405a232009-07-07 17:43:27 -0700542 node.css("display","none");
543 node.siblings().filter(".en").css("display","inline");
544 }
545 }
546}
547
548function changeNavLang(lang) {
549 var nodes = $("#side-nav").find("."+lang);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800550 for (i=0; i < nodes.length; i++) { // for each node in this language
Dirk Dougherty4405a232009-07-07 17:43:27 -0700551 var node = $(nodes[i]);
Roman Nurik78eff9f2010-03-01 18:35:13 -0800552 node.siblings().css("display","none"); // hide all siblings
553 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it
Dirk Dougherty4405a232009-07-07 17:43:27 -0700554 node.css("display","inline");
Roman Nurik78eff9f2010-03-01 18:35:13 -0800555 } else { // otherwise, show English instead
Dirk Dougherty4405a232009-07-07 17:43:27 -0700556 node.css("display","none");
557 node.siblings().filter(".en").css("display","inline");
558 }
559 }
560}
561
562function changeDocLang(lang) {
563 changeTabLang(lang);
564 changeNavLang(lang);
565}
566
567function changeLangPref(lang, refresh) {
568 var date = new Date();
569 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years
570 //alert("expires: " + expires)
571 writeCookie("pref_lang", lang, null, expires);
572 //changeDocLang(lang);
573 if (refresh) {
574 l = getBaseUri(location.pathname);
575 window.location = l;
576 }
577}
578
579function loadLangPref() {
580 var lang = readCookie("pref_lang");
581 if (lang != 0) {
582 $("#language").find("option[value='"+lang+"']").attr("selected",true);
583 }
584}
585
586function getLangPref() {
Dirk Doughertyefdcda42009-07-15 16:41:48 -0700587 var lang = $("#language").find(":selected").attr("value");
588 if (!lang) {
589 lang = readCookie("pref_lang");
590 }
591 return (lang != 0) ? lang : 'en';
Dirk Dougherty4405a232009-07-07 17:43:27 -0700592}
Scott Main462cc372009-10-23 16:19:37 -0700593
594
Scott Maine5781192011-01-12 14:33:58 -0800595/* Used to hide and reveal supplemental content, such as long code samples.
596 See the companion CSS in android-developer-docs.css */
Scott Main462cc372009-10-23 16:19:37 -0700597function toggleContent(obj) {
Scott Maine5781192011-01-12 14:33:58 -0800598 var div = $(obj.parentNode.parentNode);
Scott Main462cc372009-10-23 16:19:37 -0700599 var toggleMe = $(".toggle-content-toggleme",div);
Scott Maine5781192011-01-12 14:33:58 -0800600 if (div.hasClass("closed")) { // if it's closed, open it
Scott Main462cc372009-10-23 16:19:37 -0700601 toggleMe.slideDown();
Scott Main0a53dc42011-01-12 16:50:00 -0800602 $(".toggle-content-text", obj).toggle();
Scott Maine5781192011-01-12 14:33:58 -0800603 div.removeClass("closed").addClass("open");
604 $(".toggle-content-img", div).attr("title", "hide").attr("src", toRoot + "assets/images/triangle-opened.png");
605 } else { // if it's open, close it
Scott Main0a53dc42011-01-12 16:50:00 -0800606 toggleMe.slideUp('fast', function() { // Wait until the animation is done before closing arrow
607 $(".toggle-content-text", obj).toggle();
608 div.removeClass("open").addClass("closed");
609 $(".toggle-content-img", div).attr("title", "show").attr("src", toRoot + "assets/images/triangle-closed.png");
610 });
Scott Main462cc372009-10-23 16:19:37 -0700611 }
Scott Maine5781192011-01-12 14:33:58 -0800612 return false;
Scott Main462cc372009-10-23 16:19:37 -0700613}