common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. $(document).ready(function () {
  2. initI18N();
  3. bindEvents();
  4. function initI18N() {
  5. var path = getCommonScriptPath();
  6. var lang = utils.getLanguage();
  7. var localPath = path + "./locales",
  8. file = "/resources.js";
  9. var filePathMap = {
  10. "en-US": localPath + "/en-US" + file,
  11. "zh-CN": localPath + "/zh-CN" + file
  12. };
  13. $.get(filePathMap[lang], function () {
  14. var examplesResourceURL = path + '../examples/locales/' + lang + '/resources.js';
  15. $.get(examplesResourceURL, function () {
  16. for (var name in window.webResources) {
  17. var subWeb = window.webResources[name];
  18. var subExamples = window.examplesResources[name];
  19. //重名以webResource为准
  20. if (typeof window.webResources[name] == 'object') {
  21. if (!subExamples) {
  22. subExamples = {};
  23. }
  24. for (var name1 in subWeb) {
  25. subExamples[name1] = subWeb[name1];
  26. }
  27. } else {
  28. subExamples[name1] = subWeb[name];
  29. }
  30. }
  31. window.resources = window.examplesResources;
  32. i18next
  33. .init({
  34. lng: lang,
  35. whitelist: ["zh-CN", "en-US"], //语言列表,跟locales下的目录名对应
  36. fallbackLng: "zh-CN" //默认语言
  37. }, function () {
  38. i18next.addResourceBundle(lang, 'translation', window.resources);
  39. localize();
  40. jqueryI18next.init(i18next, $);
  41. $(".nav").localize(); //翻译nav下所有的文档
  42. });
  43. setCurrentVersion();
  44. resetCurrentVersionLink();
  45. })
  46. })
  47. };
  48. //设置头部版本号
  49. function setCurrentVersion() {
  50. var version = getVersion();
  51. if (!version && window.preRelease) {
  52. version = window.preRelease;
  53. }
  54. var versionText = version ? "" + version : " ";
  55. $('#version').html(versionText);
  56. }
  57. //重置当前版本链接,不带版本号
  58. function resetCurrentVersionLink() {
  59. if (!window.version) {
  60. return;
  61. }
  62. var version = window.version;
  63. version = version.toString();
  64. $(".icl-nav-version").each(function (key, item) {
  65. if (item.href) {
  66. var reg = new RegExp("(.*)\/(" + version + ")(\/.*)");
  67. var match = item.href.match(reg);
  68. if (match && match[1] && match[3]) {
  69. item.href = match[1] + match[3];
  70. }
  71. }
  72. });
  73. }
  74. function bindEvents() {
  75. $('.icl-header').on('click', '.lang-option', function () {
  76. var value = $(this).data('lang');
  77. utils && utils.setLanguage(value);
  78. localize();
  79. });
  80. }
  81. function localize() {
  82. var lang = utils.getLanguage();
  83. var pathname = window.location.pathname.replace("/", "/");
  84. var href = window.location.origin + pathname;
  85. // if (lang === "en-US") {
  86. //
  87. // if (pathname === "/") {
  88. // href = window.location.origin + "/en/web/index.html"
  89. // } else {
  90. // if (getVersion()) {
  91. // href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '$1$2/en');
  92. // //href = window.location.origin + pathname.replace(/([^\/]*\/){2}([^\/]*)/, '/$1$2/en');
  93. // } else if (window.isLocal) {
  94. // href = window.location.origin + pathname.replace(/(([^\/]*\/){3})([^\/]*)/, '$1$3/en')
  95. // } else {
  96. // href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '/en/$2');
  97. // //href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '/$2/en');
  98. // }
  99. //
  100. // }
  101. // }
  102. if ((window.location.origin + window.location.pathname) === href) {
  103. return;
  104. }
  105. window.location = href;
  106. }
  107. function getVersion() {
  108. var pathname = window.location.pathname.replace("/", "/");
  109. var match = pathname.match(/^\/(dev|(?:\d+\.)+\d)\/.*/); //匹配版本:dev|9.0.0
  110. return match && match[1] ? match[1] : null;
  111. }
  112. window.getVersion = getVersion;
  113. function getCommonScriptPath() {
  114. var r = new RegExp("(^|(.*?\\/))(common\.js)(\\?|$)"),
  115. s = document.getElementsByTagName('script'),
  116. relativePath;
  117. for (var i = 0; i < s.length; i++) {
  118. var src = s[i].getAttribute('src');
  119. if (src) {
  120. var m = src.match(r);
  121. if (m) {
  122. relativePath = m[1] ? m[1].replace("js/", "") : "./";
  123. break;
  124. }
  125. }
  126. }
  127. return relativePath;
  128. }
  129. //监听nav滚动添加背景色
  130. $(window).scroll(function () { //开始监听滚动条
  131. var top = $(document).scrollTop(); //滚动条距离顶部的高度
  132. if (top > 1) {
  133. $("#nav")[0].style.backgroundColor = "#191515";
  134. } else {
  135. $("#nav")[0].style.backgroundColor = "";
  136. }
  137. })
  138. });