common.js 4.0 KB

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