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