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