script.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*!
  2. * Documenter 1.6
  3. * http://rxa.li/documenter
  4. *
  5. * Copyright 2011, Xaver Birsak
  6. * http://revaxarts.com
  7. *
  8. */
  9. //if Cufon replace headings
  10. if(typeof Cufon == 'function') Cufon.replace('h1, h2, h3, h4, h5, h6');
  11. $(document).ready(function() {
  12. var timeout,
  13. sections = new Array(),
  14. sectionscount = 0,
  15. win = $(window),
  16. sidebar = $('#documenter_sidebar'),
  17. nav = $('#documenter_nav'),
  18. logo = $('#documenter_logo'),
  19. navanchors = nav.find('a'),
  20. timeoffset = 50,
  21. hash = location.hash || null;
  22. iDeviceNotOS4 = (navigator.userAgent.match(/iphone|ipod|ipad/i) && !navigator.userAgent.match(/OS 5/i)) || false,
  23. badIE = $('html').prop('class').match(/ie(6|7|8)/)|| false;
  24. //handle external links (new window)
  25. $('a[href^=http]').bind('click',function(){
  26. window.open($(this).attr('href'));
  27. return false;
  28. });
  29. //IE 8 and lower doesn't like the smooth pagescroll
  30. if(!badIE){
  31. window.scroll(0,0);
  32. $('a[href^=#]').bind('click touchstart',function(){
  33. hash = $(this).attr('href');
  34. $.scrollTo.window().queue([]).stop();
  35. goTo(hash);
  36. return false;
  37. });
  38. //if a hash is set => go to it
  39. if(hash){
  40. setTimeout(function(){
  41. goTo(hash);
  42. },500);
  43. }
  44. }
  45. //We need the position of each section until the full page with all images is loaded
  46. win.bind('load',function(){
  47. var sectionselector = 'section';
  48. //Documentation has subcategories
  49. if(nav.find('ol').length){
  50. sectionselector = 'section, h4';
  51. }
  52. //saving some information
  53. $(sectionselector).each(function(i,e){
  54. var _this = $(this);
  55. var p = {
  56. id: this.id,
  57. pos: _this.offset().top
  58. };
  59. sections.push(p);
  60. });
  61. //iPhone, iPod and iPad don't trigger the scroll event
  62. if(iDeviceNotOS4){
  63. nav.find('a').bind('click',function(){
  64. setTimeout(function(){
  65. win.trigger('scroll');
  66. },duration);
  67. });
  68. //scroll to top
  69. window.scroll(0,0);
  70. }
  71. //how many sections
  72. sectionscount = sections.length;
  73. //bind the handler to the scroll event
  74. win.bind('scroll',function(event){
  75. clearInterval(timeout);
  76. //should occur with a delay
  77. timeout = setTimeout(function(){
  78. //get the position from the very top in all browsers
  79. pos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  80. //iDeviceNotOS4s don't know the fixed property so we fake it
  81. if(iDeviceNotOS4){
  82. sidebar.css({height:document.height});
  83. logo.css({'margin-top':pos});
  84. }
  85. //activate Nav element at the current position
  86. activateNav(pos);
  87. },timeoffset);
  88. }).trigger('scroll');
  89. });
  90. //the function is called when the hash changes
  91. function hashchange(){
  92. goTo(location.hash, false);
  93. }
  94. //scroll to a section and set the hash
  95. function goTo(hash,changehash){
  96. win.unbind('hashchange', hashchange);
  97. hash = hash.replace(/!\//,'');
  98. win.stop().scrollTo(hash,duration,{
  99. easing:easing,
  100. axis:'y'
  101. });
  102. if(changehash !== false){
  103. var l = location;
  104. location.href = (l.protocol+'//'+l.host+l.pathname+'#!/'+hash.substr(1));
  105. }
  106. win.bind('hashchange', hashchange);
  107. }
  108. //activate current nav element
  109. function activateNav(pos){
  110. var offset = 100,
  111. current, next, parent, isSub, hasSub;
  112. win.unbind('hashchange', hashchange);
  113. for(var i=sectionscount;i>0;i--){
  114. if(sections[i-1].pos <= pos+offset){
  115. navanchors.removeClass('current');
  116. current = navanchors.eq(i-1);
  117. current.addClass('current');
  118. parent = current.parent().parent();
  119. next = current.next();
  120. hasSub = next.is('ol');
  121. isSub = !parent.is('#documenter_nav');
  122. nav.find('ol:visible').not(parent).slideUp('fast');
  123. if(isSub){
  124. parent.prev().addClass('current');
  125. parent.stop().slideDown('fast');
  126. }else if(hasSub){
  127. next.stop().slideDown('fast');
  128. }
  129. win.bind('hashchange', hashchange);
  130. break;
  131. };
  132. }
  133. }
  134. });