const LITERALS = { closeModal: }; class IdmTabsModal{ constructor(config){ const {title, tabs, cacheContainerSelector} = config; this.title = title; this.tabs = tabs; this.currentTabId = null; this.cacheContainer = document.querySelector(cacheContainerSelector); //before open container is cacheContainer this.container = this.cacheContainer; this.init(); } getTabsButtonsHTML(){ return this.tabs.map((tab, i) => ` `).join(""); } updateActiveItemsHelper({selector, activeClass}){ const items = this.container.querySelectorAll(selector); items.forEach(item => { const tabId = parseInt(item.dataset.tabId); const isActive = tabId === this.currentTabId; if(isActive){ item.classList.add(activeClass); return; } item.classList.remove(activeClass); }) } updateActiveButtons(){ this.updateActiveItemsHelper({selector:".idm-tabs-modal__button", activeClass:"idm-tabs-modal__button--active"}); } mountTabs(){ const content = this.container.querySelector(".idm-tabs-modal__content"); content.innerHTML = ''; this.tabs.forEach((tab, i) => { const div = document.createElement('div'); div.className = 'idm-tabs-modal__tab'; div.dataset.tabId = i; div.innerHTML = tab.html; tab.ref = div; content.appendChild(div); }); } updateActiveTabs(){ this.updateActiveItemsHelper({selector:".idm-tabs-modal__tab", activeClass:"idm-tabs-modal__tab--active"}); } async changeTab(tabId){ this.currentTabId = tabId; this.updateActiveButtons(); this.updateActiveTabs(); const currentTab = this.tabs[tabId]; const isInitialised = currentTab?.isInitialised; if(!isInitialised && typeof currentTab.onInit === 'function'){ currentTab.isInitialised = true; await currentTab?.onInit(currentTab.ref, this); } } initTabButtonsEvents(){ const buttons = this.container.querySelectorAll(".idm-tabs-modal__button"); buttons.forEach(button => button.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); const tabId = parseInt(button.dataset.tabId); this.changeTab(tabId); })); } initButtonsEvents(){ this.initTabButtonsEvents(); } getModalHTML(){ return `

${this.title}

${this.getTabsButtonsHTML()}
` } // render modal in cache container initModal(){ const html = this.getModalHTML(); this.cacheContainer.innerHTML = html; // this.modal = new Modal({html , classList:"idm-tabs-modal", afterShow: (modal)=>this.container=modal}); } show(){ const element = this.cacheContainer.querySelector(".idm-tabs-modal__wrapper"); this.modal = new Modal({element , classList:"idm-tabs-modal", afterShow: (modal)=>this.container=modal}); } initButtons(){ this.updateActiveButtons(); this.initButtonsEvents(); } initTabs(){ this.mountTabs(); this.changeTab(0); } init(){ this.initModal(); this.initButtons(); this.initTabs(); } }