[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/autocomplete-list-keys/ -> autocomplete-list-keys.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('autocomplete-list-keys', function (Y, NAME) {
   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._bindKeys, this, 'bindUI');
  27      this._initKeys();
  28  }
  29  
  30  ListKeys.prototype = {
  31      // -- Lifecycle Methods ----------------------------------------------------
  32  
  33      /**
  34      Initializes keyboard command mappings.
  35  
  36      @method _initKeys
  37      @protected
  38      @for AutoCompleteList
  39      **/
  40      _initKeys: function () {
  41          var keys        = {},
  42              keysVisible = {};
  43  
  44          // Register keyboard command handlers. _keys contains handlers that will
  45          // always be called; _keysVisible contains handlers that will only be
  46          // called when the list is visible.
  47          keys[KEY_DOWN] = this._keyDown;
  48  
  49          keysVisible[KEY_ENTER] = this._keyEnter;
  50          keysVisible[KEY_ESC]   = this._keyEsc;
  51          keysVisible[KEY_TAB]   = this._keyTab;
  52          keysVisible[KEY_UP]    = this._keyUp;
  53  
  54          this._keys        = keys;
  55          this._keysVisible = keysVisible;
  56      },
  57  
  58      destructor: function () {
  59          this._unbindKeys();
  60      },
  61  
  62      /**
  63      Binds keyboard events.
  64  
  65      @method _bindKeys
  66      @protected
  67      **/
  68      _bindKeys: function () {
  69          this._keyEvents = this._inputNode.on('keydown', this._onInputKey, this);
  70      },
  71  
  72      /**
  73      Unbinds keyboard events.
  74  
  75      @method _unbindKeys
  76      @protected
  77      **/
  78      _unbindKeys: function () {
  79          this._keyEvents && this._keyEvents.detach();
  80          this._keyEvents = null;
  81      },
  82  
  83      // -- Protected Methods ----------------------------------------------------
  84  
  85      /**
  86      Called when the down arrow key is pressed.
  87  
  88      @method _keyDown
  89      @protected
  90      **/
  91      _keyDown: function () {
  92          if (this.get('visible')) {
  93              this._activateNextItem();
  94          } else {
  95              this.show();
  96          }
  97      },
  98  
  99      /**
 100      Called when the enter key is pressed.
 101  
 102      @method _keyEnter
 103      @protected
 104      **/
 105      _keyEnter: function (e) {
 106          var item = this.get('activeItem');
 107  
 108          if (item) {
 109              this.selectItem(item, e);
 110          } else {
 111              // Don't prevent form submission when there's no active item.
 112              return false;
 113          }
 114      },
 115  
 116      /**
 117      Called when the escape key is pressed.
 118  
 119      @method _keyEsc
 120      @protected
 121      **/
 122      _keyEsc: function () {
 123          this.hide();
 124      },
 125  
 126      /**
 127      Called when the tab key is pressed.
 128  
 129      @method _keyTab
 130      @protected
 131      **/
 132      _keyTab: function (e) {
 133          var item;
 134  
 135          if (this.get('tabSelect')) {
 136              item = this.get('activeItem');
 137  
 138              if (item) {
 139                  this.selectItem(item, e);
 140                  return true;
 141              }
 142          }
 143  
 144          return false;
 145      },
 146  
 147      /**
 148      Called when the up arrow key is pressed.
 149  
 150      @method _keyUp
 151      @protected
 152      **/
 153      _keyUp: function () {
 154          this._activatePrevItem();
 155      },
 156  
 157      // -- Protected Event Handlers ---------------------------------------------
 158  
 159      /**
 160      Handles `inputNode` key events.
 161  
 162      @method _onInputKey
 163      @param {EventTarget} e
 164      @protected
 165      **/
 166      _onInputKey: function (e) {
 167          var handler,
 168              keyCode = e.keyCode;
 169  
 170          this._lastInputKey = keyCode;
 171  
 172          if (this.get('results').length) {
 173              handler = this._keys[keyCode];
 174  
 175              if (!handler && this.get('visible')) {
 176                  handler = this._keysVisible[keyCode];
 177              }
 178  
 179              if (handler) {
 180                  // A handler may return false to indicate that it doesn't wish
 181                  // to prevent the default key behavior.
 182                  if (handler.call(this, e) !== false) {
 183                      e.preventDefault();
 184                  }
 185              }
 186          }
 187      }
 188  };
 189  
 190  Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
 191  
 192  
 193  }, '3.17.2', {"requires": ["autocomplete-list", "base-build"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1