Web Interface For Lite Argus Control modules
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.

251 lines
8.2 KiB

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