1
0

_lang.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //= require ../lib/_jquery
  2. /*
  3. Copyright 2008-2013 Concur Technologies, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. not use this file except in compliance with the License. You may obtain
  6. a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. License for the specific language governing permissions and limitations
  12. under the License.
  13. */
  14. ;(function () {
  15. 'use strict';
  16. var languages = [];
  17. window.setupLanguages = setupLanguages;
  18. window.activateLanguage = activateLanguage;
  19. window.getLanguageFromQueryString = getLanguageFromQueryString;
  20. function activateLanguage(language) {
  21. if (!language) return;
  22. if (language === "") return;
  23. $(".lang-selector a").removeClass('active');
  24. $(".lang-selector a[data-language-name='" + language + "']").addClass('active');
  25. for (var i=0; i < languages.length; i++) {
  26. $(".highlight.tab-" + languages[i]).hide();
  27. $(".lang-specific." + languages[i]).hide();
  28. }
  29. $(".highlight.tab-" + language).show();
  30. $(".lang-specific." + language).show();
  31. window.recacheHeights();
  32. // scroll to the new location of the position
  33. if ($(window.location.hash).get(0)) {
  34. $(window.location.hash).get(0).scrollIntoView(true);
  35. }
  36. }
  37. // parseURL and stringifyURL are from https://github.com/sindresorhus/query-string
  38. // MIT licensed
  39. // https://github.com/sindresorhus/query-string/blob/7bee64c16f2da1a326579e96977b9227bf6da9e6/license
  40. function parseURL(str) {
  41. if (typeof str !== 'string') {
  42. return {};
  43. }
  44. str = str.trim().replace(/^(\?|#|&)/, '');
  45. if (!str) {
  46. return {};
  47. }
  48. return str.split('&').reduce(function (ret, param) {
  49. var parts = param.replace(/\+/g, ' ').split('=');
  50. var key = parts[0];
  51. var val = parts[1];
  52. key = decodeURIComponent(key);
  53. // missing `=` should be `null`:
  54. // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
  55. val = val === undefined ? null : decodeURIComponent(val);
  56. if (!ret.hasOwnProperty(key)) {
  57. ret[key] = val;
  58. } else if (Array.isArray(ret[key])) {
  59. ret[key].push(val);
  60. } else {
  61. ret[key] = [ret[key], val];
  62. }
  63. return ret;
  64. }, {});
  65. };
  66. function stringifyURL(obj) {
  67. return obj ? Object.keys(obj).sort().map(function (key) {
  68. var val = obj[key];
  69. if (Array.isArray(val)) {
  70. return val.sort().map(function (val2) {
  71. return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
  72. }).join('&');
  73. }
  74. return encodeURIComponent(key) + '=' + encodeURIComponent(val);
  75. }).join('&') : '';
  76. };
  77. // gets the language set in the query string
  78. function getLanguageFromQueryString() {
  79. if (location.search.length >= 1) {
  80. var language = parseURL(location.search).language;
  81. if (language) {
  82. return language;
  83. } else if (jQuery.inArray(location.search.substr(1), languages) != -1) {
  84. return location.search.substr(1);
  85. }
  86. }
  87. return false;
  88. }
  89. // returns a new query string with the new language in it
  90. function generateNewQueryString(language) {
  91. var url = parseURL(location.search);
  92. if (url.language) {
  93. url.language = language;
  94. return stringifyURL(url);
  95. }
  96. return language;
  97. }
  98. // if a button is clicked, add the state to the history
  99. function pushURL(language) {
  100. if (!history) { return; }
  101. var hash = window.location.hash;
  102. if (hash) {
  103. hash = hash.replace(/^#+/, '');
  104. }
  105. history.pushState({}, '', '?' + generateNewQueryString(language) + '#' + hash);
  106. // save language as next default
  107. if (localStorage) {
  108. localStorage.setItem("language", language);
  109. }
  110. }
  111. function setupLanguages(l) {
  112. var defaultLanguage = null;
  113. if (localStorage) {
  114. defaultLanguage = localStorage.getItem("language");
  115. }
  116. languages = l;
  117. var presetLanguage = getLanguageFromQueryString();
  118. if (presetLanguage) {
  119. // the language is in the URL, so use that language!
  120. activateLanguage(presetLanguage);
  121. if (localStorage) {
  122. localStorage.setItem("language", presetLanguage);
  123. }
  124. } else if ((defaultLanguage !== null) && (jQuery.inArray(defaultLanguage, languages) != -1)) {
  125. // the language was the last selected one saved in localstorage, so use that language!
  126. activateLanguage(defaultLanguage);
  127. } else {
  128. // no language selected, so use the default
  129. activateLanguage(languages[0]);
  130. }
  131. }
  132. // if we click on a language tab, activate that language
  133. $(function() {
  134. $(".lang-selector a").on("click", function() {
  135. var language = $(this).data("language-name");
  136. pushURL(language);
  137. activateLanguage(language);
  138. return false;
  139. });
  140. });
  141. })();