This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Tabs.vue
179 lines (143 loc) · 5.08 KB
/
Tabs.vue
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
<template>
<div class="tabs-component">
<ul role="tablist" class="tabs-component-tabs">
<li
v-for="(tab, i) in tabs"
:key="i"
:class="{ 'is-active': tab.isActive, 'is-disabled': tab.isDisabled }"
class="tabs-component-tab"
role="presentation"
v-show="tab.isVisible"
>
<a v-html="tab.header"
:aria-controls="tab.hash"
:aria-selected="tab.isActive"
@click="selectTab(tab.hash, $event)"
:href="tab.hash"
class="tabs-component-tab-a"
role="tab"
></a>
</li>
</ul>
<div class="tabs-component-panels">
<slot/>
</div>
</div>
</template>
<script>
import expiringStorage from '../expiringStorage';
export default {
props: {
cacheLifetime: {
default: 5,
},
options: {
type: Object,
required: false,
default: () => ({
useUrlFragment: true,
defaultTabHash: null,
}),
},
},
data: () => ({
tabs: [],
activeTabHash: '',
activeTabIndex: 0,
lastActiveTabHash: '',
}),
computed: {
storageKey() {
return `vue-tabs-component.cache.${window.location.host}${window.location.pathname}`;
},
},
created() {
this.tabs = this.$children;
},
mounted() {
window.addEventListener('hashchange', () => this.selectTab(window.location.hash));
if (this.findTab(window.location.hash)) {
this.selectTab(window.location.hash);
return;
}
const previousSelectedTabHash = expiringStorage.get(this.storageKey);
if (this.findTab(previousSelectedTabHash)) {
this.selectTab(previousSelectedTabHash);
return;
}
if(this.options.defaultTabHash !== null && this.findTab("#" + this.options.defaultTabHash)) {
this.selectTab("#" + this.options.defaultTabHash);
return;
}
if (this.tabs.length) {
this.selectTab(this.tabs[0].hash);
}
},
methods: {
findTab(hash) {
return this.tabs.find(tab => tab.hash === hash);
},
selectTab(selectedTabHash, event) {
// See if we should store the hash in the url fragment.
if (event && !this.options.useUrlFragment) {
event.preventDefault();
}
const selectedTab = this.findTab(selectedTabHash);
if (! selectedTab) {
return;
}
if (selectedTab.isDisabled) {
event.preventDefault();
return;
}
if (this.lastActiveTabHash === selectedTab.hash) {
this.$emit('clicked', { tab: selectedTab });
return;
}
this.tabs.forEach(tab => {
tab.isActive = (tab.hash === selectedTab.hash);
});
this.$emit('changed', { tab: selectedTab });
this.activeTabHash = selectedTab.hash;
this.activeTabIndex = this.getTabIndex(selectedTabHash);
this.lastActiveTabHash = this.activeTabHash = selectedTab.hash;
expiringStorage.set(this.storageKey, selectedTab.hash, this.cacheLifetime);
},
setTabVisible(hash, visible) {
const tab = this.findTab(hash);
if (! tab) {
return;
}
tab.isVisible = visible;
if (tab.isActive) {
// If tab is active, set a different one as active.
tab.isActive = visible;
this.tabs.every((tab, index, array) => {
if (tab.isVisible) {
tab.isActive = true;
return false;
}
return true;
});
}
},
getTabIndex(hash){
const tab = this.findTab(hash);
return this.tabs.indexOf(tab);
},
getTabHash(index){
const tab = this.tabs.find(tab => this.tabs.indexOf(tab) === index);
if (!tab) {
return;
}
return tab.hash;
},
getActiveTab(){
return this.findTab(this.activeTabHash);
},
getActiveTabIndex() {
return this.getTabIndex(this.activeTabHash);
},
},
};
</script>