-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathindex.js
31 lines (31 loc) · 977 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Tabs {
constructor(options) {
let defaultOptions = {
element: '',
navSelector: '[data-role="tabs-nav"]',
panesSelector: '[data-role="tabs-panes"]',
activeClassName: 'active',
}
this.options = Object.assign({}, defaultOptions, options)
this.checkOptions().bindEvents().setDefaultTab()
}
checkOptions() {
if (!this.options.element) {
throw new Error('element is required')
}
return this
}
bindEvents() {
dom.on(this.options.element, 'click', `${this.options.navSelector}>li`, (e, el) => {
let index = dom.index(el)
let children = this.options.element.querySelector(this.options.panesSelector).children
dom.uniqueClass(el, this.options.activeClassName)
dom.uniqueClass(children[index], this.options.activeClassName)
})
return this
}
setDefaultTab() {
this.options.element.querySelector(`${this.options.navSelector}>li:first-child`).click()
return this
}
}