d86f96655b506ac2c0b5e92b95193392f388b9b7.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.KeyNav
  8. * <p>
  9. * Provides a convenient wrapper for normalized keyboard navigation.
  10. * KeyNav allows you to bind navigation keys to function calls that will
  11. * get called when the keys are pressed, providing an easy way to
  12. * implement custom navigation schemes for any UI component.
  13. * </p>
  14. * <p>
  15. * The following are all of the possible keys that can be implemented:
  16. * enter, left, right, up, down, tab, esc, pageUp, pageDown, del, home,
  17. * end. Usage:
  18. * </p>
  19. *
  20. * <pre><code>
  21. * var nav = new Ext.KeyNav(&quot;my-element&quot;, {
  22. * &quot;left&quot; : function(e) {
  23. * this.moveLeft(e.ctrlKey);
  24. * },
  25. * &quot;right&quot; : function(e) {
  26. * this.moveRight(e.ctrlKey);
  27. * },
  28. * &quot;enter&quot; : function(e) {
  29. * this.save();
  30. * },
  31. * scope : this
  32. * });
  33. * </code></pre>
  34. *
  35. * @constructor
  36. * @param {Mixed}
  37. * el The element to bind to
  38. * @param {Object}
  39. * config The config
  40. */
  41. Ext.KeyNav = function(el, config) {
  42. this.el = Ext.get(el);
  43. Ext.apply(this, config);
  44. if (!this.disabled) {
  45. this.disabled = true;
  46. this.enable();
  47. }
  48. };
  49. Ext.KeyNav.prototype = {
  50. /**
  51. * @cfg {Boolean} disabled True to disable this KeyNav instance (defaults to
  52. * false)
  53. */
  54. disabled : false,
  55. /**
  56. * @cfg {String} defaultEventAction The method to call on the
  57. * {@link Ext.EventObject} after this KeyNav intercepts a key. Valid
  58. * values are {@link Ext.EventObject#stopEvent},
  59. * {@link Ext.EventObject#preventDefault} and
  60. * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
  61. */
  62. defaultEventAction : "stopEvent",
  63. /**
  64. * @cfg {Boolean} forceKeyDown Handle the keydown event instead of keypress
  65. * (defaults to false). KeyNav automatically does this for IE since IE
  66. * does not propagate special keys on keypress, but setting this to
  67. * true will force other browsers to also handle keydown instead of
  68. * keypress.
  69. */
  70. forceKeyDown : false,
  71. // private
  72. prepareEvent : function(e) {
  73. var k = e.getKey();
  74. var h = this.keyToHandler[k];
  75. // if(h && this[h]){
  76. // e.stopPropagation();
  77. // }
  78. if (Ext.isSafari && h && k >= 37 && k <= 40) {
  79. e.stopEvent();
  80. }
  81. },
  82. // private
  83. relay : function(e) {
  84. var k = e.getKey();
  85. var h = this.keyToHandler[k];
  86. if (h && this[h]) {
  87. if (this.doRelay(e, this[h], h) !== true) {
  88. e[this.defaultEventAction]();
  89. }
  90. }
  91. },
  92. // private
  93. doRelay : function(e, h, hname) {
  94. return h.call(this.scope || this, e);
  95. },
  96. // possible handlers
  97. enter : false,
  98. left : false,
  99. right : false,
  100. up : false,
  101. down : false,
  102. tab : false,
  103. esc : false,
  104. pageUp : false,
  105. pageDown : false,
  106. del : false,
  107. home : false,
  108. end : false,
  109. // quick lookup hash
  110. keyToHandler : {
  111. 37 : "left",
  112. 39 : "right",
  113. 38 : "up",
  114. 40 : "down",
  115. 33 : "pageUp",
  116. 34 : "pageDown",
  117. 46 : "del",
  118. 36 : "home",
  119. 35 : "end",
  120. 13 : "enter",
  121. 27 : "esc",
  122. 9 : "tab"
  123. },
  124. /**
  125. * Enable this KeyNav
  126. */
  127. enable : function() {
  128. if (this.disabled) {
  129. // ie won't do special keys on keypress, no one else will repeat
  130. // keys with keydown
  131. // the EventObject will normalize Safari automatically
  132. if (this.forceKeyDown || Ext.isIE || Ext.isAir) {
  133. this.el.on("keydown", this.relay, this);
  134. } else {
  135. this.el.on("keydown", this.prepareEvent, this);
  136. this.el.on("keypress", this.relay, this);
  137. }
  138. this.disabled = false;
  139. }
  140. },
  141. /**
  142. * Disable this KeyNav
  143. */
  144. disable : function() {
  145. if (!this.disabled) {
  146. if (this.forceKeyDown || Ext.isIE || Ext.isAir) {
  147. this.el.un("keydown", this.relay);
  148. } else {
  149. this.el.un("keydown", this.prepareEvent);
  150. this.el.un("keypress", this.relay);
  151. }
  152. this.disabled = true;
  153. }
  154. }
  155. };