3
<title>The source code</title>
4
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7
<body onload="prettyPrint();">
8
<pre class="prettyprint lang-js"><div id="cls-Ext.KeyMap"></div>/**
10
* Handles mapping keys to actions for an element. One key map can be used for multiple actions.
11
* The constructor accepts the same config object as defined by {@link #addBinding}.
12
* If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
13
* combination it will call the function with this signature (if the match is a multi-key
14
* combination the callback will still be called only once): (String key, Ext.EventObject e)
15
* A KeyMap can also handle a string representation of keys.<br />
18
// map one key by key code
19
var map = new Ext.KeyMap("my-element", {
20
key: 13, // or Ext.EventObject.ENTER
25
// map multiple keys to one action by string
26
var map = new Ext.KeyMap("my-element", {
32
// map multiple keys to multiple actions by strings and array of codes
33
var map = new Ext.KeyMap("my-element", [
36
fn: function(){ alert("Return was pressed"); }
39
fn: function(){ alert('a, b or c was pressed'); }
44
fn: function(){ alert('Control + shift + tab was pressed.'); }
48
* <b>Note: A KeyMap starts enabled</b>
50
* @param {Mixed} el The element to bind to
51
* @param {Object} config The config (see {@link #addBinding})
52
* @param {String} eventName (optional) The event to bind to (defaults to "keydown")
54
Ext.KeyMap = function(el, config, eventName){
55
this.el = Ext.get(el);
56
this.eventName = eventName || "keydown";
59
this.addBinding(config);
64
Ext.KeyMap.prototype = {
65
<div id="prop-Ext.KeyMap-stopEvent"></div>/**
66
* True to stop the event from bubbling and prevent the default browser action if the
67
* key was handled by the KeyMap (defaults to false)
72
<div id="method-Ext.KeyMap-addBinding"></div>/**
73
* Add a new binding to this KeyMap. The following config object properties are supported:
75
Property Type Description
76
---------- --------------- ----------------------------------------------------------------------
77
key String/Array A single keycode or an array of keycodes to handle
78
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)
79
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)
80
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)
81
handler Function The function to call when KeyMap finds the expected key combination
82
fn Function Alias of handler (for backwards-compatibility)
83
scope Object The scope of the callback function
84
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)
90
var map = new Ext.KeyMap(document, {
91
key: Ext.EventObject.ENTER,
96
//Add a new binding to the existing KeyMap later
104
* @param {Object/Array} config A single KeyMap config or an array of configs
106
addBinding : function(config){
107
if(Ext.isArray(config)){
108
Ext.each(config, function(c){
113
var keyCode = config.key,
114
fn = config.fn || config.handler,
115
scope = config.scope;
117
if (config.stopEvent) {
118
this.stopEvent = config.stopEvent;
121
if(typeof keyCode == "string"){
123
var keyString = keyCode.toUpperCase();
124
for(var j = 0, len = keyString.length; j < len; j++){
125
ks.push(keyString.charCodeAt(j));
129
var keyArray = Ext.isArray(keyCode);
131
var handler = function(e){
132
if(this.checkModifiers(config, e)){
135
for(var i = 0, len = keyCode.length; i < len; i++){
140
fn.call(scope || window, k, e);
149
fn.call(scope || window, k, e);
154
this.bindings.push(handler);
158
checkModifiers: function(config, e){
159
var val, key, keys = ['shift', 'ctrl', 'alt'];
160
for (var i = 0, len = keys.length; i < len; ++i){
163
if(!(val === undefined || (val === e[key + 'Key']))){
170
<div id="method-Ext.KeyMap-on"></div>/**
171
* 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
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 of the function
178
on : function(key, fn, scope){
179
var keyCode, shift, ctrl, alt;
180
if(typeof key == "object" && !Ext.isArray(key)){
199
handleKeyDown : function(e){
200
if(this.enabled){ //just in case
201
var b = this.bindings;
202
for(var i = 0, len = b.length; i < len; i++){
208
<div id="method-Ext.KeyMap-isEnabled"></div>/**
209
* Returns true if this KeyMap is enabled
212
isEnabled : function(){
216
<div id="method-Ext.KeyMap-enable"></div>/**
217
* Enables this KeyMap
221
this.el.on(this.eventName, this.handleKeyDown, this);
226
<div id="method-Ext.KeyMap-disable"></div>/**
227
* Disable this KeyMap
231
this.el.removeListener(this.eventName, this.handleKeyDown, this);
232
this.enabled = false;
236
<div id="method-Ext.KeyMap-setDisabled"></div>/**
237
* Convenience function for setting disabled/enabled by boolean.
238
* @param {Boolean} disabled
240
setDisabled : function(disabled){
241
this[disabled ? "disable" : "enable"]();
b'\\ No newline at end of file'