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; |
| 7 | var cookie_style = 'android_developer'; |
| 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 | |
| 73 | function getCookie(cookie) { |
| 74 | var myCookie = cookie_style+"_"+cookie+"="; |
| 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 | |
| 90 | function writeCookie(cookie, val, path, expiration) { |
| 91 | if (!val) return; |
| 92 | var date = new Date(); |
| 93 | date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week |
| 94 | expiration = expiration ? expiration : date.toGMTString(); |
| 95 | if (location.href.indexOf("/reference/") != -1) { |
| 96 | document.cookie = cookie_style+'_reference_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path; |
| 97 | } else if (location.href.indexOf("/guide/") != -1) { |
| 98 | document.cookie = cookie_style+'_guide_'+cookie+'='+val+'; expires='+expiration+'; path='+'/'+path; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function init() { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 103 | $("#side-nav").css({position:"absolute",left:0}); |
| 104 | content = $("#doc-content"); |
| 105 | resizePackagesNav = $("#resize-packages-nav"); |
| 106 | classesNav = $("#classes-nav"); |
| 107 | sidenav = $("#side-nav"); |
| 108 | devdocNav = $("#devdoc-nav"); |
| 109 | |
| 110 | if (location.href.indexOf("/reference/") != -1) { |
| 111 | var cookiePath = "reference_"; |
| 112 | } else if (location.href.indexOf("/guide/") != -1) { |
| 113 | var cookiePath = "guide_"; |
| 114 | } |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame^] | 115 | |
| 116 | if (!isMobile) { |
| 117 | $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } }); |
| 118 | $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } }); |
| 119 | var cookieWidth = getCookie(cookiePath+'width'); |
| 120 | var cookieHeight = getCookie(cookiePath+'height'); |
| 121 | if (cookieWidth) { |
| 122 | restoreWidth(cookieWidth); |
| 123 | } else if ($(".side-nav-resizable").length) { |
| 124 | resizeWidth(); |
| 125 | } |
| 126 | if (cookieHeight) { |
| 127 | restoreHeight(cookieHeight); |
| 128 | } else { |
| 129 | resizeHeight(); |
| 130 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (devdocNav.length) { // only dev guide and sdk |
| 134 | highlightNav(location.href); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | function highlightNav(fullPageName) { |
| 139 | var lastSlashPos = fullPageName.lastIndexOf("/"); |
| 140 | var firstSlashPos = (fullPageName.indexOf("/guide/") != -1) ? |
| 141 | fullPageName.indexOf("/guide/") : |
| 142 | fullPageName.indexOf("/sdk/"); // first slash after /guide or /sdk |
| 143 | if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html') |
| 144 | fullPageName = fullPageName + "index.html"; |
| 145 | } |
| 146 | var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length); |
| 147 | var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); |
| 148 | var link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); |
| 149 | if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/sdk/") != -1))) { |
| 150 | // 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) |
| 151 | lastBackstep = pathPageName.lastIndexOf("/"); |
| 152 | while (link.length == 0) { |
| 153 | backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep); |
| 154 | link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']"); |
| 155 | lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1); |
| 156 | if (lastBackstep == 0) break; |
| 157 | } |
| 158 | } |
| 159 | link.parent().addClass('selected'); |
| 160 | if (link.parent().parent().is(':hidden')) { |
| 161 | toggle(link.parent().parent().parent(), false); |
| 162 | } else if (link.parent().parent().hasClass('toggle-list')) { |
| 163 | toggle(link.parent().parent(), false); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | function resizeHeight() { |
| 168 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
| 169 | var swapperHeight = windowHeight - 13; |
| 170 | $("#swapper").css({height:swapperHeight + "px"}); |
| 171 | sidenav.css({height:windowHeight + "px"}); |
| 172 | content.css({height:windowHeight + "px"}); |
| 173 | resizePackagesNav.css({maxHeight:swapperHeight + "px"}); |
| 174 | classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); |
| 175 | $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle |
| 176 | devdocNav.css({height:sidenav.css("height")}); |
| 177 | $("#nav-tree").css({height:swapperHeight + "px"}); |
| 178 | writeCookie("height", resizePackagesNav.css("height"), "", null); |
| 179 | } |
| 180 | |
| 181 | function resizeWidth() { |
| 182 | var windowWidth = $(window).width() + "px"; |
| 183 | if (sidenav.length) { |
| 184 | var sidenavWidth = sidenav.css("width"); |
| 185 | } else { |
| 186 | var sidenavWidth = 0; |
| 187 | } |
| 188 | content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px", //account for 6px-wide handle-bar |
| 189 | width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); |
| 190 | resizePackagesNav.css({width:sidenavWidth}); |
| 191 | classesNav.css({width:sidenavWidth}); |
| 192 | $("#packages-nav").css({width:sidenavWidth}); |
| 193 | writeCookie("width", sidenavWidth, "", null); |
| 194 | } |
| 195 | |
| 196 | function resizeAll() { |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame^] | 197 | if (!isMobile) { |
| 198 | resizeHeight(); |
| 199 | if ($(".side-nav-resizable").length) { |
| 200 | resizeWidth(); |
| 201 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | function loadLast(cookiePath) { |
| 206 | var location = window.location.href; |
| 207 | if (location.indexOf("/"+cookiePath+"/") != -1) { |
| 208 | return true; |
| 209 | } |
| 210 | var lastPage = getCookie(cookiePath + "_lastpage"); |
| 211 | if (lastPage) { |
| 212 | window.location = lastPage; |
| 213 | return false; |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | $(window).unload(function(){ |
| 219 | var href = location.href; |
| 220 | if (href.indexOf("/reference/") != -1) { |
| 221 | writeCookie("lastpage", href, "", null); |
| 222 | } else if (href.indexOf("/guide/") != -1) { |
| 223 | writeCookie("lastpage", href, "", null); |
| 224 | } |
| 225 | }); |
| 226 | |
| 227 | |
| 228 | |
| 229 | function toggle(obj, slide) { |
| 230 | var ul = $("ul", obj); |
| 231 | var li = ul.parent(); |
| 232 | if (li.hasClass("closed")) { |
| 233 | if (slide) { |
| 234 | ul.slideDown("fast"); |
| 235 | } else { |
| 236 | ul.show(); |
| 237 | } |
| 238 | li.removeClass("closed"); |
| 239 | li.addClass("open"); |
| 240 | $(".toggle-img", li).attr("title", "hide pages"); |
| 241 | } else { |
| 242 | ul.slideUp("fast"); |
| 243 | li.removeClass("open"); |
| 244 | li.addClass("closed"); |
| 245 | $(".toggle-img", li).attr("title", "show pages"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | function buildToggleLists() { |
| 252 | $(".toggle-list").each( |
| 253 | function(i) { |
| 254 | $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>"); |
| 255 | $(this).addClass("closed"); |
| 256 | }); |
| 257 | } |
| 258 | |
| 259 | function getNavPref() { |
| 260 | var v = getCookie('reference_nav'); |
| 261 | if (v != NAV_PREF_TREE) { |
| 262 | v = NAV_PREF_PANELS; |
| 263 | } |
| 264 | return v; |
| 265 | } |
| 266 | |
| 267 | function chooseDefaultNav() { |
| 268 | nav_pref = getNavPref(); |
| 269 | if (nav_pref == NAV_PREF_TREE) { |
| 270 | $("#nav-panels").toggle(); |
| 271 | $("#panel-link").toggle(); |
| 272 | $("#nav-tree").toggle(); |
| 273 | $("#tree-link").toggle(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | function swapNav() { |
| 278 | if (nav_pref == NAV_PREF_TREE) { |
| 279 | nav_pref = NAV_PREF_PANELS; |
| 280 | } else { |
| 281 | nav_pref = NAV_PREF_TREE; |
| 282 | init_navtree("nav-tree", toRoot, NAVTREE_DATA); |
| 283 | } |
| 284 | var date = new Date(); |
| 285 | date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years |
| 286 | writeCookie("nav", nav_pref, "", date.toGMTString()); |
| 287 | |
| 288 | $("#nav-panels").toggle(); |
| 289 | $("#panel-link").toggle(); |
| 290 | $("#nav-tree").toggle(); |
| 291 | $("#tree-link").toggle(); |
| 292 | |
| 293 | if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree"); |
| 294 | else { |
| 295 | scrollIntoView("packages-nav"); |
| 296 | scrollIntoView("classes-nav"); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | function scrollIntoView(nav) { |
| 301 | var navObj = $("#"+nav); |
| 302 | if (navObj.is(':visible')) { |
| 303 | var selected = $(".selected", navObj); |
| 304 | if (selected.length == 0) return; |
| 305 | if (selected.is("div")) selected = selected.parent(); |
| 306 | |
| 307 | var scrolling = document.getElementById(nav); |
| 308 | var navHeight = navObj.height(); |
| 309 | var offsetTop = selected.position().top; |
| 310 | if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top; |
| 311 | if(offsetTop > navHeight - 92) { |
| 312 | scrolling.scrollTop = offsetTop - navHeight + 92; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | function toggleAllInherited(linkObj, expand) { |
| 318 | var a = $(linkObj); |
| 319 | var table = $(a.parent().parent().parent()); |
| 320 | var expandos = $(".jd-expando-trigger", table); |
| 321 | if ( (expand == null && a.text() == "[Expand]") || expand ) { |
| 322 | expandos.each(function(i) { |
| 323 | toggleInherited(this, true); |
| 324 | }); |
| 325 | a.text("[Collapse]"); |
| 326 | } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) { |
| 327 | expandos.each(function(i) { |
| 328 | toggleInherited(this, false); |
| 329 | }); |
| 330 | a.text("[Expand]"); |
| 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | function toggleAllSummaryInherited(linkObj) { |
| 336 | var a = $(linkObj); |
| 337 | var content = $(a.parent().parent().parent()); |
| 338 | var toggles = $(".toggle-all", content); |
| 339 | if (a.text() == "[Expand All]") { |
| 340 | toggles.each(function(i) { |
| 341 | toggleAllInherited(this, true); |
| 342 | }); |
| 343 | a.text("[Collapse All]"); |
| 344 | } else { |
| 345 | toggles.each(function(i) { |
| 346 | toggleAllInherited(this, false); |
| 347 | }); |
| 348 | a.text("[Expand All]"); |
| 349 | } |
| 350 | return false; |
| 351 | } |