Skip to content

Commit c3d2758

Browse files
Merge pull request #157 from Chris-Wolfgang/fix/version-picker-latest-select
fix(docs): version-picker shows selected option on /versions/latest/
2 parents 1d7e971 + 50e0a55 commit c3d2758

1 file changed

Lines changed: 14 additions & 35 deletions

File tree

‎docfx_project/public/version-picker.js‎

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,17 @@
4949
}
5050

5151
function renderPicker(versions) {
52-
// Detect the currently-viewed version from the URL.
53-
var currentVersion = 'latest';
52+
// Detect the currently-viewed version from the URL. Starts as null and
53+
// is only set when the URL actually matches /versions/<v>/. Root and
54+
// other non-versioned pages leave it null — that way the `latest`
55+
// alias entry stays out of the picker except on /versions/latest/,
56+
// and no option gets pre-selected when we can't identify the version.
57+
var currentVersion = null;
5458
var m = window.location.pathname.match(/\/versions\/([^\/]+)(?:\/|$)/);
5559
if (m) {
5660
currentVersion = m[1];
5761
}
5862

59-
// If we're on /versions/latest/, resolve 'latest' to the concrete
60-
// version it points to (matching the `latest` entry's url against
61-
// the versioned entries). Without this resolution, the browser
62-
// auto-selects the first option in the dropdown — which is usually
63-
// the same concrete version 'latest' aliases — and picking that
64-
// option doesn't fire `change`, leaving the user unable to navigate
65-
// away from /versions/latest/ to the equivalent concrete-version
66-
// URL via the picker.
67-
if (currentVersion === 'latest') {
68-
var latestEntry = null;
69-
for (var i = 0; i < versions.length; i++) {
70-
if (versions[i] && versions[i].version === 'latest') {
71-
latestEntry = versions[i];
72-
break;
73-
}
74-
}
75-
if (latestEntry && latestEntry.url) {
76-
for (var j = 0; j < versions.length; j++) {
77-
var v = versions[j];
78-
if (v && v.version !== 'latest' && v.url === latestEntry.url) {
79-
currentVersion = v.version;
80-
break;
81-
}
82-
}
83-
}
84-
}
85-
8663
// Build the <select>.
8764
var select = document.createElement('select');
8865
select.className = 'wolfgang-version-picker';
@@ -113,12 +90,14 @@
11390
var optionCount = 0;
11491
versions.forEach(function (v) {
11592
if (!v || !v.version || !v.url) return;
116-
// Skip the "latest" alias — the highest-numbered v* entry
117-
// already represents the latest release; surfacing both is
118-
// redundant in the picker. versions.json keeps the "latest"
119-
// entry so other consumers (links, scripts) can still
120-
// resolve it.
121-
if (v.version === 'latest') return;
93+
// Skip the "latest" alias EXCEPT when the reader is actually
94+
// on /versions/latest/. On every other page the highest-
95+
// numbered v* entry already represents the latest release
96+
// and surfacing both is redundant; on /versions/latest/ we
97+
// NEED `latest` in the list because otherwise the picker
98+
// would show no selected option and the reader would have
99+
// no way to know which version they are viewing.
100+
if (v.version === 'latest' && currentVersion !== 'latest') return;
122101
var opt = document.createElement('option');
123102
opt.value = v.url;
124103
opt.textContent = v.version;

0 commit comments

Comments
 (0)