switchers.js 7.0 KB

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