keyboardjs-rails 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +94 -0
- data/Rakefile +2 -0
- data/keyboardjs-rails.gemspec +21 -0
- data/lib/generators/keyboardjs/install/install_generator.rb +35 -0
- data/lib/keyboardjs/rails/engine.rb +7 -0
- data/lib/keyboardjs/rails/version.rb +6 -0
- data/lib/keyboardjs/rails.rb +6 -0
- data/lib/keyboardjs-rails.rb +1 -0
- data/vendor/assets/javascripts/keyboardjs.js +483 -0
- metadata +97 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@keyboardjs-rails --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright © 2012 Phil Cohen <github@phlippers.net>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the “Software”), to deal
|
6
|
+
in the Software without restriction, including without limitation the rights
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
12
|
+
all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# keyboardjs-rails
|
2
|
+
|
3
|
+
[KeyboardJS](http://robertwhurst.github.com/KeyboardJS/), typed up for Rails 3.0 and up. Sweet!
|
4
|
+
|
5
|
+
|
6
|
+
## Rails 3.1+
|
7
|
+
|
8
|
+
For Rails 3.1 and greater, the files will be added to the asset pipeline and available for you to use.
|
9
|
+
|
10
|
+
To enable:
|
11
|
+
|
12
|
+
* Add the following line to the file `app/assets/javascripts/application.js` (or other [sprockets](https://github.com/sstephenson/sprockets) manifest):
|
13
|
+
|
14
|
+
``` javascript
|
15
|
+
//= require keyboardjs
|
16
|
+
```
|
17
|
+
|
18
|
+
|
19
|
+
### Installation
|
20
|
+
|
21
|
+
* Add `keyboardjs-rails` to your Gemfile
|
22
|
+
* Run `bundle`
|
23
|
+
|
24
|
+
Enjoy!
|
25
|
+
|
26
|
+
|
27
|
+
## Rails 3.0
|
28
|
+
|
29
|
+
This gem adds a single generator to Rails 3, `keyboardjs:install`. Running the generator will install the JavaScript file necessary to use KeyboardJS.
|
30
|
+
|
31
|
+
### Installation
|
32
|
+
|
33
|
+
* Add `keyboardjs-rails` to your Gemfile
|
34
|
+
* Run `bundle`
|
35
|
+
* Invoke the generator: `rails generate keyboardjs:install`
|
36
|
+
* Add the following to your layout or view files:
|
37
|
+
|
38
|
+
```erb
|
39
|
+
<%= javascript_include_tag "keyboardjs" %>
|
40
|
+
```
|
41
|
+
|
42
|
+
You're done!
|
43
|
+
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
52
|
+
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
**keyboardjs-rails**
|
57
|
+
|
58
|
+
* Freely distributable and licensed under the [MIT license](http://phlipper.mit-license.org/2012/license.html).
|
59
|
+
* Copyright (c) 2012 Phil Cohen (github@phlippers.net) [![endorse](http://api.coderwall.com/phlipper/endorsecount.png)](http://coderwall.com/phlipper)
|
60
|
+
* http://phlippers.net/
|
61
|
+
|
62
|
+
**KeyboardJS**
|
63
|
+
|
64
|
+
* Licenced under the BSD License
|
65
|
+
* Copyright (c) 2011 Robert Hurst
|
66
|
+
* http://robertwhurst.github.com/KeyboardJS/
|
67
|
+
|
68
|
+
```
|
69
|
+
Copyright 2011 Robert Hurst. All rights reserved.
|
70
|
+
|
71
|
+
Redistribution and use in source and binary forms, with or without modification, are
|
72
|
+
permitted provided that the following conditions are met:
|
73
|
+
|
74
|
+
1. Redistributions of source code must retain the above copyright notice, this list of
|
75
|
+
conditions and the following disclaimer.
|
76
|
+
|
77
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
78
|
+
of conditions and the following disclaimer in the documentation and/or other materials
|
79
|
+
provided with the distribution.
|
80
|
+
|
81
|
+
THIS SOFTWARE IS PROVIDED BY ROBERT HURST ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
82
|
+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
83
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ROBERT HURST OR
|
84
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
85
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
86
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
87
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
88
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
89
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
90
|
+
|
91
|
+
The views and conclusions contained in the software and documentation are those of the
|
92
|
+
authors and should not be interpreted as representing official policies, either expressed
|
93
|
+
or implied, of Robert Hurst.
|
94
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/keyboardjs/rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Phil Cohen"]
|
6
|
+
gem.email = ["github@phlippers.net"]
|
7
|
+
gem.description = %q{KeyboardJS, typed up for the Rails 3.0 and up. Sweet!}
|
8
|
+
gem.summary = %q{KeyboardJS, typed up for the Rails 3.0 and up. Sweet!}
|
9
|
+
gem.homepage = "https://github.com/phlipper/keyboardjs-rails"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "keyboardjs-rails"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = KeyboardJS::Rails::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "railties", ">= 3.0.0"
|
19
|
+
gem.add_development_dependency "bundler", "~> 1.1.3"
|
20
|
+
gem.add_development_dependency "rake", "~> 0.9.2.2"
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rails"
|
2
|
+
|
3
|
+
# Supply generator for Rails 3.0.x or if asset pipeline is not enabled
|
4
|
+
if ::Rails.version < "3.1" || !::Rails.application.config.assets.enabled
|
5
|
+
module KeyboardJS
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < ::Rails::Generators::Base
|
8
|
+
desc "This generator installs KeyboardJS #{KeyboardJS::Rails::KEYBOARDJS_VERSION}"
|
9
|
+
source_root File.expand_path("../../../../../vendor/assets", __FILE__)
|
10
|
+
|
11
|
+
def copy_javascript
|
12
|
+
say_status("copying", "KeyboardJS", :green)
|
13
|
+
copy_file "javascripts/keyboardjs.js", "public/javascripts/keyboardjs.js"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
module KeyboardJS
|
20
|
+
module Generators
|
21
|
+
class InstallGenerator < ::Rails::Generators::Base
|
22
|
+
desc "Just show instructions so people will know what to do when mistakenly using generator for Rails 3.1 apps"
|
23
|
+
|
24
|
+
def do_nothing
|
25
|
+
say_status("deprecated", "You are using Rails 3.1 with the asset pipeline enabled, so this generator is not needed.")
|
26
|
+
say_status("", "The necessary files are already in your asset pipeline.")
|
27
|
+
say_status("", "Just add `//= require keyboardjs` to your app/assets/javascripts/application.js")
|
28
|
+
say_status("", "If you upgraded your app from Rails 3.0 and still have keyboardjs.js in your public assets folders, be sure to remove them.")
|
29
|
+
say_status("", "If you do not want the asset pipeline enabled, you may turn it off in application.rb and re-run this generator.")
|
30
|
+
# ok, nothing
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "keyboardjs/rails"
|
@@ -0,0 +1,483 @@
|
|
1
|
+
/*!
|
2
|
+
* KeyboardJS
|
3
|
+
*
|
4
|
+
* Copyright 2011, Robert William Hurst
|
5
|
+
* Licenced under the BSD License.
|
6
|
+
* See https://raw.github.com/RobertWHurst/KeyboardJS/master/license.txt
|
7
|
+
*/
|
8
|
+
(function (root, factory) {
|
9
|
+
if (typeof define === 'function' && define.amd) {
|
10
|
+
// AMD. Register as an anonymous module.
|
11
|
+
define(factory);
|
12
|
+
} else {
|
13
|
+
// Browser globals
|
14
|
+
root.KeyboardJS = factory();
|
15
|
+
}
|
16
|
+
}(this, function() {
|
17
|
+
|
18
|
+
//polyfills for ms's peice o' shit browsers
|
19
|
+
function bind(target, type, handler) { if (target.addEventListener) { target.addEventListener(type, handler, false); } else { target.attachEvent("on" + type, function(event) { return handler.call(target, event); });Â } }
|
20
|
+
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});
|
21
|
+
|
22
|
+
//locals
|
23
|
+
var locals = {
|
24
|
+
'us': {
|
25
|
+
"backspace": 8,
|
26
|
+
"tab": 9,
|
27
|
+
"enter": 13,
|
28
|
+
"shift": 16,
|
29
|
+
"ctrl": 17,
|
30
|
+
"alt": 18,
|
31
|
+
"pause": 19, "break": 19,
|
32
|
+
"capslock": 20,
|
33
|
+
"escape": 27, "esc": 27,
|
34
|
+
"space": 32, "spacebar": 32,
|
35
|
+
"pageup": 33,
|
36
|
+
"pagedown": 34,
|
37
|
+
"end": 35,
|
38
|
+
"home": 36,
|
39
|
+
"left": 37,
|
40
|
+
"up": 38,
|
41
|
+
"right": 39,
|
42
|
+
"down": 40,
|
43
|
+
"insert": 45,
|
44
|
+
"delete": 46,
|
45
|
+
"0": 48, "1": 49, "2": 50, "3": 51, "4": 52, "5": 53, "6": 54, "7": 55, "8": 56, "9": 57,
|
46
|
+
"a": 65, "b": 66, "c": 67, "d": 68, "e": 69, "f": 70, "g": 71, "h": 72, "i": 73, "j": 74, "k": 75, "l": 76, "m": 77, "n": 78, "o": 79, "p": 80, "q": 81, "r": 82, "s": 83, "t": 84, "u": 85, "v": 86, "w": 87, "x": 88, "y": 89, "z": 90,
|
47
|
+
"meta": 91, "command": 91, "windows": 91, "win": 91,
|
48
|
+
"_91": 92,
|
49
|
+
"select": 93,
|
50
|
+
"num0": 96, "num1": 97, "num2": 98, "num3": 99, "num4": 100, "num5": 101, "num6": 102, "num7": 103, "num8": 104, "num9": 105,
|
51
|
+
"multiply": 106,
|
52
|
+
"add": 107,
|
53
|
+
"subtract": 109,
|
54
|
+
"decimal": 110,
|
55
|
+
"divide": 111,
|
56
|
+
"f1": 112, "f2": 113, "f3": 114, "f4": 115, "f5": 116, "f6": 117, "f7": 118, "f8": 119, "f9": 120, "f10": 121, "f11": 122, "f12": 123,
|
57
|
+
"numlock": 144, "num": 144,
|
58
|
+
"scrolllock": 145, "scroll": 145,
|
59
|
+
"semicolon": 186,
|
60
|
+
"equal": 187, "equalsign": 187,
|
61
|
+
"comma": 188,
|
62
|
+
"dash": 189,
|
63
|
+
"period": 190,
|
64
|
+
"slash": 191, "forwardslash": 191,
|
65
|
+
"graveaccent": 192,
|
66
|
+
"openbracket": 219,
|
67
|
+
"backslash": 220,
|
68
|
+
"closebracket": 221,
|
69
|
+
"singlequote": 222
|
70
|
+
}
|
71
|
+
|
72
|
+
//If you create a new local please submit it as a pull request or post it in the issue tracker at
|
73
|
+
// http://github.com/RobertWhurst/KeyboardJS/issues/
|
74
|
+
}
|
75
|
+
|
76
|
+
//keys
|
77
|
+
var keys = locals['us'],
|
78
|
+
activeKeys = [],
|
79
|
+
activeBindings = {},
|
80
|
+
keyBindingGroups = [];
|
81
|
+
|
82
|
+
//adds keys to the active keys array
|
83
|
+
bind(document, "keydown", function(event) {
|
84
|
+
|
85
|
+
//lookup the key pressed and save it to the active keys array
|
86
|
+
for (var key in keys) {
|
87
|
+
if(keys.hasOwnProperty(key) && event.keyCode === keys[key]) {
|
88
|
+
if(activeKeys.indexOf(key) < 0) {
|
89
|
+
activeKeys.push(key);
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
//execute the first callback the longest key binding that matches the active keys
|
95
|
+
return executeActiveKeyBindings(event);
|
96
|
+
|
97
|
+
});
|
98
|
+
|
99
|
+
//removes keys from the active array
|
100
|
+
bind(document, "keyup", function (event) {
|
101
|
+
|
102
|
+
//lookup the key released and prune it from the active keys array
|
103
|
+
for(var key in keys) {
|
104
|
+
if(keys.hasOwnProperty(key) && event.keyCode === keys[key]) {
|
105
|
+
|
106
|
+
var iAK = activeKeys.indexOf(key);
|
107
|
+
|
108
|
+
if(iAK > -1) {
|
109
|
+
activeKeys.splice(iAK, 1);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
//execute the end callback on the active key binding
|
115
|
+
return pruneActiveKeyBindings(event);
|
116
|
+
|
117
|
+
});
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Generates an array of active key bindings
|
121
|
+
*/
|
122
|
+
function queryActiveBindings() {
|
123
|
+
var bindingStack = [];
|
124
|
+
|
125
|
+
//loop through the key binding groups by number of keys.
|
126
|
+
for(var keyCount = keyBindingGroups.length; keyCount > -1; keyCount -= 1) {
|
127
|
+
if(keyBindingGroups[keyCount]) {
|
128
|
+
var KeyBindingGroup = keyBindingGroups[keyCount];
|
129
|
+
|
130
|
+
//loop through the key bindings of the same key length.
|
131
|
+
for(var bindingIndex = 0; bindingIndex < KeyBindingGroup.length; bindingIndex += 1) {
|
132
|
+
var binding = KeyBindingGroup[bindingIndex],
|
133
|
+
|
134
|
+
//assume the binding is active till a required key is found to be unsatisfied
|
135
|
+
keyBindingActive = true;
|
136
|
+
|
137
|
+
//loop through each key required by the binding.
|
138
|
+
for(var keyIndex = 0; keyIndex < binding.keys.length; keyIndex += 1) {
|
139
|
+
var key = binding.keys[keyIndex];
|
140
|
+
|
141
|
+
//if the current key is not in the active keys array the mark the binding as inactive
|
142
|
+
if(activeKeys.indexOf(key) < 0) {
|
143
|
+
keyBindingActive = false;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
//if the key combo is still active then push it into the binding stack
|
148
|
+
if(keyBindingActive) {
|
149
|
+
bindingStack.push(binding);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
return bindingStack;
|
156
|
+
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Collects active keys, sets active binds and fires on key down callbacks
|
160
|
+
* @param event
|
161
|
+
*/
|
162
|
+
function executeActiveKeyBindings(event) {
|
163
|
+
|
164
|
+
if(activeKeys < 1) {
|
165
|
+
return true;
|
166
|
+
}
|
167
|
+
|
168
|
+
var bindingStack = queryActiveBindings(),
|
169
|
+
spentKeys = [],
|
170
|
+
output;
|
171
|
+
|
172
|
+
//loop through each active binding
|
173
|
+
for (var bindingIndex = 0; bindingIndex < bindingStack.length; bindingIndex += 1) {
|
174
|
+
var binding = bindingStack[bindingIndex],
|
175
|
+
usesSpentKey = false;
|
176
|
+
|
177
|
+
//check each of the required keys. Make sure they have not been used by another binding
|
178
|
+
for(var keyIndex = 0; keyIndex < binding.keys.length; keyIndex += 1) {
|
179
|
+
var key = binding.keys[keyIndex];
|
180
|
+
if(spentKeys.indexOf(key) > -1) {
|
181
|
+
usesSpentKey = true;
|
182
|
+
break;
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
//if the binding does not use a key that has been spent then execute it
|
187
|
+
if(!usesSpentKey) {
|
188
|
+
|
189
|
+
//fire the callback
|
190
|
+
if(typeof binding.callback === "function") {
|
191
|
+
if(!binding.callback(event, binding.keys, binding.keyCombo)) {
|
192
|
+
output = false
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
//add the binding's combo to the active bindings array
|
197
|
+
if(!activeBindings[binding.keyCombo]) {
|
198
|
+
activeBindings[binding.keyCombo] = binding;
|
199
|
+
}
|
200
|
+
|
201
|
+
//add the current key binding's keys to the spent keys array
|
202
|
+
for(var keyIndex = 0; keyIndex < binding.keys.length; keyIndex += 1) {
|
203
|
+
var key = binding.keys[keyIndex];
|
204
|
+
if(spentKeys.indexOf(key) < 0) {
|
205
|
+
spentKeys.push(key);
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
//if there are spent keys then we know a binding was fired
|
212
|
+
// and that we need to tell jQuery to prevent event bubbling.
|
213
|
+
if(spentKeys.length) {
|
214
|
+
return false;
|
215
|
+
}
|
216
|
+
|
217
|
+
return output;
|
218
|
+
}
|
219
|
+
|
220
|
+
/**
|
221
|
+
* Removes no longer active keys and fires the on key up callbacks for associated active bindings.
|
222
|
+
* @param event
|
223
|
+
*/
|
224
|
+
function pruneActiveKeyBindings(event) {
|
225
|
+
var bindingStack = queryActiveBindings();
|
226
|
+
var output;
|
227
|
+
|
228
|
+
//loop through the active combos
|
229
|
+
for(var bindingCombo in activeBindings) {
|
230
|
+
if(activeBindings.hasOwnProperty(bindingCombo)) {
|
231
|
+
var binding = activeBindings[bindingCombo],
|
232
|
+
active = false;
|
233
|
+
|
234
|
+
//loop thorugh the active bindings
|
235
|
+
for(var bindingIndex = 0; bindingIndex < bindingStack.length; bindingIndex += 1) {
|
236
|
+
var activeCombo = bindingStack[bindingIndex].keyCombo;
|
237
|
+
|
238
|
+
//check to see if the combo is still active
|
239
|
+
if(activeCombo === bindingCombo) {
|
240
|
+
active = true;
|
241
|
+
break;
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
//if the combo is no longer active then fire its end callback and remove it
|
246
|
+
if(!active) {
|
247
|
+
|
248
|
+
if(typeof binding.endCallback === "function") {
|
249
|
+
if(!binding.endCallback(event, binding.keys, binding.keyCombo)) {
|
250
|
+
output = false
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
delete activeBindings[bindingCombo];
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
return output;
|
260
|
+
}
|
261
|
+
|
262
|
+
/**
|
263
|
+
* Binds a on key down and on key up callback to a key or key combo. Accepts a string containing the name of each
|
264
|
+
* key you want to bind to comma separated. If you want to bind a combo the use the plus sign to link keys together.
|
265
|
+
* Example: 'ctrl + x, ctrl + c' Will fire if Control and x or y are pressed at the same time.
|
266
|
+
* @param keyCombo
|
267
|
+
* @param callback
|
268
|
+
* @param endCallback
|
269
|
+
*/
|
270
|
+
function bindKey(keyCombo, callback, endCallback) {
|
271
|
+
|
272
|
+
function clear() {
|
273
|
+
if(keys && keys.length) {
|
274
|
+
var keyBindingGroup = keyBindingGroups[keys.length];
|
275
|
+
|
276
|
+
if(keyBindingGroup.indexOf(keyBinding) > -1) {
|
277
|
+
var index = keyBindingGroups[keys.length].indexOf(keyBinding);
|
278
|
+
keyBindingGroups[keys.length].splice(index, 1);
|
279
|
+
}
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
//create an array of combos from the first argument
|
284
|
+
var bindSets = keyCombo.toLowerCase().replace(/\s/g, '').split(',');
|
285
|
+
|
286
|
+
//create a binding for each key combo
|
287
|
+
for(var i = 0; i < bindSets.length; i += 1) {
|
288
|
+
|
289
|
+
//split up the keys
|
290
|
+
var keys = bindSets[i].split('+');
|
291
|
+
|
292
|
+
//if there are keys in the current combo
|
293
|
+
if(keys.length) {
|
294
|
+
if(!keyBindingGroups[keys.length]) { keyBindingGroups[keys.length] = []; }
|
295
|
+
|
296
|
+
//define the
|
297
|
+
var keyBinding = {
|
298
|
+
"callback": callback,
|
299
|
+
"endCallback": endCallback,
|
300
|
+
"keyCombo": bindSets[i],
|
301
|
+
"keys": keys
|
302
|
+
};
|
303
|
+
|
304
|
+
//save the binding sorted by length
|
305
|
+
keyBindingGroups[keys.length].push(keyBinding);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
return {
|
310
|
+
"clear": clear
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
/**
|
315
|
+
* Binds keys or key combos to an axis. The keys should be in the following order; up, down, left, right. If any
|
316
|
+
* of the the binded key or key combos are active the callback will fire. The callback will be passed an array
|
317
|
+
* containing two numbers. The first represents x and the second represents y. Both have a possible range of -1,
|
318
|
+
* 0, or 1 depending on the axis direction.
|
319
|
+
* @param up
|
320
|
+
* @param down
|
321
|
+
* @param left
|
322
|
+
* @param right
|
323
|
+
* @param callback
|
324
|
+
*/
|
325
|
+
function bindAxis(up, down, left, right, callback) {
|
326
|
+
|
327
|
+
function clear() {
|
328
|
+
if(typeof clearUp === 'function') { clearUp(); }
|
329
|
+
if(typeof clearDown === 'function') { clearDown(); }
|
330
|
+
if(typeof clearLeft === 'function') { clearLeft(); }
|
331
|
+
if(typeof clearRight === 'function') { clearRight(); }
|
332
|
+
if(typeof timer === 'function') { clearInterval(timer); }
|
333
|
+
}
|
334
|
+
|
335
|
+
var axis = [0, 0];
|
336
|
+
|
337
|
+
if(typeof callback !== 'function') {
|
338
|
+
return false;
|
339
|
+
}
|
340
|
+
|
341
|
+
//up
|
342
|
+
var clearUp = bindKey(up, function () {
|
343
|
+
if(axis[0] === 0) {
|
344
|
+
axis[0] = -1;
|
345
|
+
}
|
346
|
+
}, function() {
|
347
|
+
axis[0] = 0;
|
348
|
+
}).clear;
|
349
|
+
|
350
|
+
//down
|
351
|
+
var clearDown = bindKey(down, function () {
|
352
|
+
if(axis[0] === 0) {
|
353
|
+
axis[0] = 1;
|
354
|
+
}
|
355
|
+
}, function() {
|
356
|
+
axis[0] = 0;
|
357
|
+
}).clear;
|
358
|
+
|
359
|
+
//left
|
360
|
+
var clearLeft = bindKey(left, function () {
|
361
|
+
if(axis[1] === 0) {
|
362
|
+
axis[1] = -1;
|
363
|
+
}
|
364
|
+
}, function() {
|
365
|
+
axis[1] = 0;
|
366
|
+
}).clear;
|
367
|
+
|
368
|
+
//right
|
369
|
+
var clearRight = bindKey(right, function () {
|
370
|
+
if(axis[1] === 0) {
|
371
|
+
axis[1] = 1;
|
372
|
+
}
|
373
|
+
}, function() {
|
374
|
+
axis[1] = 0;
|
375
|
+
}).clear;
|
376
|
+
|
377
|
+
var timer = setInterval(function(){
|
378
|
+
|
379
|
+
//NO CHANGE
|
380
|
+
if(axis[0] === 0 && axis[1] === 0) {
|
381
|
+
return;
|
382
|
+
}
|
383
|
+
|
384
|
+
//run the callback
|
385
|
+
callback(axis);
|
386
|
+
|
387
|
+
}, 1);
|
388
|
+
|
389
|
+
return {
|
390
|
+
"clear": clear
|
391
|
+
}
|
392
|
+
}
|
393
|
+
|
394
|
+
/**
|
395
|
+
* Clears all key and key combo binds containing a given key or keys.
|
396
|
+
* @param keys
|
397
|
+
*/
|
398
|
+
function unbindKey(keys) {
|
399
|
+
|
400
|
+
if(keys === 'all') {
|
401
|
+
keyBindingGroups = [];
|
402
|
+
return;
|
403
|
+
}
|
404
|
+
|
405
|
+
keys = keys.replace(/\s/g, '').split(',');
|
406
|
+
|
407
|
+
//loop through the key binding groups.
|
408
|
+
for(var iKCL = keyBindingGroups.length; iKCL > -1; iKCL -= 1) {
|
409
|
+
if(keyBindingGroups[iKCL]) {
|
410
|
+
var KeyBindingGroup = keyBindingGroups[iKCL];
|
411
|
+
|
412
|
+
//loop through the key bindings.
|
413
|
+
for(var iB = 0; iB < KeyBindingGroup.length; iB += 1) {
|
414
|
+
var keyBinding = KeyBindingGroup[iB],
|
415
|
+
remove = false;
|
416
|
+
|
417
|
+
//loop through the current key binding keys.
|
418
|
+
for(var iKB = 0; iKB < keyBinding.keys.length; iKB += 1) {
|
419
|
+
var key = keyBinding.keys[iKB];
|
420
|
+
|
421
|
+
//loop through the keys to be removed
|
422
|
+
for(var iKR = 0; iKR < keys.length; iKR += 1) {
|
423
|
+
var keyToRemove = keys[iKR];
|
424
|
+
if(keyToRemove === key) {
|
425
|
+
remove = true;
|
426
|
+
break;
|
427
|
+
}
|
428
|
+
}
|
429
|
+
if(remove) { break; }
|
430
|
+
}
|
431
|
+
if(remove) {
|
432
|
+
keyBindingGroups[iKCL].splice(iB, 1); iB -= 1;
|
433
|
+
if(keyBindingGroups[iKCL].length < 1) {
|
434
|
+
delete keyBindingGroups[iKCL];
|
435
|
+
}
|
436
|
+
}
|
437
|
+
}
|
438
|
+
}
|
439
|
+
}
|
440
|
+
}
|
441
|
+
|
442
|
+
/**
|
443
|
+
* Gets an array of active keys
|
444
|
+
*/
|
445
|
+
function getActiveKeys() {
|
446
|
+
return activeKeys;
|
447
|
+
}
|
448
|
+
|
449
|
+
/**
|
450
|
+
* Adds a new keyboard local not supported by keyboard JS
|
451
|
+
* @param local
|
452
|
+
* @param keys
|
453
|
+
*/
|
454
|
+
function addLocale(local, keys) {
|
455
|
+
locals[local] = keys;
|
456
|
+
}
|
457
|
+
|
458
|
+
/**
|
459
|
+
* Changes the keyboard local
|
460
|
+
* @param local
|
461
|
+
*/
|
462
|
+
function setLocale(local) {
|
463
|
+
if(locals[local]) {
|
464
|
+
keys = locals[local];
|
465
|
+
}
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
return {
|
470
|
+
"bind": {
|
471
|
+
"key": bindKey,
|
472
|
+
"axis": bindAxis
|
473
|
+
},
|
474
|
+
"activeKeys": getActiveKeys,
|
475
|
+
"unbind": {
|
476
|
+
"key": unbindKey
|
477
|
+
},
|
478
|
+
"locale": {
|
479
|
+
"add": addLocale,
|
480
|
+
"set": setLocale
|
481
|
+
}
|
482
|
+
}
|
483
|
+
}));
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: keyboardjs-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Phil Cohen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: &70350993998700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70350993998700
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &70350993996460 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.3
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70350993996460
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70350994011160 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.9.2.2
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70350994011160
|
47
|
+
description: KeyboardJS, typed up for the Rails 3.0 and up. Sweet!
|
48
|
+
email:
|
49
|
+
- github@phlippers.net
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rvmrc
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- keyboardjs-rails.gemspec
|
61
|
+
- lib/generators/keyboardjs/install/install_generator.rb
|
62
|
+
- lib/keyboardjs-rails.rb
|
63
|
+
- lib/keyboardjs/rails.rb
|
64
|
+
- lib/keyboardjs/rails/engine.rb
|
65
|
+
- lib/keyboardjs/rails/version.rb
|
66
|
+
- vendor/assets/javascripts/keyboardjs.js
|
67
|
+
homepage: https://github.com/phlipper/keyboardjs-rails
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
hash: 2981307322630233303
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
hash: 2981307322630233303
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.15
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: KeyboardJS, typed up for the Rails 3.0 and up. Sweet!
|
97
|
+
test_files: []
|