From e3e297a3d0b144531ab19ef0e25a70623eb501f2 Mon Sep 17 00:00:00 2001 From: Anastasia Alexandrova Date: Tue, 17 Dec 2024 12:16:27 +0100 Subject: [PATCH] Updated version-select.js (#711) --- docs/js/version-select.js | 68 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/docs/js/version-select.js b/docs/js/version-select.js index 1bf17f74d..b24febf38 100644 --- a/docs/js/version-select.js +++ b/docs/js/version-select.js @@ -1,25 +1,27 @@ /* * Custom version of same taken from mike code for injecting version switcher into percona.com -*/ + */ -window.addEventListener("DOMContentLoaded", function() { +window.addEventListener('DOMContentLoaded', function () { // This is a bit hacky. Figure out the base URL from a known CSS file the // template refers to... - var ex = new RegExp("/?css/version-select.css$"); + var ex = new RegExp('/?css/version-select.css$'); var sheet = document.querySelector('link[href$="version-select.css"]'); - var ABS_BASE_URL = sheet.href.replace(ex, ""); - var CURRENT_VERSION = ABS_BASE_URL.split("/").pop(); + if (!sheet) { + return; + } + + var ABS_BASE_URL = sheet.href.replace(ex, ''); + var CURRENT_VERSION = ABS_BASE_URL.split('/').pop(); function makeSelect(options, selected) { - var select = document.createElement("select"); -// select.classList.add("form-control"); - select.classList.add("btn"); - select.classList.add("btn-primary"); - - options.forEach(function(i) { - var option = new Option(i.text, i.value, undefined, - i.value === selected); + var select = document.createElement('select'); + select.classList.add('btn'); + select.classList.add('btn-primary'); + + options.forEach(function (i) { + var option = new Option(i.text, i.value, undefined, i.value === selected); select.add(option); }); @@ -27,36 +29,36 @@ window.addEventListener("DOMContentLoaded", function() { } var xhr = new XMLHttpRequest(); - xhr.open("GET", ABS_BASE_URL + "/../versions.json"); - xhr.onload = function() { + xhr.open('GET', ABS_BASE_URL + '/../versions.json'); + xhr.onload = function () { var versions = JSON.parse(this.responseText); - var realVersion = versions.find(function(i) { - return i.version === CURRENT_VERSION || - i.aliases.includes(CURRENT_VERSION); + var realVersion = versions.find(function (i) { + return ( + i.version === CURRENT_VERSION || i.aliases.includes(CURRENT_VERSION) + ); }).version; - var select = makeSelect(versions.map(function(i) { - return {text: i.title, value: i.version}; - }), realVersion); - select.addEventListener("change", function(event) { - window.location.href = ABS_BASE_URL + "/../" + this.value; + var select = makeSelect( + versions.map(function (i) { + return { text: i.title, value: i.version }; + }), + realVersion + ); + select.addEventListener('change', function (event) { + window.location.href = ABS_BASE_URL + '/../' + this.value; }); - var container = document.createElement("div"); - container.id = "custom_select"; - container.classList.add("side-column-block"); - - // Label -// var label = document.createElement("span"); -// label.textContent = "PMM version: "; -// container.appendChild(label); + var container = document.createElement('div'); + container.id = 'custom_select'; + container.classList.add('side-column-block'); // Add menu container.appendChild(select); - var sidebar = document.querySelector("#version-select-wrapper"); // Inject menu into element with this ID + var sidebar = document.querySelector('#version-select-wrapper'); // Inject menu into element with this ID sidebar.appendChild(container); }; + xhr.send(); -}); +}); \ No newline at end of file