This repository was archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathivy-tab-list.js
201 lines (175 loc) · 4.62 KB
/
ivy-tab-list.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
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
import Ember from 'ember';
import layout from '../templates/components/ivy-tab-list';
/**
* @module ivy-tabs
*/
/**
* @class IvyTabListComponent
* @namespace IvyTabs
* @extends Ember.Component
*/
export default Ember.Component.extend({
layout: layout,
tagName: 'ul',
attributeBindings: ['aria-multiselectable', 'role'],
classNames: ['ivy-tab-list'],
init() {
this._super(...arguments);
Ember.run.once(this, this._registerWithTabsContainer);
},
willDestroy() {
this._super(...arguments);
Ember.run.once(this, this._unregisterWithTabsContainer);
},
/**
* Tells screenreaders that only one tab can be selected at a time.
*
* @property aria-multiselectable
* @type String
* @default 'false'
*/
'aria-multiselectable': 'false',
/**
* The `role` attribute of the tab list element.
*
* See http://www.w3.org/TR/wai-aria/roles#tablist
*
* @property role
* @type String
* @default 'tablist'
*/
role: 'tablist',
/**
* Gives focus to the selected tab.
*
* @method focusSelectedTab
*/
focusSelectedTab() {
this.get('selectedTab').$().focus();
},
/**
* Event handler for navigating tabs via arrow keys. The left (or up) arrow
* selects the previous tab, while the right (or down) arrow selects the next
* tab.
*
* @method navigateOnKeyDown
* @param {Event} event
*/
navigateOnKeyDown: Ember.on('keyDown', function(event) {
switch (event.keyCode) {
case 37: /* left */
case 38: /* up */
this.selectPreviousTab();
break;
case 39: /* right */
case 40: /* down */
this.selectNextTab();
break;
default:
return;
}
event.preventDefault();
Ember.run.scheduleOnce('afterRender', this, this.focusSelectedTab);
}),
/**
* Adds a tab to the `tabs` array.
*
* @method registerTab
* @param {IvyTabs.IvyTabComponent} tab
*/
registerTab(tab) {
this.get('tabs').pushObject(tab);
},
/**
* Selects the next tab in the list, if any.
*
* @method selectNextTab
*/
selectNextTab() {
let index = this.get('selected-index') + 1;
if (index === this.get('tabs.length')) { index = 0; }
this.selectTabByIndex(index);
},
/**
* Selects the previous tab in the list, if any.
*
* @method selectPreviousTab
*/
selectPreviousTab() {
let index = this.get('selected-index') - 1;
// Previous from the first tab should select the last tab.
if (index < 0) { index = this.get('tabs.length') - 1; }
// This would only happen if there are no tabs, so stay at 0.
if (index < 0) { index = 0; }
this.selectTabByIndex(index);
},
'selected-index': Ember.computed.alias('tabsContainer.selected-index'),
/**
* The currently-selected `ivy-tab` instance.
*
* @property selectedTab
* @type IvyTabs.IvyTabComponent
*/
selectedTab: Ember.computed(function() {
return this.get('tabs').objectAt(this.get('selected-index'));
}).property('selected-index', 'tabs.[]'),
/**
* Select the given tab.
*
* @method selectTab
* @param {IvyTabs.IvyTabComponent} tab
*/
selectTab(tab) {
this.selectTabByIndex(this.get('tabs').indexOf(tab));
},
/**
* Select the tab at `index`.
*
* @method selectTabByIndex
* @param {Number} index
*/
selectTabByIndex(index) {
this.set('selected-index', index);
},
tabs: Ember.computed(function() {
return Ember.A();
}).readOnly(),
_deprecatedParentViewBasedTabsContainer: Ember.computed('parentView', function() {
Ember.deprecate('Inferring `tabsContainer` from `parentView` on `{{ivy-tab-list}}` is deprecated. Please assign in an instance of `{{ivy-tabs}}` to the `tabsContainer` property.', false, {
id: 'ivy-tabs.ivy-tab-list.tabs-container-missing',
until: '2.0.0'
});
return this.get('parentView');
}).readOnly(),
/**
* The `ivy-tabs` component.
*
* @property tabsContainer
* @type IvyTabs.IvyTabsComponent
* @readOnly
*/
tabsContainer: Ember.computed.oneWay('_deprecatedParentViewBasedTabsContainer'),
/**
* Removes a tab from the `tabs` array.
*
* @method unregisterTab
* @param {IvyTabs.IvyTabComponent} tab
*/
unregisterTab(tab) {
const index = tab.get('index');
this.get('tabs').removeObject(tab);
if (index < this.get('selected-index')) {
this.selectPreviousTab();
} else if (tab.get('isSelected')) {
if (index !== 0) {
this.selectPreviousTab();
}
}
},
_registerWithTabsContainer() {
this.get('tabsContainer').registerTabList(this);
},
_unregisterWithTabsContainer() {
this.get('tabsContainer').unregisterTabList(this);
}
});