/**
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() {
this._initKeys();
}
// -- 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.
this._keysVisible = keysVisible;
},
destructor: function () {
this._unbindKeys();
},
/**
Binds keyboard events.
@method _bindKeys
@protected
**/
_bindKeys: function () {
},
/**
Unbinds keyboard events.
@method _unbindKeys
@protected
**/
_unbindKeys: function () {
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) {
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')) {
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,
this._lastInputKey = keyCode;
}
if (handler) {
// A handler may return false to indicate that it doesn't wish
// to prevent the default key behavior.
e.preventDefault();
}
}
}
}
};