forked from dojo/dojo1-dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard.js
218 lines (195 loc) · 6.44 KB
/
Keyboard.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
define([
"dojo/_base/declare",
"dojo/aspect",
"dojo/on",
"./List",
"dojo/_base/lang",
"dojo/has",
"put-selector/put",
"dojo/_base/Deferred",
"dojo/_base/sniff"
], function(declare, aspect, on, List, lang, has, put, Deferred){
var delegatingInputTypes = {
checkbox: 1,
radio: 1,
button: 1
},
hasGridCellClass = /\bdgrid-cell\b/,
hasGridRowClass = /\bdgrid-row\b/;
has.add("dom-contains", function(){
return !!document.createElement("a").contains;
});
function contains(parent, node){
// summary:
// Checks to see if an element is contained by another element.
if(has("dom-contains")){
return parent.contains(node);
}else{
return parent.compareDocumentPosition(node) & 8 /* DOCUMENT_POSITION_CONTAINS */;
}
}
return declare([List], {
// summary:
// Add keyboard navigation capability to a grid/list
pageSkip: 10,
tabIndex: 0,
postCreate: function(){
this.inherited(arguments);
var grid = this;
function handledEvent(event){
// text boxes and other inputs that can use direction keys should be ignored and not affect cell/row navigation
var target = event.target;
return target.type && (!delegatingInputTypes[target.type] || event.keyCode == 32);
}
function navigateArea(areaNode){
var isFocusableClass = grid.cellNavigation ? hasGridCellClass : hasGridRowClass,
cellFocusedElement = areaNode,
next;
function focusOnCell(element, event, dontFocus){
var cell = grid[grid.cellNavigation ? "cell" : "row"](element);
element = cell && cell.element;
if(!element){ return; }
if(!event.bubbles){
// IE doesn't always have a bubbles property already true, Opera will throw an error if you try to set it to true if it is already true
event.bubbles = true;
}
// clean up previously-focused element
// remove the class name and the tabIndex attribute
put(cellFocusedElement, "!dgrid-focus[!tabIndex]");
if(cellFocusedElement){
if(has("ie") < 8){
// clean up after workaround below (for non-input cases)
cellFocusedElement.style.position = "";
}
event.cell = cellFocusedElement;
on.emit(element, "dgrid-cellfocusout", event);
}
cellFocusedElement = element;
event.cell = element;
if(!dontFocus){
if(has("ie") < 8){
// setting the position to relative magically makes the outline
// work properly for focusing later on with old IE.
// (can't be done a priori with CSS or screws up the entire table)
element.style.position = "relative";
}
element.tabIndex = grid.tabIndex;
element.focus();
}
put(element, ".dgrid-focus");
on.emit(cellFocusedElement, "dgrid-cellfocusin", lang.mixin({ parentType: event.type }, event));
}
while((next = cellFocusedElement.firstChild) && next.tagName){
cellFocusedElement = next;
}
if(areaNode === grid.contentNode){
aspect.after(grid, "renderArray", function(ret){
// summary:
// Ensures the first element of a grid is always keyboard selectable after data has been
// retrieved if there is not already a valid focused element.
return Deferred.when(ret, function(ret){
// do not update the focused element if we already have a valid one
if(isFocusableClass.test(cellFocusedElement.className) && contains(areaNode, cellFocusedElement)){
return ret;
}
// ensure that the focused element is actually a grid cell, not a
// dgrid-preload or dgrid-content element, which should not be focusable,
// even when data is loaded asynchronously
for(var i = 0, elements = areaNode.getElementsByTagName("*"), element; (element = elements[i]); ++i){
if(isFocusableClass.test(element.className)){
cellFocusedElement = element;
break;
}
}
cellFocusedElement.tabIndex = grid.tabIndex;
return ret;
});
});
}else if(isFocusableClass.test(cellFocusedElement.className)){
cellFocusedElement.tabIndex = grid.tabIndex;
}
on(areaNode, "mousedown", function(event){
if(!handledEvent(event)){
focusOnCell(event.target, event);
}
});
on(areaNode, "keydown", function(event){
// For now, don't squash browser-specific functionalities by letting
// ALT and META function as they would natively
if(event.metaKey || event.altKey) {
return;
}
var focusedElement = event.target;
var keyCode = event.keyCode;
if(handledEvent(event)){
// text boxes and other inputs that can use direction keys should be ignored and not affect cell/row navigation
return;
}
var move = {
32: 0, // space bar
33: -grid.pageSkip, // page up
34: grid.pageSkip,// page down
37: -1, // left
38: -1, // up
39: 1, // right
40: 1, // down
35: 10000, //end
36: -10000 // home
}[keyCode];
if(isNaN(move)){
return;
}
var nextSibling, columnId, cell = grid.cell(cellFocusedElement);
var orientation;
if(keyCode == 37 || keyCode == 39){
// horizontal movement (left and right keys)
if(!grid.cellNavigation){
return; // do nothing for row-only navigation
}
orientation = "right";
}else{
// other keys are vertical
orientation = "down";
columnId = cell && cell.column && cell.column.id;
cell = grid.row(cellFocusedElement);
}
if(move){
cell = cell && grid[orientation](cell, move, true);
}
var nextFocus = cell && cell.element;
if(nextFocus){
if(columnId){
nextFocus = grid.cell(nextFocus, columnId).element;
}
if(grid.cellNavigation){
var inputs = nextFocus.getElementsByTagName("input");
var inputFocused;
for(var i = 0;i < inputs.length; i++){
var input = inputs[i];
if((input.tabIndex != -1 || "lastValue" in input) && !input.disabled){
// focusing here requires the same workaround for IE<8,
// though here we can get away with doing it all at once.
if(has("ie") < 8){ input.style.position = "relative"; }
input.focus();
if(has("ie") < 8){ input.style.position = ""; }
inputFocused = true;
break;
}
}
}
focusOnCell(nextFocus, event, inputFocused);
}
event.preventDefault();
});
return function(target){
target = target || cellFocusedElement;
focusOnCell(target, { target: target });
}
}
if(this.tabableHeader){
this.focusHeader = navigateArea(this.headerNode);
}
this.focus = navigateArea(this.contentNode);
}
});
});