-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreegrid.js
362 lines (320 loc) · 10.2 KB
/
treegrid.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// TODO put aria-readme everywhere
function onReady(treegrid, moveByWordModifier) {
function isChecked(id) {
return document.getElementById(id).checked;
}
function shouldKeepColAfterRowNav() {
return isChecked('keepColAfterRowNav');
}
function shouldResetToRowModeAfterBlur() {
return isChecked('resetToRowModeAfterBlur');
}
function shouldFocusFirstColumn() {
return isChecked('focusFirstColumn');
}
function addTabIndex() {
// Add tabindex="0" to first row, "-1" to other rows
// We will use the roving tabindex method since aria-activedescendant
// does not work in IE
var rows = getAllRows();
var index = rows.length;
while (index -- ) {
rows[index].tabIndex = index ? -1 : 0;
}
}
function getAllRows() {
var nodeList = treegrid.querySelectorAll('tbody > tr');
return Array.prototype.slice.call(nodeList);
}
function getAllNavigableRows() {
var nodeList = treegrid.querySelectorAll('tbody > tr[tabindex]:not([aria-hidden="true"])');
// Convert to array so that we can use array methods on it
return Array.prototype.slice.call(nodeList);
}
function getNavigableCols(currentRow) {
var nodeList = currentRow.getElementsByTagName('td');
return Array.prototype.slice.call(nodeList);
}
function restrictIndex(index, numItems) {
if (index < 0) {
return 0;
}
return index >= numItems ? index - 1: index;
}
function focus(elem) {
elem.tabIndex = 0; // Ensure focusable
elem.focus();
}
// Restore tabIndex to what it should be when focus switches from
// one treegrid item to another
function onFocusIn(event) {
var prevTreeGridFocus = onFocusIn.prevTreeGridFocus;
var newTreeGridFocus =
event.target !== window && treegrid.contains(event.target) && event.target;
if (!newTreeGridFocus) {
// Moved out of treegrid
if (prevTreeGridFocus && prevTreeGridFocus.localName === 'td' &&
shouldResetToRowModeAfterBlur()) {
// When focus leaves treegrid, reset focus mode back to rows
prevTreeGridFocus.removeAttribute('tabindex');
setTimeout(function() {
// Wait for a moment so that we don't end up back on row when
// trying to shift+tab out of grid
prevTreeGridFocus.parentElement.tabIndex = 0;
}, 0);
}
onFocusIn.prevTreeGridFocus = null;
return;
}
if (prevTreeGridFocus) {
// Stayed in treegrid
if (prevTreeGridFocus.localName === 'td') {
// Cells are focusable via click, navigation is only via keystroke
prevTreeGridFocus.removeAttribute('tabindex');
}
else if (prevTreeGridFocus.localName === 'tr') {
// Rows are focusable via click
prevTreeGridFocus.tabIndex = -1;
}
}
// This is the new element to tab into within the container
// Waiting fixes bug with tabbing when screen readers active in IE
setTimeout(function() {
newTreeGridFocus.tabIndex = 0;
}, 0);
// In tree grid
onFocusIn.prevTreeGridFocus = newTreeGridFocus;
}
function getCurrentRow() {
var possibleRow = document.activeElement;
if (treegrid.contains(possibleRow)) {
while (possibleRow !== treegrid) {
if (possibleRow.localName === 'tr') {
return possibleRow;
}
possibleRow = possibleRow.parentElement;
}
}
}
function getCurrentColumn(currentRow) {
if (currentRow) {
var possibleCol = document.activeElement;
if (currentRow.contains(possibleCol)) {
while (possibleCol !== currentRow) {
if (possibleCol.localName === 'td') {
return possibleCol;
}
possibleCol = possibleCol.parentElement;
}
}
}
}
function getLevel(row) {
return row && parseInt(row.getAttribute('aria-level'));
}
// Move backwards (direction = -1) or forwards (direction = 1)
// If we also need to move down/up a level, requireLevelChange = true
// When
function moveByRow(direction, requireLevelChange) {
var currentRow = getCurrentRow();
var requiredLevel = requireLevelChange && currentRow &&
getLevel(currentRow) + direction;
var rows = getAllNavigableRows();
var numRows = rows.length;
var rowIndex = currentRow ? rows.indexOf(currentRow) : -1;
// When moving down a level, only allow moving to next row as the
// first child will never be farther than that
var maxDistance = requireLevelChange && direction === 1 ? 1 : NaN;
// Move in direction until required level is found
do {
if (maxDistance -- === 0) {
return; // Failed to find required level, return without focus change
}
rowIndex = restrictIndex(rowIndex + direction, numRows);
}
while (requiredLevel && requiredLevel !== getLevel(rows[rowIndex]));
if (!shouldKeepColAfterRowNav() ||
!switchRowAndColFocus(currentRow, rows[rowIndex])) {
focus(rows[rowIndex]);
}
}
function switchRowAndColFocus(fromRow, toRow) {
var currentCol = getCurrentColumn(fromRow);
if (!currentCol) {
return;
}
var fromCols = getNavigableCols(fromRow);
var currentColIndex = fromCols.indexOf(currentCol);
var firstFocusableColIndex = shouldFocusFirstColumn() ? 0 : 1;
if (currentColIndex < firstFocusableColIndex) {
return;
}
var toCols = getNavigableCols(toRow);
focus(toCols[currentColIndex]);
return true;
}
function moveByCol(direction) {
var currentRow = getCurrentRow();
if (!currentRow) {
return;
}
var cols = getNavigableCols(currentRow);
var numCols = cols.length;
var currentCol = getCurrentColumn(currentRow);
var currentColIndex = cols.indexOf(currentCol);
var newColIndex;
var firstFocusableColIndex = shouldFocusFirstColumn() ? 0 : 1;
// First alt/ctrl+right moves to first column
newColIndex = (currentCol || direction < 0) ? currentColIndex + direction :
firstFocusableColIndex;
// Moving past beginning focuses row
if (newColIndex < firstFocusableColIndex) {
focus(currentRow);
return;
}
newColIndex = restrictIndex(newColIndex, numCols);
focus(cols[newColIndex]);
}
function moveToExtreme(direction) {
var currentRow = getCurrentRow();
if (!currentRow) {
return;
}
var currentCol = getCurrentColumn(currentRow);
if (currentCol) {
moveToExtremeCol(direction, currentRow);
}
else {
// Move to first/last row
moveToExtremeRow(direction);
}
}
function moveToExtremeCol(direction, currentRow) {
// Move to first/last col
var cols = getNavigableCols(currentRow);
if (direction === -1) {
if (shouldFocusFirstColumn()) {
focus(cols[0]);
}
else {
focus(currentRow);
}
}
else {
focus(cols[cols.length - 1]);
}
}
function moveToExtremeRow(direction) {
var rows = getAllNavigableRows();
var newRow = rows[direction > 0 ? rows.length - 1 : 0];
focus(newRow);
}
function changeExpanded(doExpand) {
var currentRow = getCurrentRow();
if (!currentRow) {
return;
}
var currentLevel = getLevel(currentRow);
var rows = getAllRows();
var currentRowIndex = rows.indexOf(currentRow);
console.assert(currentRowIndex >= 0);
var didChange;
var doExpandLevel = [];
doExpandLevel[currentLevel + 1] = doExpand;
while (++ currentRowIndex < rows.length) {
var nextRow = rows[currentRowIndex];
var rowLevel = getLevel(nextRow);
if (rowLevel <= currentLevel) {
break; // Next row is not a level down from current row
}
// Only expand the next level if this level is expanded
// and previous level is expanded
doExpandLevel[rowLevel + 1] =
doExpandLevel[rowLevel] &&
nextRow.getAttribute('aria-expanded') === 'true';
var willHideRow = !doExpandLevel[rowLevel];
var isRowHidden = nextRow.getAttribute('aria-hidden') === 'true';
if (willHideRow !== isRowHidden) {
nextRow.setAttribute('aria-hidden', willHideRow);
didChange = true;
}
}
if (didChange) {
currentRow.setAttribute('aria-expanded', doExpand);
return true;
}
}
function onKeyDown(event) {
function isMoveByWordModifierPressed() {
// Be very strict about move-by-word keystroke detection as we don't
// want to prevent other commands in OS or screen reader
if (!event[moveByWordModifier]) {
return;
}
var numModifiersPressed = Boolean(event.ctrlKey) + Boolean(event.altKey);
return numModifiersPressed === 1; // No more than one modifer pressed
}
function isAltOrCtrlPressed() {
return event.ctrlKey || event.altKey;
}
function isUnusedModifierCombo() {
if (event.metaKey || event.shiftKey) {
return true; // We ignore these no matter what
}
if (event.keyCode === LEFT || event.keyCode === RIGHT) {
if (isAltOrCtrlPressed() && !isMoveByWordModifierPressed()) {
return true;
}
}
else if (isAltOrCtrlPressed()) {
return true;
}
}
var UP = 38;
var DOWN = 40;
var LEFT = 37;
var RIGHT = 39;
var HOME = 36;
var END = 35;
if (isUnusedModifierCombo(event)) {
return;
}
switch (event.keyCode) {
case DOWN:
moveByRow(1); break;
case UP:
moveByRow(-1); break;
case LEFT:
if (isMoveByWordModifierPressed()) {
moveByCol(-1);
}
else {
changeExpanded(false) || moveByRow(-1, true);
}
break;
case RIGHT:
if (isMoveByWordModifierPressed()) {
moveByCol(1);
}
else {
changeExpanded(true) || moveByRow(1, true);
}
break;
case HOME:
moveToExtreme(-1); break;
case END:
moveToExtreme(1); break;
default:
return;
}
// Important: don't use key for anything else, such as scrolling
event.preventDefault();
}
addTabIndex();
treegrid.addEventListener('keydown', onKeyDown);
window.addEventListener('focusin', onFocusIn);
}
document.addEventListener('DOMContentLoaded', function() {
var isMac = navigator.platform.substr(0,3) === 'Mac';
onReady(document.getElementById('treegrid'), isMac ? 'altKey' : 'ctrlKey');
});