-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsee_all_versions.js
More file actions
131 lines (117 loc) · 3.47 KB
/
Copy pathsee_all_versions.js
File metadata and controls
131 lines (117 loc) · 3.47 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function multiDocumenterSeeAllVersions() {
var configEl = document.getElementById("multidoc-see-all-versions-config");
if (!configEl) return;
var config;
try {
config = JSON.parse(configEl.textContent);
} catch (_err) {
return;
}
if (!config || !config.target) return;
var sentinel = config.sentinel || "__MULTIDOC_SEE_ALL_VERSIONS__";
var label = config.label || "See All Versions";
function selectors() {
return document.querySelectorAll(
".docs-version-selector select, #documenter-version-selector",
);
}
function rememberAndAppend() {
var all = selectors();
for (var i = 0; i < all.length; i++) {
var sel = all[i];
if (!sel.options.length) continue;
if (sel.options[sel.options.length - 1].value === sentinel) continue;
if (sel.dataset.seeAllPrevIdx === undefined) {
sel.dataset.seeAllPrevIdx = String(Math.max(0, sel.selectedIndex));
}
var opt = document.createElement("option");
opt.textContent = label;
opt.value = sentinel;
sel.appendChild(opt);
}
}
function hasUninitializedSelector() {
var all = selectors();
for (var i = 0; i < all.length; i++) {
if (!all[i].options.length) return true;
}
return false;
}
function pollForSelector() {
var tries = 0;
function step() {
rememberAndAppend();
if (!hasUninitializedSelector() || tries >= 120) return;
tries += 1;
setTimeout(step, 50);
}
setTimeout(step, 0);
}
function startup() {
rememberAndAppend();
if (hasUninitializedSelector()) {
pollForSelector();
}
var primary = document.getElementById("documenter-version-selector");
if (primary && window.MutationObserver) {
new MutationObserver(rememberAndAppend).observe(primary, {
childList: true,
subtree: true,
});
}
}
var resetting = false;
document.addEventListener(
"change",
function (ev) {
if (resetting) return;
var sel = ev.target;
if (sel.tagName !== "SELECT") return;
if (
!sel.closest(".docs-version-selector") &&
sel.id !== "documenter-version-selector"
) {
return;
}
if (sel.value !== sentinel) {
var idx = sel.selectedIndex;
var all = selectors();
for (var i = 0; i < all.length; i++) {
all[i].dataset.seeAllPrevIdx = String(idx);
}
return;
}
ev.preventDefault();
ev.stopImmediatePropagation();
resetting = true;
window.open(config.target, "_blank", "noopener");
var prevIdx = parseInt(sel.dataset.seeAllPrevIdx, 10);
if (isNaN(prevIdx) || prevIdx < 0) prevIdx = 0;
var maxIdx = sel.options.length - 2;
if (maxIdx < 0) maxIdx = 0;
var clampedIdx = Math.min(Math.max(0, prevIdx), maxIdx);
var all = selectors();
for (var i = 0; i < all.length; i++) {
if (!all[i].options.length) continue;
all[i].selectedIndex = clampedIdx;
all[i].dataset.seeAllPrevIdx = String(clampedIdx);
}
resetting = false;
},
true,
);
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", startup);
} else {
startup();
}
window.addEventListener("load", rememberAndAppend);
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
setTimeout(multiDocumenterSeeAllVersions, 1);
} else {
document.addEventListener("DOMContentLoaded", multiDocumenterSeeAllVersions);
}