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