Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default class TabSwitcher {
- constructor(info) {
- this.params = {
- buttonSwitch: info.buttonSwitch || true,
- tabs: info.tabs,
- buttons: info.buttons,
- };
- }
- init() {
- const { buttonSwitch, tabs, buttons } = this.params;
- for (let i = 0; i < buttons.length; i += 1) {
- const currBtn = buttons[i];
- const currTab = tabs[i];
- const { dataset } = currTab;
- let currTabID = 0;
- for (let x = 0; x < dataset.length; x += 1) {
- currTabID = dataset[x];
- }
- currBtn.addEventListener('click', (e) => {
- const { currentTarget } = e;
- const targetData = currentTarget.dataset;
- const activeItems = [];
- let tabID = 0;
- for (let x = 0; x < targetData.length; x += 1) {
- tabID = targetData[x];
- }
- for (let x = 0; x < tabs.length; x += 1) {
- const activeTab = tabs[x].classList.contains('active');
- const activeButton = buttons[x].classList.contains('active');
- if (activeTab) activeItems.push(tabs[x]);
- if (activeButton) activeItems.push(buttons[x]);
- }
- for (let x = 0; x < activeItems.length; x += 1) {
- const item = activeItems[x];
- const itemData = item.dataset;
- let isButton = false;
- let dataAttr = 0;
- for (let y = 0; y < itemData.length; y += 1) {
- dataAttr = itemData[y];
- if (dataAttr.match(/Btn/)) isButton = true;
- }
- const isButtonChange = isButton && buttonSwitch;
- if (isButtonChange || !isButton) item.classList.remove('active');
- }
- if (currTabID === tabID) currTab.classList.add('active');
- if (buttonSwitch) currentTarget.classList.add('active');
- });
- }
- }
- }
Add Comment
Please, Sign In to add comment