The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | var resizePackagesNav; |
| 2 | var classesNav; |
| 3 | var devdocNav; |
| 4 | var sidenav; |
| 5 | var content; |
| 6 | var HEADER_HEIGHT = 117; |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 7 | var cookie_namespace = 'android_developer'; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 8 | var NAV_PREF_TREE = "tree"; |
| 9 | var NAV_PREF_PANELS = "panels"; |
| 10 | var nav_pref; |
| 11 | var toRoot; |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 12 | var isMobile = false; // true if mobile, so we can adjust some layout |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 13 | |
| 14 | function addLoadEvent(newfun) { |
| 15 | var current = window.onload; |
| 16 | if (typeof window.onload != 'function') { |
| 17 | window.onload = newfun; |
| 18 | } else { |
| 19 | window.onload = function() { |
| 20 | current(); |
| 21 | newfun(); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 26 | var agent = navigator['userAgent']; |
| 27 | if ((agent.indexOf("Mobile") != -1) || |
| 28 | (agent.indexOf("BlackBerry") != -1) || |
| 29 | (agent.indexOf("Mini") != -1)) { |
| 30 | isMobile = true; |
| 31 | addLoadEvent(mobileSetup); |
| 32 | } |
| 33 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | window.onresize = resizeAll; |
| 35 | |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 36 | function mobileSetup() { |
| 37 | $("body").css({'overflow':'auto'}); |
| 38 | $("html").css({'overflow':'auto'}); |
| 39 | $("#body-content").css({'position':'relative', 'top':'0'}); |
| 40 | $("#doc-content").css({'overflow':'visible', 'border-left':'3px solid #DDD'}); |
| 41 | $("#side-nav").css({'padding':'0'}); |
| 42 | $("#nav-tree").css({'overflow-y': 'auto'}); |
| 43 | } |
| 44 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | function setToRoot(root) { |
| 46 | toRoot = root; |
| 47 | // note: toRoot also used by carousel.js |
| 48 | } |
| 49 | |
| 50 | function restoreWidth(navWidth) { |
| 51 | var windowWidth = $(window).width() + "px"; |
| 52 | content.css({marginLeft:parseInt(navWidth) + 6 + "px", //account for 6px-wide handle-bar |
| 53 | width:parseInt(windowWidth) - parseInt(navWidth) - 6 + "px"}); |
| 54 | sidenav.css({width:navWidth}); |
| 55 | resizePackagesNav.css({width:navWidth}); |
| 56 | classesNav.css({width:navWidth}); |
| 57 | $("#packages-nav").css({width:navWidth}); |
| 58 | } |
| 59 | |
| 60 | function restoreHeight(packageHeight) { |
| 61 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
| 62 | var swapperHeight = windowHeight - 13; |
| 63 | $("#swapper").css({height:swapperHeight + "px"}); |
| 64 | sidenav.css({height:windowHeight + "px"}); |
| 65 | content.css({height:windowHeight + "px"}); |
| 66 | resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight}); |
| 67 | classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"}); |
| 68 | $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle |
| 69 | devdocNav.css({height:sidenav.css("height")}); |
| 70 | $("#nav-tree").css({height:swapperHeight + "px"}); |
| 71 | } |
| 72 | |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 73 | function readCookie(cookie) { |
| 74 | var myCookie = cookie_namespace+"_"+cookie+"="; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 75 | if (document.cookie) { |
| 76 | var index = document.cookie.indexOf(myCookie); |
| 77 | if (index != -1) { |
| 78 | var valStart = index + myCookie.length; |
| 79 | var valEnd = document.cookie.indexOf(";", valStart); |
| 80 | if (valEnd == -1) { |
| 81 | valEnd = document.cookie.length; |
| 82 | } |
| 83 | var val = document.cookie.substring(valStart, valEnd); |
| 84 | return val; |
| 85 | } |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 90 | function writeCookie(cookie, val, section, expiration) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 91 | if (!val) return; |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 92 | section = section == null ? "_" : "_"+section+"_"; |
| 93 | if (expiration == null) { |
| 94 | var date = new Date(); |
| 95 | date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week |
| 96 | expiration = date.toGMTString(); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 97 | } |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 98 | document.cookie = cookie_namespace+section+cookie+"="+val+"; expires="+expiration+"; path=/"; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | function init() { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 102 | $("#side-nav").css({position:"absolute",left:0}); |
| 103 | content = $("#doc-content"); |
| 104 | resizePackagesNav = $("#resize-packages-nav"); |
| 105 | classesNav = $("#classes-nav"); |
| 106 | sidenav = $("#side-nav"); |
| 107 | devdocNav = $("#devdoc-nav"); |
| 108 | |
| 109 | if (location.href.indexOf("/reference/") != -1) { |
| 110 | var cookiePath = "reference_"; |
| 111 | } else if (location.href.indexOf("/guide/") != -1) { |
| 112 | var cookiePath = "guide_"; |
| 113 | } |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 114 | |
| 115 | if (!isMobile) { |
| 116 | $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } }); |
| 117 | $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } }); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 118 | var cookieWidth = readCookie(cookiePath+'width'); |
| 119 | var cookieHeight = readCookie(cookiePath+'height'); |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 120 | if (cookieWidth) { |
| 121 | restoreWidth(cookieWidth); |
| 122 | } else if ($(".side-nav-resizable").length) { |
| 123 | resizeWidth(); |
| 124 | } |
| 125 | if (cookieHeight) { |
| 126 | restoreHeight(cookieHeight); |
| 127 | } else { |
| 128 | resizeHeight(); |
| 129 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (devdocNav.length) { // only dev guide and sdk |
| 133 | highlightNav(location.href); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | function highlightNav(fullPageName) { |
| 138 | var lastSlashPos = fullPageName.lastIndexOf("/"); |
| 139 | var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ? |
| 140 | fullPageName.indexOf("/guide/") : |
| 141 | fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk |
| 142 | if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html') |
| 143 | fullPageName = fullPageName + "index.html"; |
| 144 | } |
| 145 | var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length); |
| 146 | var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); |
| 147 | var link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); |
| 148 | if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) { |
| 149 | // if there's no match, then let's backstep through the directory until we find an index.html page that matches our ancestor directories (only for dev guide and sdk) |
| 150 | lastBackstep = pathPageName.lastIndexOf("/"); |
| 151 | while (link.length == 0) { |
| 152 | backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep); |
| 153 | link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']"); |
| 154 | lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1); |
| 155 | if (lastBackstep == 0) break; |
| 156 | } |
| 157 | } |
| 158 | link.parent().addClass('selected'); |
| 159 | if (link.parent().parent().is(':hidden')) { |
| 160 | toggle(link.parent().parent().parent(), false); |
| 161 | } else if (link.parent().parent().hasClass('toggle-list')) { |
| 162 | toggle(link.parent().parent(), false); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | function resizeHeight() { |
| 167 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
| 168 | var swapperHeight = windowHeight - 13; |
| 169 | $("#swapper").css({height:swapperHeight + "px"}); |
| 170 | sidenav.css({height:windowHeight + "px"}); |
| 171 | content.css({height:windowHeight + "px"}); |
| 172 | resizePackagesNav.css({maxHeight:swapperHeight + "px"}); |
| 173 | classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); |
| 174 | $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle |
| 175 | devdocNav.css({height:sidenav.css("height")}); |
| 176 | $("#nav-tree").css({height:swapperHeight + "px"}); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 177 | |
| 178 | var section = location.pathname.substring(1,location.pathname.indexOf("/",1)); |
| 179 | writeCookie("height", resizePackagesNav.css("height"), section, null); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | function resizeWidth() { |
| 183 | var windowWidth = $(window).width() + "px"; |
| 184 | if (sidenav.length) { |
| 185 | var sidenavWidth = sidenav.css("width"); |
| 186 | } else { |
| 187 | var sidenavWidth = 0; |
| 188 | } |
| 189 | content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar |
| 190 | width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); |
| 191 | resizePackagesNav.css({width:sidenavWidth}); |
| 192 | classesNav.css({width:sidenavWidth}); |
| 193 | $("#packages-nav").css({width:sidenavWidth}); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 194 | |
| 195 | var section = location.pathname.substring(1,location.pathname.indexOf("/",1)); |
| 196 | writeCookie("width", sidenavWidth, section, null); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | function resizeAll() { |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 200 | if (!isMobile) { |
| 201 | resizeHeight(); |
| 202 | if ($(".side-nav-resizable").length) { |
| 203 | resizeWidth(); |
| 204 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 208 | function getBaseUri(uri) { |
| 209 | intlUrl = uri.substring(0,6) == "/intl/"; |
| 210 | if (intlUrl) { |
| 211 | base = uri.substring(uri.indexOf('intl/')+5,uri.length); |
| 212 | base = base.substring(base.indexOf('/')+1, base.length); |
| 213 | //alert("intl, returning base url: /" + base); |
| 214 | return ("/" + base); |
| 215 | } else { |
| 216 | //alert("not intl, returning uri as found."); |
| 217 | return uri; |
| 218 | } |
| 219 | } |
| 220 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | function loadLast(cookiePath) { |
| 222 | var location = window.location.href; |
| 223 | if (location.indexOf("/"+cookiePath+"/") != -1) { |
| 224 | return true; |
| 225 | } |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 226 | var lastPage = readCookie(cookiePath + "_lastpage"); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 227 | if (lastPage) { |
| 228 | window.location = lastPage; |
| 229 | return false; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | $(window).unload(function(){ |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 235 | var path = getBaseUri(location.pathname); |
| 236 | if (path.indexOf("/reference/") != -1) { |
| 237 | writeCookie("lastpage", path, "reference", null); |
| 238 | } else if (path.indexOf("/guide/") != -1) { |
| 239 | writeCookie("lastpage", path, "guide", null); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 240 | } |
| 241 | }); |
| 242 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 243 | function toggle(obj, slide) { |
| 244 | var ul = $("ul", obj); |
| 245 | var li = ul.parent(); |
| 246 | if (li.hasClass("closed")) { |
| 247 | if (slide) { |
| 248 | ul.slideDown("fast"); |
| 249 | } else { |
| 250 | ul.show(); |
| 251 | } |
| 252 | li.removeClass("closed"); |
| 253 | li.addClass("open"); |
| 254 | $(".toggle-img", li).attr("title", "hide pages"); |
| 255 | } else { |
| 256 | ul.slideUp("fast"); |
| 257 | li.removeClass("open"); |
| 258 | li.addClass("closed"); |
| 259 | $(".toggle-img", li).attr("title", "show pages"); |
| 260 | } |
| 261 | } |
| 262 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | function buildToggleLists() { |
| 264 | $(".toggle-list").each( |
| 265 | function(i) { |
| 266 | $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>"); |
| 267 | $(this).addClass("closed"); |
| 268 | }); |
| 269 | } |
| 270 | |
| 271 | function getNavPref() { |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 272 | var v = readCookie('reference_nav'); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 273 | if (v != NAV_PREF_TREE) { |
| 274 | v = NAV_PREF_PANELS; |
| 275 | } |
| 276 | return v; |
| 277 | } |
| 278 | |
| 279 | function chooseDefaultNav() { |
| 280 | nav_pref = getNavPref(); |
| 281 | if (nav_pref == NAV_PREF_TREE) { |
| 282 | $("#nav-panels").toggle(); |
| 283 | $("#panel-link").toggle(); |
| 284 | $("#nav-tree").toggle(); |
| 285 | $("#tree-link").toggle(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | function swapNav() { |
| 290 | if (nav_pref == NAV_PREF_TREE) { |
| 291 | nav_pref = NAV_PREF_PANELS; |
| 292 | } else { |
| 293 | nav_pref = NAV_PREF_TREE; |
| 294 | init_navtree("nav-tree", toRoot, NAVTREE_DATA); |
| 295 | } |
| 296 | var date = new Date(); |
| 297 | date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 298 | writeCookie("nav", nav_pref, "reference", date.toGMTString()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 299 | |
| 300 | $("#nav-panels").toggle(); |
| 301 | $("#panel-link").toggle(); |
| 302 | $("#nav-tree").toggle(); |
| 303 | $("#tree-link").toggle(); |
| 304 | |
| 305 | if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree"); |
| 306 | else { |
| 307 | scrollIntoView("packages-nav"); |
| 308 | scrollIntoView("classes-nav"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | function scrollIntoView(nav) { |
| 313 | var navObj = $("#"+nav); |
| 314 | if (navObj.is(':visible')) { |
| 315 | var selected = $(".selected", navObj); |
| 316 | if (selected.length == 0) return; |
| 317 | if (selected.is("div")) selected = selected.parent(); |
| 318 | |
| 319 | var scrolling = document.getElementById(nav); |
| 320 | var navHeight = navObj.height(); |
| 321 | var offsetTop = selected.position().top; |
| 322 | if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top; |
| 323 | if(offsetTop > navHeight - 92) { |
| 324 | scrolling.scrollTop = offsetTop - navHeight + 92; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | function toggleAllInherited(linkObj, expand) { |
| 330 | var a = $(linkObj); |
| 331 | var table = $(a.parent().parent().parent()); |
| 332 | var expandos = $(".jd-expando-trigger", table); |
| 333 | if ( (expand == null && a.text() == "[Expand]") || expand ) { |
| 334 | expandos.each(function(i) { |
| 335 | toggleInherited(this, true); |
| 336 | }); |
| 337 | a.text("[Collapse]"); |
| 338 | } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) { |
| 339 | expandos.each(function(i) { |
| 340 | toggleInherited(this, false); |
| 341 | }); |
| 342 | a.text("[Expand]"); |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | function toggleAllSummaryInherited(linkObj) { |
| 348 | var a = $(linkObj); |
| 349 | var content = $(a.parent().parent().parent()); |
| 350 | var toggles = $(".toggle-all", content); |
| 351 | if (a.text() == "[Expand All]") { |
| 352 | toggles.each(function(i) { |
| 353 | toggleAllInherited(this, true); |
| 354 | }); |
| 355 | a.text("[Collapse All]"); |
| 356 | } else { |
| 357 | toggles.each(function(i) { |
| 358 | toggleAllInherited(this, false); |
| 359 | }); |
| 360 | a.text("[Expand All]"); |
| 361 | } |
| 362 | return false; |
| 363 | } |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame^] | 364 | |
| 365 | |
| 366 | function changeTabLang(lang) { |
| 367 | var nodes = $("#header-tabs").find("."+lang); |
| 368 | for (i=0; i < nodes.length; i++) { // for each node in this language |
| 369 | var node = $(nodes[i]); |
| 370 | node.siblings().css("display","none"); // hide all siblings |
| 371 | if (node.not(":empty").length != 0) { //if this languages node has a translation, show it |
| 372 | node.css("display","inline"); |
| 373 | } else { //otherwise, show English instead |
| 374 | node.css("display","none"); |
| 375 | node.siblings().filter(".en").css("display","inline"); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | function changeNavLang(lang) { |
| 381 | var nodes = $("#side-nav").find("."+lang); |
| 382 | for (i=0; i < nodes.length; i++) { // for each node in this language |
| 383 | var node = $(nodes[i]); |
| 384 | node.siblings().css("display","none"); // hide all siblings |
| 385 | if (node.not(":empty").length != 0) { // if this languages node has a translation, show it |
| 386 | node.css("display","inline"); |
| 387 | } else { // otherwise, show English instead |
| 388 | node.css("display","none"); |
| 389 | node.siblings().filter(".en").css("display","inline"); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | function changeDocLang(lang) { |
| 395 | changeTabLang(lang); |
| 396 | changeNavLang(lang); |
| 397 | } |
| 398 | |
| 399 | function changeLangPref(lang, refresh) { |
| 400 | var date = new Date(); |
| 401 | expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years |
| 402 | //alert("expires: " + expires) |
| 403 | writeCookie("pref_lang", lang, null, expires); |
| 404 | //changeDocLang(lang); |
| 405 | if (refresh) { |
| 406 | l = getBaseUri(location.pathname); |
| 407 | window.location = l; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | function loadLangPref() { |
| 412 | var lang = readCookie("pref_lang"); |
| 413 | if (lang != 0) { |
| 414 | $("#language").find("option[value='"+lang+"']").attr("selected",true); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | function getLangPref() { |
| 419 | return $("#language").find(":selected").attr("value"); |
| 420 | } |