common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. $(function() {
  2. 'use strict';
  3. var defaultTranslation = 'en',
  4. translations = ['en','zh','es'];
  5. window.getLocale = function () {
  6. if (!localStorage) {
  7. return defaultTranslation;
  8. }
  9. if (location.search === '?locale=zh') {
  10. localStorage.locale = 'zh';
  11. }
  12. return localStorage.locale;
  13. };
  14. function main() {
  15. $(window).scroll(showGotoTop);
  16. $(window).resize(showGotoTop);
  17. $('.goto-top').click(function() {
  18. document.body.scrollTop = 0;
  19. document.documentElement.scrollTop = 0;
  20. return false;
  21. });
  22. initLocale();
  23. initScrollspy();
  24. showGotoTop();
  25. $('#bulletin').bulletin();
  26. }
  27. function initLocale() {
  28. var $locale = $('#locale');
  29. if (!localStorage) {
  30. $locale.hide();
  31. return;
  32. }
  33. switch (getLocale()) {
  34. case 'zh':
  35. setGlobalLanguage($locale, '简体中文', 'zh');
  36. break;
  37. case 'es':
  38. setGlobalLanguage($locale, 'Español', 'es');
  39. break;
  40. default:
  41. setGlobalLanguage($locale, 'English', 'en');
  42. break;
  43. };
  44. $('[data-locale]').click(function () {
  45. localStorage.locale = $(this).data('locale');
  46. location.reload(true);
  47. });
  48. }
  49. function setGlobalLanguage($locale, text, languageCode) {
  50. for(i = 0; i < translations.length; i++) {
  51. if(translations[i] !== languageCode) {
  52. $locale.find('[data-locale="'+ translations[i] + '"]').removeClass('active').end()
  53. .find('.dropdown-toggle img').removeClass('flag-'+ translations[i] +'').end();
  54. }
  55. }
  56. $locale.find('.language').text(text).end()
  57. .find('[data-locale="'+languageCode + '"]').addClass('active').end()
  58. .find('.dropdown-toggle img').addClass('flag-'+ languageCode +'').end();
  59. $('[data-'+ languageCode + ']').each(function () {
  60. $(this).html($(this).data(languageCode));
  61. });
  62. }
  63. function initScrollspy() {
  64. var $window = $(window),
  65. $body = $(document.body),
  66. html = [];
  67. $('.page-header').find('h1, h2').each(function (i) {
  68. var $this = $(this),
  69. parent = $this.is('h1'),
  70. link = '<a href="#' + $this.attr('id') + '">' + $.trim($this.text()) + '</a>';
  71. if (parent) {
  72. if (i > 0) {
  73. html.push('</ul></li>');
  74. }
  75. html.push('<li>', link, '<ul class="nav">');
  76. } else {
  77. html.push('<li>', link, '</li>');
  78. }
  79. });
  80. html.push('</ul></li>');
  81. $('.bs-sidenav').html(html.join(''));
  82. $body.scrollspy({
  83. target: '.bs-sidebar',
  84. offset: $('.navbar').outerHeight(true) + 10
  85. });
  86. $body.scrollspy('refresh');
  87. // affix
  88. setTimeout(function () {
  89. var $sideBar = $('.bs-sidebar');
  90. $sideBar.affix({
  91. offset: {
  92. top: function () {
  93. var offsetTop = $sideBar.offset().top;
  94. var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10);
  95. var navOuterHeight = $('.bs-docs-nav').height();
  96. return (this.top = offsetTop - navOuterHeight - sideBarMargin);
  97. },
  98. bottom: function () {
  99. return (this.bottom = $('.bs-footer').outerHeight(true));
  100. }
  101. }
  102. });
  103. }, 100);
  104. }
  105. function showGotoTop() {
  106. var $gotoTop = $('.goto-top'),
  107. $bdshare = $('#bdshare');
  108. if ($(document).scrollTop() > 0) {
  109. $gotoTop.fadeIn('slow');
  110. $bdshare.fadeOut('slow');
  111. } else {
  112. $gotoTop.fadeOut('slow');
  113. $bdshare.fadeIn('slow');
  114. }
  115. }
  116. main();
  117. });