You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.KeyNav
  17. * <p>Provides a convenient wrapper for normalized keyboard navigation. KeyNav allows you to bind
  18. * navigation keys to function calls that will get called when the keys are pressed, providing an easy
  19. * way to implement custom navigation schemes for any UI component.</p>
  20. * <p>The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
  21. * pageUp, pageDown, del, home, end. Usage:</p>
  22. <pre><code>
  23. var nav = new Ext.KeyNav("my-element", {
  24. "left" : function(e){
  25. this.moveLeft(e.ctrlKey);
  26. },
  27. "right" : function(e){
  28. this.moveRight(e.ctrlKey);
  29. },
  30. "enter" : function(e){
  31. this.save();
  32. },
  33. scope : this
  34. });
  35. </code></pre>
  36. * @constructor
  37. * @param {Mixed} el The element to bind to
  38. * @param {Object} config The config
  39. */
  40. Ext.KeyNav = function(el, config){
  41. this.el = Ext.get(el);
  42. Ext.apply(this, config);
  43. if(!this.disabled){
  44. this.disabled = true;
  45. this.enable();
  46. }
  47. };
  48. Ext.KeyNav.prototype = {
  49. /**
  50. * @cfg {Boolean} disabled
  51. * True to disable this KeyNav instance (defaults to false)
  52. */
  53. disabled : false,
  54. /**
  55. * @cfg {String} defaultEventAction
  56. * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key. Valid values are
  57. * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
  58. * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
  59. */
  60. defaultEventAction: "stopEvent",
  61. /**
  62. * @cfg {Boolean} forceKeyDown
  63. * Handle the keydown event instead of keypress (defaults to false). KeyNav automatically does this for IE since
  64. * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
  65. * handle keydown instead of keypress.
  66. */
  67. forceKeyDown : false,
  68. // private
  69. relay : function(e){
  70. var k = e.getKey(),
  71. h = this.keyToHandler[k];
  72. if(h && this[h]){
  73. if(this.doRelay(e, this[h], h) !== true){
  74. e[this.defaultEventAction]();
  75. }
  76. }
  77. },
  78. // private
  79. doRelay : function(e, h, hname){
  80. return h.call(this.scope || this, e, hname);
  81. },
  82. // possible handlers
  83. enter : false,
  84. left : false,
  85. right : false,
  86. up : false,
  87. down : false,
  88. tab : false,
  89. esc : false,
  90. pageUp : false,
  91. pageDown : false,
  92. del : false,
  93. home : false,
  94. end : false,
  95. space : false,
  96. // quick lookup hash
  97. keyToHandler : {
  98. 37 : "left",
  99. 39 : "right",
  100. 38 : "up",
  101. 40 : "down",
  102. 33 : "pageUp",
  103. 34 : "pageDown",
  104. 46 : "del",
  105. 36 : "home",
  106. 35 : "end",
  107. 13 : "enter",
  108. 27 : "esc",
  109. 9 : "tab",
  110. 32 : "space"
  111. },
  112. stopKeyUp: function(e) {
  113. var k = e.getKey();
  114. if (k >= 37 && k <= 40) {
  115. // *** bugfix - safari 2.x fires 2 keyup events on cursor keys
  116. // *** (note: this bugfix sacrifices the "keyup" event originating from keyNav elements in Safari 2)
  117. e.stopEvent();
  118. }
  119. },
  120. /**
  121. * Destroy this KeyNav (this is the same as calling disable).
  122. */
  123. destroy: function(){
  124. this.disable();
  125. },
  126. /**
  127. * Enable this KeyNav
  128. */
  129. enable: function() {
  130. if (this.disabled) {
  131. if (Ext.isSafari2) {
  132. // call stopKeyUp() on "keyup" event
  133. this.el.on('keyup', this.stopKeyUp, this);
  134. }
  135. this.el.on(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
  136. this.disabled = false;
  137. }
  138. },
  139. /**
  140. * Disable this KeyNav
  141. */
  142. disable: function() {
  143. if (!this.disabled) {
  144. if (Ext.isSafari2) {
  145. // remove "keyup" event handler
  146. this.el.un('keyup', this.stopKeyUp, this);
  147. }
  148. this.el.un(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
  149. this.disabled = true;
  150. }
  151. },
  152. /**
  153. * Convenience function for setting disabled/enabled by boolean.
  154. * @param {Boolean} disabled
  155. */
  156. setDisabled : function(disabled){
  157. this[disabled ? "disable" : "enable"]();
  158. },
  159. // private
  160. isKeydown: function(){
  161. return this.forceKeyDown || Ext.EventManager.useKeydown;
  162. }
  163. };