switchers.js.in 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. var root = DOCUMENTATION_OPTIONS.URL_ROOT;
  115. var urlarray = url.split('/');
  116. // Trim off anything after '/'
  117. urlarray.pop();
  118. var depth = (root.match(/\.\.\//g) || []).length;
  119. for (var i = 0; i < depth; i++) {
  120. urlarray.pop();
  121. }
  122. return urlarray.join('/') + '/';
  123. }
  124. function on_version_switch() {
  125. var selected_version = $(this).children('option:selected').attr('value');
  126. var url = window.location.href;
  127. var current_version = DOCUMENTATION_OPTIONS.VERSION;
  128. var docroot = get_docroot_url()
  129. var new_versionpath = selected_version + '/';
  130. // latest tag is also the default page (without version information)
  131. if (docroot.endsWith(current_version + '/') == false) {
  132. var new_url = docroot + new_versionpath + url.replace(docroot, "");
  133. var fallback_url = docroot + new_versionpath;
  134. } else {
  135. // check for named releases (e.g. dunfell) in the subpath
  136. $.each(all_releases, function(idx, release) {
  137. if (docroot.endsWith('/' + release + '/')) {
  138. current_version = release;
  139. return false;
  140. }
  141. });
  142. var new_url = url.replace('/' + current_version + '/', '/' + new_versionpath);
  143. var fallback_url = new_url.replace(url.replace(docroot, ""), "");
  144. }
  145. console.log(get_docroot_url())
  146. console.log(url + " to url " + new_url);
  147. console.log(url + " to fallback " + fallback_url);
  148. if (new_url != url) {
  149. navigate_to_first_existing([
  150. new_url,
  151. fallback_url,
  152. 'https://www.yoctoproject.org/docs/',
  153. ]);
  154. }
  155. }
  156. function on_doctype_switch() {
  157. var selected_doctype = $(this).children('option:selected').attr('value');
  158. var url = window.location.href;
  159. if (selected_doctype == 'mega') {
  160. var docroot = get_docroot_url()
  161. var current_version = DOCUMENTATION_OPTIONS.VERSION;
  162. // Assume manuals before 3.2 are using old docbook mega-manual
  163. if (ver_compare(current_version, "3.2") < 0) {
  164. var new_url = docroot + "mega-manual/mega-manual.html";
  165. } else {
  166. var new_url = docroot + "singleindex.html";
  167. }
  168. } else {
  169. var new_url = url.replace("singleindex.html", "index.html")
  170. }
  171. if (new_url != url) {
  172. navigate_to_first_existing([
  173. new_url,
  174. 'https://www.yoctoproject.org/docs/',
  175. ]);
  176. }
  177. }
  178. // Returns the current doctype based upon the url
  179. function doctype_segment_from_url(url) {
  180. if (url.includes("singleindex") || url.includes("mega-manual"))
  181. return "mega";
  182. return "single";
  183. }
  184. $(document).ready(function() {
  185. var release = DOCUMENTATION_OPTIONS.VERSION;
  186. var current_doctype = doctype_segment_from_url(window.location.href);
  187. var current_series = release.substr(0, 3);
  188. var version_select = build_version_select(current_series, release);
  189. $('.version_switcher_placeholder').html(version_select);
  190. $('.version_switcher_placeholder select').bind('change', on_version_switch);
  191. var doctype_select = build_doctype_select(current_doctype);
  192. $('.doctype_switcher_placeholder').html(doctype_select);
  193. $('.doctype_switcher_placeholder select').bind('change', on_doctype_switch);
  194. if (release != "dev") {
  195. $.each(switcher_versions, function(version, vers_data) {
  196. var series = version.substr(0, 3);
  197. if (series == current_series) {
  198. if (version != release && release.endsWith('.999') == false) {
  199. $('#outdated-warning').html('This document is for outdated version ' + release + ', you should select the latest release version in this series, ' + version + '.');
  200. $('#outdated-warning').css('padding', '.5em');
  201. return false;
  202. }
  203. if (vers_data["obsolete"]) {
  204. $('#outdated-warning').html('Version ' + release + ' of the project is now considered obsolete, please select and use a more recent version');
  205. $('#outdated-warning').css('padding', '.5em');
  206. return false;
  207. }
  208. }
  209. });
  210. }
  211. });
  212. })();