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