1/** 2 * @file 3 * Boots Weglot and renders the language picker in the top menu. 4 */ 5 6(function (drupalSettings) { 7 'use strict'; 8 9 var settings = drupalSettings.fairfin && drupalSettings.fairfin.weglot; 10 var apiKey = settings && settings.apiKey; 11 var Weglot = window.Weglot; 12 13 if (!apiKey || !Weglot) { 14 return; 15 } 16 17 // This file is loaded from the head on purpose: Weglot can only swap the text 18 // before the first paint if it starts before the body is parsed. 19 Weglot.initialize({ 20 api_key: apiKey, 21 // The picker below takes the place of Weglot's own floating switcher. 22 hide_switcher: true 23 }); 24 25 /** 26 * Returns the source language, followed by every enabled target language. 27 */ 28 function languageCodes() { 29 var codes = [Weglot.options.language_from]; 30 31 (Weglot.options.languages || []).forEach(function (language) { 32 if (language.enabled) { 33 codes.push(language.custom_code || language.language_to); 34 } 35 }); 36 37 return codes; 38 } 39 40 /** 41 * Marks the language that is currently on screen. 42 */ 43 function markActive(container, current) { 44 var buttons = container.querySelectorAll('.lang-picker__button'); 45 46 Array.prototype.forEach.call(buttons, function (button) { 47 var isActive = button.lang === current; 48 49 button.classList.toggle('is-active', isActive); 50 51 // Weglot rewrites the page in place, so aria-current is the only thing 52 // telling a screen reader which language it is reading. 53 if (isActive) { 54 button.setAttribute('aria-current', 'true'); 55 } else { 56 button.removeAttribute('aria-current'); 57 } 58 }); 59 } 60 61 function render(container) { 62 var codes = languageCodes(); 63 64 if (codes.length < 2) { 65 return; 66 } 67 68 var list = document.createElement('ul'); 69 list.className = 'lang-picker__list'; 70 71 codes.forEach(function (code) { 72 var item = document.createElement('li'); 73 item.className = 'lang-picker__item'; 74 75 var button = document.createElement('button'); 76 button.type = 'button'; 77 button.className = 'lang-picker__button'; 78 button.lang = code; 79 button.textContent = code.toUpperCase(); 80 button.addEventListener('click', function () { 81 Weglot.switchTo(code); 82 }); 83 84 item.appendChild(button); 85 list.appendChild(item); 86 }); 87 88 container.appendChild(list); 89 container.hidden = false; 90 markActive(container, Weglot.getCurrentLang()); 91 } 92 93 function whenReady(callback) { 94 if (document.readyState === 'loading') { 95 document.addEventListener('DOMContentLoaded', callback); 96 } else { 97 callback(); 98 } 99 } 100 101 function boot() { 102 whenReady(function () { 103 var container = document.querySelector('[data-lang-picker]'); 104 105 if (!container) { 106 return; 107 } 108 109 render(container); 110 111 Weglot.on('languageChanged', function (newLang) { 112 markActive(container, newLang); 113 }); 114 }); 115 } 116 117 // The language list only exists once Weglot has fetched the project config. 118 if (Weglot.initialized) { 119 boot(); 120 } else { 121 Weglot.on('initialized', boot); 122 } 123})(drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.