common.js 4.2 KB

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