/
autocomplete-list-keys.js
186 lines (146 loc) · 4 KB
/
autocomplete-list-keys.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
YUI.add('autocomplete-list-keys', function(Y) {
/**
Mixes keyboard support into AutoCompleteList. By default, this module is not
loaded for iOS and Android devices.
@module autocomplete
@submodule autocomplete-list-keys
**/
// keyCode constants.
var KEY_DOWN = 40,
KEY_ENTER = 13,
KEY_ESC = 27,
KEY_TAB = 9,
KEY_UP = 38;
function ListKeys() {
Y.before(this._bindKeys, this, 'bindUI');
this._initKeys();
}
ListKeys.prototype = {
// -- Lifecycle Methods ----------------------------------------------------
/**
Initializes keyboard command mappings.
@method _initKeys
@protected
@for AutoCompleteList
**/
_initKeys: function () {
var keys = {},
keysVisible = {};
// Register keyboard command handlers. _keys contains handlers that will
// always be called; _keysVisible contains handlers that will only be
// called when the list is visible.
keys[KEY_DOWN] = this._keyDown;
keysVisible[KEY_ENTER] = this._keyEnter;
keysVisible[KEY_ESC] = this._keyEsc;
keysVisible[KEY_TAB] = this._keyTab;
keysVisible[KEY_UP] = this._keyUp;
this._keys = keys;
this._keysVisible = keysVisible;
},
destructor: function () {
this._unbindKeys();
},
/**
Binds keyboard events.
@method _bindKeys
@protected
**/
_bindKeys: function () {
this._keyEvents = this._inputNode.on('keydown', this._onInputKey, this);
},
/**
Unbinds keyboard events.
@method _unbindKeys
@protected
**/
_unbindKeys: function () {
this._keyEvents && this._keyEvents.detach();
this._keyEvents = null;
},
// -- Protected Methods ----------------------------------------------------
/**
Called when the down arrow key is pressed.
@method _keyDown
@protected
**/
_keyDown: function () {
if (this.get('visible')) {
this._activateNextItem();
} else {
this.show();
}
},
/**
Called when the enter key is pressed.
@method _keyEnter
@protected
**/
_keyEnter: function (e) {
var item = this.get('activeItem');
if (item) {
this.selectItem(item, e);
} else {
// Don't prevent form submission when there's no active item.
return false;
}
},
/**
Called when the escape key is pressed.
@method _keyEsc
@protected
**/
_keyEsc: function () {
this.hide();
},
/**
Called when the tab key is pressed.
@method _keyTab
@protected
**/
_keyTab: function (e) {
var item;
if (this.get('tabSelect')) {
item = this.get('activeItem');
if (item) {
this.selectItem(item, e);
return true;
}
}
return false;
},
/**
Called when the up arrow key is pressed.
@method _keyUp
@protected
**/
_keyUp: function () {
this._activatePrevItem();
},
// -- Protected Event Handlers ---------------------------------------------
/**
Handles `inputNode` key events.
@method _onInputKey
@param {EventTarget} e
@protected
**/
_onInputKey: function (e) {
var handler,
keyCode = e.keyCode;
this._lastInputKey = keyCode;
if (this.get('results').length) {
handler = this._keys[keyCode];
if (!handler && this.get('visible')) {
handler = this._keysVisible[keyCode];
}
if (handler) {
// A handler may return false to indicate that it doesn't wish
// to prevent the default key behavior.
if (handler.call(this, e) !== false) {
e.preventDefault();
}
}
}
}
};
Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
}, '@VERSION@' ,{requires:['autocomplete-list', 'base-build']});