forked from dojo/dijit-oldmirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_KeyNavContainer.js
More file actions
168 lines (143 loc) · 5.09 KB
/
_KeyNavContainer.js
File metadata and controls
168 lines (143 loc) · 5.09 KB
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
define([
"dojo/_base/array", // array.forEach
"dojo/_base/declare", // declare
"dojo/dom-attr", // domAttr.set
"dojo/_base/event", // event.stop
"dojo/_base/kernel", // kernel.deprecated
"dojo/keys", // keys.END keys.HOME
"dojo/_base/lang", // lang.hitch
"./registry",
"./_Container",
"./_FocusMixin",
"./_KeyNavMixin"
], function(array, declare, domAttr, event, kernel, keys, lang, registry, _Container, _FocusMixin, _KeyNavMixin){
// module:
// dijit/_KeyNavContainer
return declare("dijit._KeyNavContainer", [_FocusMixin, _Container, _KeyNavMixin], {
// summary:
// A _Container with keyboard navigation of its children.
// description:
// Provides normalized keyboard and focusing code for Container widgets.
// To use this mixin, call connectKeyNavHandlers() in postCreate().
// Also, child widgets must implement a focus() method.
connectKeyNavHandlers: function(/*keys[]*/ prevKeyCodes, /*keys[]*/ nextKeyCodes){
// summary:
// Call in postCreate() to attach the keyboard handlers to the container.
// prevKeyCodes: keys[]
// Key codes for navigating to the previous child.
// nextKeyCodes: keys[]
// Key codes for navigating to the next child.
// tags:
// protected
// TODO: remove for 2.0, and make subclasses override _onLeftArrow, _onRightArrow etc. instead.
var keyCodes = (this._keyNavCodes = {});
var prev = lang.hitch(this, "focusPrev");
var next = lang.hitch(this, "focusNext");
array.forEach(prevKeyCodes, function(code){ keyCodes[code] = prev; });
array.forEach(nextKeyCodes, function(code){ keyCodes[code] = next; });
keyCodes[keys.HOME] = lang.hitch(this, "focusFirstChild");
keyCodes[keys.END] = lang.hitch(this, "focusLastChild");
},
startupKeyNavChildren: function(){
kernel.deprecated("startupKeyNavChildren() call no longer needed", "", "2.0");
},
startup: function(){
this.inherited(arguments);
array.forEach(this.getChildren(), lang.hitch(this, "_startupChild"));
},
addChild: function(/*dijit/_WidgetBase*/ widget, /*int?*/ insertIndex){
this.inherited(arguments);
this._startupChild(widget);
},
_startupChild: function(/*dijit/_WidgetBase*/ widget){
// summary:
// Setup for each child widget.
// description:
// Sets tabIndex=-1 on each child, so that the tab key will
// leave the container rather than visiting each child.
//
// Note: if you add children by a different method than addChild(), then need to call this manually
// or at least make sure the child's tabIndex is -1.
//
// Note: see also _LayoutWidget.setupChild(), which is also called for each child widget.
// tags:
// private
widget.set("tabIndex", "-1");
},
_getFirstFocusableChild: function(){
// summary:
// Returns first child that can be focused.
// Leverage _getNextFocusableChild() to skip disabled children
return this._getNextFocusableChild(null, 1); // dijit/_WidgetBase
},
_getLastFocusableChild: function(){
// summary:
// Returns last child that can be focused.
// Leverage _getNextFocusableChild() to skip disabled children
return this._getNextFocusableChild(null, -1); // dijit/_WidgetBase
},
focusFirstChild: function(){
// summary:
// Focus the first focusable child in the container.
// tags:
// protected
this.focusChild(this._getFirstFocusableChild());
},
focusLastChild: function(){
// summary:
// Focus the last focusable child in the container.
// tags:
// protected
this.focusChild(this._getLastFocusableChild());
},
focusNext: function(){
// summary:
// Focus the next widget
// tags:
// protected
this.focusChild(this._getNextFocusableChild(this.focusedChild, 1));
},
focusPrev: function(){
// summary:
// Focus the last focusable node in the previous widget
// (ex: go to the ComboButton icon section rather than button section)
// tags:
// protected
this.focusChild(this._getNextFocusableChild(this.focusedChild, -1), true);
},
_getNextFocusableChild: function(child, dir){
// summary:
// Returns the next or previous focusable child, compared to "child".
// Implements and extends _KeyNavMixin._getNextFocusableChild() for a _Container.
// child: Widget
// The current widget
// dir: Integer
// - 1 = after
// - -1 = before
// tags:
// abstract extension
if(child){
child = this._getSiblingOfChild(child, dir);
}
var children = this.getChildren();
for(var i=0; i < children.length; i++){
if(!child){
child = children[(dir>0) ? 0 : (children.length-1)];
}
if(child.isFocusable()){
return child; // dijit/_WidgetBase
}
child = this._getSiblingOfChild(child, dir);
}
// no focusable child found
return null; // dijit/_WidgetBase
},
childSelector: function(/*DOMNode*/ node){
// Implement _KeyNavMixin.childSelector, to identify focusable child nodes.
// If we allowed a dojo/query dependency from this module this could more simply be a string "> *"
// instead of this function.
var node = registry.byNode(node);
return node && node.getParent() == this;
}
});
});