blob: 8c016859bc80f03a6322bbea516864f75185c05a [file] [log] [blame]
Andrew Geissler9aee5002022-03-30 16:27:02 +00001/*
2NOTE FOR RELEASE MAINTAINERS:
3This file only needs updating in the development release ("master" branch)
4When documentation for stable releases is built,
5the latest version from "master" is used
6by https://git.yoctoproject.org/yocto-autobuilder-helper/tree/scripts/run-docs-build
7*/
8
9(function() {
10 'use strict';
11
Patrick Williams03907ee2022-05-01 06:28:52 -050012 var all_releases =
13 ALL_RELEASES_PLACEHOLDER
14 ;
15
16 var switcher_versions = {
Andrew Geissler9aee5002022-03-30 16:27:02 +000017 VERSIONS_PLACEHOLDER
18 };
19
20 var all_doctypes = {
21 'single': 'Individual Webpages',
22 'mega': "All-in-one 'Mega' Manual",
23 };
24
25 // Simple version comparision
26 // Return 1 if a > b
27 // Return -1 if a < b
28 // Return 0 if a == b
29 function ver_compare(a, b) {
30 if (a == "dev") {
31 return 1;
32 }
33
34 if (a === b) {
35 return 0;
36 }
37
38 var a_components = a.split(".");
39 var b_components = b.split(".");
40
41 var len = Math.min(a_components.length, b_components.length);
42
43 // loop while the components are equal
44 for (var i = 0; i < len; i++) {
45 // A bigger than B
46 if (parseInt(a_components[i]) > parseInt(b_components[i])) {
47 return 1;
48 }
49
50 // B bigger than A
51 if (parseInt(a_components[i]) < parseInt(b_components[i])) {
52 return -1;
53 }
54 }
55
56 // If one's a prefix of the other, the longer one is greater.
57 if (a_components.length > b_components.length) {
58 return 1;
59 }
60
61 if (a_components.length < b_components.length) {
62 return -1;
63 }
64
65 // Otherwise they are the same.
66 return 0;
67 }
68
69 function build_version_select(current_series, current_version) {
70 var buf = ['<select>'];
71
Patrick Williams03907ee2022-05-01 06:28:52 -050072 $.each(switcher_versions, function(version, vers_data) {
Andrew Geissler9aee5002022-03-30 16:27:02 +000073 var series = version.substr(0, 3);
74 if (series == current_series) {
75 if (version == current_version)
Patrick Williams03907ee2022-05-01 06:28:52 -050076 buf.push('<option value="' + version + '" selected="selected">' + vers_data["title"] + '</option>');
77 else
78 buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
Andrew Geissler9aee5002022-03-30 16:27:02 +000079 } else {
Patrick Williams03907ee2022-05-01 06:28:52 -050080 buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
Andrew Geissler9aee5002022-03-30 16:27:02 +000081 }
82 });
83
84 buf.push('</select>');
85 return buf.join('');
86 }
87
88 function build_doctype_select(current_doctype) {
89 var buf = ['<select>'];
90
91 $.each(all_doctypes, function(doctype, title) {
92 if (doctype == current_doctype)
93 buf.push('<option value="' + doctype + '" selected="selected">' +
94 all_doctypes[current_doctype] + '</option>');
95 else
96 buf.push('<option value="' + doctype + '">' + title + '</option>');
97 });
98 if (!(current_doctype in all_doctypes)) {
99 // In case we're browsing a doctype that is not yet in all_doctypes.
100 buf.push('<option value="' + current_doctype + '" selected="selected">' +
101 current_doctype + '</option>');
102 all_doctypes[current_doctype] = current_doctype;
103 }
104 buf.push('</select>');
105 return buf.join('');
106 }
107
108 function navigate_to_first_existing(urls) {
109 // Navigate to the first existing URL in urls.
110 var url = urls.shift();
111
112 // Web browsers won't redirect file:// urls to file urls using ajax but
113 // its useful for local testing
114 if (url.startsWith("file://")) {
115 window.location.href = url;
116 return;
117 }
118
119 if (urls.length == 0) {
120 window.location.href = url;
121 return;
122 }
123 $.ajax({
124 url: url,
125 success: function() {
126 window.location.href = url;
127 },
128 error: function() {
129 navigate_to_first_existing(urls);
130 }
131 });
132 }
133
134 function get_docroot_url() {
135 var url = window.location.href;
136 var root = DOCUMENTATION_OPTIONS.URL_ROOT;
137
138 var urlarray = url.split('/');
139 // Trim off anything after '/'
140 urlarray.pop();
141 var depth = (root.match(/\.\.\//g) || []).length;
142 for (var i = 0; i < depth; i++) {
143 urlarray.pop();
144 }
145
146 return urlarray.join('/') + '/';
147 }
148
149 function on_version_switch() {
150 var selected_version = $(this).children('option:selected').attr('value');
151 var url = window.location.href;
152 var current_version = DOCUMENTATION_OPTIONS.VERSION;
153 var docroot = get_docroot_url()
154
155 var new_versionpath = selected_version + '/';
Andrew Geissler9aee5002022-03-30 16:27:02 +0000156
Patrick Williams03907ee2022-05-01 06:28:52 -0500157 // latest tag is also the default page (without version information)
158 if (docroot.endsWith(current_version + '/') == false) {
Andrew Geissler9aee5002022-03-30 16:27:02 +0000159 var new_url = docroot + new_versionpath + url.replace(docroot, "");
160 var fallback_url = docroot + new_versionpath;
161 } else {
Patrick Williams03907ee2022-05-01 06:28:52 -0500162 // check for named releases (e.g. dunfell) in the subpath
163 $.each(all_releases, function(idx, release) {
164 if (docroot.endsWith('/' + release + '/')) {
165 current_version = release;
166 return false;
167 }
168 });
169
Andrew Geissler9aee5002022-03-30 16:27:02 +0000170 var new_url = url.replace('/' + current_version + '/', '/' + new_versionpath);
171 var fallback_url = new_url.replace(url.replace(docroot, ""), "");
172 }
173
174 console.log(get_docroot_url())
175 console.log(url + " to url " + new_url);
176 console.log(url + " to fallback " + fallback_url);
177
178 if (new_url != url) {
179 navigate_to_first_existing([
180 new_url,
181 fallback_url,
182 'https://www.yoctoproject.org/docs/',
183 ]);
184 }
185 }
186
187 function on_doctype_switch() {
188 var selected_doctype = $(this).children('option:selected').attr('value');
189 var url = window.location.href;
190 if (selected_doctype == 'mega') {
191 var docroot = get_docroot_url()
192 var current_version = DOCUMENTATION_OPTIONS.VERSION;
193 // Assume manuals before 3.2 are using old docbook mega-manual
194 if (ver_compare(current_version, "3.2") < 0) {
195 var new_url = docroot + "mega-manual/mega-manual.html";
196 } else {
197 var new_url = docroot + "singleindex.html";
198 }
199 } else {
200 var new_url = url.replace("singleindex.html", "index.html")
201 }
202
203 if (new_url != url) {
204 navigate_to_first_existing([
205 new_url,
206 'https://www.yoctoproject.org/docs/',
207 ]);
208 }
209 }
210
211 // Returns the current doctype based upon the url
212 function doctype_segment_from_url(url) {
213 if (url.includes("singleindex") || url.includes("mega-manual"))
214 return "mega";
215 return "single";
216 }
217
218 $(document).ready(function() {
219 var release = DOCUMENTATION_OPTIONS.VERSION;
220 var current_doctype = doctype_segment_from_url(window.location.href);
221 var current_series = release.substr(0, 3);
222 var version_select = build_version_select(current_series, release);
223
224 $('.version_switcher_placeholder').html(version_select);
225 $('.version_switcher_placeholder select').bind('change', on_version_switch);
226
227 var doctype_select = build_doctype_select(current_doctype);
228
229 $('.doctype_switcher_placeholder').html(doctype_select);
230 $('.doctype_switcher_placeholder select').bind('change', on_doctype_switch);
231
Patrick Williams03907ee2022-05-01 06:28:52 -0500232 if (release != "dev") {
233 $.each(switcher_versions, function(version, vers_data) {
Andrew Geissler9aee5002022-03-30 16:27:02 +0000234 var series = version.substr(0, 3);
Patrick Williams03907ee2022-05-01 06:28:52 -0500235 if (series == current_series) {
236 if (version != release && release.endsWith('.999') == false) {
237 $('#outdated-warning').html('This document is for outdated version ' + release + ', you should select the latest release version in this series, ' + version + '.');
238 $('#outdated-warning').css('padding', '.5em');
239 return false;
240 }
241 if (vers_data["obsolete"]) {
242 $('#outdated-warning').html('Version ' + release + ' of the project is now considered obsolete, please select and use a more recent version');
243 $('#outdated-warning').css('padding', '.5em');
244 return false;
245 }
Andrew Geissler9aee5002022-03-30 16:27:02 +0000246 }
247 });
248 }
249 });
250})();