Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactorings + release #6

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 105 additions & 70 deletions dropdowns.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,115 @@
/**
* Handles Weglot language switcher dropdowns in Webflow.
* {@link https://weglot-2-0-ui-kit.webflow.io/}
*/
var Webflow = Webflow || [];
Webflow.push(function () {
const dropdownElements = document.querySelectorAll("[weglot='dropdown']");

Weglot.on('initialized', () => {
dropdownElements.forEach(dropdown => {
initializeSwitchers(dropdown);
updateSwitcherLanguage(window.Weglot.getCurrentLang(), dropdown);
});
const dropdownElements = document.querySelectorAll("[weglot='dropdown']");

window.Weglot.on("initialized", () => {
dropdownElements.forEach((dropdown) => {
const currentLang = window.Weglot.getCurrentLang();

initializeSwitcher(dropdown);
updateSwitcherLanguage(currentLang, dropdown);
});
});

window.Weglot.on("languageChanged", (activeLanguage) => {
dropdownElements.forEach((dropdown) => {
updateSwitcherLanguage(activeLanguage, dropdown);
});
});

/**
* Initializes a language switcher dropdown
* @param {Element} dropdown
*/
function initializeSwitcher(dropdown) {
const languageOptions = dropdown.querySelectorAll(".w-dropdown-list a");

Weglot.on('languageChanged', (activeLanguage) => {
dropdownElements.forEach(dropdown => {
updateSwitcherLanguage(activeLanguage, dropdown);
});
languageOptions.forEach((link) => {
// Make sure the language option isn't translated
link.setAttribute("data-wg-notranslate", "");
// Add the click listener
link.addEventListener("click", handleClick);
});

function initializeSwitchers(dropdown) {
const languageOptions = dropdown.querySelectorAll(".w-dropdown-list a");

languageOptions.forEach((link) => {
// Make sure the language option isn't translated
link.setAttribute('data-wg-notranslate', '');
// Add the click listener
link.addEventListener('click', handleClick);
});

function handleClick(e) {
e.preventDefault();
const langOption = e.target.closest(`a[lang]`);
if (!langOption) {
return;
}
Weglot.switchTo(langOption.getAttribute('lang'));
}
function handleClick(e) {
e.preventDefault();

const langOption = e.target.closest(`a[lang]`);
if (!langOption) return;

const newLang = langOption.getAttribute("lang");

window.Weglot.switchTo(newLang);
}
}

/**
* Updates the language switcher dropdown
* @param {string} newLanguage
* @param {Element} dropdown
*/
function updateSwitcherLanguage(newLanguage, dropdown) {
const newLanguageLinks = dropdown.querySelectorAll(
`.w-dropdown-list a[lang="${newLanguage}"]`
);
const toggles = dropdown.querySelectorAll(".w-dropdown-toggle");

if (!newLanguageLinks.length || !toggles.length) {
return;
}

function updateSwitcherLanguage(newLanguage, dropdown) {
const newLanguageLinks = dropdown.querySelectorAll(`.w-dropdown-list a[lang="${newLanguage}"]`);
const oldToggles = dropdown.querySelectorAll(".w-dropdown-toggle");

if (!newLanguageLinks.length || !oldToggles) {
return;
}

let oldSrc = dropdown.querySelector(`.w-dropdown-toggle [weglot='flag']`);

if (oldSrc) {
oldSrc = oldSrc.getAttribute('src');
const newSrc = dropdown.querySelector(`.w-dropdown-list a[lang="${newLanguage}"] img`).getAttribute('src');

newLanguageLinks.forEach((link) => {
const image = link.querySelector("[weglot='flag']");
image.src = oldSrc;
});

oldToggles.forEach((toggle) => {
const image = toggle.querySelector("[weglot='flag']");
image.src = newSrc;
});
}

const oldText = dropdown.querySelector(`.w-dropdown-toggle [weglot='text']`).innerText;
const oldLanguage = dropdown.querySelector(".w-dropdown-toggle").getAttribute('lang');

const newText = dropdown.querySelector(`.w-dropdown-list a[lang="${newLanguage}"] [weglot='text']`).innerText;

newLanguageLinks.forEach((link) => {
const text = link.querySelector("[weglot='text']");
text.innerText = oldText;
link.setAttribute('lang', oldLanguage);
});

oldToggles.forEach((toggle) => {
const text = toggle.querySelector("[weglot='text']");
text.innerText = newText;
toggle.setAttribute('lang', newLanguage);
});
const oldFlag = dropdown.querySelector(
`.w-dropdown-toggle [weglot='flag']`
);

if (oldFlag) {
const oldFlagSrc = oldFlag.getAttribute("src");
const newFlagSrc = dropdown
.querySelector(`.w-dropdown-list a[lang="${newLanguage}"] img`)
?.getAttribute("src");

newLanguageLinks.forEach((link) => {
const image = link.querySelector("[weglot='flag']");
image.src = oldFlagSrc;
});

toggles.forEach((toggle) => {
const image = toggle.querySelector("[weglot='flag']");
image.src = newFlagSrc;
});
}

const oldText = dropdown.querySelector(
`.w-dropdown-toggle [weglot='text']`
).innerText;
const oldLanguage = dropdown
.querySelector(".w-dropdown-toggle")
?.getAttribute("lang");

const newText = dropdown.querySelector(
`.w-dropdown-list a[lang="${newLanguage}"] [weglot='text']`
).innerText;

newLanguageLinks.forEach((link) => {
const text = link.querySelector("[weglot='text']");
if (!text) return;

text.innerText = oldText;

link.setAttribute("lang", oldLanguage);
});

toggles.forEach((toggle) => {
const text = toggle.querySelector("[weglot='text']");
if (!text) return;

text.innerText = newText;

toggle.setAttribute("lang", newLanguage);
});
}
});