MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / autocomplete-list-keys / autocomplete-list-keys-debug.js
blob354d381becf12be9365ad612ea24971fff1f04a4
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('autocomplete-list-keys', function(Y) {
9 /**
10 Mixes keyboard support into AutoCompleteList. By default, this module is not
11 loaded for iOS and Android devices.
13 @module autocomplete
14 @submodule autocomplete-list-keys
15 **/
17  // keyCode constants.
18 var KEY_DOWN  = 40,
19     KEY_ENTER = 13,
20     KEY_ESC   = 27,
21     KEY_TAB   = 9,
22     KEY_UP    = 38;
24 function ListKeys() {
25     Y.before(this._bindKeys, this, 'bindUI');
26     this._initKeys();
29 ListKeys.prototype = {
30     // -- Lifecycle Methods ----------------------------------------------------
32     /**
33     Initializes keyboard command mappings.
35     @method _initKeys
36     @protected
37     @for AutoCompleteList
38     **/
39     _initKeys: function () {
40         var keys        = {},
41             keysVisible = {};
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;
53         this._keys        = keys;
54         this._keysVisible = keysVisible;
55     },
57     destructor: function () {
58         this._unbindKeys();
59     },
61     /**
62     Binds keyboard events.
64     @method _bindKeys
65     @protected
66     **/
67     _bindKeys: function () {
68         this._keyEvents = this._inputNode.on('keydown', this._onInputKey, this);
69     },
71     /**
72     Unbinds keyboard events.
74     @method _unbindKeys
75     @protected
76     **/
77     _unbindKeys: function () {
78         this._keyEvents && this._keyEvents.detach();
79         this._keyEvents = null;
80     },
82     // -- Protected Methods ----------------------------------------------------
84     /**
85     Called when the down arrow key is pressed.
87     @method _keyDown
88     @protected
89     **/
90     _keyDown: function () {
91         if (this.get('visible')) {
92             this._activateNextItem();
93         } else {
94             this.show();
95         }
96     },
98     /**
99     Called when the enter key is pressed.
101     @method _keyEnter
102     @protected
103     **/
104     _keyEnter: function (e) {
105         var item = this.get('activeItem');
107         if (item) {
108             this.selectItem(item, e);
109         } else {
110             // Don't prevent form submission when there's no active item.
111             return false;
112         }
113     },
115     /**
116     Called when the escape key is pressed.
118     @method _keyEsc
119     @protected
120     **/
121     _keyEsc: function () {
122         this.hide();
123     },
125     /**
126     Called when the tab key is pressed.
128     @method _keyTab
129     @protected
130     **/
131     _keyTab: function (e) {
132         var item;
134         if (this.get('tabSelect')) {
135             item = this.get('activeItem');
137             if (item) {
138                 this.selectItem(item, e);
139                 return true;
140             }
141         }
143         return false;
144     },
146     /**
147     Called when the up arrow key is pressed.
149     @method _keyUp
150     @protected
151     **/
152     _keyUp: function () {
153         this._activatePrevItem();
154     },
156     // -- Protected Event Handlers ---------------------------------------------
158     /**
159     Handles `inputNode` key events.
161     @method _onInputKey
162     @param {EventTarget} e
163     @protected
164     **/
165     _onInputKey: function (e) {
166         var handler,
167             keyCode = e.keyCode;
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];
176             }
178             if (handler) {
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) {
182                     e.preventDefault();
183                 }
184             }
185         }
186     }
189 Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
192 }, '3.5.1' ,{requires:['autocomplete-list', 'base-build']});