author | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
commit | 541bb378ddece2eab135a8066a16994e94436dea (patch) (unidiff) | |
tree | ff160ea3e26f7fe07fcfd401387c5a0232ca715e /frontend/beta/js/YUI-extensions/KeyMap.js | |
parent | 1bf431fd3d45cbdf4afa3e12afefe5d24f4d3bc7 (diff) | |
parent | ecad5e895831337216544e81f1a467e0c68c4a6a (diff) | |
download | clipperz-541bb378ddece2eab135a8066a16994e94436dea.zip clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.gz clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.bz2 |
Merge pull request #1 from gcsolaroli/master
First version of the restructured repository
Diffstat (limited to 'frontend/beta/js/YUI-extensions/KeyMap.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/KeyMap.js | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/KeyMap.js b/frontend/beta/js/YUI-extensions/KeyMap.js new file mode 100644 index 0000000..c5af567 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/KeyMap.js | |||
@@ -0,0 +1,135 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.KeyMap | ||
3 | * Handles mapping keys to actions for an element. One key map can be used for multiple actions. | ||
4 | * A KeyMap can also handle a string representation of keys.<br /> | ||
5 | * Usage: | ||
6 | <pre><code> | ||
7 | // map one key by key code | ||
8 | var map = new YAHOO.ext.KeyMap('my-element', { | ||
9 | key: 13, | ||
10 | fn: myHandler, | ||
11 | scope: myObject | ||
12 | }); | ||
13 | |||
14 | // map multiple keys to one action by string | ||
15 | var map = new YAHOO.ext.KeyMap('my-element', { | ||
16 | key: "a\r\n\t", | ||
17 | fn: myHandler, | ||
18 | scope: myObject | ||
19 | }); | ||
20 | |||
21 | // map multiple keys to multiple actions by strings and array of codes | ||
22 | var map = new YAHOO.ext.KeyMap('my-element', [ | ||
23 | { | ||
24 | key: [10,13], | ||
25 | fn: function(){ alert('Return was pressed'); } | ||
26 | }, { | ||
27 | key: "abc", | ||
28 | fn: function(){ alert('a, b or c was pressed'); } | ||
29 | }, { | ||
30 | key: "\t", | ||
31 | ctrl:true, | ||
32 | shift:true, | ||
33 | fn: function(){ alert('Control + shift + tab was pressed.'); } | ||
34 | } | ||
35 | ]); | ||
36 | </code></pre> | ||
37 | * <b>Note: A KepMap starts enabled</b> | ||
38 | * @constructor | ||
39 | * @param {String/HTMLElement/YAHOO.ext.Element} el The element to bind to | ||
40 | * @param {Object} config The config | ||
41 | * @param {String} eventName (optional) The event to bind to (Defaults to "keydown"). | ||
42 | */ | ||
43 | YAHOO.ext.KeyMap = function(el, config, eventName){ | ||
44 | this.el = getEl(el); | ||
45 | this.eventName = eventName || 'keydown'; | ||
46 | this.bindings = []; | ||
47 | if(config instanceof Array){ | ||
48 | for(var i = 0, len = config.length; i < len; i++){ | ||
49 | this.addBinding(config[i]); | ||
50 | } | ||
51 | }else{ | ||
52 | this.addBinding(config); | ||
53 | } | ||
54 | this.keyDownDelegate = YAHOO.ext.EventManager.wrap(this.handleKeyDown, this, true); | ||
55 | this.enable(); | ||
56 | } | ||
57 | |||
58 | YAHOO.ext.KeyMap.prototype = { | ||
59 | /** | ||
60 | * Add a new binding to this KeyMap | ||
61 | * @param {Object} config A single KeyMap config | ||
62 | */ | ||
63 | addBinding : function(config){ | ||
64 | var keyCode = config.key, | ||
65 | shift = config.shift, | ||
66 | ctrl = config.ctrl, | ||
67 | alt = config.alt, | ||
68 | fn = config.fn, | ||
69 | scope = config.scope; | ||
70 | if(typeof keyCode == 'string'){ | ||
71 | var ks = []; | ||
72 | var keyString = keyCode.toUpperCase(); | ||
73 | for(var j = 0, len = keyString.length; j < len; j++){ | ||
74 | ks.push(keyString.charCodeAt(j)); | ||
75 | } | ||
76 | keyCode = ks; | ||
77 | } | ||
78 | var keyArray = keyCode instanceof Array; | ||
79 | var handler = function(e){ | ||
80 | if((!shift || e.shiftKey) && (!ctrl || e.ctrlKey) && (!alt || e.altKey)){ | ||
81 | var k = e.getKey(); | ||
82 | if(keyArray){ | ||
83 | for(var i = 0, len = keyCode.length; i < len; i++){ | ||
84 | if(keyCode[i] == k){ | ||
85 | fn.call(scope || window, k, e); | ||
86 | return; | ||
87 | } | ||
88 | } | ||
89 | }else{ | ||
90 | if(k == keyCode){ | ||
91 | fn.call(scope || window, k, e); | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | }; | ||
96 | this.bindings.push(handler); | ||
97 | }, | ||
98 | |||
99 | handleKeyDown : function(e){ | ||
100 | if(this.enabled){ //just in case | ||
101 | var b = this.bindings; | ||
102 | for(var i = 0, len = b.length; i < len; i++){ | ||
103 | b[i](e); | ||
104 | } | ||
105 | } | ||
106 | }, | ||
107 | |||
108 | /** | ||
109 | * Returns true if this KepMap is enabled | ||
110 | * @return {Boolean} | ||
111 | */ | ||
112 | isEnabled : function(){ | ||
113 | return this.enabled; | ||
114 | }, | ||
115 | |||
116 | /** | ||
117 | * Enable this KeyMap | ||
118 | */ | ||
119 | enable: function(){ | ||
120 | if(!this.enabled){ | ||
121 | this.el.on(this.eventName, this.keyDownDelegate); | ||
122 | this.enabled = true; | ||
123 | } | ||
124 | }, | ||
125 | |||
126 | /** | ||
127 | * Disable this KeyMap | ||
128 | */ | ||
129 | disable: function(){ | ||
130 | if(this.enabled){ | ||
131 | this.el.removeListener(this.eventName, this.keyDownDelegate); | ||
132 | this.enabled = false; | ||
133 | } | ||
134 | } | ||
135 | }; | ||