|
1 | 1 | const project = "MetPy"; |
2 | 2 |
|
3 | | -$(document).ready(function() { |
4 | | - cur_ver = DOCUMENTATION_OPTIONS.VERSION; |
5 | | - end = cur_ver.lastIndexOf('.'); |
6 | | - if (end > -1) { |
7 | | - cur_ver = 'v' + cur_ver.substring(0, end); |
8 | | - } |
9 | | - console.log('cur_ver: ' + cur_ver); |
10 | | - |
11 | | - $.getJSON('/' + project + '/versions.json', function(data) { |
12 | | - if (cur_ver !== data.latest) { |
13 | | - let msg; |
14 | | - if (cur_ver.includes('dev') || data.prereleases.indexOf(cur_ver) > -1) { |
15 | | - msg = 'development / pre-release'; |
16 | | - } else { |
17 | | - msg = 'previous'; |
18 | | - } |
19 | | - content = $('<div class="alert alert-secondary alert-version" role="alert">This documentation page is for a ' + msg + |
20 | | - ' version. For the latest release version, go to <a class="alert-link" href="https://unidata.github.io/MetPy/latest/">https://unidata.github.io/MetPy/latest/</a>'); |
21 | | - $('#banner').append(content); |
| 3 | +function documentReady(callback) { |
| 4 | + if (document.readyState != "loading") callback(); |
| 5 | + else document.addEventListener("DOMContentLoaded", callback); |
| 6 | +} |
| 7 | + |
| 8 | +documentReady(function () { |
| 9 | + // Use PST version metadata to match current doc version |
| 10 | + const cur_ver = DOCUMENTATION_OPTIONS.theme_switcher_version_match; |
| 11 | + console.log("cur_ver: " + cur_ver); |
| 12 | + |
| 13 | + fetch("/" + project + "/pst-versions.json") |
| 14 | + .then(function (response) { |
| 15 | + return response.json(); |
| 16 | + }) |
| 17 | + .then(function (data) { |
| 18 | + // Find matching version entry in PST version list |
| 19 | + const entry = data[data.findIndex((obj) => obj.version == cur_ver)]; |
| 20 | + |
| 21 | + // Find out if matched version is latest |
| 22 | + // and construct alert message |
| 23 | + if (entry.is_latest != true) { |
| 24 | + let rel_type; |
| 25 | + if (cur_ver.includes("dev") || entry.is_prerelease == true) { |
| 26 | + rel_type = "development/pre-release"; |
| 27 | + } else { |
| 28 | + rel_type = "previous"; |
22 | 29 | } |
23 | 30 |
|
24 | | - $.each(data.versions, function() { |
25 | | - if (this !== 'latest') { |
26 | | - const url = DOCUMENTATION_OPTIONS.URL_ROOT + '../' + this; |
27 | | - const name = this.startsWith('v') ? this.substring(1) : this; |
28 | | - $('#version-menu').append('<a class="dropdown-item" href="' + url + '">' + name + '</a>'); |
29 | | - } |
30 | | - }); |
| 31 | + let msg = |
| 32 | + `This documentation page is for a ${rel_type} version. For the latest release version, go to <a class="alert-link" href="https://unidata.github.io/MetPy/latest/">https://unidata.github.io/MetPy/latest/</a>`; |
| 33 | + |
| 34 | + // Create alert div and fill with message content |
| 35 | + let content = document.createElement("div"); |
| 36 | + content.classList.add("alert", "alert-secondary", "alert-version"); |
| 37 | + content.setAttribute("role", "alert"); |
| 38 | + content.innerHTML = msg; |
| 39 | + |
| 40 | + // Append alert div to banner div under navbar |
| 41 | + document.querySelector("#banner").appendChild(content); |
| 42 | + } else { |
| 43 | + console.log("MetPy version latest."); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch(function (err) { |
| 47 | + console.warn("Something went wrong.", err); |
31 | 48 | }); |
32 | 49 | }); |
33 | 50 |
|
34 | | -// Borrowed from Bokeh docs to look for a banner.html at the base of the docs repo and add that |
35 | | -// to the banner if present. |
36 | | -$(document).ready(function () { |
37 | | - $.get('/' + project + '/banner.html', function (data) { |
38 | | - if (data.length > 0) { |
39 | | - console.log(data); |
40 | | - $('#banner').prepend(data); |
| 51 | +documentReady(function () { |
| 52 | + fetch("/" + project + "/banner.html") |
| 53 | + .then(function (response) { |
| 54 | + return response.text(); |
| 55 | + }) |
| 56 | + .then(function (html) { |
| 57 | + // If any banner.html exists, parse it and add to banner div |
| 58 | + if (html.length > 0) { |
| 59 | + let parser = new DOMParser(); |
| 60 | + let doc = parser.parseFromString(html.trim(), "text/html"); |
| 61 | + |
| 62 | + // Get all div elements from banner.html |
| 63 | + // and prepend them to banner div under navbar |
| 64 | + let divs = doc.getElementsByTagName("div"); |
| 65 | + for (let div of divs) { |
| 66 | + document.querySelector("#banner").prepend(div); |
41 | 67 | } |
| 68 | + } else { |
| 69 | + console.log("Banner empty."); |
| 70 | + } |
42 | 71 | }) |
43 | | - }) |
| 72 | + .catch(function (err) { |
| 73 | + return console.warn("Something went wrong.", err); |
| 74 | + }); |
| 75 | +}); |
0 commit comments