2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('autocomplete-list-keys', function(Y) {
11 * Mixes keyboard support into AutoCompleteList. By default, this module is not
12 * loaded for iOS and Android devices.
14 * @module autocomplete
15 * @submodule autocomplete-list-keys
26 Y.before(this._unbindKeys, this, 'destructor');
27 Y.before(this._bindKeys, this, 'bindUI');
32 ListKeys.prototype = {
33 // -- Lifecycle Methods ----------------------------------------------------
36 * Initializes keyboard command mappings.
40 * @for AutoCompleteList
42 _initKeys: function () {
48 // Register keyboard command handlers. _keys contains handlers that will
49 // always be called; _keysVisible contains handlers that will only be
50 // called when the list is visible.
51 keys[KEY_DOWN] = this._keyDown;
53 keysVisible[KEY_ENTER] = this._keyEnter;
54 keysVisible[KEY_ESC] = this._keyEsc;
55 keysVisible[KEY_TAB] = this._keyTab;
56 keysVisible[KEY_UP] = this._keyUp;
59 this._keysVisible = keysVisible;
63 * Binds keyboard events.
68 _bindKeys: function () {
69 this._keyEvents.push(this._inputNode.on(
70 Y.UA.gecko ? 'keypress' : 'keydown', this._onInputKey, this));
74 * Unbinds keyboard events.
79 _unbindKeys: function () {
80 while (this._keyEvents.length) {
81 this._keyEvents.pop().detach();
85 // -- Protected Methods ----------------------------------------------------
88 * Called when the down arrow key is pressed.
93 _keyDown: function () {
94 if (this.get('visible')) {
95 this._activateNextItem();
102 * Called when the enter key is pressed.
107 _keyEnter: function () {
108 var item = this.get('activeItem');
111 this.selectItem(item);
113 // Don't prevent form submission when there's no active item.
119 * Called when the escape key is pressed.
124 _keyEsc: function () {
129 * Called when the tab key is pressed.
134 _keyTab: function () {
137 if (this.get('tabSelect')) {
138 item = this.get('activeItem');
141 this.selectItem(item);
150 * Called when the up arrow key is pressed.
155 _keyUp: function () {
156 this._activatePrevItem();
159 // -- Protected Event Handlers ---------------------------------------------
162 * Handles <code>inputNode</code> key events.
164 * @method _onInputKey
165 * @param {EventTarget} e
168 _onInputKey: function (e) {
172 this._lastInputKey = keyCode;
174 if (this.get('results').length) {
175 handler = this._keys[keyCode];
177 if (!handler && this.get('visible')) {
178 handler = this._keysVisible[keyCode];
182 // A handler may return false to indicate that it doesn't wish
183 // to prevent the default key behavior.
184 if (handler.call(this, e) !== false) {
192 Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
195 }, '3.3.0' ,{requires:['autocomplete-list', 'base-build']});