xref: /openbmc/openbmc/poky/documentation/sphinx-static/switchers.js.in (revision 8460358c3d24c71d9d38fd126c745854a6301564)
1/*
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_releases =
13	ALL_RELEASES_PLACEHOLDER
14  ;
15
16  var switcher_versions = {
17    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
72    $.each(switcher_versions, function(version, vers_data) {
73      var series = version.substr(0, 3);
74      if (series == current_series) {
75        if (version == current_version)
76            buf.push('<option value="' + version + '" selected="selected">' + vers_data["title"] + '</option>');
77	      else
78            buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
79      } else {
80        buf.push('<option value="' + version + '">' + vers_data["title"] + '</option>');
81      }
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    // Try to get the variable from documentation_options.js
137    var root = DOCUMENTATION_OPTIONS.URL_ROOT;
138    if (root == null) {
139      // In recent versions of Sphinx, URL_ROOT was removed from
140      // documentation_options.js, so get it like searchtools.js does.
141      root = document.documentElement.dataset.content_root;
142    }
143
144    var urlarray = url.split('/');
145    // Trim off anything after '/'
146    urlarray.pop();
147    var depth = (root.match(/\.\.\//g) || []).length;
148    for (var i = 0; i < depth; i++) {
149      urlarray.pop();
150    }
151
152    return urlarray.join('/') + '/';
153  }
154
155  function on_version_switch() {
156    var selected_version = $(this).children('option:selected').attr('value');
157    var url = window.location.href;
158    var current_version = DOCUMENTATION_OPTIONS.VERSION;
159    var docroot = get_docroot_url()
160
161    var new_versionpath = selected_version + '/';
162
163    // latest tag is also the default page (without version information)
164    if (docroot.endsWith(current_version + '/') == false) {
165        var new_url = docroot + new_versionpath + url.replace(docroot, "");
166        var fallback_url = docroot + new_versionpath;
167    } else {
168	// check for named releases (e.g. dunfell) in the subpath
169        $.each(all_releases, function(idx, release) {
170		if (docroot.endsWith('/' + release + '/')) {
171			current_version = release;
172			return false;
173		}
174	});
175
176        var new_url = url.replace('/' + current_version + '/', '/' + new_versionpath);
177        var fallback_url = new_url.replace(url.replace(docroot, ""), "");
178    }
179
180    console.log(get_docroot_url())
181    console.log(url + " to url " + new_url);
182    console.log(url + " to fallback " + fallback_url);
183
184    if (new_url != url) {
185      navigate_to_first_existing([
186        new_url,
187        fallback_url,
188        'https://www.yoctoproject.org/docs/',
189      ]);
190    }
191  }
192
193  function on_doctype_switch() {
194    var selected_doctype = $(this).children('option:selected').attr('value');
195    var url = window.location.href;
196    if (selected_doctype == 'mega') {
197      var docroot = get_docroot_url()
198      var current_version = DOCUMENTATION_OPTIONS.VERSION;
199      // Assume manuals before 3.2 are using old docbook mega-manual
200      if (ver_compare(current_version, "3.2") < 0) {
201        var new_url = docroot + "mega-manual/mega-manual.html";
202      } else {
203        var new_url = docroot + "singleindex.html";
204      }
205    } else {
206      var new_url = url.replace("singleindex.html", "index.html")
207    }
208
209    if (new_url != url) {
210      navigate_to_first_existing([
211        new_url,
212        'https://www.yoctoproject.org/docs/',
213      ]);
214    }
215  }
216
217  // Returns the current doctype based upon the url
218  function doctype_segment_from_url(url) {
219    if (url.includes("singleindex") || url.includes("mega-manual"))
220      return "mega";
221    return "single";
222  }
223
224  $(document).ready(function() {
225    var release = DOCUMENTATION_OPTIONS.VERSION;
226    var current_doctype = doctype_segment_from_url(window.location.href);
227    var current_series = release.substr(0, 3);
228    var version_select = build_version_select(current_series, release);
229
230    $('.version_switcher_placeholder').html(version_select);
231    $('.version_switcher_placeholder select').bind('change', on_version_switch);
232
233    var doctype_select = build_doctype_select(current_doctype);
234
235    $('.doctype_switcher_placeholder').html(doctype_select);
236    $('.doctype_switcher_placeholder select').bind('change', on_doctype_switch);
237
238    if (release != "dev") {
239      $.each(switcher_versions, function(version, vers_data) {
240        var series = version.substr(0, 3);
241        if (series == current_series) {
242          if (version != release && release.endsWith('.999') == false) {
243            $('#outdated-warning').html('This document is for outdated version ' + release + ', you should select the latest release version in this series, ' + version + '.');
244            $('#outdated-warning').css('padding', '.5em');
245            return false;
246          }
247          if (vers_data["obsolete"]) {
248            $('#outdated-warning').html('Version ' + release + ' of the project is now considered obsolete, please select and use a more recent version');
249            $('#outdated-warning').css('padding', '.5em');
250            return false;
251          }
252        }
253      });
254    }
255  });
256})();
257