jwerty-rails 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +21 -0
- data/lib/jwerty/rails.rb +8 -0
- data/lib/jwerty/rails/version.rb +5 -0
- data/vendor/assets/javascripts/jwerty.js +534 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11dde52e39daac0e738211340cbee3ca4cb9b309
|
4
|
+
data.tar.gz: 84c9eee4011b90f9abbee05ecec3f3e6899796bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bda16f64053982389cc24d5315160a9688a0e13a2db41746b3b21609e565d4eca2943175a72571a35d185094d8e6741395434922b741b6a62d98fd4e77edfd3b
|
7
|
+
data.tar.gz: f42c6874426e21e560f034213c9746ab2e1c38108e9be8c30e4790fb7532964936a3c6534421286feb84cb881dfb8c83f5932114d1a0d3f3e65e2adfab0d81a9
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Adam Griffis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# jwerty-rails
|
2
|
+
|
3
|
+
jwery-rails wraps the [jwerty.js](https://github.com/keithamus/jwerty) library in a rails engine for simple
|
4
|
+
use with the asset pipeline provided by rails 4.0. The gem includes the development (non-minified)
|
5
|
+
source for ease of exploration. The asset pipeline will minify in production.
|
6
|
+
|
7
|
+
jwerty is a JS lib which allows you to bind, fire and assert key combination strings against elements and events. It normalises the poor std api into something easy to use and clear. Please see the [documentation](https://github.com/keithamus/jwerty/blob/master/README-DETAILED.md) for details.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Add the following to your gemfile:
|
12
|
+
|
13
|
+
gem 'jwerty-rails'
|
14
|
+
|
15
|
+
Add the following directive to your Javascript manifest file (application.js):
|
16
|
+
|
17
|
+
//= require jwerty
|
18
|
+
|
19
|
+
## Versioning
|
20
|
+
|
21
|
+
jwerty-rails 0.3.2 == jwerty.js 0.3.2
|
data/lib/jwerty/rails.rb
ADDED
@@ -0,0 +1,534 @@
|
|
1
|
+
/*
|
2
|
+
* jwerty - Awesome handling of keyboard events
|
3
|
+
*
|
4
|
+
* jwerty is a JS lib which allows you to bind, fire and assert key combination
|
5
|
+
* strings against elements and events. It normalises the poor std api into
|
6
|
+
* something easy to use and clear.
|
7
|
+
*
|
8
|
+
* This code is licensed under the MIT
|
9
|
+
* For the full license see: http://keithamus.mit-license.org/
|
10
|
+
* For more information see: http://keithamus.github.com/jwerty
|
11
|
+
*
|
12
|
+
* @author Keith Cirkel ('keithamus') <jwerty@keithcirkel.co.uk>
|
13
|
+
* @license http://keithamus.mit-license.org/
|
14
|
+
* @copyright Copyright © 2011, Keith Cirkel
|
15
|
+
*
|
16
|
+
*/
|
17
|
+
(function (global, exports) {
|
18
|
+
|
19
|
+
// Try require external librairies in Node.js context
|
20
|
+
function tryRequire(mod) {
|
21
|
+
if (typeof require == 'function' && typeof module !== 'undefined' && module.exports) {
|
22
|
+
try {
|
23
|
+
return require(mod.toLowerCase());
|
24
|
+
} catch (err) {}
|
25
|
+
} else {
|
26
|
+
return global[mod];
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
// Helper methods & vars:
|
31
|
+
var $d = global.document,
|
32
|
+
$ = (tryRequire('jquery') || tryRequire('zepto') || tryRequire('ender') || $d),
|
33
|
+
$$, // Element selector function
|
34
|
+
$b, // Event binding function
|
35
|
+
$f, // Event firing function
|
36
|
+
ke = 'keydown';
|
37
|
+
|
38
|
+
function realTypeOf(v, s) {
|
39
|
+
return (v === null) ? s === 'null'
|
40
|
+
: (v === undefined) ? s === 'undefined'
|
41
|
+
: (v.is && v instanceof $) ? s === 'element'
|
42
|
+
: Object.prototype.toString.call(v).toLowerCase().indexOf(s) > 7;
|
43
|
+
}
|
44
|
+
|
45
|
+
if ($ === $d) {
|
46
|
+
$$ = function (selector, context) {
|
47
|
+
return selector ? $.querySelector(selector, context || $) : $;
|
48
|
+
};
|
49
|
+
$b = function (e, fn) { e.addEventListener(ke, fn, false); };
|
50
|
+
$f = function (e, jwertyEv) {
|
51
|
+
var ret = $d.createEvent('Event'),
|
52
|
+
i;
|
53
|
+
|
54
|
+
ret.initEvent(ke, true, true);
|
55
|
+
|
56
|
+
for (i in jwertyEv) ret[i] = jwertyEv[i];
|
57
|
+
|
58
|
+
return (e || $).dispatchEvent(ret);
|
59
|
+
};
|
60
|
+
} else {
|
61
|
+
$$ = function (selector, context) { return $(selector || $d, context); };
|
62
|
+
$b = function (e, fn) { $(e).bind(ke + '.jwerty', fn); };
|
63
|
+
$f = function (e, ob) { $(e || $d).trigger($.Event(ke, ob)); };
|
64
|
+
}
|
65
|
+
|
66
|
+
// Private
|
67
|
+
var _modProps = { 16: 'shiftKey', 17: 'ctrlKey', 18: 'altKey', 91: 'metaKey' };
|
68
|
+
|
69
|
+
// Generate key mappings for common keys that are not printable.
|
70
|
+
var _keys = {
|
71
|
+
|
72
|
+
// MOD aka toggleable keys
|
73
|
+
mods: {
|
74
|
+
// Shift key, ⇧
|
75
|
+
'⇧': 16,
|
76
|
+
shift: 16,
|
77
|
+
// CTRL key, on Mac: ⌃
|
78
|
+
'⌃': 17,
|
79
|
+
ctrl: 17,
|
80
|
+
// ALT key, on Mac: ⌥ (Alt)
|
81
|
+
'⌥': 18,
|
82
|
+
alt: 18,
|
83
|
+
option: 18,
|
84
|
+
// META, on Mac: ⌘ (CMD), on Windows (Win), on Linux (Super)
|
85
|
+
'⌘': 91,
|
86
|
+
meta: 91,
|
87
|
+
cmd: 91,
|
88
|
+
'super': 91,
|
89
|
+
win: 91
|
90
|
+
},
|
91
|
+
|
92
|
+
// Normal keys
|
93
|
+
keys: {
|
94
|
+
// Backspace key, on Mac: ⌫ (Backspace)
|
95
|
+
'⌫': 8,
|
96
|
+
backspace: 8,
|
97
|
+
// Tab Key, on Mac: ⇥ (Tab), on Windows ⇥⇥
|
98
|
+
'⇥': 9,
|
99
|
+
'⇆': 9,
|
100
|
+
tab: 9,
|
101
|
+
// Return key, ↩
|
102
|
+
'↩': 13,
|
103
|
+
'return': 13,
|
104
|
+
enter: 13,
|
105
|
+
'⌅': 13,
|
106
|
+
// Pause/Break key
|
107
|
+
'pause': 19,
|
108
|
+
'pause-break': 19,
|
109
|
+
// Caps Lock key, ⇪
|
110
|
+
'⇪': 20,
|
111
|
+
caps: 20,
|
112
|
+
'caps-lock': 20,
|
113
|
+
// Escape key, on Mac: ⎋, on Windows: Esc
|
114
|
+
'⎋': 27,
|
115
|
+
escape: 27,
|
116
|
+
esc: 27,
|
117
|
+
// Space key
|
118
|
+
space: 32,
|
119
|
+
// Page-Up key, or pgup, on Mac: ↖
|
120
|
+
'↖': 33,
|
121
|
+
pgup: 33,
|
122
|
+
'page-up': 33,
|
123
|
+
// Page-Down key, or pgdown, on Mac: ↘
|
124
|
+
'↘': 34,
|
125
|
+
pgdown: 34,
|
126
|
+
'page-down': 34,
|
127
|
+
// END key, on Mac: ⇟
|
128
|
+
'⇟': 35,
|
129
|
+
end: 35,
|
130
|
+
// HOME key, on Mac: ⇞
|
131
|
+
'⇞': 36,
|
132
|
+
home: 36,
|
133
|
+
// Insert key, or ins
|
134
|
+
ins: 45,
|
135
|
+
insert: 45,
|
136
|
+
// Delete key, on Mac: ⌫ (Delete)
|
137
|
+
del: 46,
|
138
|
+
'delete': 46,
|
139
|
+
|
140
|
+
// Left Arrow Key, or ←
|
141
|
+
'←': 37,
|
142
|
+
left: 37,
|
143
|
+
'arrow-left': 37,
|
144
|
+
// Up Arrow Key, or ↑
|
145
|
+
'↑': 38,
|
146
|
+
up: 38,
|
147
|
+
'arrow-up': 38,
|
148
|
+
// Right Arrow Key, or →
|
149
|
+
'→': 39,
|
150
|
+
right: 39,
|
151
|
+
'arrow-right': 39,
|
152
|
+
// Up Arrow Key, or ↓
|
153
|
+
'↓': 40,
|
154
|
+
down: 40,
|
155
|
+
'arrow-down': 40,
|
156
|
+
|
157
|
+
// odities, printing characters that come out wrong:
|
158
|
+
// Num-Multiply, or *
|
159
|
+
'*': 106,
|
160
|
+
star: 106,
|
161
|
+
asterisk: 106,
|
162
|
+
multiply: 106,
|
163
|
+
// Num-Plus or +
|
164
|
+
'+': 107,
|
165
|
+
'plus': 107,
|
166
|
+
// Num-Subtract, or -
|
167
|
+
'-': 109,
|
168
|
+
subtract: 109,
|
169
|
+
'num-.': 110,
|
170
|
+
'num-period': 110,
|
171
|
+
'num-dot': 110,
|
172
|
+
'num-full-stop': 110,
|
173
|
+
'num-delete': 110,
|
174
|
+
// Semicolon
|
175
|
+
';': 186,
|
176
|
+
semicolon: 186,
|
177
|
+
// = or equals
|
178
|
+
'=': 187,
|
179
|
+
'equals': 187,
|
180
|
+
// Comma, or ,
|
181
|
+
',': 188,
|
182
|
+
comma: 188,
|
183
|
+
//'-': 189, //???
|
184
|
+
// Period, or ., or full-stop
|
185
|
+
'.': 190,
|
186
|
+
period: 190,
|
187
|
+
'full-stop': 190,
|
188
|
+
// Slash, or /, or forward-slash
|
189
|
+
'/': 191,
|
190
|
+
slash: 191,
|
191
|
+
'forward-slash': 191,
|
192
|
+
// Tick, or `, or back-quote
|
193
|
+
'`': 192,
|
194
|
+
tick: 192,
|
195
|
+
'back-quote': 192,
|
196
|
+
// Open bracket, or [
|
197
|
+
'[': 219,
|
198
|
+
'open-bracket': 219,
|
199
|
+
// Back slash, or \
|
200
|
+
'\\': 220,
|
201
|
+
'back-slash': 220,
|
202
|
+
// Close backet, or ]
|
203
|
+
']': 221,
|
204
|
+
'close-bracket': 221,
|
205
|
+
// Apostraphe, or Quote, or '
|
206
|
+
'\'': 222,
|
207
|
+
quote: 222,
|
208
|
+
apostraphe: 222
|
209
|
+
}
|
210
|
+
|
211
|
+
};
|
212
|
+
|
213
|
+
// To minimise code bloat, add all of the 0-9 and NUMPAD 0-9 keys in a loop
|
214
|
+
var i = 47,
|
215
|
+
n = 0;
|
216
|
+
while (++i < 106) {
|
217
|
+
_keys.keys[n] = i;
|
218
|
+
_keys.keys['num-' + n] = i + 48;
|
219
|
+
++n;
|
220
|
+
}
|
221
|
+
|
222
|
+
// To minimise code bloat, add all of the F1-F25 keys in a loop
|
223
|
+
i = 111,
|
224
|
+
n = 1;
|
225
|
+
while (++i < 136) {
|
226
|
+
_keys.keys['f' + n] = i;
|
227
|
+
++n;
|
228
|
+
}
|
229
|
+
|
230
|
+
// To minimise code bloat, add all of the letters of the alphabet in a loop
|
231
|
+
i = 64;
|
232
|
+
while (++i < 91) {
|
233
|
+
_keys.keys[String.fromCharCode(i).toLowerCase()] = i;
|
234
|
+
}
|
235
|
+
|
236
|
+
function JwertyCode(jwertyCode) {
|
237
|
+
var i,
|
238
|
+
c,
|
239
|
+
n,
|
240
|
+
z,
|
241
|
+
keyCombo,
|
242
|
+
optionals,
|
243
|
+
jwertyCodeFragment,
|
244
|
+
rangeMatches,
|
245
|
+
rangeI;
|
246
|
+
|
247
|
+
// In-case we get called with an instance of ourselves, just return that.
|
248
|
+
if (jwertyCode instanceof JwertyCode) return jwertyCode;
|
249
|
+
|
250
|
+
// If jwertyCode isn't an array, cast it as a string and split into array.
|
251
|
+
if (!realTypeOf(jwertyCode, 'array')) {
|
252
|
+
jwertyCode = (String(jwertyCode)).replace(/\s/g, '').toLowerCase()
|
253
|
+
.match(/(?:\+,|[^,])+/g);
|
254
|
+
}
|
255
|
+
|
256
|
+
// Loop through each key sequence in jwertyCode
|
257
|
+
for (i = 0, c = jwertyCode.length; i < c; ++i) {
|
258
|
+
|
259
|
+
// If the key combo at this part of the sequence isn't an array,
|
260
|
+
// cast as a string and split into an array.
|
261
|
+
if (!realTypeOf(jwertyCode[i], 'array')) {
|
262
|
+
jwertyCode[i] = String(jwertyCode[i])
|
263
|
+
.match(/(?:\+\/|[^\/])+/g);
|
264
|
+
}
|
265
|
+
|
266
|
+
// Parse the key optionals in this sequence
|
267
|
+
optionals = [],
|
268
|
+
n = jwertyCode[i].length;
|
269
|
+
while (n--) {
|
270
|
+
|
271
|
+
// Begin creating the object for this key combo
|
272
|
+
jwertyCodeFragment = jwertyCode[i][n];
|
273
|
+
|
274
|
+
keyCombo = {
|
275
|
+
jwertyCombo: String(jwertyCodeFragment),
|
276
|
+
shiftKey: false,
|
277
|
+
ctrlKey: false,
|
278
|
+
altKey: false,
|
279
|
+
metaKey: false
|
280
|
+
};
|
281
|
+
|
282
|
+
// If jwertyCodeFragment isn't an array then cast as a string
|
283
|
+
// and split it into one.
|
284
|
+
if (!realTypeOf(jwertyCodeFragment, 'array')) {
|
285
|
+
jwertyCodeFragment = String(jwertyCodeFragment).toLowerCase()
|
286
|
+
.match(/(?:(?:[^\+])+|\+\+|^\+$)/g);
|
287
|
+
}
|
288
|
+
|
289
|
+
z = jwertyCodeFragment.length;
|
290
|
+
while (z--) {
|
291
|
+
|
292
|
+
// Normalise matching errors
|
293
|
+
if (jwertyCodeFragment[z] === '++') jwertyCodeFragment[z] = '+';
|
294
|
+
|
295
|
+
// Inject either keyCode or ctrl/meta/shift/altKey into keyCombo
|
296
|
+
if (jwertyCodeFragment[z] in _keys.mods) {
|
297
|
+
keyCombo[_modProps[_keys.mods[jwertyCodeFragment[z]]]] = true;
|
298
|
+
} else if (jwertyCodeFragment[z] in _keys.keys) {
|
299
|
+
keyCombo.keyCode = _keys.keys[jwertyCodeFragment[z]];
|
300
|
+
} else {
|
301
|
+
rangeMatches = jwertyCodeFragment[z].match(/^\[([^-]+\-?[^-]*)-([^-]+\-?[^-]*)\]$/);
|
302
|
+
}
|
303
|
+
}
|
304
|
+
if (realTypeOf(keyCombo.keyCode, 'undefined')) {
|
305
|
+
// If we picked up a range match earlier...
|
306
|
+
if (rangeMatches && (rangeMatches[1] in _keys.keys) && (rangeMatches[2] in _keys.keys)) {
|
307
|
+
rangeMatches[2] = _keys.keys[rangeMatches[2]];
|
308
|
+
rangeMatches[1] = _keys.keys[rangeMatches[1]];
|
309
|
+
|
310
|
+
// Go from match 1 and capture all key-comobs up to match 2
|
311
|
+
for (rangeI = rangeMatches[1]; rangeI < rangeMatches[2]; ++rangeI) {
|
312
|
+
optionals.push({
|
313
|
+
altKey: keyCombo.altKey,
|
314
|
+
shiftKey: keyCombo.shiftKey,
|
315
|
+
metaKey: keyCombo.metaKey,
|
316
|
+
ctrlKey: keyCombo.ctrlKey,
|
317
|
+
keyCode: rangeI,
|
318
|
+
jwertyCombo: String(jwertyCodeFragment)
|
319
|
+
});
|
320
|
+
|
321
|
+
}
|
322
|
+
keyCombo.keyCode = rangeI;
|
323
|
+
// Inject either keyCode or ctrl/meta/shift/altKey into keyCombo
|
324
|
+
} else {
|
325
|
+
keyCombo.keyCode = 0;
|
326
|
+
}
|
327
|
+
}
|
328
|
+
optionals.push(keyCombo);
|
329
|
+
|
330
|
+
}
|
331
|
+
this[i] = optionals;
|
332
|
+
}
|
333
|
+
this.length = i;
|
334
|
+
return this;
|
335
|
+
}
|
336
|
+
|
337
|
+
var jwerty = exports.jwerty = {
|
338
|
+
/**
|
339
|
+
* jwerty.event
|
340
|
+
*
|
341
|
+
* `jwerty.event` will return a function, which expects the first
|
342
|
+
* argument to be a key event. When the key event matches `jwertyCode`,
|
343
|
+
* `callbackFunction` is fired. `jwerty.event` is used by `jwerty.key`
|
344
|
+
* to bind the function it returns. `jwerty.event` is useful for
|
345
|
+
* attaching to your own event listeners. It can be used as a decorator
|
346
|
+
* method to encapsulate functionality that you only want to fire after
|
347
|
+
* a specific key combo. If `callbackContext` is specified then it will
|
348
|
+
* be supplied as `callbackFunction`'s context - in other words, the
|
349
|
+
* keyword `this` will be set to `callbackContext` inside the
|
350
|
+
* `callbackFunction` function.
|
351
|
+
*
|
352
|
+
* @param {Mixed} jwertyCode can be an array, or string of key
|
353
|
+
* combinations, which includes optinals and or sequences
|
354
|
+
* @param {Function} callbackFucntion is a function (or boolean) which
|
355
|
+
* is fired when jwertyCode is matched. Return false to
|
356
|
+
* preventDefault()
|
357
|
+
* @param {Object} callbackContext (Optional) The context to call
|
358
|
+
* `callback` with (i.e this)
|
359
|
+
*
|
360
|
+
*/
|
361
|
+
event: function (jwertyCode, callbackFunction, callbackContext /*? this */) {
|
362
|
+
|
363
|
+
// Construct a function out of callbackFunction, if it is a boolean.
|
364
|
+
if (realTypeOf(callbackFunction, 'boolean')) {
|
365
|
+
var bool = callbackFunction;
|
366
|
+
callbackFunction = function () { return bool; };
|
367
|
+
}
|
368
|
+
|
369
|
+
jwertyCode = new JwertyCode(jwertyCode);
|
370
|
+
|
371
|
+
// Initialise in-scope vars.
|
372
|
+
var i = 0,
|
373
|
+
c = jwertyCode.length - 1,
|
374
|
+
returnValue,
|
375
|
+
jwertyCodeIs;
|
376
|
+
|
377
|
+
// This is the event listener function that gets returned...
|
378
|
+
return function (event) {
|
379
|
+
|
380
|
+
// if jwertyCodeIs returns truthy (string)...
|
381
|
+
if ((jwertyCodeIs = jwerty.is(jwertyCode, event, i))) {
|
382
|
+
// ... and this isn't the last key in the sequence,
|
383
|
+
// incriment the key in sequence to check.
|
384
|
+
if (i < c) {
|
385
|
+
++i;
|
386
|
+
return;
|
387
|
+
// ... and this is the last in the sequence (or the only
|
388
|
+
// one in sequence), then fire the callback
|
389
|
+
} else {
|
390
|
+
returnValue = callbackFunction.call(
|
391
|
+
callbackContext || this, event, jwertyCodeIs);
|
392
|
+
|
393
|
+
// If the callback returned false, then we should run
|
394
|
+
// preventDefault();
|
395
|
+
if (returnValue === false) event.preventDefault();
|
396
|
+
|
397
|
+
// Reset i for the next sequence to fire.
|
398
|
+
i = 0;
|
399
|
+
return;
|
400
|
+
}
|
401
|
+
}
|
402
|
+
|
403
|
+
// If the event didn't hit this time, we should reset i to 0,
|
404
|
+
// that is, unless this combo was the first in the sequence,
|
405
|
+
// in which case we should reset i to 1.
|
406
|
+
i = jwerty.is(jwertyCode, event) ? 1 : 0;
|
407
|
+
};
|
408
|
+
},
|
409
|
+
|
410
|
+
/**
|
411
|
+
* jwerty.is
|
412
|
+
*
|
413
|
+
* `jwerty.is` will return a boolean value, based on if `event` matches
|
414
|
+
* `jwertyCode`. `jwerty.is` is called by `jwerty.event` to check
|
415
|
+
* whether or not to fire the callback. `event` can be a DOM event, or
|
416
|
+
* a jQuery/Zepto/Ender manufactured event. The properties of
|
417
|
+
* `jwertyCode` (speficially ctrlKey, altKey, metaKey, shiftKey and
|
418
|
+
* keyCode) should match `jwertyCode`'s properties - if they do, then
|
419
|
+
* `jwerty.is` will return `true`. If they don't, `jwerty.is` will
|
420
|
+
* return `false`.
|
421
|
+
*
|
422
|
+
* @param {Mixed} jwertyCode can be an array, or string of key
|
423
|
+
* combinations, which includes optinals and or sequences
|
424
|
+
* @param {KeyboardEvent} event is the KeyboardEvent to assert against
|
425
|
+
* @param {Integer} i (Optional) checks the `i` key in jwertyCode
|
426
|
+
* sequence
|
427
|
+
*
|
428
|
+
*/
|
429
|
+
is: function (jwertyCode, event, i /*? 0*/) {
|
430
|
+
jwertyCode = new JwertyCode(jwertyCode);
|
431
|
+
// Default `i` to 0
|
432
|
+
i = i || 0;
|
433
|
+
// We are only interested in `i` of jwertyCode;
|
434
|
+
jwertyCode = jwertyCode[i];
|
435
|
+
// jQuery stores the *real* event in `originalEvent`, which we use
|
436
|
+
// because it does annoything stuff to `metaKey`
|
437
|
+
event = event.originalEvent || event;
|
438
|
+
|
439
|
+
// We'll look at each optional in this jwertyCode sequence...
|
440
|
+
var n = jwertyCode.length,
|
441
|
+
returnValue = false;
|
442
|
+
|
443
|
+
// Loop through each fragment of jwertyCode
|
444
|
+
while (n--) {
|
445
|
+
returnValue = jwertyCode[n].jwertyCombo;
|
446
|
+
// For each property in the jwertyCode object, compare to `event`
|
447
|
+
for (var p in jwertyCode[n]) {
|
448
|
+
// ...except for jwertyCode.jwertyCombo...
|
449
|
+
if (p !== 'jwertyCombo' && event[p] != jwertyCode[n][p]) returnValue = false;
|
450
|
+
}
|
451
|
+
// If this jwertyCode optional wasn't falsey, then we can return early.
|
452
|
+
if (returnValue !== false) return returnValue;
|
453
|
+
}
|
454
|
+
return returnValue;
|
455
|
+
},
|
456
|
+
|
457
|
+
/**
|
458
|
+
* jwerty.key
|
459
|
+
*
|
460
|
+
* `jwerty.key` will attach an event listener and fire
|
461
|
+
* `callbackFunction` when `jwertyCode` matches. The event listener is
|
462
|
+
* attached to `document`, meaning it will listen for any key events
|
463
|
+
* on the page (a global shortcut listener). If `callbackContext` is
|
464
|
+
* specified then it will be supplied as `callbackFunction`'s context
|
465
|
+
* - in other words, the keyword `this` will be set to
|
466
|
+
* `callbackContext` inside the `callbackFunction` function.
|
467
|
+
*
|
468
|
+
* @param {Mixed} jwertyCode can be an array, or string of key
|
469
|
+
* combinations, which includes optinals and or sequences
|
470
|
+
* @param {Function} callbackFunction is a function (or boolean) which
|
471
|
+
* is fired when jwertyCode is matched. Return false to
|
472
|
+
* preventDefault()
|
473
|
+
* @param {Object} callbackContext (Optional) The context to call
|
474
|
+
* `callback` with (i.e this)
|
475
|
+
* @param {Mixed} selector can be a string, jQuery/Zepto/Ender object,
|
476
|
+
* or an HTML*Element on which to bind the eventListener
|
477
|
+
* @param {Mixed} selectorContext can be a string, jQuery/Zepto/Ender
|
478
|
+
* object, or an HTML*Element on which to scope the selector
|
479
|
+
*
|
480
|
+
*/
|
481
|
+
key: function (jwertyCode, callbackFunction, callbackContext /*? this */, selector /*? document */, selectorContext /*? body */) {
|
482
|
+
// Because callbackContext is optional, we should check if the
|
483
|
+
// `callbackContext` is a string or element, and if it is, then the
|
484
|
+
// function was called without a context, and `callbackContext` is
|
485
|
+
// actually `selector`
|
486
|
+
var realSelector = realTypeOf(callbackContext, 'element') || realTypeOf(callbackContext, 'string') ? callbackContext : selector,
|
487
|
+
// If `callbackContext` is undefined, or if we skipped it (and
|
488
|
+
// therefore it is `realSelector`), set context to `global`.
|
489
|
+
realcallbackContext = realSelector === callbackContext ? global : callbackContext,
|
490
|
+
// Finally if we did skip `callbackContext`, then shift
|
491
|
+
// `selectorContext` to the left (take it from `selector`)
|
492
|
+
realSelectorContext = realSelector === callbackContext ? selector : selectorContext;
|
493
|
+
|
494
|
+
// If `realSelector` is already a jQuery/Zepto/Ender/DOM element,
|
495
|
+
// then just use it neat, otherwise find it in DOM using $$()
|
496
|
+
$b(
|
497
|
+
realTypeOf(realSelector, 'element') ? realSelector : $$(realSelector, realSelectorContext),
|
498
|
+
jwerty.event(jwertyCode, callbackFunction, realcallbackContext)
|
499
|
+
);
|
500
|
+
},
|
501
|
+
|
502
|
+
/**
|
503
|
+
* jwerty.fire
|
504
|
+
*
|
505
|
+
* `jwerty.fire` will construct a keyup event to fire, based on
|
506
|
+
* `jwertyCode`. The event will be fired against `selector`.
|
507
|
+
* `selectorContext` is used to search for `selector` within
|
508
|
+
* `selectorContext`, similar to jQuery's
|
509
|
+
* `$('selector', 'context')`.
|
510
|
+
*
|
511
|
+
* @param {Mixed} jwertyCode can be an array, or string of key
|
512
|
+
* combinations, which includes optinals and or sequences
|
513
|
+
* @param {Mixed} selector can be a string, jQuery/Zepto/Ender object,
|
514
|
+
* or an HTML*Element on which to bind the eventListener
|
515
|
+
* @param {Mixed} selectorContext can be a string, jQuery/Zepto/Ender
|
516
|
+
* object, or an HTML*Element on which to scope the selector
|
517
|
+
*
|
518
|
+
*/
|
519
|
+
fire: function (jwertyCode, selector /*? document */, selectorContext /*? body */, i) {
|
520
|
+
jwertyCode = new JwertyCode(jwertyCode);
|
521
|
+
var realI = realTypeOf(selectorContext, 'number') ? selectorContext : i;
|
522
|
+
|
523
|
+
// If `realSelector` is already a jQuery/Zepto/Ender/DOM element,
|
524
|
+
// then just use it neat, otherwise find it in DOM using $$()
|
525
|
+
$f(
|
526
|
+
realTypeOf(selector, 'element') ? selector : $$(selector, selectorContext),
|
527
|
+
jwertyCode[realI || 0][0]
|
528
|
+
);
|
529
|
+
},
|
530
|
+
|
531
|
+
KEYS: _keys
|
532
|
+
};
|
533
|
+
|
534
|
+
}(typeof global !== 'undefined' && global.window || this, (typeof module !== 'undefined' && module.exports ? module.exports : this)));
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jwerty-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Griffis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
55
|
+
description: Gem to automate use of the jwerty.js using asset pipeline.
|
56
|
+
email:
|
57
|
+
- abgriff@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- lib/jwerty/rails.rb
|
65
|
+
- lib/jwerty/rails/version.rb
|
66
|
+
- vendor/assets/javascripts/jwerty.js
|
67
|
+
homepage: https://github.com/adamgriffis/jwerty-rails
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.2.2
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Gem to automate use of the jwerty.js using asset pipeline.
|
91
|
+
test_files: []
|