Files
landing_page/qdrant-landing/themes/qdrant/static/js/lang-switcher.js
T
e301681488 Lang switcher (#7)
* language switcher for documentation pages

* some cleanup and an example of usage

* fix highlight elements ordering

* fix for unexpected behavior

* styles fix

* changed tag

* copy button for code blocks in documentation

* warning and info styled elements for documentation

* docs auto-sync

* fixed 1px extra padding and prevented tabs generation when there is only one language

* docs auto-sync

* fix

* docs auto-sync

* moved css for information aside blocks to the article file

* fix

Co-authored-by: Andrey Vasnetsov <andrey@vasnetsov.com>
Co-authored-by: qdrant <qdrant@users.noreply.github.com>
2022-03-15 11:06:01 +01:00

102 lines
2.7 KiB
JavaScript

(function () {
/**
* @class LangSwitcher
* create tabs with buttons to switching
* between languages variants,
* works with highlights.js
*/
class LangSwitcher {
constructor(tabs, index) {
this.tabs = tabs || [];
this.index = index || 0;
this.langButtons = null;
}
/**
* creates tabs with languages names in the interface
*/
initLangButtons() {
if (this.tabs.length < 2) {
return null;
}
// create a wrapper element
this.langButtons = document.createElement('div');
this.langButtons.classList.add('lang-tabs');
// adds a button for each tab
this.tabs.forEach((tab, i) => {
const lang = this.getLang(tab);
let button = document.createElement('span');
button.classList.add('lang-tabs__button');
(i == 0) && button.classList.add('active');
button.dataset.lang = lang;
button.innerText = lang;
// append new button into wrapper element
this.langButtons.append(button);
});
this.tabs[0].before(this.langButtons);
}
switchLanguage(lang) {
const activeTab = this.tabs.find(t => {
return t.querySelectorAll(`code.language-${lang}`).length > 0
})
this.tabs.forEach(t => t.style.display = 'none');
activeTab.style.display = 'block';
this.#setActiveButton(lang);
}
setTabs(tabs) {
tabs[0].active = true;
this.tabs = tabs;
}
getLang(tab) {
return tab.getElementsByTagName('code')[0].dataset.lang;
}
getLangButtons() {
return this.langButtons;
}
// 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');
}
}
const allTabs = document.getElementsByClassName('highlight')
let tabsGroups = [];
let groupArr = [];
/**
* go through all tabs (elements with class .highlight)
*/
for (let hl of allTabs) {
groupArr.push(hl);
let isLastInGroup = !hl.nextElementSibling?.classList?.contains('highlight');
if (isLastInGroup) {
tabsGroups.push(groupArr);
groupArr = [];
}
}
/**
* init switcher for each group of tabs
*/
tabsGroups.forEach((g, i) => {
const langSwitcher = new LangSwitcher(g);
langSwitcher.initLangButtons();
const tabBtns = langSwitcher.getLangButtons();
if (tabBtns) {
langSwitcher.getLangButtons().addEventListener("click", (e) => {
e.target?.dataset?.lang && langSwitcher.switchLanguage(e.target.dataset.lang);
})
}
})
}).call(this);