/ext-4.0.7/docs/source/KeyMap.html

https://bitbucket.org/srogerf/javascript · HTML · 333 lines · 303 code · 30 blank · 0 comment · 0 complexity · 20c435e272271e2e4f03d5e13850b7d0 MD5 · raw file

  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-util-KeyMap'>/**
  19. </span> * @class Ext.util.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. By default KeyMap starts enabled.&lt;br /&gt;
  26. * Usage:
  27. &lt;pre&gt;&lt;code&gt;
  28. // map one key by key code
  29. var map = new Ext.util.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.util.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.util.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. */
  57. Ext.define('Ext.util.KeyMap', {
  58. alternateClassName: 'Ext.KeyMap',
  59. <span id='Ext-util-KeyMap-method-constructor'> /**
  60. </span> * Creates new KeyMap.
  61. * @param {String/HTMLElement/Ext.Element} el The element or its ID to bind to
  62. * @param {Object} binding The binding (see {@link #addBinding})
  63. * @param {String} [eventName=&quot;keydown&quot;] The event to bind to
  64. */
  65. constructor: function(el, binding, eventName){
  66. var me = this;
  67. Ext.apply(me, {
  68. el: Ext.get(el),
  69. eventName: eventName || me.eventName,
  70. bindings: []
  71. });
  72. if (binding) {
  73. me.addBinding(binding);
  74. }
  75. me.enable();
  76. },
  77. eventName: 'keydown',
  78. <span id='Ext-util-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. defaultEventAction String A default action to apply to the event. Possible values are: stopEvent, stopPropagation, preventDefault. If no value is set no action is performed.
  91. &lt;/pre&gt;
  92. *
  93. * Usage:
  94. * &lt;pre&gt;&lt;code&gt;
  95. // Create a KeyMap
  96. var map = new Ext.util.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/Object[]} binding A single KeyMap config or an array of configs
  110. */
  111. addBinding : function(binding){
  112. if (Ext.isArray(binding)) {
  113. Ext.each(binding, this.addBinding, this);
  114. return;
  115. }
  116. var keyCode = binding.key,
  117. processed = false,
  118. key,
  119. keys,
  120. keyString,
  121. i,
  122. len;
  123. if (Ext.isString(keyCode)) {
  124. keys = [];
  125. keyString = keyCode.toUpperCase();
  126. for (i = 0, len = keyString.length; i &lt; len; ++i){
  127. keys.push(keyString.charCodeAt(i));
  128. }
  129. keyCode = keys;
  130. processed = true;
  131. }
  132. if (!Ext.isArray(keyCode)) {
  133. keyCode = [keyCode];
  134. }
  135. if (!processed) {
  136. for (i = 0, len = keyCode.length; i &lt; len; ++i) {
  137. key = keyCode[i];
  138. if (Ext.isString(key)) {
  139. keyCode[i] = key.toUpperCase().charCodeAt(0);
  140. }
  141. }
  142. }
  143. this.bindings.push(Ext.apply({
  144. keyCode: keyCode
  145. }, binding));
  146. },
  147. <span id='Ext-util-KeyMap-method-handleKeyDown'> /**
  148. </span> * Process any keydown events on the element
  149. * @private
  150. * @param {Ext.EventObject} event
  151. */
  152. handleKeyDown: function(event) {
  153. if (this.enabled) { //just in case
  154. var bindings = this.bindings,
  155. i = 0,
  156. len = bindings.length;
  157. event = this.processEvent(event);
  158. for(; i &lt; len; ++i){
  159. this.processBinding(bindings[i], event);
  160. }
  161. }
  162. },
  163. <span id='Ext-util-KeyMap-method-processEvent'> /**
  164. </span> * Ugly hack to allow this class to be tested. Currently WebKit gives
  165. * no way to raise a key event properly with both
  166. * a) A keycode
  167. * b) The alt/ctrl/shift modifiers
  168. * So we have to simulate them here. Yuk!
  169. * This is a stub method intended to be overridden by tests.
  170. * More info: https://bugs.webkit.org/show_bug.cgi?id=16735
  171. * @private
  172. */
  173. processEvent: function(event){
  174. return event;
  175. },
  176. <span id='Ext-util-KeyMap-method-processBinding'> /**
  177. </span> * Process a particular binding and fire the handler if necessary.
  178. * @private
  179. * @param {Object} binding The binding information
  180. * @param {Ext.EventObject} event
  181. */
  182. processBinding: function(binding, event){
  183. if (this.checkModifiers(binding, event)) {
  184. var key = event.getKey(),
  185. handler = binding.fn || binding.handler,
  186. scope = binding.scope || this,
  187. keyCode = binding.keyCode,
  188. defaultEventAction = binding.defaultEventAction,
  189. i,
  190. len,
  191. keydownEvent = new Ext.EventObjectImpl(event);
  192. for (i = 0, len = keyCode.length; i &lt; len; ++i) {
  193. if (key === keyCode[i]) {
  194. if (handler.call(scope, key, event) !== true &amp;&amp; defaultEventAction) {
  195. keydownEvent[defaultEventAction]();
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. },
  202. <span id='Ext-util-KeyMap-method-checkModifiers'> /**
  203. </span> * Check if the modifiers on the event match those on the binding
  204. * @private
  205. * @param {Object} binding
  206. * @param {Ext.EventObject} event
  207. * @return {Boolean} True if the event matches the binding
  208. */
  209. checkModifiers: function(binding, e){
  210. var keys = ['shift', 'ctrl', 'alt'],
  211. i = 0,
  212. len = keys.length,
  213. val, key;
  214. for (; i &lt; len; ++i){
  215. key = keys[i];
  216. val = binding[key];
  217. if (!(val === undefined || (val === e[key + 'Key']))) {
  218. return false;
  219. }
  220. }
  221. return true;
  222. },
  223. <span id='Ext-util-KeyMap-method-on'> /**
  224. </span> * Shorthand for adding a single key listener
  225. * @param {Number/Number[]/Object} key Either the numeric key code, array of key codes or an object with the
  226. * following options:
  227. * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
  228. * @param {Function} fn The function to call
  229. * @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.
  230. */
  231. on: function(key, fn, scope) {
  232. var keyCode, shift, ctrl, alt;
  233. if (Ext.isObject(key) &amp;&amp; !Ext.isArray(key)) {
  234. keyCode = key.key;
  235. shift = key.shift;
  236. ctrl = key.ctrl;
  237. alt = key.alt;
  238. } else {
  239. keyCode = key;
  240. }
  241. this.addBinding({
  242. key: keyCode,
  243. shift: shift,
  244. ctrl: ctrl,
  245. alt: alt,
  246. fn: fn,
  247. scope: scope
  248. });
  249. },
  250. <span id='Ext-util-KeyMap-method-isEnabled'> /**
  251. </span> * Returns true if this KeyMap is enabled
  252. * @return {Boolean}
  253. */
  254. isEnabled : function(){
  255. return this.enabled;
  256. },
  257. <span id='Ext-util-KeyMap-method-enable'> /**
  258. </span> * Enables this KeyMap
  259. */
  260. enable: function(){
  261. var me = this;
  262. if (!me.enabled) {
  263. me.el.on(me.eventName, me.handleKeyDown, me);
  264. me.enabled = true;
  265. }
  266. },
  267. <span id='Ext-util-KeyMap-method-disable'> /**
  268. </span> * Disable this KeyMap
  269. */
  270. disable: function(){
  271. var me = this;
  272. if (me.enabled) {
  273. me.el.removeListener(me.eventName, me.handleKeyDown, me);
  274. me.enabled = false;
  275. }
  276. },
  277. <span id='Ext-util-KeyMap-method-setDisabled'> /**
  278. </span> * Convenience function for setting disabled/enabled by boolean.
  279. * @param {Boolean} disabled
  280. */
  281. setDisabled : function(disabled){
  282. if (disabled) {
  283. this.disable();
  284. } else {
  285. this.enable();
  286. }
  287. },
  288. <span id='Ext-util-KeyMap-method-destroy'> /**
  289. </span> * Destroys the KeyMap instance and removes all handlers.
  290. * @param {Boolean} removeEl True to also remove the attached element
  291. */
  292. destroy: function(removeEl){
  293. var me = this;
  294. me.bindings = [];
  295. me.disable();
  296. if (removeEl === true) {
  297. me.el.remove();
  298. }
  299. delete me.el;
  300. }
  301. });</pre>
  302. </body>
  303. </html>