This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-grid-focusable-cell-container-behavior.html
181 lines (153 loc) · 5.67 KB
/
vaadin-grid-focusable-cell-container-behavior.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
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
<script>
window.vaadin = window.vaadin || {};
vaadin.elements = vaadin.elements || {};
vaadin.elements.grid = vaadin.elements.grid || {};
/**
* @polymerBehavior vaadin.elements.grid.FocusableCellContainerBehavior
*/
vaadin.elements.grid.FocusableCellContainerBehavior = {
properties: {
focused: {
type: Boolean,
reflectToAttribute: true
},
_focusedRow: Object,
_focusedRowIndex: Number,
_focusedCell: Object,
_focusedCellIndex: Number,
_lastFocusedCell: Object
},
observers: [
'_announceFocusedCell(_focusedCell, focused)',
'_dispatchEvents(_focusedCell, focused)',
'_focusedCellChanged(_focusedRowIndex, _focusedCellIndex)'
],
_announceFocusedCell: function(cell, focused) {
if (cell === undefined || focused === undefined) {
return;
}
// changing activeTarget steals focus so we don't want to do that while interacting with
// cell contents.
// TODO: remember to change activeTarget when navigation mode is activated back.
if (!this.domHost.navigating || !focused) {
return;
}
// note: VoiceOver doesn't work with dynamic IDs.
var activeTarget = Polymer.Element ? cell._cellContent.getAttribute('slot') : cell._cellContent.id;
if (this.is === 'vaadin-grid-table-body' && !cell.hasAttribute('detailscell')) {
// note: having `cell.column.name` property for announcing would maybe be a good option here?
var index = Array.prototype.indexOf.call(Polymer.dom(cell.parentElement).querySelectorAll('.vaadin-grid-cell'), cell);
var lastHeaderRow = this.domHost.$.header.lastElementChild;
var headerCell = lastHeaderRow.children[index];
activeTarget = headerCell._cellContent.id + ' ' + activeTarget;
}
this.domHost.$.footerFocusTrap.activeTarget = activeTarget;
},
_dispatchEvents: function(cell, focused) {
if (cell === undefined || focused === undefined) {
return;
}
// Dispatches cell-focusin and cell-focusout events on the cell content
// for the virtual focus. This helps cell contents to detect if the cell
// has focus, which is useful for the accessibility reasons. Used by
// <vaadin-grid-sorter>.
if (this._lastFocusedCell) {
this._lastFocusedCell._cellContent.dispatchEvent(new CustomEvent('cell-focusout'));
this._lastFocusedCell = undefined;
}
if (focused) {
cell._cellContent.dispatchEvent(new CustomEvent('cell-focusin'));
this._lastFocusedCell = cell;
}
},
_focusedCellChanged: function(rowIndex, cellIndex) {
if (rowIndex === undefined || cellIndex === undefined) {
return;
}
Array.prototype.forEach.call(Polymer.dom(this).children, function(row, i) {
row.focused = i === rowIndex;
if (row.focused) {
this._focusedRow = row;
this._focusedCellIndex = Math.min(cellIndex, row.children.length - 1);
this._focusedCell = row.children[this._focusedCellIndex];
}
row.cells.forEach(function(cell, j) {
cell.focused = j === this._focusedCellIndex;
}.bind(this));
}.bind(this));
},
focusLeft: function() {
if (this._focusedCell.hasAttribute('detailscell')) {
return;
}
var visibleCells = this._visibleCellIndexes();
if (visibleCells.length > 0) {
var current = visibleCells.indexOf(this._focusedCellIndex);
this._focusedCellIndex = visibleCells[Math.max(0, current - 1)];
}
},
focusDown: function() {
this._focusedRowIndex = Math.min(this._focusedRowIndex + 1, this.children.length - 1);
},
_visibleCellIndexes: function() {
var indexes = [];
if (this._focusedRow && this._focusedRow.children) {
var children = this._focusedRow.children;
for(var i = 0; i < children.length; i++) {
if (!children[i].hidden && children[i] !== this._focusedRow._rowDetailsCell) {
indexes.push(i);
}
}
indexes.sort(function(i1, i2) {
return children[i1].column._order < children[i2].column._order ? -1 : 1;
});
}
return indexes;
},
focusPageDown: function() {
this._focusedRowIndex = Math.min(this._focusedRowIndex + 10, this.children.length - 1);
},
focusPageUp: function() {
this._focusedRowIndex = Math.max(0, this._focusedRowIndex - 10);
},
focusRight: function() {
if (this._focusedCell.hasAttribute('detailscell')) {
return;
}
var visibleCells = this._visibleCellIndexes();
if (visibleCells.length > 0) {
var current = visibleCells.indexOf(this._focusedCellIndex);
this._focusedCellIndex = visibleCells[Math.min(current + 1, visibleCells.length - 1)];
}
},
focusUp: function() {
this._focusedRowIndex = Math.max(0, this._focusedRowIndex - 1);
},
focusHome: function() {
if (this._focusedCell.hasAttribute('detailscell')) {
return;
}
var visibleCells = this._visibleCellIndexes();
if (visibleCells.length > 0) {
this._focusedCellIndex = visibleCells[0];
}
},
focusEnd: function() {
if (this._focusedCell.hasAttribute('detailscell')) {
return;
}
var visibleCells = this._visibleCellIndexes();
if (visibleCells.length > 0) {
this._focusedCellIndex = visibleCells[visibleCells.length - 1];
}
},
focusFirst: function(e) {
this._focusedRowIndex = 0;
this.focusHome();
},
focusLast: function(e) {
this._focusedRowIndex = this.children.length - 1;
this.focusEnd();
}
};
</script>