forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_switcher.js
More file actions
119 lines (97 loc) · 3.23 KB
/
version_switcher.js
File metadata and controls
119 lines (97 loc) · 3.23 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
/*
* Version switcher for the user guide sidebar.
*
* Injects the sphinx_rtd_theme's native ".switch-menus > .version-switch"
* scaffolding above the search box in the left sidebar, containing a <select>
* so readers can jump between:
* - stable: https://codeigniter.com/user_guide/
* - latest (in-progress): https://codeigniter4.github.io/userguide/
* - local build: file:// or localhost, etc. (only shown when not on a deployed host)
*/
(() => {
const VERSIONS = [
{
slug: 'stable',
label: 'Stable',
host: 'codeigniter.com',
pathPrefix: '/user_guide/',
},
{
slug: 'latest (in-progress)',
label: 'Latest (in-progress)',
host: 'codeigniter4.github.io',
pathPrefix: '/userguide/',
},
];
const LOCAL = Object.freeze({
slug: 'local build',
label: 'Local build',
host: null,
pathPrefix: null,
});
const detect_current = () =>
VERSIONS.find((version) => version.host === window.location.hostname) ?? LOCAL;
/*
* Derive the absolute URL of the documentation root by inspecting the
* <script> tag Sphinx always injects for documentation_options.js. Its
* resolved .src is absolute, so we can back out the directory that
* contains _static/, which is the doc root. This works identically for
* http(s), file://, and localhost.
*/
const doc_root_url = () => {
const opts = document.querySelector('script[src*="documentation_options.js"]');
if (!opts) {
return null;
}
const idx = opts.src.lastIndexOf('/_static/');
return idx < 0 ? null : opts.src.slice(0, idx + 1);
};
const current_page_path = () => {
const root = doc_root_url();
if (!root) {
return '';
}
const here = window.location.href.split('#')[0].split('?')[0];
return here.startsWith(root) ? here.slice(root.length) : '';
};
const url_for = ({ host, pathPrefix }) =>
`https://${host}${pathPrefix}${current_page_path()}${window.location.hash}`;
const build = () => {
const searchArea = document.querySelector('.wy-side-nav-search');
const searchForm = searchArea?.querySelector('[role="search"]');
if (!searchArea || !searchForm) {
return;
}
if (searchArea.querySelector('.switch-menus')) {
return;
}
const current = detect_current();
const entries = current === LOCAL ? [LOCAL, ...VERSIONS] : VERSIONS;
const select = document.createElement('select');
select.setAttribute('aria-label', 'Select documentation version');
for (const entry of entries) {
const option = document.createElement('option');
option.value = entry.host ? url_for(entry) : '';
option.textContent = entry.label;
option.selected = entry.slug === current.slug;
select.append(option);
}
select.addEventListener('change', () => {
if (select.value) {
window.location.href = select.value;
}
});
const versionSwitch = document.createElement('div');
versionSwitch.className = 'version-switch';
versionSwitch.append(select);
const switchMenus = document.createElement('div');
switchMenus.className = 'switch-menus';
switchMenus.append(versionSwitch);
searchArea.insertBefore(switchMenus, searchForm);
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', build, { once: true });
} else {
build();
}
})();