switchers.js 7.0 KB

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