]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/autocomplete/autocomplete-list-keys.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / autocomplete / autocomplete-list-keys.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('autocomplete-list-keys', function(Y) {
9
10 /**
11  * Mixes keyboard support into AutoCompleteList. By default, this module is not
12  * loaded for iOS and Android devices.
13  *
14  * @module autocomplete
15  * @submodule autocomplete-list-keys
16  */
17
18  // keyCode constants.
19 var KEY_DOWN  = 40,
20     KEY_ENTER = 13,
21     KEY_ESC   = 27,
22     KEY_TAB   = 9,
23     KEY_UP    = 38;
24
25 function ListKeys() {
26     Y.before(this._unbindKeys, this, 'destructor');
27     Y.before(this._bindKeys, this, 'bindUI');
28
29     this._initKeys();
30 }
31
32 ListKeys.prototype = {
33     // -- Lifecycle Methods ----------------------------------------------------
34
35     /**
36      * Initializes keyboard command mappings.
37      *
38      * @method _initKeys
39      * @protected
40      * @for AutoCompleteList
41      */
42     _initKeys: function () {
43         var keys        = {},
44             keysVisible = {};
45
46         this._keyEvents = [];
47
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;
52
53         keysVisible[KEY_ENTER] = this._keyEnter;
54         keysVisible[KEY_ESC]   = this._keyEsc;
55         keysVisible[KEY_TAB]   = this._keyTab;
56         keysVisible[KEY_UP]    = this._keyUp;
57
58         this._keys        = keys;
59         this._keysVisible = keysVisible;
60     },
61
62     /**
63      * Binds keyboard events.
64      *
65      * @method _bindKeys
66      * @protected
67      */
68     _bindKeys: function () {
69         this._keyEvents.push(this._inputNode.on(
70                 Y.UA.gecko ? 'keypress' : 'keydown', this._onInputKey, this));
71     },
72
73     /**
74      * Unbinds keyboard events.
75      *
76      * @method _unbindKeys
77      * @protected
78      */
79     _unbindKeys: function () {
80         while (this._keyEvents.length) {
81             this._keyEvents.pop().detach();
82         }
83     },
84
85     // -- Protected Methods ----------------------------------------------------
86
87     /**
88      * Called when the down arrow key is pressed.
89      *
90      * @method _keyDown
91      * @protected
92      */
93     _keyDown: function () {
94         if (this.get('visible')) {
95             this._activateNextItem();
96         } else {
97             this.show();
98         }
99     },
100
101     /**
102      * Called when the enter key is pressed.
103      *
104      * @method _keyEnter
105      * @protected
106      */
107     _keyEnter: function () {
108         var item = this.get('activeItem');
109
110         if (item) {
111             this.selectItem(item);
112         } else {
113             // Don't prevent form submission when there's no active item.
114             return false;
115         }
116     },
117
118     /**
119      * Called when the escape key is pressed.
120      *
121      * @method _keyEsc
122      * @protected
123      */
124     _keyEsc: function () {
125         this.hide();
126     },
127
128     /**
129      * Called when the tab key is pressed.
130      *
131      * @method _keyTab
132      * @protected
133      */
134     _keyTab: function () {
135         var item;
136
137         if (this.get('tabSelect')) {
138             item = this.get('activeItem');
139
140             if (item) {
141                 this.selectItem(item);
142                 return true;
143             }
144         }
145
146         return false;
147     },
148
149     /**
150      * Called when the up arrow key is pressed.
151      *
152      * @method _keyUp
153      * @protected
154      */
155     _keyUp: function () {
156         this._activatePrevItem();
157     },
158
159     // -- Protected Event Handlers ---------------------------------------------
160
161     /**
162      * Handles <code>inputNode</code> key events.
163      *
164      * @method _onInputKey
165      * @param {EventTarget} e
166      * @protected
167      */
168     _onInputKey: function (e) {
169         var handler,
170             keyCode = e.keyCode;
171
172         this._lastInputKey = keyCode;
173
174         if (this.get('results').length) {
175             handler = this._keys[keyCode];
176
177             if (!handler && this.get('visible')) {
178                 handler = this._keysVisible[keyCode];
179             }
180
181             if (handler) {
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) {
185                     e.preventDefault();
186                 }
187             }
188         }
189     }
190 };
191
192 Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
193
194
195 }, '3.3.0' ,{requires:['autocomplete-list', 'base-build']});