The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | var resizePackagesNav; |
| 2 | var classesNav; |
| 3 | var devdocNav; |
| 4 | var sidenav; |
| 5 | var content; |
| 6 | var HEADER_HEIGHT = 103; |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 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; |
| 12 | |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [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 | |
| 26 | addLoadEvent(prepare); |
| 27 | window.onresize = resizeAll; |
| 28 | |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 29 | function setToRoot(root) { |
| 30 | toRoot = root; |
| 31 | } |
| 32 | |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | function restoreWidth(navWidth) { |
| 34 | var windowWidth = $(window).width() + "px"; |
| 35 | content.css({marginLeft:navWidth, width:parseInt(windowWidth) - parseInt(navWidth) + "px"}); |
| 36 | sidenav.css({width:navWidth}); |
| 37 | resizePackagesNav.css({width:navWidth}); |
| 38 | classesNav.css({width:navWidth}); |
| 39 | $("#packages-nav").css({width:navWidth}); |
| 40 | } |
| 41 | |
| 42 | function restoreHeight(packageHeight) { |
| 43 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 44 | var swapperHeight = windowHeight - 13; |
| 45 | $("#swapper").css({height:swapperHeight + "px"}); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | sidenav.css({height:windowHeight + "px"}); |
| 47 | content.css({height:windowHeight + "px"}); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 48 | resizePackagesNav.css({maxHeight:swapperHeight + "px", height:packageHeight}); |
| 49 | classesNav.css({height:swapperHeight - parseInt(packageHeight) + "px"}); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | $("#packages-nav").css({height:parseInt(packageHeight) - 6 + "px"}); //move 6px to give space for the resize handle |
| 51 | devdocNav.css({height:sidenav.css("height")}); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 52 | $("#nav-tree").css({height:swapperHeight + "px"}); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | function getCookie(cookie) { |
| 56 | var myCookie = cookie_style+"_"+cookie+"="; |
| 57 | if (document.cookie) { |
| 58 | var index = document.cookie.indexOf(myCookie); |
| 59 | if (index != -1) { |
| 60 | var valStart = index + myCookie.length; |
| 61 | var valEnd = document.cookie.indexOf(";", valStart); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 62 | if (valEnd == -1) { |
| 63 | valEnd = document.cookie.length; |
| 64 | } |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | var val = document.cookie.substring(valStart, valEnd); |
| 66 | return val; |
| 67 | } |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 72 | function writeCookie(cookie, val, path, expiration) { |
| 73 | if (!val) return; |
| 74 | if (location.href.indexOf("/reference/") != -1) { |
| 75 | document.cookie = cookie_style+'_reference_'+cookie+'='+ val+'; path=' + toRoot + path + |
| 76 | ((expiration) ? '; expires=' + expiration : ''); |
| 77 | } else if (location.href.indexOf("/guide/") != -1) { |
| 78 | document.cookie = cookie_style+'_guide_'+cookie+'='+val+'; path=' + toRoot + path + |
| 79 | ((expiration) ? '; expires=' + expiration : ''); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | function prepare() { |
| 84 | $("#side-nav").css({position:"absolute",left:0}); |
| 85 | content = $("#doc-content"); |
| 86 | resizePackagesNav = $("#resize-packages-nav"); |
| 87 | classesNav = $("#classes-nav"); |
| 88 | sidenav = $("#side-nav"); |
| 89 | devdocNav = $("#devdoc-nav"); |
| 90 | |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 91 | |
| 92 | if (location.href.indexOf("/reference/") != -1) { |
| 93 | var cookiePath = "reference_"; |
| 94 | } else if (location.href.indexOf("/guide/") != -1) { |
| 95 | var cookiePath = "guide_"; |
| 96 | } |
| 97 | var cookieWidth = getCookie(cookiePath+'width'); |
| 98 | var cookieHeight = getCookie(cookiePath+'height'); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 99 | if (cookieWidth) { |
| 100 | restoreWidth(cookieWidth); |
| 101 | } else { |
| 102 | resizeWidth(); |
| 103 | } |
| 104 | if (cookieHeight) { |
| 105 | restoreHeight(cookieHeight); |
| 106 | } else { |
| 107 | resizeHeight(); |
| 108 | } |
| 109 | |
| 110 | if (devdocNav.length) { |
| 111 | highlightNav(location.href); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | function highlightNav(fullPageName) { |
| 116 | var lastSlashPos = fullPageName.lastIndexOf("/"); |
| 117 | var firstSlashPos = fullPageName.indexOf("/",8); // first slash after http:// |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 118 | if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html') |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | fullPageName = fullPageName + "index.html"; |
| 120 | } |
| 121 | var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 122 | var pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); |
| 123 | var link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); |
| 124 | if (link.length == 0) { // if there's no match, then the nav url must be the parent dir (ie, this doc isn't listed, so highlight the parent |
| 125 | link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, pathPageName.lastIndexOf("/") + 1)+"']"); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | } |
| 127 | link.parent().addClass('selected'); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 128 | if (link.parent().parent().is(':hidden')) { |
| 129 | toggle(link.parent().parent().parent(), false); |
| 130 | } else if (link.parent().parent().hasClass('toggle-list')) { |
| 131 | toggle(link.parent().parent(), false); |
| 132 | } |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | function resizeHeight() { |
| 136 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 137 | var swapperHeight = windowHeight - 13; |
| 138 | $("#swapper").css({height:swapperHeight + "px"}); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | sidenav.css({height:windowHeight + "px"}); |
| 140 | content.css({height:windowHeight + "px"}); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 141 | resizePackagesNav.css({maxHeight:swapperHeight + "px"}); |
| 142 | classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle |
| 144 | devdocNav.css({height:sidenav.css("height")}); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 145 | $("#nav-tree").css({height:swapperHeight + "px"}); |
| 146 | writeCookie("height", resizePackagesNav.css("height"), "reference/", null); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | function resizeWidth() { |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 150 | if (location.href.indexOf("/reference/") != -1) { |
| 151 | var path = "reference/"; |
| 152 | } else if (location.href.indexOf("/guide/") != -1) { |
| 153 | var path = "guide/"; |
| 154 | } |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | var windowWidth = $(window).width() + "px"; |
| 156 | if (sidenav.length) { |
| 157 | var sidenavWidth = sidenav.css("width"); |
| 158 | } else { |
| 159 | var sidenavWidth = 0; |
| 160 | } |
| 161 | content.css({marginLeft:sidenavWidth, width:parseInt(windowWidth) - parseInt(sidenavWidth) + "px"}); |
| 162 | resizePackagesNav.css({width:sidenavWidth}); |
| 163 | classesNav.css({width:sidenavWidth}); |
| 164 | $("#packages-nav").css({width:sidenavWidth}); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 165 | writeCookie("width", sidenavWidth, path, null); |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | function resizeAll() { |
| 169 | resizeHeight(); |
| 170 | resizeWidth(); |
| 171 | } |
| 172 | |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 173 | function loadLast(cookiePath) { |
| 174 | var lastPage = getCookie(cookiePath + "_lastpage"); |
| 175 | if (lastPage) { |
| 176 | window.location = lastPage; |
| 177 | return false; |
| 178 | } |
| 179 | return true; |
The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | $(document).ready(function(){ |
| 183 | $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizeHeight(); } }); |
| 184 | $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } }); |
The Android Open Source Project | dcc08f0 | 2008-12-17 18:03:49 -0800 | [diff] [blame^] | 185 | }); |
| 186 | |
| 187 | $(window).unload(function(){ |
| 188 | var href = location.href; |
| 189 | if (href.indexOf("/reference/") != -1) { |
| 190 | writeCookie("lastpage", href, "", null); |
| 191 | } else if (href.indexOf("/guide/") != -1) { |
| 192 | writeCookie("lastpage", href, "", null); |
| 193 | } |
| 194 | }); |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | function toggle(obj, slide) { |
| 200 | var ul = $("ul", obj); |
| 201 | var li = ul.parent(); |
| 202 | if (li.hasClass("closed")) { |
| 203 | if (slide) { |
| 204 | ul.slideDown("fast"); |
| 205 | } else { |
| 206 | ul.show(); |
| 207 | } |
| 208 | li.removeClass("closed"); |
| 209 | li.addClass("open"); |
| 210 | $(".toggle-img", li).attr("title", "hide pages"); |
| 211 | } else { |
| 212 | ul.slideUp("fast"); |
| 213 | li.removeClass("open"); |
| 214 | li.addClass("closed"); |
| 215 | $(".toggle-img", li).attr("title", "show pages"); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | function buildToggleLists() { |
| 222 | $(".toggle-list").each( |
| 223 | function(i) { |
| 224 | $("div", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>"); |
| 225 | $(this).addClass("closed"); |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | function getNavPref() { |
| 230 | var v = getCookie('reference_nav'); |
| 231 | if (v != NAV_PREF_TREE) { |
| 232 | v = NAV_PREF_PANELS; |
| 233 | } |
| 234 | return v; |
| 235 | } |
| 236 | |
| 237 | function chooseDefaultNav() { |
| 238 | nav_pref = getNavPref(); |
| 239 | if (nav_pref == NAV_PREF_TREE) { |
| 240 | $("#nav-panels").toggle(); |
| 241 | $("#panel-link").toggle(); |
| 242 | $("#nav-tree").toggle(); |
| 243 | $("#tree-link").toggle(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | function swapNav() { |
| 248 | if (nav_pref == NAV_PREF_TREE) { |
| 249 | nav_pref = NAV_PREF_PANELS; |
| 250 | } else { |
| 251 | nav_pref = NAV_PREF_TREE; |
| 252 | init_navtree("nav-tree", toRoot, NAVTREE_DATA); |
| 253 | } |
| 254 | var date = new Date(); |
| 255 | date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years |
| 256 | writeCookie("nav", nav_pref, "reference/", date.toGMTString()); |
| 257 | |
| 258 | $("#nav-panels").toggle(); |
| 259 | $("#panel-link").toggle(); |
| 260 | $("#nav-tree").toggle(); |
| 261 | $("#tree-link").toggle(); |
| 262 | |
| 263 | if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree"); |
| 264 | else { |
| 265 | scrollIntoView("packages-nav"); |
| 266 | scrollIntoView("classes-nav"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | function scrollIntoView(nav) { |
| 271 | var navObj = $("#"+nav); |
| 272 | if (navObj.is(':visible')) { |
| 273 | var selected = $(".selected", navObj); |
| 274 | if (selected.length == 0) return; |
| 275 | |
| 276 | var scrolling = document.getElementById(nav); |
| 277 | var navHeight = navObj.height(); |
| 278 | var offset = selected.position(); |
| 279 | if(offset.top > navHeight - 92) { |
| 280 | scrolling.scrollTop = offset.top - navHeight + 92; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |