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.

255 lines
8.6 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-KeyMap-method-constructor'><span id='Ext-KeyMap'>/**
  19. </span></span> * @class Ext.KeyMap
  20. * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
  21. * The constructor accepts the same config object as defined by {@link #addBinding}.
  22. * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
  23. * combination it will call the function with this signature (if the match is a multi-key
  24. * combination the callback will still be called only once): (String key, Ext.EventObject e)
  25. * A KeyMap can also handle a string representation of keys.&lt;br /&gt;
  26. * Usage:
  27. &lt;pre&gt;&lt;code&gt;
  28. // map one key by key code
  29. var map = new Ext.KeyMap(&quot;my-element&quot;, {
  30. key: 13, // or Ext.EventObject.ENTER
  31. fn: myHandler,
  32. scope: myObject
  33. });
  34. // map multiple keys to one action by string
  35. var map = new Ext.KeyMap(&quot;my-element&quot;, {
  36. key: &quot;a\r\n\t&quot;,
  37. fn: myHandler,
  38. scope: myObject
  39. });
  40. // map multiple keys to multiple actions by strings and array of codes
  41. var map = new Ext.KeyMap(&quot;my-element&quot;, [
  42. {
  43. key: [10,13],
  44. fn: function(){ alert(&quot;Return was pressed&quot;); }
  45. }, {
  46. key: &quot;abc&quot;,
  47. fn: function(){ alert('a, b or c was pressed'); }
  48. }, {
  49. key: &quot;\t&quot;,
  50. ctrl:true,
  51. shift:true,
  52. fn: function(){ alert('Control + shift + tab was pressed.'); }
  53. }
  54. ]);
  55. &lt;/code&gt;&lt;/pre&gt;
  56. * &lt;b&gt;Note: A KeyMap starts enabled&lt;/b&gt;
  57. * @constructor
  58. * @param {Mixed} el The element to bind to
  59. * @param {Object} config The config (see {@link #addBinding})
  60. * @param {String} eventName (optional) The event to bind to (defaults to &quot;keydown&quot;)
  61. */
  62. Ext.KeyMap = function(el, config, eventName){
  63. this.el = Ext.get(el);
  64. this.eventName = eventName || &quot;keydown&quot;;
  65. this.bindings = [];
  66. if(config){
  67. this.addBinding(config);
  68. }
  69. this.enable();
  70. };
  71. Ext.KeyMap.prototype = {
  72. <span id='Ext-KeyMap-property-stopEvent'> /**
  73. </span> * True to stop the event from bubbling and prevent the default browser action if the
  74. * key was handled by the KeyMap (defaults to false)
  75. * @type Boolean
  76. */
  77. stopEvent : false,
  78. <span id='Ext-KeyMap-method-addBinding'> /**
  79. </span> * Add a new binding to this KeyMap. The following config object properties are supported:
  80. * &lt;pre&gt;
  81. Property Type Description
  82. ---------- --------------- ----------------------------------------------------------------------
  83. key String/Array A single keycode or an array of keycodes to handle
  84. shift Boolean True to handle key only when shift is pressed, False to handle the key only when shift is not pressed (defaults to undefined)
  85. ctrl Boolean True to handle key only when ctrl is pressed, False to handle the key only when ctrl is not pressed (defaults to undefined)
  86. alt Boolean True to handle key only when alt is pressed, False to handle the key only when alt is not pressed (defaults to undefined)
  87. handler Function The function to call when KeyMap finds the expected key combination
  88. fn Function Alias of handler (for backwards-compatibility)
  89. scope Object The scope of the callback function
  90. stopEvent Boolean True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap (defaults to false)
  91. &lt;/pre&gt;
  92. *
  93. * Usage:
  94. * &lt;pre&gt;&lt;code&gt;
  95. // Create a KeyMap
  96. var map = new Ext.KeyMap(document, {
  97. key: Ext.EventObject.ENTER,
  98. fn: handleKey,
  99. scope: this
  100. });
  101. //Add a new binding to the existing KeyMap later
  102. map.addBinding({
  103. key: 'abc',
  104. shift: true,
  105. fn: handleKey,
  106. scope: this
  107. });
  108. &lt;/code&gt;&lt;/pre&gt;
  109. * @param {Object/Array} config A single KeyMap config or an array of configs
  110. */
  111. addBinding : function(config){
  112. if(Ext.isArray(config)){
  113. Ext.each(config, function(c){
  114. this.addBinding(c);
  115. }, this);
  116. return;
  117. }
  118. var keyCode = config.key,
  119. fn = config.fn || config.handler,
  120. scope = config.scope;
  121. if (config.stopEvent) {
  122. this.stopEvent = config.stopEvent;
  123. }
  124. if(typeof keyCode == &quot;string&quot;){
  125. var ks = [];
  126. var keyString = keyCode.toUpperCase();
  127. for(var j = 0, len = keyString.length; j &lt; len; j++){
  128. ks.push(keyString.charCodeAt(j));
  129. }
  130. keyCode = ks;
  131. }
  132. var keyArray = Ext.isArray(keyCode);
  133. var handler = function(e){
  134. if(this.checkModifiers(config, e)){
  135. var k = e.getKey();
  136. if(keyArray){
  137. for(var i = 0, len = keyCode.length; i &lt; len; i++){
  138. if(keyCode[i] == k){
  139. if(this.stopEvent){
  140. e.stopEvent();
  141. }
  142. fn.call(scope || window, k, e);
  143. return;
  144. }
  145. }
  146. }else{
  147. if(k == keyCode){
  148. if(this.stopEvent){
  149. e.stopEvent();
  150. }
  151. fn.call(scope || window, k, e);
  152. }
  153. }
  154. }
  155. };
  156. this.bindings.push(handler);
  157. },
  158. // private
  159. checkModifiers: function(config, e){
  160. var val, key, keys = ['shift', 'ctrl', 'alt'];
  161. for (var i = 0, len = keys.length; i &lt; len; ++i){
  162. key = keys[i];
  163. val = config[key];
  164. if(!(val === undefined || (val === e[key + 'Key']))){
  165. return false;
  166. }
  167. }
  168. return true;
  169. },
  170. <span id='Ext-KeyMap-method-on'> /**
  171. </span> * Shorthand for adding a single key listener
  172. * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
  173. * following options:
  174. * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
  175. * @param {Function} fn The function to call
  176. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the browser window.
  177. */
  178. on : function(key, fn, scope){
  179. var keyCode, shift, ctrl, alt;
  180. if(typeof key == &quot;object&quot; &amp;&amp; !Ext.isArray(key)){
  181. keyCode = key.key;
  182. shift = key.shift;
  183. ctrl = key.ctrl;
  184. alt = key.alt;
  185. }else{
  186. keyCode = key;
  187. }
  188. this.addBinding({
  189. key: keyCode,
  190. shift: shift,
  191. ctrl: ctrl,
  192. alt: alt,
  193. fn: fn,
  194. scope: scope
  195. });
  196. },
  197. // private
  198. handleKeyDown : function(e){
  199. if(this.enabled){ //just in case
  200. var b = this.bindings;
  201. for(var i = 0, len = b.length; i &lt; len; i++){
  202. b[i].call(this, e);
  203. }
  204. }
  205. },
  206. <span id='Ext-KeyMap-method-isEnabled'> /**
  207. </span> * Returns true if this KeyMap is enabled
  208. * @return {Boolean}
  209. */
  210. isEnabled : function(){
  211. return this.enabled;
  212. },
  213. <span id='Ext-KeyMap-method-enable'> /**
  214. </span> * Enables this KeyMap
  215. */
  216. enable: function(){
  217. if(!this.enabled){
  218. this.el.on(this.eventName, this.handleKeyDown, this);
  219. this.enabled = true;
  220. }
  221. },
  222. <span id='Ext-KeyMap-method-disable'> /**
  223. </span> * Disable this KeyMap
  224. */
  225. disable: function(){
  226. if(this.enabled){
  227. this.el.removeListener(this.eventName, this.handleKeyDown, this);
  228. this.enabled = false;
  229. }
  230. },
  231. <span id='Ext-KeyMap-method-setDisabled'> /**
  232. </span> * Convenience function for setting disabled/enabled by boolean.
  233. * @param {Boolean} disabled
  234. */
  235. setDisabled : function(disabled){
  236. this[disabled ? &quot;disable&quot; : &quot;enable&quot;]();
  237. }
  238. };</pre>
  239. </body>
  240. </html>