|
1 | 1 | 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; |
9 | 7 |
|
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 | + } |
14 | 27 |
|
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("/") |
20 | 35 | var regx = /\/v[0-9\.]+/ |
21 | 36 | 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}`) |
28 | 38 | } else { |
29 | | - new_location += current_location.slice(1); |
| 39 | + window.location = `/${target_version}${current_location}` |
30 | 40 | } |
31 | 41 | } |
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 | | -} |
49 | 42 |
|
50 | | -// SHOW/HIDE MENU |
| 43 | + // SHOW/HIDE MENU |
51 | 44 | // Get all "navbar-burger" elements |
52 | 45 | const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); |
53 | 46 |
|
|
0 commit comments