1var resizePackagesNav; 2var classesNav; 3var devdocNav; 4var sidenav; 5var content; 6var HEADER_HEIGHT = 117; 7var cookie_namespace = 'android_developer'; 8var NAV_PREF_TREE = "tree"; 9var NAV_PREF_PANELS = "panels"; 10var nav_pref; 11var toRoot; 12var isMobile = false; // true if mobile, so we can adjust some layout 13var isIE6 = false; // true if IE6 14 15function addLoadEvent(newfun) { 16 var current = window.onload; 17 if (typeof window.onload != 'function') { 18 window.onload = newfun; 19 } else { 20 window.onload = function() { 21 current(); 22 newfun(); 23 } 24 } 25} 26 27var agent = navigator['userAgent'].toLowerCase(); 28// If a mobile phone, set flag and do mobile setup 29if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod 30 (agent.indexOf("blackberry") != -1) || 31 (agent.indexOf("webos") != -1) || 32 (agent.indexOf("mini") != -1)) { // opera mini browsers 33 isMobile = true; 34 addLoadEvent(mobileSetup); 35// If not a mobile browser, set the onresize event for IE6, and others 36} else if (agent.indexOf("msie 6") != -1) { 37 isIE6 = true; 38 addLoadEvent(function() { 39 window.onresize = resizeAll; 40 }); 41} else { 42 addLoadEvent(function() { 43 window.onresize = resizeHeight; 44 }); 45} 46 47function mobileSetup() { 48 $("body").css({'overflow':'auto'}); 49 $("html").css({'overflow':'auto'}); 50 $("#body-content").css({'position':'relative', 'top':'0'}); 51 $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'}); 52 $("#side-nav").css({'padding':'0'}); 53 $("#nav-tree").css({'overflow-y': 'auto'}); 54} 55 56/* loads the lists.js file to the page. 57Loading this in the head was slowing page load time */ 58addLoadEvent( function() { 59 var lists = document.createElement("script"); 60 lists.setAttribute("type","text/javascript"); 61 lists.setAttribute("src", toRoot+"reference/lists.js"); 62 document.getElementsByTagName("head")[0].appendChild(lists); 63} ); 64 65function setToRoot(root) { 66 toRoot = root; 67 // note: toRoot also used by carousel.js 68} 69 70function restoreWidth(navWidth) { 71 var windowWidth = $(window).width() + "px"; 72 content.css({marginLeft:parseInt(navWidth) + 6 + "px"}); //account for 6px-wide handle-bar 73 74 if (isIE6) { 75 content.css({width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); // necessary in order for scrollbars to be visible 76 } 77 78 sidenav.css({width:navWidth}); 79 resizePackagesNav.css({width:navWidth}); 80 classesNav.css({width:navWidth}); 81 $("#packages-nav").css({width:navWidth}); 82} 83 84function restoreHeight(packageHeight) { 85 var windowHeight = ($(window).height() - HEADER_HEIGHT); 86 var swapperHeight = windowHeight - 13; 87 $("#swapper").css({height:swapperHeight + "px"}); 88 sidenav.css({height:windowHeight + "px"}); 89 content.css({height:windowHeight + "px"}); 90 resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight}); 91 classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"}); 92 $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle 93 devdocNav.css({height:sidenav.css("height")}); 94 $("#nav-tree").css({height:swapperHeight + "px"}); 95} 96 97function readCookie(cookie) { 98 var myCookie = cookie_namespace+"_"+cookie+"="; 99 if (document.cookie) { 100 var index = document.cookie.indexOf(myCookie); 101 if (index != -1) { 102 var valStart = index + myCookie.length; 103 var valEnd = document.cookie.indexOf(";", valStart); 104 if (valEnd == -1) { 105 valEnd = document.cookie.length; 106 } 107 var val = document.cookie.substring(valStart, valEnd); 108 return val; 109 } 110 } 111 return 0; 112} 113 114function writeCookie(cookie, val, section, expiration) { 115 if (val==undefined) return; 116 section = section == null ? "_" : "_"+section+"_"; 117 if (expiration == null) { 118 var date = new Date(); 119 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week 120 expiration = date.toGMTString(); 121 } 122 document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/"; 123} 124 125function init() { 126 $("#side-nav").css({position:"absolute",left:0}); 127 content = $("#doc-content"); 128 resizePackagesNav = $("#resize-packages-nav"); 129 classesNav = $("#classes-nav"); 130 sidenav = $("#side-nav"); 131 devdocNav = $("#devdoc-nav"); 132 133 if (location.href.indexOf("/reference/") != -1) { 134 var cookiePath = "reference_"; 135 } else if (location.href.indexOf("/guide/") != -1) { 136 var cookiePath = "guide_"; 137 } 138 139 if (!isMobile) { 140 $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } }); 141 $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } }); 142 var cookieWidth = readCookie(cookiePath+'width'); 143 var cookieHeight = readCookie(cookiePath+'height'); 144 if (cookieWidth) { 145 restoreWidth(cookieWidth); 146 } else if ($(".side-nav-resizable").length) { 147 resizeWidth(); 148 } 149 if (cookieHeight) { 150 restoreHeight(cookieHeight); 151 } else { 152 resizeHeight(); 153 } 154 } 155 156 if (devdocNav.length) { // only dev guide and sdk 157 highlightNav(location.href); 158 } 159} 160 161function highlightNav(fullPageName) { 162 var lastSlashPos = fullPageName.lastIndexOf("/"); 163 var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ? 164 fullPageName.indexOf("/guide/") : 165 fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk 166 if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html') 167 fullPageName = fullPageName + "index.html"; 168 } 169 var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length); 170 var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); 171 var link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); 172 if ((link.length == 0) && (fullPageName.indexOf("/guide/") != -1)) { 173// 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) 174 lastBackstep = pathPageName.lastIndexOf("/"); 175 while (link.length == 0) { 176 backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep); 177 link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']"); 178 lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1); 179 if (lastBackstep == 0) break; 180 } 181 } 182 link.parent().addClass('selected'); 183 if (link.parent().parent().is(':hidden')) { 184 toggle(link.parent().parent().parent(), false); 185 } else if (link.parent().parent().hasClass('toggle-list')) { 186 toggle(link.parent().parent(), false); 187 } 188} 189 190/* Resize the height of the nav panels in the reference, 191 * and save the new size to a cookie */ 192function resizePackagesHeight() { 193 var windowHeight = ($(window).height() - HEADER_HEIGHT); 194 var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom 195 resizePackagesNav.css({maxHeight:swapperHeight + "px"}); 196 classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); 197 198 $("#swapper").css({height:swapperHeight + "px"}); 199 $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle 200 201 var basePath = getBaseUri(location.pathname); 202 var section = basePath.substring(1,basePath.indexOf("/",1)); 203 writeCookie("height", resizePackagesNav.css("height"), section, null); 204} 205 206/* Resize the height of the side-nav and doc-content divs, 207 * which creates the frame effect */ 208function resizeHeight() { 209 // Get the window height and always resize the doc-content and side-nav divs 210 var windowHeight = ($(window).height() - HEADER_HEIGHT); 211 content.css({height:windowHeight + "px"}); 212 sidenav.css({height:windowHeight + "px"}); 213 214 var href = location.href; 215 // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs 216 if (href.indexOf("/reference/") != -1) { 217 var swapperHeight = windowHeight - 13; 218 $("#swapper").css({height:swapperHeight + "px"}); 219 $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); 220 $("#nav-tree").css({height:swapperHeight + "px"}); 221 222 // If in the dev guide docs, also resize the "devdoc-nav" div 223 } else if (href.indexOf("/guide/") != -1) { 224 $("#devdoc-nav").css({height:sidenav.css("height")}); 225 } 226} 227 228/* Resize the width of the "side-nav" and the left margin of the "doc-content" div, 229 * which creates the resizable side bar */ 230function resizeWidth() { 231 var windowWidth = $(window).width() + "px"; 232 if (sidenav.length) { 233 var sidenavWidth = sidenav.css("width"); 234 } else { 235 var sidenavWidth = 0; 236 } 237 content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar 238 239 if (isIE6) { 240 content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible 241 } 242 243 resizePackagesNav.css({width:sidenavWidth}); 244 classesNav.css({width:sidenavWidth}); 245 $("#packages-nav").css({width:sidenavWidth}); 246 247 var basePath = getBaseUri(location.pathname); 248 var section = basePath.substring(1,basePath.indexOf("/",1)); 249 writeCookie("width", sidenavWidth, section, null); 250} 251 252/* For IE6 only, 253 * because it can't properly perform auto width for "doc-content" div, 254 * avoiding this for all browsers provides better performance */ 255function resizeAll() { 256 resizeHeight(); 257 resizeWidth(); 258} 259 260function getBaseUri(uri) { 261 var intlUrl = (uri.substring(0,6) == "/intl/"); 262 if (intlUrl) { 263 base = uri.substring(uri.indexOf('intl/')+5,uri.length); 264 base = base.substring(base.indexOf('/')+1, base.length); 265 //alert("intl, returning base url: /" + base); 266 return ("/" + base); 267 } else { 268 //alert("not intl, returning uri as found."); 269 return uri; 270 } 271} 272 273function requestAppendHL(uri) { 274//append "?hl=<lang> to an outgoing request (such as to blog) 275 var lang = getLangPref(); 276 if (lang) { 277 var q = 'hl=' + lang; 278 uri += '?' + q; 279 window.location = uri; 280 return false; 281 } else { 282 return true; 283 } 284} 285 286function loadLast(cookiePath) { 287 var location = window.location.href; 288 if (location.indexOf("/"+cookiePath+"/") != -1) { 289 return true; 290 } 291 var lastPage = readCookie(cookiePath + "_lastpage"); 292 if (lastPage) { 293 window.location = lastPage; 294 return false; 295 } 296 return true; 297} 298 299$(window).unload(function(){ 300 var path = getBaseUri(location.pathname); 301 if (path.indexOf("/reference/") != -1) { 302 writeCookie("lastpage", path, "reference", null); 303 } else if (path.indexOf("/guide/") != -1) { 304 writeCookie("lastpage", path, "guide", null); 305 } 306}); 307 308function toggle(obj, slide) { 309 var ul = $("ul", obj); 310 var li = ul.parent(); 311 if (li.hasClass("closed")) { 312 if (slide) { 313 ul.slideDown("fast"); 314 } else { 315 ul.show(); 316 } 317 li.removeClass("closed"); 318 li.addClass("open"); 319 $(".toggle-img", li).attr("title", "hide pages"); 320 } else { 321 ul.slideUp("fast"); 322 li.removeClass("open"); 323 li.addClass("closed"); 324 $(".toggle-img", li).attr("title", "show pages"); 325 } 326} 327 328function buildToggleLists() { 329 $(".toggle-list").each( 330 function(i) { 331 $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>"); 332 $(this).addClass("closed"); 333 }); 334} 335 336function getNavPref() { 337 var v = readCookie('reference_nav'); 338 if (v != NAV_PREF_TREE) { 339 v = NAV_PREF_PANELS; 340 } 341 return v; 342} 343 344function chooseDefaultNav() { 345 nav_pref = getNavPref(); 346 if (nav_pref == NAV_PREF_TREE) { 347 $("#nav-panels").toggle(); 348 $("#panel-link").toggle(); 349 $("#nav-tree").toggle(); 350 $("#tree-link").toggle(); 351 } 352} 353 354function swapNav() { 355 if (nav_pref == NAV_PREF_TREE) { 356 nav_pref = NAV_PREF_PANELS; 357 } else { 358 nav_pref = NAV_PREF_TREE; 359 init_default_navtree(toRoot); 360 } 361 var date = new Date(); 362 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years 363 writeCookie("nav", nav_pref, "reference", date.toGMTString()); 364 365 $("#nav-panels").toggle(); 366 $("#panel-link").toggle(); 367 $("#nav-tree").toggle(); 368 $("#tree-link").toggle(); 369 370 if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree"); 371 else { 372 scrollIntoView("packages-nav"); 373 scrollIntoView("classes-nav"); 374 } 375} 376 377function scrollIntoView(nav) { 378 var navObj = $("#"+nav); 379 if (navObj.is(':visible')) { 380 var selected = $(".selected", navObj); 381 if (selected.length == 0) return; 382 if (selected.is("div")) selected = selected.parent(); 383 384 var scrolling = document.getElementById(nav); 385 var navHeight = navObj.height(); 386 var offsetTop = selected.position().top; 387 if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top; 388 if(offsetTop > navHeight - 92) { 389 scrolling.scrollTop = offsetTop - navHeight + 92; 390 } 391 } 392} 393 394function toggleAllInherited(linkObj, expand) { 395 var a = $(linkObj); 396 var table = $(a.parent().parent().parent()); 397 var expandos = $(".jd-expando-trigger", table); 398 if ( (expand == null && a.text() == "[Expand]") || expand ) { 399 expandos.each(function(i) { 400 toggleInherited(this, true); 401 }); 402 a.text("[Collapse]"); 403 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) { 404 expandos.each(function(i) { 405 toggleInherited(this, false); 406 }); 407 a.text("[Expand]"); 408 } 409 return false; 410} 411 412function toggleAllSummaryInherited(linkObj) { 413 var a = $(linkObj); 414 var content = $(a.parent().parent().parent()); 415 var toggles = $(".toggle-all", content); 416 if (a.text() == "[Expand All]") { 417 toggles.each(function(i) { 418 toggleAllInherited(this, true); 419 }); 420 a.text("[Collapse All]"); 421 } else { 422 toggles.each(function(i) { 423 toggleAllInherited(this, false); 424 }); 425 a.text("[Expand All]"); 426 } 427 return false; 428} 429 430 431function changeTabLang(lang) { 432 var nodes = $("#header-tabs").find("."+lang); 433 for (i=0; i < nodes.length; i++) { // for each node in this language 434 var node = $(nodes[i]); 435 node.siblings().css("display","none"); // hide all siblings 436 if (node.not(":empty").length != 0) { //if this languages node has a translation, show it 437 node.css("display","inline"); 438 } else { //otherwise, show English instead 439 node.css("display","none"); 440 node.siblings().filter(".en").css("display","inline"); 441 } 442 } 443} 444 445function changeNavLang(lang) { 446 var nodes = $("#side-nav").find("."+lang); 447 for (i=0; i < nodes.length; i++) { // for each node in this language 448 var node = $(nodes[i]); 449 node.siblings().css("display","none"); // hide all siblings 450 if (node.not(":empty").length != 0) { // if this languages node has a translation, show it 451 node.css("display","inline"); 452 } else { // otherwise, show English instead 453 node.css("display","none"); 454 node.siblings().filter(".en").css("display","inline"); 455 } 456 } 457} 458 459function changeDocLang(lang) { 460 changeTabLang(lang); 461 changeNavLang(lang); 462} 463 464function changeLangPref(lang, refresh) { 465 var date = new Date(); 466 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years 467 //alert("expires: " + expires) 468 writeCookie("pref_lang", lang, null, expires); 469 //changeDocLang(lang); 470 if (refresh) { 471 l = getBaseUri(location.pathname); 472 window.location = l; 473 } 474} 475 476function loadLangPref() { 477 var lang = readCookie("pref_lang"); 478 if (lang != 0) { 479 $("#language").find("option[value='"+lang+"']").attr("selected",true); 480 } 481} 482 483function getLangPref() { 484 var lang = $("#language").find(":selected").attr("value"); 485 if (!lang) { 486 lang = readCookie("pref_lang"); 487 } 488 return (lang != 0) ? lang : 'en'; 489} 490 491 492function toggleContent(obj) { 493 var button = $(obj); 494 var div = $(obj.parentNode); 495 var toggleMe = $(".toggle-content-toggleme",div); 496 if (button.hasClass("show")) { 497 toggleMe.slideDown(); 498 button.removeClass("show").addClass("hide"); 499 } else { 500 toggleMe.slideUp(); 501 button.removeClass("hide").addClass("show"); 502 } 503 $("span", button).toggle(); 504} 505