template.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  2. $.browser.ipad = /ipad/.test(navigator.userAgent.toLowerCase());
  3. /**
  4. * Initializes page contents for progressive enhancement.
  5. */
  6. function initializeContents()
  7. {
  8. // hide all more buttons because they are not needed with JS
  9. $(".element a.more").hide();
  10. $(".clickable.class,.clickable.interface,.clickable.trait").click(function() {
  11. document.location = $("a.more", this).attr('href');
  12. });
  13. // change the cursor to a pointer to make it more explicit that this it clickable
  14. // do a background color change on hover to emphasize the clickability eveb more
  15. // we do not use CSS for this because when JS is disabled this behaviour does not
  16. // apply and we do not want the hover
  17. $(".element.method,.element.function,.element.class.clickable,.element.interface.clickable,.element.trait.clickable,.element.property.clickable")
  18. .css("cursor", "pointer")
  19. .hover(function() {
  20. $(this).css('backgroundColor', '#F8FDF6')
  21. }, function(){
  22. $(this).css('backgroundColor', 'white')}
  23. );
  24. $("ul.side-nav.nav.nav-list li.nav-header").contents()
  25. .filter(function(){return this.nodeType == 3 && $.trim($(this).text()).length > 0})
  26. .wrap('<span class="side-nav-header" />');
  27. $("ul.side-nav.nav.nav-list li.nav-header span.side-nav-header")
  28. .css("cursor", "pointer");
  29. // do not show tooltips on iPad; it will cause the user having to click twice
  30. if (!$.browser.ipad) {
  31. $('.btn-group.visibility,.btn-group.view,.btn-group.type-filter,.icon-custom')
  32. .tooltip({'placement':'bottom'});
  33. $('.element').tooltip({'placement':'left'});
  34. }
  35. $('.btn-group.visibility,.btn-group.view,.btn-group.type-filter')
  36. .show()
  37. .css('display', 'inline-block')
  38. .find('button')
  39. .find('i').click(function(){ $(this).parent().click(); });
  40. // set the events for the visibility buttons and enable by default.
  41. $('.visibility button.public').click(function(){
  42. $('.element.public,.side-nav li.public').toggle($(this).hasClass('active'));
  43. }).click();
  44. $('.visibility button.protected').click(function(){
  45. $('.element.protected,.side-nav li.protected').toggle($(this).hasClass('active'));
  46. }).click();
  47. $('.visibility button.private').click(function(){
  48. $('.element.private,.side-nav li.private').toggle($(this).hasClass('active'));
  49. }).click();
  50. $('.visibility button.inherited').click(function(){
  51. $('.element.inherited,.side-nav li.inherited').toggle($(this).hasClass('active'));
  52. }).click();
  53. $('.type-filter button.critical').click(function(){
  54. $('tr.critical').toggle($(this).hasClass('active'));
  55. });
  56. $('.type-filter button.error').click(function(){
  57. $('tr.error').toggle($(this).hasClass('active'));
  58. });
  59. $('.type-filter button.notice').click(function(){
  60. $('tr.notice').toggle($(this).hasClass('active'));
  61. });
  62. $('.view button.details').click(function(){
  63. $('.side-nav li.view-simple').removeClass('view-simple');
  64. }).button('toggle').click();
  65. $('.view button.simple').click(function(){
  66. $('.side-nav li').addClass('view-simple');
  67. });
  68. $('ul.side-nav.nav.nav-list li.nav-header span.side-nav-header').click(function(){
  69. $(this).siblings('ul').collapse('toggle');
  70. });
  71. // sorting example
  72. // $('ol li').sort(
  73. // function(a, b) { return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1; }
  74. // ).appendTo('ol');
  75. }
  76. $(document).ready(function() {
  77. prettyPrint();
  78. initializeContents();
  79. // do not show tooltips on iPad; it will cause the user having to click twice
  80. if(!$.browser.ipad) {
  81. $(".side-nav a").tooltip({'placement': 'top'});
  82. }
  83. // chrome cannot deal with certain situations; warn the user about reduced features
  84. if ($.browser.chrome && (window.location.protocol == 'file:')) {
  85. $("body > .container").prepend(
  86. '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a>' +
  87. 'You are using Google Chrome in a local environment; AJAX interaction has been ' +
  88. 'disabled because Chrome cannot <a href="http://code.google.com/p/chromium/issues/detail?id=40787">' +
  89. 'retrieve files using Ajax</a>.</div>'
  90. );
  91. }
  92. $('ul.nav-namespaces li a, ul.nav-packages li a').click(function(){
  93. // Google Chrome does not do Ajax locally
  94. if ($.browser.chrome && (window.location.protocol == 'file:'))
  95. {
  96. return true;
  97. }
  98. $(this).parents('.side-nav').find('.active').removeClass('active');
  99. $(this).parent().addClass('active');
  100. $('div.namespace-contents').load(
  101. this.href + ' div.namespace-contents', function(){
  102. initializeContents();
  103. $(window).scrollTop($('div.namespace-contents').position().top);
  104. }
  105. );
  106. $('div.package-contents').load(
  107. this.href + ' div.package-contents', function(){
  108. initializeContents();
  109. $(window).scrollTop($('div.package-contents').position().top);
  110. }
  111. );
  112. return false;
  113. });
  114. function filterPath(string)
  115. {
  116. return string
  117. .replace(/^\//, '')
  118. .replace(/(index|default).[a-zA-Z]{3,4}$/, '')
  119. .replace(/\/$/, '');
  120. }
  121. var locationPath = filterPath(location.pathname);
  122. // the ipad already smoothly scrolls and does not detect the scrollable
  123. // element if top=0; as such we disable this behaviour for the iPad
  124. if (!$.browser.ipad) {
  125. $('a[href*=#]').each(function ()
  126. {
  127. var thisPath = filterPath(this.pathname) || locationPath;
  128. if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, ''))
  129. {
  130. var target = decodeURIComponent(this.hash.replace(/#/,''));
  131. // note: I'm using attribute selector, because id selector can't match elements with '$'
  132. var $target = $('[id="'+target+'"]');
  133. if ($target.length > 0)
  134. {
  135. $(this).click(function (event)
  136. {
  137. var scrollElem = scrollableElement('html', 'body');
  138. var targetOffset = $target.offset().top;
  139. event.preventDefault();
  140. $(scrollElem).animate({scrollTop:targetOffset}, 400, function ()
  141. {
  142. location.hash = target;
  143. });
  144. });
  145. }
  146. }
  147. });
  148. }
  149. // use the first element that is "scrollable"
  150. function scrollableElement(els)
  151. {
  152. for (var i = 0, argLength = arguments.length; i < argLength; i++)
  153. {
  154. var el = arguments[i], $scrollElement = $(el);
  155. if ($scrollElement.scrollTop() > 0)
  156. {
  157. return el;
  158. }
  159. else
  160. {
  161. $scrollElement.scrollTop(1);
  162. var isScrollable = $scrollElement.scrollTop() > 0;
  163. $scrollElement.scrollTop(0);
  164. if (isScrollable)
  165. {
  166. return el;
  167. }
  168. }
  169. }
  170. return [];
  171. }
  172. // Hide API Documentation menu if it's empty
  173. $('.nav .dropdown a[href=#api]').next().filter(function(el) {
  174. if ($(el).children().length == 0) {
  175. return true;
  176. }
  177. }).parent().hide();
  178. });