Skip to content

Commit

Permalink
feat: lang preference persist
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed May 15, 2024
1 parent 7201634 commit eca52c6
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions qdrant-landing/themes/qdrant/static/js/lang-switcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
(function () {

const SWITCH_EVENT = 'switch-lang';
const DEFAULT_LANG = 'http';
const LANG_PREFERENCE_KEY = 'lang-preference';

/**
* @class LangSwitcher
* create tabs with buttons to switching
Expand Down Expand Up @@ -43,8 +48,10 @@
})

this.tabs.forEach(t => t.style.display = 'none');
activeTab.style.display = 'block';
if (activeTab) activeTab.style.display = 'block';
this.#setActiveButton(lang);

localStorage.setItem(LANG_PREFERENCE_KEY, lang);
}

setTabs(tabs) {
Expand All @@ -62,8 +69,8 @@

// should be used together with changing of the active tab visibility
#setActiveButton(lang) {
this.langButtons.querySelector('.active').classList.remove('active');
this.langButtons.querySelector(`[data-lang=${lang}]`).classList.add('active');
this.langButtons?.querySelector('.active')?.classList.remove('active');
this.langButtons?.querySelector(`[data-lang=${lang}]`)?.classList.add('active');
}
}

Expand Down Expand Up @@ -92,11 +99,25 @@
const langSwitcher = new LangSwitcher(g);
langSwitcher.initLangButtons();

const preferredLang = localStorage.getItem(LANG_PREFERENCE_KEY) ?? DEFAULT_LANG;
langSwitcher.switchLanguage(preferredLang);

const tabBtns = langSwitcher.getLangButtons();
if (tabBtns) {
langSwitcher.getLangButtons().addEventListener("click", (e) => {
e.target?.dataset?.lang && langSwitcher.switchLanguage(e.target.dataset.lang);
})
}

if (!tabBtns) return;

// Add global listeners for switching language in all all LangSwitcher instances
document.addEventListener(SWITCH_EVENT, (e) => {
langSwitcher.switchLanguage(e.detail.lang);
});

tabBtns.addEventListener('click', (e) => {
document.dispatchEvent(new CustomEvent(SWITCH_EVENT, {
detail: {
bubbles: true,
lang: e.target?.dataset?.lang
}
}))
});
})
}).call(this);

0 comments on commit eca52c6

Please sign in to comment.