forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaadin-grid-a11y-mixin.html
120 lines (104 loc) · 3.6 KB
/
vaadin-grid-a11y-mixin.html
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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<script>
window.Vaadin = window.Vaadin || {};
Vaadin.Grid = Vaadin.Grid || {};
/**
* @polymerMixin
*/
Vaadin.Grid.A11yMixin = superClass => class A11yMixin extends superClass {
static get observers() {
return [
'_a11yUpdateGridSize(size, _columnTree, _columnTree.*)'
];
}
_a11yGetHeaderRowCount(_columnTree) {
return _columnTree.filter(level => level.some(col => col.headerTemplate)).length;
}
_a11yGetFooterRowCount(_columnTree) {
return _columnTree.filter(level => level.some(col => col.headerTemplate)).length;
}
_a11yUpdateGridSize(size, _columnTree) {
if (size === undefined || _columnTree === undefined) {
return;
}
const bodyColumns = _columnTree[_columnTree.length - 1];
this.$.table.setAttribute(
'aria-rowcount',
size + this._a11yGetHeaderRowCount(_columnTree) + this._a11yGetFooterRowCount(_columnTree)
);
this.$.table.setAttribute('aria-colcount', bodyColumns && bodyColumns.length || 0);
this._a11yUpdateHeaderRows();
this._a11yUpdateFooterRows();
}
_a11yUpdateHeaderRows() {
Array.from(this.$.header.children).forEach((headerRow, index) =>
headerRow.setAttribute('aria-rowindex', index + 1)
);
}
_a11yUpdateFooterRows() {
Array.from(this.$.footer.children).forEach((footerRow, index) =>
footerRow.setAttribute(
'aria-rowindex',
this._a11yGetHeaderRowCount(this._columnTree) + this.size + index + 1
)
);
}
_a11yUpdateRowRowindex(row, index) {
row.setAttribute('aria-rowindex', index + this._a11yGetHeaderRowCount(this._columnTree) + 1);
}
_a11yUpdateRowSelected(row, selected) {
// Jaws reads selection only for rows, NVDA only for cells
row.setAttribute('aria-selected', Boolean(selected));
Array.from(row.children).forEach(cell =>
cell.setAttribute('aria-selected', Boolean(selected))
);
}
_a11yUpdateRowLevel(row, level) {
Array.from(row.children).forEach(cell =>
// aria-level indexing starts from 1
cell.setAttribute('aria-level', level + 1)
);
row.setAttribute('aria-level', level + 1);
}
_a11yUpdateRowDetailsOpened(row, detailsOpened) {
Array.from(row.children).forEach(cell => {
if (typeof detailsOpened === 'boolean') {
cell.setAttribute('aria-expanded', detailsOpened);
} else {
if (cell.hasAttribute('aria-expanded')) {
cell.removeAttribute('aria-expanded');
}
}
});
}
_a11ySetRowDetailsCell(row, detailsCell) {
Array.from(row.children).forEach(cell => {
if (cell !== detailsCell) {
cell.setAttribute('aria-controls', detailsCell.id);
}
});
}
_a11yUpdateCellColspan(cell, colspan) {
cell.setAttribute('aria-colspan', Number(colspan));
}
_a11yUpdateSorters() {
Array.from(this.querySelectorAll('vaadin-grid-sorter')).forEach(sorter => {
let cellContent = sorter.parentNode;
while (cellContent && cellContent.localName !== 'vaadin-grid-cell-content') {
cellContent = cellContent.parentNode;
}
if (cellContent) {
const cell = cellContent.assignedSlot.parentNode;
cell.setAttribute('aria-sort', {
'asc': 'ascending',
'desc': 'descending'
}[String(sorter.direction)] || 'none');
}
});
}
};
</script>