switchers.js.in 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. NOTE FOR RELEASE MAINTAINERS:
  3. This file only needs updating in the development release ("master" branch)
  4. When documentation for stable releases is built,
  5. the latest version from "master" is used
  6. by https://git.yoctoproject.org/yocto-autobuilder-helper/tree/scripts/run-docs-build
  7. */
  8. (function() {
  9. 'use strict';
  10. var all_releases =
  11. ALL_RELEASES_PLACEHOLDER
  12. ;
  13. var switcher_versions = {
  14. VERSIONS_PLACEHOLDER
  15. };
  16. var all_doctypes = {
  17. 'single': 'Individual Webpages',
  18. 'mega': "All-in-one 'Mega' Manual",
  19. };
  20. // Simple version comparision
  21. // Return 1 if a > b
  22. // Return -1 if a < b
  23. // Return 0 if a == b
  24. function ver_compare(a, b) {
  25. if (a == "dev") {
  26. return 1;
  27. }
  28. if (a === b) {
  29. return 0;
  30. }
  31. var a_components = a.split(".");
  32. var b_components = b.split(".");
  33. var len = Math.min(a_components.length, b_components.length);
  34. // loop while the components are equal
  35. for (var i = 0; i < len; i++) {
  36. // A bigger than B
  37. if (parseInt(a_components[i]) > parseInt(b_components[i])) {
  38. return 1;
  39. }
  40. // B bigger than A
  41. if (parseInt(a_components[i]) < parseInt(b_components[i])) {
  42. return -1;
  43. }
  44. }
  45. // If one's a prefix of the other, the longer one is greater.
  46. if (a_components.length > b_components.length) {
  47. return 1;
  48. }
  49. if (a_components.length < b_components.length) {
  50. return -1;
  51. }
  52. // Otherwise they are the same.
  53. return 0;
  54. }
  55. function build_version_select(current_series, current_version) {
  56. var buf = ['<select>'];
  57. $.each(switcher_versions, function(version, vers_data) {
  58. var series = version.substr(0, 3);
  59. if (series == current_series) {
  60. if (version == current_version)
  61. buf.push('<option value="' + version + '" selected="selected">' + vers_data["title"] + '</option>');
  62. else
  63. buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
  64. } else {
  65. buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
  66. }
  67. });
  68. buf.push('</select>');
  69. return buf.join('');
  70. }
  71. function build_doctype_select(current_doctype) {
  72. var buf = ['<select>'];
  73. $.each(all_doctypes, function(doctype, title) {
  74. if (doctype == current_doctype)
  75. buf.push('<option value="' + doctype + '" selected="selected">' +
  76. all_doctypes[current_doctype] + '</option>');
  77. else
  78. buf.push('<option value="' + doctype + '">' + title + '</option>');
  79. });
  80. if (!(current_doctype in all_doctypes)) {
  81. // In case we're browsing a doctype that is not yet in all_doctypes.
  82. buf.push('<option value="' + current_doctype + '" selected="selected">' +
  83. current_doctype + '</option>');
  84. all_doctypes[current_doctype] = current_doctype;
  85. }
  86. buf.push('</select>');
  87. return buf.join('');
  88. }
  89. function navigate_to_first_existing(urls) {
  90. // Navigate to the first existing URL in urls.
  91. var url = urls.shift();
  92. // Web browsers won't redirect file:// urls to file urls using ajax but
  93. // its useful for local testing
  94. if (url.startsWith("file://")) {
  95. window.location.href = url;
  96. return;
  97. }
  98. if (urls.length == 0) {
  99. window.location.href = url;
  100. return;
  101. }
  102. $.ajax({
  103. url: url,
  104. success: function() {
  105. window.location.href = url;
  106. },
  107. error: function() {
  108. navigate_to_first_existing(urls);
  109. }
  110. });
  111. }
  112. function get_docroot_url() {
  113. var url = window.location.href;
  114. // Try to get the variable from documentation_options.js
  115. var root = DOCUMENTATION_OPTIONS.URL_ROOT;
  116. if (root == null) {
  117. // In recent versions of Sphinx, URL_ROOT was removed from
  118. // documentation_options.js, so get it like searchtools.js does.
  119. root = document.documentElement.dataset.content_root;
  120. }
  121. var urlarray = url.split('/');
  122. // Trim off anything after '/'
  123. urlarray.pop();
  124. var depth = (root.match(/\.\.\//g) || []).length;
  125. for (var i = 0; i < depth; i++) {
  126. urlarray.pop();
  127. }
  128. return urlarray.join('/') + '/';
  129. }
  130. function on_version_switch() {
  131. var selected_version = $(this).children('option:selected').attr('value');
  132. var url = window.location.href;
  133. var current_version = DOCUMENTATION_OPTIONS.VERSION;
  134. var docroot = get_docroot_url()
  135. var new_versionpath = selected_version + '/';
  136. // latest tag is also the default page (without version information)
  137. if (docroot.endsWith(current_version + '/') == false) {
  138. var new_url = docroot + new_versionpath + url.replace(docroot, "");
  139. var fallback_url = docroot + new_versionpath;
  140. } else {
  141. // check for named releases (e.g. dunfell) in the subpath
  142. $.each(all_releases, function(idx, release) {
  143. if (docroot.endsWith('/' + release + '/')) {
  144. current_version = release;
  145. return false;
  146. }
  147. });
  148. var new_url = url.replace('/' + current_version + '/', '/' + new_versionpath);
  149. var fallback_url = new_url.replace(url.replace(docroot, ""), "");
  150. }
  151. console.log(get_docroot_url())
  152. console.log(url + " to url " + new_url);
  153. console.log(url + " to fallback " + fallback_url);
  154. if (new_url != url) {
  155. navigate_to_first_existing([
  156. new_url,
  157. fallback_url,
  158. 'https://www.yoctoproject.org/docs/',
  159. ]);
  160. }
  161. }
  162. function on_doctype_switch() {
  163. var selected_doctype = $(this).children('option:selected').attr('value');
  164. var url = window.location.href;
  165. if (selected_doctype == 'mega') {
  166. var docroot = get_docroot_url()
  167. var current_version = DOCUMENTATION_OPTIONS.VERSION;
  168. // Assume manuals before 3.2 are using old docbook mega-manual
  169. if (ver_compare(current_version, "3.2") < 0) {
  170. var new_url = docroot + "mega-manual/mega-manual.html";
  171. } else {
  172. var new_url = docroot + "singleindex.html";
  173. }
  174. } else {
  175. var new_url = url.replace("singleindex.html", "index.html")
  176. }
  177. if (new_url != url) {
  178. navigate_to_first_existing([
  179. new_url,
  180. 'https://www.yoctoproject.org/docs/',
  181. ]);
  182. }
  183. }
  184. // Returns the current doctype based upon the url
  185. function doctype_segment_from_url(url) {
  186. if (url.includes("singleindex") || url.includes("mega-manual"))
  187. return "mega";
  188. return "single";
  189. }
  190. $(document).ready(function() {
  191. var release = DOCUMENTATION_OPTIONS.VERSION;
  192. var current_doctype = doctype_segment_from_url(window.location.href);
  193. var current_series = release.substr(0, 3);
  194. var version_select = build_version_select(current_series, release);
  195. $('.version_switcher_placeholder').html(version_select);
  196. $('.version_switcher_placeholder select').bind('change', on_version_switch);
  197. var doctype_select = build_doctype_select(current_doctype);
  198. $('.doctype_switcher_placeholder').html(doctype_select);
  199. $('.doctype_switcher_placeholder select').bind('change', on_doctype_switch);
  200. if (release != "dev") {
  201. $.each(switcher_versions, function(version, vers_data) {
  202. var series = version.substr(0, 3);
  203. if (series == current_series) {
  204. if (version != release && release.endsWith('.999') == false) {
  205. $('#outdated-warning').html('This document is for outdated version ' + release + ', you should select the latest release version in this series, ' + version + '.');
  206. $('#outdated-warning').css('padding', '.5em');
  207. return false;
  208. }
  209. if (vers_data["obsolete"]) {
  210. $('#outdated-warning').html('Version ' + release + ' of the project is now considered obsolete, please select and use a more recent version');
  211. $('#outdated-warning').css('padding', '.5em');
  212. return false;
  213. }
  214. }
  215. });
  216. }
  217. });
  218. })();