3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('autocomplete-list-keys', function(Y) {
10 Mixes keyboard support into AutoCompleteList. By default, this module is not
11 loaded for iOS and Android devices.
14 @submodule autocomplete-list-keys
25 Y.before(this._bindKeys, this, 'bindUI');
29 ListKeys.prototype = {
30 // -- Lifecycle Methods ----------------------------------------------------
33 Initializes keyboard command mappings.
39 _initKeys: function () {
43 // Register keyboard command handlers. _keys contains handlers that will
44 // always be called; _keysVisible contains handlers that will only be
45 // called when the list is visible.
46 keys[KEY_DOWN] = this._keyDown;
48 keysVisible[KEY_ENTER] = this._keyEnter;
49 keysVisible[KEY_ESC] = this._keyEsc;
50 keysVisible[KEY_TAB] = this._keyTab;
51 keysVisible[KEY_UP] = this._keyUp;
54 this._keysVisible = keysVisible;
57 destructor: function () {
62 Binds keyboard events.
67 _bindKeys: function () {
68 this._keyEvents = this._inputNode.on('keydown', this._onInputKey, this);
72 Unbinds keyboard events.
77 _unbindKeys: function () {
78 this._keyEvents && this._keyEvents.detach();
79 this._keyEvents = null;
82 // -- Protected Methods ----------------------------------------------------
85 Called when the down arrow key is pressed.
90 _keyDown: function () {
91 if (this.get('visible')) {
92 this._activateNextItem();
99 Called when the enter key is pressed.
104 _keyEnter: function (e) {
105 var item = this.get('activeItem');
108 this.selectItem(item, e);
110 // Don't prevent form submission when there's no active item.
116 Called when the escape key is pressed.
121 _keyEsc: function () {
126 Called when the tab key is pressed.
131 _keyTab: function (e) {
134 if (this.get('tabSelect')) {
135 item = this.get('activeItem');
138 this.selectItem(item, e);
147 Called when the up arrow key is pressed.
152 _keyUp: function () {
153 this._activatePrevItem();
156 // -- Protected Event Handlers ---------------------------------------------
159 Handles `inputNode` key events.
162 @param {EventTarget} e
165 _onInputKey: function (e) {
169 this._lastInputKey = keyCode;
171 if (this.get('results').length) {
172 handler = this._keys[keyCode];
174 if (!handler && this.get('visible')) {
175 handler = this._keysVisible[keyCode];
179 // A handler may return false to indicate that it doesn't wish
180 // to prevent the default key behavior.
181 if (handler.call(this, e) !== false) {
189 Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
192 }, '3.5.1' ,{requires:['autocomplete-list', 'base-build']});