autocomplete-list-keys-debug.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. YUI 3.4.1 (build 4118)
  3. Copyright 2011 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) {
  8. /**
  9. * Mixes keyboard support into AutoCompleteList. By default, this module is not
  10. * loaded for iOS and Android devices.
  11. *
  12. * @module autocomplete
  13. * @submodule autocomplete-list-keys
  14. */
  15. // keyCode constants.
  16. var KEY_DOWN = 40,
  17. KEY_ENTER = 13,
  18. KEY_ESC = 27,
  19. KEY_TAB = 9,
  20. KEY_UP = 38;
  21. function ListKeys() {
  22. Y.before(this._unbindKeys, this, 'destructor');
  23. Y.before(this._bindKeys, this, 'bindUI');
  24. this._initKeys();
  25. }
  26. ListKeys.prototype = {
  27. // -- Lifecycle Methods ----------------------------------------------------
  28. /**
  29. * Initializes keyboard command mappings.
  30. *
  31. * @method _initKeys
  32. * @protected
  33. * @for AutoCompleteList
  34. */
  35. _initKeys: function () {
  36. var keys = {},
  37. keysVisible = {};
  38. this._keyEvents = [];
  39. // Register keyboard command handlers. _keys contains handlers that will
  40. // always be called; _keysVisible contains handlers that will only be
  41. // called when the list is visible.
  42. keys[KEY_DOWN] = this._keyDown;
  43. keysVisible[KEY_ENTER] = this._keyEnter;
  44. keysVisible[KEY_ESC] = this._keyEsc;
  45. keysVisible[KEY_TAB] = this._keyTab;
  46. keysVisible[KEY_UP] = this._keyUp;
  47. this._keys = keys;
  48. this._keysVisible = keysVisible;
  49. },
  50. /**
  51. * Binds keyboard events.
  52. *
  53. * @method _bindKeys
  54. * @protected
  55. */
  56. _bindKeys: function () {
  57. this._keyEvents.push(this._inputNode.on('keydown', this._onInputKey,
  58. this));
  59. },
  60. /**
  61. * Unbinds keyboard events.
  62. *
  63. * @method _unbindKeys
  64. * @protected
  65. */
  66. _unbindKeys: function () {
  67. while (this._keyEvents.length) {
  68. this._keyEvents.pop().detach();
  69. }
  70. },
  71. // -- Protected Methods ----------------------------------------------------
  72. /**
  73. * Called when the down arrow key is pressed.
  74. *
  75. * @method _keyDown
  76. * @protected
  77. */
  78. _keyDown: function () {
  79. if (this.get('visible')) {
  80. this._activateNextItem();
  81. } else {
  82. this.show();
  83. }
  84. },
  85. /**
  86. * Called when the enter key is pressed.
  87. *
  88. * @method _keyEnter
  89. * @protected
  90. */
  91. _keyEnter: function (e) {
  92. var item = this.get('activeItem');
  93. if (item) {
  94. this.selectItem(item, e);
  95. } else {
  96. // Don't prevent form submission when there's no active item.
  97. return false;
  98. }
  99. },
  100. /**
  101. * Called when the escape key is pressed.
  102. *
  103. * @method _keyEsc
  104. * @protected
  105. */
  106. _keyEsc: function () {
  107. this.hide();
  108. },
  109. /**
  110. * Called when the tab key is pressed.
  111. *
  112. * @method _keyTab
  113. * @protected
  114. */
  115. _keyTab: function (e) {
  116. var item;
  117. if (this.get('tabSelect')) {
  118. item = this.get('activeItem');
  119. if (item) {
  120. this.selectItem(item, e);
  121. return true;
  122. }
  123. }
  124. return false;
  125. },
  126. /**
  127. * Called when the up arrow key is pressed.
  128. *
  129. * @method _keyUp
  130. * @protected
  131. */
  132. _keyUp: function () {
  133. this._activatePrevItem();
  134. },
  135. // -- Protected Event Handlers ---------------------------------------------
  136. /**
  137. * Handles <code>inputNode</code> key events.
  138. *
  139. * @method _onInputKey
  140. * @param {EventTarget} e
  141. * @protected
  142. */
  143. _onInputKey: function (e) {
  144. var handler,
  145. keyCode = e.keyCode;
  146. this._lastInputKey = keyCode;
  147. if (this.get('results').length) {
  148. handler = this._keys[keyCode];
  149. if (!handler && this.get('visible')) {
  150. handler = this._keysVisible[keyCode];
  151. }
  152. if (handler) {
  153. // A handler may return false to indicate that it doesn't wish
  154. // to prevent the default key behavior.
  155. if (handler.call(this, e) !== false) {
  156. e.preventDefault();
  157. }
  158. }
  159. }
  160. }
  161. };
  162. Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
  163. }, '3.4.1' ,{requires:['autocomplete-list', 'base-build']});