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