|
| 1 | +/** |
| 2 | + * JS related to our product switcher in the sidebar |
| 3 | + */ |
| 4 | + |
| 5 | +// helpers ------------------- |
| 6 | + |
| 7 | +function getElById(id) { |
| 8 | + return document.getElementById(id) |
| 9 | +} |
| 10 | + |
| 11 | +var products = { |
| 12 | + "en": {name: "Cloud", url: "docs/2.0/"}, |
| 13 | + "ja": {name: "Server", url: "docs/server/2.x/"} |
| 14 | +} |
| 15 | + |
| 16 | +// List of elements that have dynamic content based on our selected product |
| 17 | +var els = { |
| 18 | + sidebarProdSelect: getElById("sidebarProdSelect"), |
| 19 | +}; |
| 20 | + |
| 21 | +function reloadWithNewProduct(prodCode) { |
| 22 | + window.location.href = "https://circleci.com/" + products[prodCode].url |
| 23 | +} |
| 24 | + |
| 25 | +// Sets the sidebar product picker to the currently selected product |
| 26 | +function handleSetProductOnLoad() { |
| 27 | + var currentProd = window.currentProd; // current prod is set in _includes/sidebar.html |
| 28 | + |
| 29 | + // Set value for 'sidebar' |
| 30 | + for(var i, j = 0; i = els.sidebaProdSelect.options[j]; j++) { |
| 31 | + if(i.value == currentProd) { |
| 32 | + els.sidebarProdSelect.selectedIndex = j; |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Function to run when the sidebar product <select> is changed. |
| 40 | + * It checks the newly selected product code and reloads the page in that product. |
| 41 | + */ |
| 42 | +function handleChangeProductSidebar() { |
| 43 | + els.sidebarProdSelect.addEventListener("change", function(e) { |
| 44 | + switch(e.target.value) { |
| 45 | + case "server": |
| 46 | + reloadWithNewLocale("server") |
| 47 | + break; |
| 48 | + case "en": |
| 49 | + reloadWithNewLocale("en") |
| 50 | + break; |
| 51 | + default: |
| 52 | + return; |
| 53 | + } |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +export function init() { |
| 59 | + handleChangeProductSidebar(); |
| 60 | + handleSetProductOnLoad(); |
| 61 | +} |
0 commit comments