|
| 1 | +window.addEventListener("DOMContentLoaded", function () { |
| 2 | + // This is a bit hacky. Figure out the base URL from a known CSS file the |
| 3 | + // template refers to... |
| 4 | + var ex = new RegExp("/?assets/fonts/material-icons.css$"); |
| 5 | + var sheet = document.querySelector('link[href$="material-icons.css"]'); |
| 6 | + |
| 7 | + var REL_BASE_URL = sheet.getAttribute("href").replace(ex, ""); |
| 8 | + var ABS_BASE_URL = sheet.href.replace(ex, ""); |
| 9 | + var CURRENT_VERSION = ABS_BASE_URL.split("/").pop(); |
| 10 | + |
| 11 | + function makeSelect(options, selected) { |
| 12 | + var select = document.createElement("select"); |
| 13 | + select.classList.add("form-control"); |
| 14 | + |
| 15 | + options.forEach(function (i) { |
| 16 | + var option = new Option(i.text, i.value, undefined, |
| 17 | + i.value === selected); |
| 18 | + select.add(option); |
| 19 | + }); |
| 20 | + |
| 21 | + return select; |
| 22 | + } |
| 23 | + |
| 24 | + var xhr = new XMLHttpRequest(); |
| 25 | + xhr.open("GET", ABS_BASE_URL + "/../versions.json"); |
| 26 | + xhr.onload = function () { |
| 27 | + var versions = JSON.parse(this.responseText); |
| 28 | + |
| 29 | + var realVersion = versions.find(function (i) { |
| 30 | + return i.version === CURRENT_VERSION || |
| 31 | + i.aliases.includes(CURRENT_VERSION); |
| 32 | + }).version; |
| 33 | + |
| 34 | + var select = makeSelect(versions.map(function (i) { |
| 35 | + return { text: i.title, value: i.version }; |
| 36 | + }), realVersion); |
| 37 | + select.addEventListener("change", function (event) { |
| 38 | + window.location.href = REL_BASE_URL + "/../" + this.value; |
| 39 | + }); |
| 40 | + |
| 41 | + var container = document.createElement("div"); |
| 42 | + container.id = "version-selector"; |
| 43 | + container.className = "md-nav__item"; |
| 44 | + container.appendChild(select); |
| 45 | + |
| 46 | + var sidebar = document.querySelector(".md-nav--primary > .md-nav__list"); |
| 47 | + sidebar.parentNode.insertBefore(container, sidebar); |
| 48 | + }; |
| 49 | + xhr.send(); |
| 50 | +}); |
0 commit comments