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 |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 13 | var isIE6 = false; // true if IE6 |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 14 | |
Scott Main | f8af887 | 2009-12-02 20:59:40 -0800 | [diff] [blame] | 15 | // TODO: use $(document).ready instead |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 16 | function 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 Main | d8115ff | 2009-11-03 18:54:26 -0800 | [diff] [blame] | 28 | var agent = navigator['userAgent'].toLowerCase(); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 29 | // If a mobile phone, set flag and do mobile setup |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 30 | if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod |
Scott Main | d8115ff | 2009-11-03 18:54:26 -0800 | [diff] [blame] | 31 | (agent.indexOf("blackberry") != -1) || |
| 32 | (agent.indexOf("webos") != -1) || |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 33 | (agent.indexOf("mini") != -1)) { // opera mini browsers |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 34 | isMobile = true; |
| 35 | addLoadEvent(mobileSetup); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 36 | // If not a mobile browser, set the onresize event for IE6, and others |
Scott Main | d8115ff | 2009-11-03 18:54:26 -0800 | [diff] [blame] | 37 | } else if (agent.indexOf("msie 6") != -1) { |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 38 | isIE6 = true; |
| 39 | addLoadEvent(function() { |
| 40 | window.onresize = resizeAll; |
| 41 | }); |
| 42 | } else { |
| 43 | addLoadEvent(function() { |
| 44 | window.onresize = resizeHeight; |
| 45 | }); |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 46 | } |
| 47 | |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 48 | function 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 Main | 25fda19 | 2009-08-04 11:26:30 -0700 | [diff] [blame] | 57 | /* loads the lists.js file to the page. |
| 58 | Loading this in the head was slowing page load time */ |
| 59 | addLoadEvent( function() { |
| 60 | var lists = document.createElement("script"); |
| 61 | lists.setAttribute("type","text/javascript"); |
| 62 | lists.setAttribute("src", toRoot+"reference/lists.js"); |
Scott Main | 1da8238 | 2009-09-17 10:54:05 -0700 | [diff] [blame] | 63 | document.getElementsByTagName("head")[0].appendChild(lists); |
Scott Main | 25fda19 | 2009-08-04 11:26:30 -0700 | [diff] [blame] | 64 | } ); |
| 65 | |
Scott Main | f8af887 | 2009-12-02 20:59:40 -0800 | [diff] [blame] | 66 | addLoadEvent( function() { |
Scott Main | 945ed36 | 2010-01-15 17:30:38 -0800 | [diff] [blame] | 67 | $("pre:not(.no-pretty-print)").addClass("prettyprint"); |
Scott Main | f8af887 | 2009-12-02 20:59:40 -0800 | [diff] [blame] | 68 | prettyPrint(); |
| 69 | } ); |
| 70 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 71 | function setToRoot(root) { |
| 72 | toRoot = root; |
| 73 | // note: toRoot also used by carousel.js |
| 74 | } |
| 75 | |
| 76 | function restoreWidth(navWidth) { |
| 77 | var windowWidth = $(window).width() + "px"; |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 78 | 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 Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | sidenav.css({width:navWidth}); |
| 85 | resizePackagesNav.css({width:navWidth}); |
| 86 | classesNav.css({width:navWidth}); |
| 87 | $("#packages-nav").css({width:navWidth}); |
| 88 | } |
| 89 | |
| 90 | function 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 Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 103 | function readCookie(cookie) { |
| 104 | var myCookie = cookie_namespace+"_"+cookie+"="; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 105 | 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 Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 120 | function writeCookie(cookie, val, section, expiration) { |
Scott Main | 9b5fdb9 | 2009-10-27 15:09:15 -0700 | [diff] [blame] | 121 | if (val==undefined) return; |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 122 | 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 Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 127 | } |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 128 | document.cookie = cookie_namespace + section + cookie + "=" + val + "; expires=" + expiration+"; path=/"; |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 129 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | |
| 131 | function init() { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 132 | $("#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 Main | a39d3bb | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 139 | var cookiePath = ""; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 140 | if (location.href.indexOf("/reference/") != -1) { |
Scott Main | a39d3bb | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 141 | cookiePath = "reference_"; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | } else if (location.href.indexOf("/guide/") != -1) { |
Scott Main | a39d3bb | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 143 | cookiePath = "guide_"; |
Dirk Dougherty | 502c498 | 2009-12-02 18:01:16 -0800 | [diff] [blame] | 144 | } else if (location.href.indexOf("/resources/") != -1) { |
Scott Main | a39d3bb | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 145 | cookiePath = "resources_"; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 146 | } |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 147 | |
| 148 | if (!isMobile) { |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 149 | $("#resize-packages-nav").resizable({handles: "s", resize: function(e, ui) { resizePackagesHeight(); } }); |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 150 | $(".side-nav-resizable").resizable({handles: "e", resize: function(e, ui) { resizeWidth(); } }); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 151 | var cookieWidth = readCookie(cookiePath+'width'); |
| 152 | var cookieHeight = readCookie(cookiePath+'height'); |
The Android Open Source Project | fdd3a10 | 2009-03-11 12:11:54 -0700 | [diff] [blame] | 153 | if (cookieWidth) { |
| 154 | restoreWidth(cookieWidth); |
| 155 | } else if ($(".side-nav-resizable").length) { |
| 156 | resizeWidth(); |
| 157 | } |
| 158 | if (cookieHeight) { |
| 159 | restoreHeight(cookieHeight); |
| 160 | } else { |
| 161 | resizeHeight(); |
| 162 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 165 | if (devdocNav.length) { // only dev guide, resources, and sdk |
| 166 | tryPopulateResourcesNav(); |
| 167 | highlightNav(location.href); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 171 | function tryPopulateResourcesNav() { |
| 172 | var sampleList = $('#devdoc-nav-sample-list'); |
| 173 | var articleList = $('#devdoc-nav-article-list'); |
| 174 | var tutorialList = $('#devdoc-nav-tutorial-list'); |
| 175 | var topicList = $('#devdoc-nav-topic-list'); |
| 176 | |
| 177 | if (!topicList.length || !ANDROID_TAGS || !ANDROID_RESOURCES) |
| 178 | return; |
| 179 | |
| 180 | var topics = []; |
| 181 | for (var topic in ANDROID_TAGS['topic']) { |
| 182 | topics.push({name:topic,title:ANDROID_TAGS['topic'][topic]}); |
| 183 | } |
| 184 | topics.sort(function(x,y){ return (x.title < y.title) ? -1 : 1; }); |
| 185 | for (var i = 0; i < topics.length; i++) { |
| 186 | topicList.append( |
| 187 | $('<li>').append( |
| 188 | $('<a>') |
| 189 | .attr('href', toRoot + "resources/browser.html?tag=" + topics[i].name) |
| 190 | .append($('<span>') |
| 191 | .addClass('en') |
| 192 | .html(topics[i].title) |
| 193 | ) |
| 194 | ) |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | var _renderResourceList = function(tag, listNode) { |
| 199 | var resources = []; |
| 200 | var tags; |
| 201 | var resource; |
| 202 | var i, j; |
| 203 | for (i = 0; i < ANDROID_RESOURCES.length; i++) { |
| 204 | resource = ANDROID_RESOURCES[i]; |
| 205 | tags = resource.tags || []; |
| 206 | var hasTag = false; |
| 207 | for (j = 0; j < tags.length; j++) |
| 208 | if (tags[j] == tag) { |
| 209 | hasTag = true; |
| 210 | break; |
| 211 | } |
| 212 | if (!hasTag) |
| 213 | continue; |
| 214 | resources.push(resource); |
| 215 | } |
| 216 | //resources.sort(function(x,y){ return (x.title.en < y.title.en) ? -1 : 1; }); |
| 217 | for (i = 0; i < resources.length; i++) { |
| 218 | resource = resources[i]; |
| 219 | var listItemNode = $('<li>').append( |
| 220 | $('<a>') |
| 221 | .attr('href', toRoot + "resources/" + resource.path) |
| 222 | .append($('<span>') |
| 223 | .addClass('en') |
| 224 | .html(resource.title.en) |
| 225 | ) |
| 226 | ); |
| 227 | tags = resource.tags || []; |
| 228 | for (j = 0; j < tags.length; j++) { |
| 229 | if (tags[j] == 'new') { |
| 230 | listItemNode.get(0).innerHTML += ' <span class="new">new!</span>'; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | listNode.append(listItemNode); |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | _renderResourceList('sample', sampleList); |
| 239 | _renderResourceList('article', articleList); |
| 240 | _renderResourceList('tutorial', tutorialList); |
| 241 | } |
| 242 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 243 | function highlightNav(fullPageName) { |
| 244 | var lastSlashPos = fullPageName.lastIndexOf("/"); |
Dirk Dougherty | 502c498 | 2009-12-02 18:01:16 -0800 | [diff] [blame] | 245 | var firstSlashPos; |
| 246 | if (fullPageName.indexOf("/guide/") != -1) { |
| 247 | firstSlashPos = fullPageName.indexOf("/guide/"); |
| 248 | } else if (fullPageName.indexOf("/sdk/") != -1) { |
| 249 | firstSlashPos = fullPageName.indexOf("/sdk/"); |
| 250 | } else { |
| 251 | firstSlashPos = fullPageName.indexOf("/resources/"); |
| 252 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 253 | if (lastSlashPos == (fullPageName.length - 1)) { // if the url ends in slash (add 'index.html') |
| 254 | fullPageName = fullPageName + "index.html"; |
| 255 | } |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 256 | // First check if the exact URL, with query string and all, is in the navigation menu |
| 257 | var pathPageName = fullPageName.substr(firstSlashPos); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 258 | var link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 259 | if (link.length == 0) { |
| 260 | var htmlPos = fullPageName.lastIndexOf(".html", fullPageName.length); |
| 261 | pathPageName = fullPageName.slice(firstSlashPos, htmlPos + 5); // +5 advances past ".html" |
| 262 | link = $("#devdoc-nav a[href$='"+ pathPageName+"']"); |
| 263 | if ((link.length == 0) && ((fullPageName.indexOf("/guide/") != -1) || (fullPageName.indexOf("/resources/") != -1))) { |
| 264 | // if there's no match, then let's backstep through the directory until we find an index.html page |
| 265 | // that matches our ancestor directories (only for dev guide and resources) |
| 266 | lastBackstep = pathPageName.lastIndexOf("/"); |
| 267 | while (link.length == 0) { |
| 268 | backstepDirectory = pathPageName.lastIndexOf("/", lastBackstep); |
| 269 | link = $("#devdoc-nav a[href$='"+ pathPageName.slice(0, backstepDirectory + 1)+"index.html']"); |
| 270 | lastBackstep = pathPageName.lastIndexOf("/", lastBackstep - 1); |
| 271 | if (lastBackstep == 0) break; |
| 272 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
Scott Main | ee629d9 | 2010-02-05 14:30:08 -0800 | [diff] [blame] | 275 | |
| 276 | // add 'selected' to the <li> or <div> that wraps this <a> |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 277 | link.parent().addClass('selected'); |
Scott Main | ee629d9 | 2010-02-05 14:30:08 -0800 | [diff] [blame] | 278 | |
| 279 | // if we're in a toggleable root link (<li class=toggle-list><div><a>) |
| 280 | if (link.parent().parent().hasClass('toggle-list')) { |
| 281 | toggle(link.parent().parent(), false); // open our own list |
| 282 | // then also check if we're in a third-level nested list that's toggleable |
| 283 | if (link.parent().parent().parent().is(':hidden')) { |
| 284 | toggle(link.parent().parent().parent().parent(), false); // open the super parent list |
| 285 | } |
| 286 | } |
| 287 | // if we're in a normal nav link (<li><a>) and the parent <ul> is hidden |
| 288 | else if (link.parent().parent().is(':hidden')) { |
| 289 | toggle(link.parent().parent().parent(), false); // open the parent list |
| 290 | // then also check if the parent list is also nested in a hidden list |
| 291 | if (link.parent().parent().parent().parent().is(':hidden')) { |
| 292 | toggle(link.parent().parent().parent().parent().parent(), false); // open the super parent list |
| 293 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 297 | /* Resize the height of the nav panels in the reference, |
| 298 | * and save the new size to a cookie */ |
| 299 | function resizePackagesHeight() { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 300 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 301 | var swapperHeight = windowHeight - 13; // move 13px for swapper link at the bottom |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 302 | resizePackagesNav.css({maxHeight:swapperHeight + "px"}); |
| 303 | classesNav.css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 304 | |
| 305 | $("#swapper").css({height:swapperHeight + "px"}); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 306 | $("#packages-nav").css({height:parseInt(resizePackagesNav.css("height")) - 6 + "px"}); //move 6px for handle |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 307 | |
Dirk Dougherty | c13d045 | 2009-07-08 14:39:31 -0700 | [diff] [blame] | 308 | var basePath = getBaseUri(location.pathname); |
| 309 | var section = basePath.substring(1,basePath.indexOf("/",1)); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 310 | writeCookie("height", resizePackagesNav.css("height"), section, null); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 313 | /* Resize the height of the side-nav and doc-content divs, |
| 314 | * which creates the frame effect */ |
| 315 | function resizeHeight() { |
Scott Main | 13b79b2 | 2010-01-12 13:37:00 -0800 | [diff] [blame] | 316 | var docContent = $("#doc-content"); |
| 317 | |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 318 | // Get the window height and always resize the doc-content and side-nav divs |
| 319 | var windowHeight = ($(window).height() - HEADER_HEIGHT); |
Scott Main | 13b79b2 | 2010-01-12 13:37:00 -0800 | [diff] [blame] | 320 | docContent.css({height:windowHeight + "px"}); |
| 321 | $("#side-nav").css({height:windowHeight + "px"}); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 322 | |
| 323 | var href = location.href; |
| 324 | // If in the reference docs, also resize the "swapper", "classes-nav", and "nav-tree" divs |
| 325 | if (href.indexOf("/reference/") != -1) { |
| 326 | var swapperHeight = windowHeight - 13; |
| 327 | $("#swapper").css({height:swapperHeight + "px"}); |
| 328 | $("#classes-nav").css({height:swapperHeight - parseInt(resizePackagesNav.css("height")) + "px"}); |
| 329 | $("#nav-tree").css({height:swapperHeight + "px"}); |
| 330 | |
| 331 | // If in the dev guide docs, also resize the "devdoc-nav" div |
| 332 | } else if (href.indexOf("/guide/") != -1) { |
| 333 | $("#devdoc-nav").css({height:sidenav.css("height")}); |
Dirk Dougherty | 502c498 | 2009-12-02 18:01:16 -0800 | [diff] [blame] | 334 | } else if (href.indexOf("/resources/") != -1) { |
| 335 | $("#devdoc-nav").css({height:sidenav.css("height")}); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 336 | } |
Scott Main | 13b79b2 | 2010-01-12 13:37:00 -0800 | [diff] [blame] | 337 | |
| 338 | // Hide the "Go to top" link if there's no vertical scroll |
| 339 | if ( parseInt($("#jd-content").css("height")) <= parseInt(docContent.css("height")) ) { |
| 340 | $("a[href='#top']").css({'display':'none'}); |
| 341 | } else { |
| 342 | $("a[href='#top']").css({'display':'inline'}); |
| 343 | } |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* Resize the width of the "side-nav" and the left margin of the "doc-content" div, |
| 347 | * which creates the resizable side bar */ |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 348 | function resizeWidth() { |
| 349 | var windowWidth = $(window).width() + "px"; |
| 350 | if (sidenav.length) { |
| 351 | var sidenavWidth = sidenav.css("width"); |
| 352 | } else { |
| 353 | var sidenavWidth = 0; |
| 354 | } |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 355 | content.css({marginLeft:parseInt(sidenavWidth) + 6 + "px"}); //account for 6px-wide handle-bar |
| 356 | |
| 357 | if (isIE6) { |
| 358 | content.css({width:parseInt(windowWidth) - parseInt(sidenavWidth) - 6 + "px"}); // necessary in order to for scrollbars to be visible |
| 359 | } |
| 360 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 361 | resizePackagesNav.css({width:sidenavWidth}); |
| 362 | classesNav.css({width:sidenavWidth}); |
| 363 | $("#packages-nav").css({width:sidenavWidth}); |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 364 | |
Scott Main | b6da480 | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 365 | if ($(".side-nav-resizable").length) { // Must check if the nav is resizable because IE6 calls resizeWidth() from resizeAll() for all pages |
| 366 | var basePath = getBaseUri(location.pathname); |
| 367 | var section = basePath.substring(1,basePath.indexOf("/",1)); |
| 368 | writeCookie("width", sidenavWidth, section, null); |
| 369 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 372 | /* For IE6 only, |
| 373 | * because it can't properly perform auto width for "doc-content" div, |
| 374 | * avoiding this for all browsers provides better performance */ |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 375 | function resizeAll() { |
Scott Main | 3f7d1df | 2009-11-02 15:31:04 -0800 | [diff] [blame] | 376 | resizeHeight(); |
Scott Main | b6da480 | 2010-01-06 14:18:02 -0800 | [diff] [blame] | 377 | resizeWidth(); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 380 | function getBaseUri(uri) { |
Dirk Dougherty | efdcda4 | 2009-07-15 16:41:48 -0700 | [diff] [blame] | 381 | var intlUrl = (uri.substring(0,6) == "/intl/"); |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 382 | if (intlUrl) { |
| 383 | base = uri.substring(uri.indexOf('intl/')+5,uri.length); |
| 384 | base = base.substring(base.indexOf('/')+1, base.length); |
| 385 | //alert("intl, returning base url: /" + base); |
| 386 | return ("/" + base); |
| 387 | } else { |
| 388 | //alert("not intl, returning uri as found."); |
| 389 | return uri; |
| 390 | } |
| 391 | } |
| 392 | |
Dirk Dougherty | efdcda4 | 2009-07-15 16:41:48 -0700 | [diff] [blame] | 393 | function requestAppendHL(uri) { |
| 394 | //append "?hl=<lang> to an outgoing request (such as to blog) |
| 395 | var lang = getLangPref(); |
| 396 | if (lang) { |
| 397 | var q = 'hl=' + lang; |
| 398 | uri += '?' + q; |
| 399 | window.location = uri; |
| 400 | return false; |
| 401 | } else { |
| 402 | return true; |
| 403 | } |
| 404 | } |
| 405 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 406 | function loadLast(cookiePath) { |
| 407 | var location = window.location.href; |
| 408 | if (location.indexOf("/"+cookiePath+"/") != -1) { |
| 409 | return true; |
| 410 | } |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 411 | var lastPage = readCookie(cookiePath + "_lastpage"); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 412 | if (lastPage) { |
| 413 | window.location = lastPage; |
| 414 | return false; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | $(window).unload(function(){ |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 420 | var path = getBaseUri(location.pathname); |
| 421 | if (path.indexOf("/reference/") != -1) { |
| 422 | writeCookie("lastpage", path, "reference", null); |
| 423 | } else if (path.indexOf("/guide/") != -1) { |
| 424 | writeCookie("lastpage", path, "guide", null); |
Dirk Dougherty | 502c498 | 2009-12-02 18:01:16 -0800 | [diff] [blame] | 425 | } else if (path.indexOf("/resources/") != -1) { |
| 426 | writeCookie("lastpage", path, "resources", null); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 427 | } |
| 428 | }); |
| 429 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 430 | function toggle(obj, slide) { |
Scott Main | ee629d9 | 2010-02-05 14:30:08 -0800 | [diff] [blame] | 431 | var ul = $("ul:first", obj); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 432 | var li = ul.parent(); |
| 433 | if (li.hasClass("closed")) { |
| 434 | if (slide) { |
| 435 | ul.slideDown("fast"); |
| 436 | } else { |
| 437 | ul.show(); |
| 438 | } |
| 439 | li.removeClass("closed"); |
| 440 | li.addClass("open"); |
| 441 | $(".toggle-img", li).attr("title", "hide pages"); |
| 442 | } else { |
| 443 | ul.slideUp("fast"); |
| 444 | li.removeClass("open"); |
| 445 | li.addClass("closed"); |
| 446 | $(".toggle-img", li).attr("title", "show pages"); |
| 447 | } |
| 448 | } |
| 449 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 450 | function buildToggleLists() { |
| 451 | $(".toggle-list").each( |
| 452 | function(i) { |
Scott Main | ee629d9 | 2010-02-05 14:30:08 -0800 | [diff] [blame] | 453 | $("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 Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 454 | $(this).addClass("closed"); |
| 455 | }); |
| 456 | } |
| 457 | |
| 458 | function getNavPref() { |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 459 | var v = readCookie('reference_nav'); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 460 | if (v != NAV_PREF_TREE) { |
| 461 | v = NAV_PREF_PANELS; |
| 462 | } |
| 463 | return v; |
| 464 | } |
| 465 | |
| 466 | function chooseDefaultNav() { |
| 467 | nav_pref = getNavPref(); |
| 468 | if (nav_pref == NAV_PREF_TREE) { |
| 469 | $("#nav-panels").toggle(); |
| 470 | $("#panel-link").toggle(); |
| 471 | $("#nav-tree").toggle(); |
| 472 | $("#tree-link").toggle(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | function swapNav() { |
| 477 | if (nav_pref == NAV_PREF_TREE) { |
| 478 | nav_pref = NAV_PREF_PANELS; |
| 479 | } else { |
| 480 | nav_pref = NAV_PREF_TREE; |
Dirk Dougherty | 3b1ee37 | 2009-07-29 12:25:44 -0700 | [diff] [blame] | 481 | init_default_navtree(toRoot); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 482 | } |
| 483 | var date = new Date(); |
| 484 | 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] | 485 | writeCookie("nav", nav_pref, "reference", date.toGMTString()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 486 | |
| 487 | $("#nav-panels").toggle(); |
| 488 | $("#panel-link").toggle(); |
| 489 | $("#nav-tree").toggle(); |
| 490 | $("#tree-link").toggle(); |
| 491 | |
| 492 | if ($("#nav-tree").is(':visible')) scrollIntoView("nav-tree"); |
| 493 | else { |
| 494 | scrollIntoView("packages-nav"); |
| 495 | scrollIntoView("classes-nav"); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | function scrollIntoView(nav) { |
| 500 | var navObj = $("#"+nav); |
| 501 | if (navObj.is(':visible')) { |
| 502 | var selected = $(".selected", navObj); |
| 503 | if (selected.length == 0) return; |
| 504 | if (selected.is("div")) selected = selected.parent(); |
| 505 | |
| 506 | var scrolling = document.getElementById(nav); |
| 507 | var navHeight = navObj.height(); |
| 508 | var offsetTop = selected.position().top; |
| 509 | if (selected.parent().parent().is(".toggle-list")) offsetTop += selected.parent().parent().position().top; |
| 510 | if(offsetTop > navHeight - 92) { |
| 511 | scrolling.scrollTop = offsetTop - navHeight + 92; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 516 | function changeTabLang(lang) { |
| 517 | var nodes = $("#header-tabs").find("."+lang); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 518 | for (i=0; i < nodes.length; i++) { // for each node in this language |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 519 | var node = $(nodes[i]); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 520 | node.siblings().css("display","none"); // hide all siblings |
| 521 | if (node.not(":empty").length != 0) { //if this languages node has a translation, show it |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 522 | node.css("display","inline"); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 523 | } else { //otherwise, show English instead |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 524 | node.css("display","none"); |
| 525 | node.siblings().filter(".en").css("display","inline"); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | function changeNavLang(lang) { |
| 531 | var nodes = $("#side-nav").find("."+lang); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 532 | for (i=0; i < nodes.length; i++) { // for each node in this language |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 533 | var node = $(nodes[i]); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 534 | node.siblings().css("display","none"); // hide all siblings |
| 535 | if (node.not(":empty").length != 0) { // if this languages node has a translation, show it |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 536 | node.css("display","inline"); |
Roman Nurik | 78eff9f | 2010-03-01 18:35:13 -0800 | [diff] [blame] | 537 | } else { // otherwise, show English instead |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 538 | node.css("display","none"); |
| 539 | node.siblings().filter(".en").css("display","inline"); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | function changeDocLang(lang) { |
| 545 | changeTabLang(lang); |
| 546 | changeNavLang(lang); |
| 547 | } |
| 548 | |
| 549 | function changeLangPref(lang, refresh) { |
| 550 | var date = new Date(); |
| 551 | expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000))); // keep this for 50 years |
| 552 | //alert("expires: " + expires) |
| 553 | writeCookie("pref_lang", lang, null, expires); |
| 554 | //changeDocLang(lang); |
| 555 | if (refresh) { |
| 556 | l = getBaseUri(location.pathname); |
| 557 | window.location = l; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | function loadLangPref() { |
| 562 | var lang = readCookie("pref_lang"); |
| 563 | if (lang != 0) { |
| 564 | $("#language").find("option[value='"+lang+"']").attr("selected",true); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | function getLangPref() { |
Dirk Dougherty | efdcda4 | 2009-07-15 16:41:48 -0700 | [diff] [blame] | 569 | var lang = $("#language").find(":selected").attr("value"); |
| 570 | if (!lang) { |
| 571 | lang = readCookie("pref_lang"); |
| 572 | } |
| 573 | return (lang != 0) ? lang : 'en'; |
Dirk Dougherty | 4405a23 | 2009-07-07 17:43:27 -0700 | [diff] [blame] | 574 | } |
Scott Main | 462cc37 | 2009-10-23 16:19:37 -0700 | [diff] [blame] | 575 | |
| 576 | |
| 577 | function toggleContent(obj) { |
| 578 | var button = $(obj); |
| 579 | var div = $(obj.parentNode); |
| 580 | var toggleMe = $(".toggle-content-toggleme",div); |
| 581 | if (button.hasClass("show")) { |
| 582 | toggleMe.slideDown(); |
| 583 | button.removeClass("show").addClass("hide"); |
| 584 | } else { |
| 585 | toggleMe.slideUp(); |
| 586 | button.removeClass("hide").addClass("show"); |
| 587 | } |
| 588 | $("span", button).toggle(); |
| 589 | } |