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

feat: Persist snippets language preference #900

Closed
wants to merge 1 commit into from
Closed
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
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);
Loading