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