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.7 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-KeyNav-method-constructor'><span id='Ext-KeyNav'>/**
  19. </span></span> * @class Ext.KeyNav
  20. * &lt;p&gt;Provides a convenient wrapper for normalized keyboard navigation. KeyNav allows you to bind
  21. * navigation keys to function calls that will get called when the keys are pressed, providing an easy
  22. * way to implement custom navigation schemes for any UI component.&lt;/p&gt;
  23. * &lt;p&gt;The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
  24. * pageUp, pageDown, del, home, end. Usage:&lt;/p&gt;
  25. &lt;pre&gt;&lt;code&gt;
  26. var nav = new Ext.KeyNav(&quot;my-element&quot;, {
  27. &quot;left&quot; : function(e){
  28. this.moveLeft(e.ctrlKey);
  29. },
  30. &quot;right&quot; : function(e){
  31. this.moveRight(e.ctrlKey);
  32. },
  33. &quot;enter&quot; : function(e){
  34. this.save();
  35. },
  36. scope : this
  37. });
  38. &lt;/code&gt;&lt;/pre&gt;
  39. * @constructor
  40. * @param {Mixed} el The element to bind to
  41. * @param {Object} config The config
  42. */
  43. Ext.KeyNav = function(el, config){
  44. this.el = Ext.get(el);
  45. Ext.apply(this, config);
  46. if(!this.disabled){
  47. this.disabled = true;
  48. this.enable();
  49. }
  50. };
  51. Ext.KeyNav.prototype = {
  52. <span id='Ext-KeyNav-cfg-disabled'> /**
  53. </span> * @cfg {Boolean} disabled
  54. * True to disable this KeyNav instance (defaults to false)
  55. */
  56. disabled : false,
  57. <span id='Ext-KeyNav-cfg-defaultEventAction'> /**
  58. </span> * @cfg {String} defaultEventAction
  59. * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key. Valid values are
  60. * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
  61. * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
  62. */
  63. defaultEventAction: &quot;stopEvent&quot;,
  64. <span id='Ext-KeyNav-cfg-forceKeyDown'> /**
  65. </span> * @cfg {Boolean} forceKeyDown
  66. * Handle the keydown event instead of keypress (defaults to false). KeyNav automatically does this for IE since
  67. * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
  68. * handle keydown instead of keypress.
  69. */
  70. forceKeyDown : false,
  71. // private
  72. relay : function(e){
  73. var k = e.getKey(),
  74. h = this.keyToHandler[k];
  75. if(h &amp;&amp; this[h]){
  76. if(this.doRelay(e, this[h], h) !== true){
  77. e[this.defaultEventAction]();
  78. }
  79. }
  80. },
  81. // private
  82. doRelay : function(e, h, hname){
  83. return h.call(this.scope || this, e, hname);
  84. },
  85. // possible handlers
  86. enter : false,
  87. left : false,
  88. right : false,
  89. up : false,
  90. down : false,
  91. tab : false,
  92. esc : false,
  93. pageUp : false,
  94. pageDown : false,
  95. del : false,
  96. home : false,
  97. end : false,
  98. space : false,
  99. // quick lookup hash
  100. keyToHandler : {
  101. 37 : &quot;left&quot;,
  102. 39 : &quot;right&quot;,
  103. 38 : &quot;up&quot;,
  104. 40 : &quot;down&quot;,
  105. 33 : &quot;pageUp&quot;,
  106. 34 : &quot;pageDown&quot;,
  107. 46 : &quot;del&quot;,
  108. 36 : &quot;home&quot;,
  109. 35 : &quot;end&quot;,
  110. 13 : &quot;enter&quot;,
  111. 27 : &quot;esc&quot;,
  112. 9 : &quot;tab&quot;,
  113. 32 : &quot;space&quot;
  114. },
  115. stopKeyUp: function(e) {
  116. var k = e.getKey();
  117. if (k &gt;= 37 &amp;&amp; k &lt;= 40) {
  118. // *** bugfix - safari 2.x fires 2 keyup events on cursor keys
  119. // *** (note: this bugfix sacrifices the &quot;keyup&quot; event originating from keyNav elements in Safari 2)
  120. e.stopEvent();
  121. }
  122. },
  123. <span id='Ext-KeyNav-method-destroy'> /**
  124. </span> * Destroy this KeyNav (this is the same as calling disable).
  125. */
  126. destroy: function(){
  127. this.disable();
  128. },
  129. <span id='Ext-KeyNav-method-enable'> /**
  130. </span> * Enable this KeyNav
  131. */
  132. enable: function() {
  133. if (this.disabled) {
  134. if (Ext.isSafari2) {
  135. // call stopKeyUp() on &quot;keyup&quot; event
  136. this.el.on('keyup', this.stopKeyUp, this);
  137. }
  138. this.el.on(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
  139. this.disabled = false;
  140. }
  141. },
  142. <span id='Ext-KeyNav-method-disable'> /**
  143. </span> * Disable this KeyNav
  144. */
  145. disable: function() {
  146. if (!this.disabled) {
  147. if (Ext.isSafari2) {
  148. // remove &quot;keyup&quot; event handler
  149. this.el.un('keyup', this.stopKeyUp, this);
  150. }
  151. this.el.un(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
  152. this.disabled = true;
  153. }
  154. },
  155. <span id='Ext-KeyNav-method-setDisabled'> /**
  156. </span> * Convenience function for setting disabled/enabled by boolean.
  157. * @param {Boolean} disabled
  158. */
  159. setDisabled : function(disabled){
  160. this[disabled ? &quot;disable&quot; : &quot;enable&quot;]();
  161. },
  162. // private
  163. isKeydown: function(){
  164. return this.forceKeyDown || Ext.EventManager.useKeydown;
  165. }
  166. };
  167. </pre>
  168. </body>
  169. </html>