forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtabs.ts
347 lines (309 loc) · 10.5 KB
/
tabs.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import anim from "animejs";
import { Carousel } from "./carousel";
import { Component, BaseOptions, InitElements, MElement } from "./component";
export interface TabsOptions extends BaseOptions {
/**
* Transition duration in milliseconds.
* @default 300
*/
duration: number;
/**
* Callback for when a new tab content is shown.
* @default null
*/
onShow: (newContent: Element) => void;
/**
* Set to true to enable swipeable tabs.
* This also uses the responsiveThreshold option.
* @default false
*/
swipeable: boolean;
/**
* The maximum width of the screen, in pixels,
* where the swipeable functionality initializes.
* @default infinity
*/
responsiveThreshold: number;
};
let _defaults: TabsOptions = {
duration: 300,
onShow: null,
swipeable: false,
responsiveThreshold: Infinity // breakpoint for swipeable
};
export class Tabs extends Component<TabsOptions> {
_tabLinks: NodeListOf<HTMLAnchorElement>;
_index: number;
_indicator: any;
_tabWidth: number;
_tabsWidth: number;
_tabsCarousel: any;
_activeTabLink: any;
_content: any;
constructor(el: HTMLElement, options: Partial<TabsOptions>) {
super(el, options, Tabs);
(this.el as any).M_Tabs = this;
this.options = {
...Tabs.defaults,
...options
};
this._tabLinks = this.el.querySelectorAll('li.tab > a');
this._index = 0;
this._setupActiveTabLink();
if (this.options.swipeable) {
this._setupSwipeableTabs();
} else {
this._setupNormalTabs();
}
// Setup tabs indicator after content to ensure accurate widths
this._setTabsAndTabWidth();
this._createIndicator();
this._setupEventHandlers();
}
static get defaults(): TabsOptions {
return _defaults;
}
/**
* Initializes instance of Tabs.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLElement, options?: Partial<TabsOptions>): Tabs;
/**
* Initializes instances of Tabs.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: InitElements<MElement>, options?: Partial<TabsOptions>): Tabs[];
/**
* Initializes instances of Tabs.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: HTMLElement | InitElements<MElement>, options: Partial<TabsOptions> = {}): Tabs | Tabs[] {
return super.init(els, options, Tabs);
}
static getInstance(el: HTMLElement): Tabs {
return (el as any).M_Tabs;
}
destroy() {
this._removeEventHandlers();
this._indicator.parentNode.removeChild(this._indicator);
if (this.options.swipeable) {
this._teardownSwipeableTabs();
}
else {
this._teardownNormalTabs();
}
(this.el as any).M_Tabs = undefined;
}
/**
* The index of tab that is currently shown.
*/
get index(){ return this._index; }
_setupEventHandlers() {
window.addEventListener('resize', this._handleWindowResize);
this.el.addEventListener('click', this._handleTabClick);
}
_removeEventHandlers() {
window.removeEventListener('resize', this._handleWindowResize);
this.el.removeEventListener('click', this._handleTabClick);
}
_handleWindowResize = () => {
this._setTabsAndTabWidth();
if (this._tabWidth !== 0 && this._tabsWidth !== 0) {
this._indicator.style.left = this._calcLeftPos(this._activeTabLink)+'px';
this._indicator.style.right = this._calcRightPos(this._activeTabLink)+'px';
}
}
_handleTabClick = (e: MouseEvent) => {
const tabLink = e.target as HTMLAnchorElement;
const tab = tabLink.parentElement;
// Handle click on tab link only
if (!tabLink || !tab.classList.contains('tab')) return;
// is disabled?
if (tab.classList.contains('disabled')) {
e.preventDefault();
return;
}
// Act as regular link if target attribute is specified.
if (tabLink.hasAttribute('target')) return;
// Make the old tab inactive.
this._activeTabLink.classList.remove('active');
const _oldContent = this._content;
// Update the variables with the new link and content
this._activeTabLink = tabLink;
if (tabLink.hash)
this._content = document.querySelector(tabLink.hash);
this._tabLinks = this.el.querySelectorAll('li.tab > a');
// Make the tab active
this._activeTabLink.classList.add('active');
const prevIndex = this._index;
this._index = Math.max(Array.from(this._tabLinks).indexOf(tabLink), 0);
// Swap content
if (this.options.swipeable) {
if (this._tabsCarousel) {
this._tabsCarousel.set(this._index, () => {
if (typeof this.options.onShow === 'function')
this.options.onShow.call(this, this._content);
});
}
} else {
if (this._content) {
this._content.style.display = 'block';
this._content.classList.add('active');
if (typeof this.options.onShow === 'function')
this.options.onShow.call(this, this._content);
if (_oldContent && _oldContent !== this._content) {
_oldContent.style.display = 'none';
_oldContent.classList.remove('active');
}
}
}
// Update widths after content is swapped (scrollbar bugfix)
this._setTabsAndTabWidth();
this._animateIndicator(prevIndex);
e.preventDefault();
}
_createIndicator() {
const indicator = document.createElement('li');
indicator.classList.add('indicator');
this.el.appendChild(indicator);
this._indicator = indicator;
this._indicator.style.left = this._calcLeftPos(this._activeTabLink)+'px';
this._indicator.style.right = this._calcRightPos(this._activeTabLink)+'px';
}
_setupActiveTabLink() {
// If the location.hash matches one of the links, use that as the active tab.
this._activeTabLink = Array.from(this._tabLinks).find((a: HTMLAnchorElement) => a.getAttribute('href') === location.hash);
// If no match is found, use the first link or any with class 'active' as the initial active tab.
if (!this._activeTabLink) {
this._activeTabLink = this.el.querySelector('li.tab a.active');
}
if (this._activeTabLink.length === 0) {
this._activeTabLink = this.el.querySelector('li.tab a');
}
Array.from(this._tabLinks).forEach((a: HTMLAnchorElement) => a.classList.remove('active'));
this._activeTabLink.classList.add('active');
this._index = Math.max(Array.from(this._tabLinks).indexOf(this._activeTabLink), 0);
if (this._activeTabLink && this._activeTabLink.hash) {
this._content = document.querySelector(this._activeTabLink.hash);
this._content.classList.add('active');
}
}
_setupSwipeableTabs() {
// Change swipeable according to responsive threshold
if (window.innerWidth > this.options.responsiveThreshold)
this.options.swipeable = false;
const tabsContent = [];
this._tabLinks.forEach(a => {
if (a.hash) {
const currContent = document.querySelector(a.hash);
currContent.classList.add('carousel-item');
tabsContent.push(currContent);
}
});
// Create Carousel-Wrapper around Tab-Contents
const tabsWrapper = document.createElement('div');
tabsWrapper.classList.add('tabs-content', 'carousel', 'carousel-slider');
// Wrap around
tabsContent[0].parentElement.insertBefore(tabsWrapper, tabsContent[0]);
tabsContent.forEach(tabContent => {
tabsWrapper.appendChild(tabContent);
tabContent.style.display = '';
});
// Keep active tab index to set initial carousel slide
const tab = this._activeTabLink.parentElement;
const activeTabIndex = Array.from(tab.parentNode.children).indexOf(tab);
this._tabsCarousel = Carousel.init(tabsWrapper, {
fullWidth: true,
noWrap: true,
onCycleTo: (item) => {
const prevIndex = this._index;
this._index = Array.from(item.parentNode.children).indexOf(item);
this._activeTabLink.classList.remove('active');
this._activeTabLink = Array.from(this._tabLinks)[this._index];
this._activeTabLink.classList.add('active');
this._animateIndicator(prevIndex);
if (typeof this.options.onShow === 'function')
this.options.onShow.call(this, this._content);
}
});
// Set initial carousel slide to active tab
this._tabsCarousel.set(activeTabIndex);
}
_teardownSwipeableTabs() {
const tabsWrapper = this._tabsCarousel.el;
this._tabsCarousel.destroy();
// Unwrap
tabsWrapper.after(tabsWrapper.children);
tabsWrapper.remove();
}
_setupNormalTabs() {
// Hide Tabs Content
Array.from(this._tabLinks).forEach((a) => {
if (a === this._activeTabLink) return;
if ((<HTMLAnchorElement>a).hash) {
const currContent = document.querySelector((<HTMLAnchorElement>a).hash);
if (currContent) (<HTMLElement>currContent).style.display = 'none';
}
});
}
_teardownNormalTabs() {
// show Tabs Content
this._tabLinks.forEach((a) => {
if (a.hash) {
const currContent = document.querySelector(a.hash) as HTMLElement;
if (currContent) currContent.style.display = '';
}
});
}
_setTabsAndTabWidth() {
this._tabsWidth = this.el.getBoundingClientRect().width;
this._tabWidth = Math.max(this._tabsWidth, this.el.scrollWidth) / this._tabLinks.length;
}
_calcRightPos(el) {
return Math.ceil(this._tabsWidth - el.offsetLeft - el.getBoundingClientRect().width);
}
_calcLeftPos(el) {
return Math.floor(el.offsetLeft);
}
/**
* Recalculate tab indicator position. This is useful when
* the indicator position is not correct.
*/
updateTabIndicator() {
this._setTabsAndTabWidth();
this._animateIndicator(this._index);
}
_animateIndicator(prevIndex) {
let leftDelay = 0, rightDelay = 0;
if (this._index - prevIndex >= 0)
leftDelay = 90;
else
rightDelay = 90;
const animOptions = {
targets: this._indicator,
left: {
value: this._calcLeftPos(this._activeTabLink),
delay: leftDelay
},
right: {
value: this._calcRightPos(this._activeTabLink),
delay: rightDelay
},
duration: this.options.duration,
easing: 'easeOutQuad'
};
anim.remove(this._indicator);
anim(animOptions);
}
/**
* Show tab content that corresponds to the tab with the id.
* @param tabId The id of the tab that you want to switch to.
*/
select(tabId: string) {
const tab = Array.from(this._tabLinks).find((a: HTMLAnchorElement) => a.getAttribute('href') === '#'+tabId);
if (tab) (<HTMLAnchorElement>tab).click();
}
}