Skip to content

Commit b5f7ce5

Browse files
authored
Merge pull request #210 from hughrun/fix-versions
Fix versions logic and clean up JS file
2 parents 99d86c7 + 8846ed8 commit b5f7ce5

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

site/static/js/bookwyrm.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
11
document.addEventListener('DOMContentLoaded', () => {
2-
// LANGUAGE / LOCALIZATION
3-
// TODO: WHY DOESN'T THIS WORK?
4-
var languageSelector = document.getElementById("language_selection");
5-
languageSelector.onchange = function(event) {
6-
console.log("changed")
7-
// var current_locale = "{{ locale['slug']|safe }}";
8-
var current_location = window.location.pathname;
2+
const languageSelector = document.getElementById("language_selection");
3+
languageSelector.onchange = function(event) {
4+
var current_location = window.location.pathname;
5+
var locale_index = current_location.indexOf(current_locale);
6+
var new_location = "/" + event.target.value;
97

10-
var locale_index = current_location.indexOf(current_locale);
11-
console.log(locale_index)
12-
var new_location = "/" + event.target.value;
13-
console.log(new_location)
8+
if (locale_index) {
9+
// we're in a locale - swap it out
10+
new_location = current_location.replace(current_locale, event.target.value)
11+
} else {
12+
// are we in a version?
13+
var regx = /\/v[0-9\.]+/
14+
if (regx.test(current_location)) {
15+
// split current location
16+
arr = current_location.split("/")
17+
// insert new locale
18+
arr.splice(2, 0, event.target.value.replace("/", ""))
19+
// gather it all together again
20+
new_location = arr.join("/")
21+
} else {
22+
new_location += current_location.slice(1);
23+
}
24+
}
25+
window.location = new_location;
26+
}
1427

15-
if (locale_index) {
16-
// we're in a locale - swap it out
17-
new_location = current_location.replace(current_locale, event.target.value)
18-
} else {
19-
// are we in a version?
28+
// SELECT VERSION
29+
var versionSelector = document.getElementById("version_selection");
30+
versionSelector.onchange = function(event) {
31+
current_version = "{{ current_version|safe }}"
32+
var current_location = window.location.pathname;
33+
var target_version = event.target.value == "development" ? "" : event.target.value;
34+
var arr = current_location.split("/")
2035
var regx = /\/v[0-9\.]+/
2136
if (regx.test(current_location)) {
22-
// split current location
23-
arr = current_location.split("/")
24-
// insert new locale
25-
arr.splice(2, 0, event.target.value.replace("/", ""))
26-
// gather it all together again
27-
new_location = arr.join("/")
37+
window.location = window.location.href.replace(regx,`${target_version}`)
2838
} else {
29-
new_location += current_location.slice(1);
39+
window.location = `/${target_version}${current_location}`
3040
}
3141
}
32-
window.location = new_location;
33-
}
34-
35-
// SELECT VERSION
36-
var versionSelector = document.getElementById("version_selection");
37-
versionSelector.onchange = function(event) {
38-
current_version = "{{ current_version|safe }}"
39-
var current_location = window.location.pathname;
40-
var target_version = event.target.value == "latest" ? "" : event.target.value;
41-
var arr = current_location.split("/")
42-
var regx = /\/v[0-9\.]+/
43-
if (regx.test(current_location)) {
44-
window.location = window.location.href.replace(regx,`${target_version}`)
45-
} else {
46-
window.location = `/${target_version}${current_location}`
47-
}
48-
}
4942

50-
// SHOW/HIDE MENU
43+
// SHOW/HIDE MENU
5144
// Get all "navbar-burger" elements
5245
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
5346

templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ <h3 class="title is-6">{{ _("Learn more") }}</h3>
157157

158158
<script>
159159
var current_locale = "{{ locale['slug']|safe }}";
160+
var current_version = "{{ current_version }}";
160161
</script>
161162
<script src="/static/js/bookwyrm.js"></script>
162163
</body>

0 commit comments

Comments
 (0)