Created
May 17, 2017 20:31
-
-
Save rodwyn/22ec40115203adcd3e28aa849a6a5b03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tab { | |
static readonly ACTIVE_CLASS: string = 'active'; | |
static readonly EVENT_ACTIVE: string = 'click'; | |
static readonly ATTR: string = 'data-tab-content-id'; | |
public handler: HTMLAnchorElement; | |
public content: Element; | |
public swipers: NodeListOf<Element>; | |
public selectors: NodeListOf<Element>; | |
constructor(handler: HTMLAnchorElement) { | |
this.handler = handler; | |
this.content = document.getElementById(handler.getAttribute(Tab.ATTR)); | |
this.selectors = document.querySelectorAll('.selector-item'); | |
this.toggle = this.toggle.bind(this); | |
this.handler.addEventListener(Tab.EVENT_ACTIVE, this.toggle); | |
} | |
public updateSwipers() { | |
const swipers = this.content | |
.querySelectorAll(`[${ Swiper.SWIPER_UID_ATTR }]`); | |
DOMUtils.syncForEach(swiper => { | |
const uId = swiper.getAttribute(Swiper.SWIPER_UID_ATTR); | |
DOMUtils.dispatchCustomEvent(uId, swiper); | |
}, swipers); | |
} | |
public toggle(event): void { | |
event.preventDefault(); | |
if (!DOMUtils.containsClass(this.handler, Tab.ACTIVE_CLASS)) { | |
const contentItems = this.content.parentElement.children; | |
const handlerItems = this.handler.parentElement.children; | |
const selector = document.querySelectorAll(`[data-tab-content-id = ${ this.handler.dataset.tabContentId }]`); | |
DOMUtils.removeClassToItems(contentItems, Tab.ACTIVE_CLASS); | |
DOMUtils.removeClassToItems(this.selectors, Tab.ACTIVE_CLASS); | |
DOMUtils.addClass(this.content, Tab.ACTIVE_CLASS); | |
DOMUtils.addClassToItems(selector, Tab.ACTIVE_CLASS); | |
this.updateSwipers(); | |
} | |
} | |
} | |
export { Tab }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment