|
| 1 | +const TRUNK_VERSION = "trunk/develop"; |
| 2 | +const NONE_VERSION = "none" |
| 3 | + |
| 4 | +function loadVersions() { |
| 5 | + return import('/versions.js').then((imported_versions_module) => { |
| 6 | + versions = imported_versions_module.versions |
| 7 | + return versions |
| 8 | + } |
| 9 | + ).catch(() => { |
| 10 | + console.log("Versions loading failed") |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | + |
1 | 15 | const addModal = () => { |
2 | 16 | const modalHandler = () => { |
3 | 17 | const modal = document.getElementById('main-nav') |
@@ -62,41 +76,136 @@ const remove_legacy_searchbox = () => { |
62 | 76 | } |
63 | 77 |
|
64 | 78 | const old_docs_version = () => { |
65 | | - const version = "v1.0" |
66 | | - const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0]; |
67 | | - brief.textContent += " " + version; |
68 | | - const base_page_url = window.location.pathname.split('/' + version + '/')[1]; |
| 79 | + const version = get_current_version() |
| 80 | + |
| 81 | + if (version != TRUNK_VERSION) { |
| 82 | + const brief = document.getElementById('projectbrief').getElementsByTagName('a')[0]; |
| 83 | + brief.textContent += " " + version; |
| 84 | + } |
69 | 85 |
|
70 | 86 | var warning = document.createElement("div"); |
71 | 87 | warning.style.width = '100%'; |
72 | 88 | warning.style.display = 'flex'; |
73 | 89 | warning.style.flexDirection = 'column'; |
74 | 90 | warning.innerHTML = ` |
75 | | - <a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/` + base_page_url + `"> |
| 91 | + <a style="padding: 16px; margin-bottom: 20px; text-align: center; border: 1px solid var(--warning-color-dark); border-radius: var(--border-radius-large);" href="/"> |
76 | 92 | ⚠️ This is the documentation for an old userver version. Click here to switch to the latest version. |
77 | 93 | </a> |
78 | 94 | `; |
79 | 95 | const titlearea = document.getElementById('titlearea'); |
80 | 96 | titlearea.parentNode.insertBefore(warning, titlearea); |
81 | 97 | } |
82 | 98 |
|
| 99 | +const get_current_version = () => { |
| 100 | + const { pathname } = window.location; |
| 101 | + |
| 102 | + const urlSplitLimiter = 3; |
| 103 | + const versionTokenPosition = 2; |
| 104 | + |
| 105 | + if (pathname.startsWith("/docs/")) { |
| 106 | + return pathname.split("/", urlSplitLimiter)[versionTokenPosition] |
| 107 | + } |
| 108 | + |
| 109 | + if (pathname.startsWith("/d4/de0/versions.html")) { |
| 110 | + return NONE_VERSION |
| 111 | + } |
| 112 | + |
| 113 | + return TRUNK_VERSION; |
| 114 | +} |
| 115 | + |
| 116 | +const getPreviousVersion = (versions, current_version) => { |
| 117 | + let index = -1; |
| 118 | + |
| 119 | + if (typeof current_version.index === "number") { |
| 120 | + index = current_version.index; |
| 121 | + } else if ("value" in current_version) { |
| 122 | + index = versions.indexOf(current_version.value); |
| 123 | + } |
| 124 | + |
| 125 | + if (index <= 0 || index >= versions.length) { |
| 126 | + return undefined; |
| 127 | + } |
| 128 | + |
| 129 | + return { |
| 130 | + index: index - 1, |
| 131 | + value: versions[index - 1] |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +const getVersionMajorPart = (version) => { |
| 136 | + const major = version.split(".")[0].replace("v", ""); |
| 137 | + |
| 138 | + const major_as_number = parseInt(major, 10); |
| 139 | + |
| 140 | + return major_as_number; |
| 141 | +}; |
| 142 | + |
| 143 | + |
| 144 | +const floor_to_major_version = (versions, target_version) => { |
| 145 | + let target_major_version = getVersionMajorPart(target_version) |
| 146 | + let version_iterator = { value: target_version, index: versions.length - 1 } |
| 147 | + |
| 148 | + while (getVersionMajorPart(getPreviousVersion(versions, version_iterator).value) == target_major_version) { |
| 149 | + version_iterator = getPreviousVersion(versions, version_iterator) |
| 150 | + } |
| 151 | + |
| 152 | + return version_iterator.value |
| 153 | +} |
| 154 | + |
| 155 | +const add_docs_versioning = () => { |
| 156 | + |
| 157 | + loadVersions().then((versions) => { |
| 158 | + const latest_version = versions[versions.length - 1] |
| 159 | + let second_likely_popular_version = floor_to_major_version(versions, latest_version) |
| 160 | + |
| 161 | + if (second_likely_popular_version == latest_version) { |
| 162 | + second_likely_popular_version = getPreviousVersion(versions, { value: latest_version }).value |
| 163 | + } |
| 164 | + |
| 165 | + const current_version = get_current_version() |
| 166 | + |
| 167 | + if (current_version != latest_version && |
| 168 | + current_version != TRUNK_VERSION && |
| 169 | + current_version != NONE_VERSION) { |
| 170 | + old_docs_version() |
| 171 | + } |
| 172 | + |
| 173 | + const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0]; |
| 174 | + const footer_prefix = ` |
| 175 | + <li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;"> |
| 176 | + <span style="color: var(--toc-foreground);">Docs version:</span> |
| 177 | + ` |
| 178 | + |
| 179 | + let footer_infix = "" |
| 180 | + |
| 181 | + if (current_version == TRUNK_VERSION) { |
| 182 | + footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${TRUNK_VERSION}</span>, ` |
| 183 | + } else { |
| 184 | + footer_infix += `<a href="/index.html">${TRUNK_VERSION}</a>, ` |
| 185 | + } |
| 186 | + |
| 187 | + not_trunk_versions = [latest_version, second_likely_popular_version] |
| 188 | + |
| 189 | + for (let version of not_trunk_versions) { |
| 190 | + if (current_version != version) { |
| 191 | + footer_infix += `<a href="/docs/${version}/index.html">${version}</a>, ` |
| 192 | + } else { |
| 193 | + footer_infix += `<span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">${version}</span>, ` |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + footer_infix += `<a href="/d4/de0/versions.html">others</a>` |
| 198 | + |
| 199 | + footer.innerHTML = footer_prefix + footer_infix |
| 200 | + + footer.innerHTML; |
| 201 | + }) |
| 202 | + |
| 203 | +} |
| 204 | + |
83 | 205 | const init_header = () => { |
84 | 206 | addModal(); |
85 | 207 | create_nav_wrapper(); |
86 | 208 | remove_legacy_searchbox(); |
87 | 209 | onBurger(); |
88 | | - old_docs_version(); |
89 | | - |
90 | | - const breif = document.getElementById('projectbrief').getElementsByTagName('a')[0]; |
91 | | - breif.textContent += " v1.0"; |
92 | | - |
93 | | - const footer = document.getElementById('nav-path').getElementsByTagName('ul')[0]; |
94 | | - footer.innerHTML = ` |
95 | | - <li style="box-shadow: inset -1px 0 0 0 var(--separator-color); background-image: none; margin-right: 48px;"> |
96 | | - <span style="color: var(--toc-foreground);">Docs version:</span> |
97 | | - <span style="background-image: none; color: var(--toc-active-color); font-weight: bold;">v1.0</span>, |
98 | | - <a href="/docs/v2.0/` + window.location.pathname.split('/v1.0/')[1] + `">v2.0</a>, |
99 | | - <a href="/` + window.location.pathname.split('/v1.0/')[1] + `">trunk/develop</a> |
100 | | - </li>` |
101 | | - + footer.innerHTML; |
| 210 | + add_docs_versioning(); |
102 | 211 | } |
0 commit comments