-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion-switcher.js
More file actions
87 lines (74 loc) · 2.53 KB
/
version-switcher.js
File metadata and controls
87 lines (74 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(function () {
'use strict';
function getVersionFromPath(pathname) {
var match = pathname.match(/\/(\d+\.\d+)\//);
return match ? match[1] : null;
}
function getPathAfterVersion(pathname) {
var match = pathname.match(/\/\d+\.\d+\/(.*)/);
return match ? match[1] : '';
}
function getSiteRoot() {
var meta = document.querySelector('meta[name="docfx:rel"]');
return meta ? meta.getAttribute('content') : './';
}
function initVersionPicker(versions, latest) {
var select = document.getElementById('version-picker');
if (!select) return;
var currentVersion = getVersionFromPath(window.location.pathname);
var relativePath = getPathAfterVersion(window.location.pathname);
versions.forEach(function (v) {
var option = document.createElement('option');
option.value = v;
option.textContent = v === latest ? 'v' + v + ' (latest)' : 'v' + v;
if (v === currentVersion) {
option.selected = true;
}
select.appendChild(option);
});
if (!currentVersion) {
var placeholder = document.createElement('option');
placeholder.value = '';
placeholder.textContent = 'Select version';
placeholder.selected = true;
placeholder.disabled = true;
select.insertBefore(placeholder, select.firstChild);
}
select.addEventListener('change', function () {
var targetVersion = select.value;
if (!targetVersion) return;
var newPathname;
if (currentVersion && relativePath) {
newPathname = window.location.pathname.replace(
'/' + currentVersion + '/',
'/' + targetVersion + '/'
);
} else {
var siteRootPath = new URL(getSiteRoot(), window.location.href).pathname.replace(/\/$/, '');
newPathname = siteRootPath + '/' + targetVersion + '/PolylineAlgorithm.html';
}
window.location.pathname = newPathname;
});
}
function loadVersions() {
var root = getSiteRoot();
var url = root + 'versions.json';
fetch(url)
.then(function (r) {
if (!r.ok) throw new Error('versions.json not found');
return r.json();
})
.then(function (data) {
initVersionPicker(data.versions, data.latest);
})
.catch(function () {
var container = document.getElementById('version-picker-container');
if (container) container.style.display = 'none';
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadVersions);
} else {
loadVersions();
}
}());