mousetrapjs 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # MousetrapJS
2
+ Mousetrap is a simple library for handling keyboard shortcuts in Javascript, with no external dependencies.
3
+ Documentation can be found in [here](http://craig.is/killing/mice).
4
+
2
5
 
3
6
  ## Installation
4
7
 
@@ -16,15 +19,10 @@ Or install it yourself as:
16
19
 
17
20
  ## Usage
18
21
 
19
- You know
22
+ Just add this line to your `application.js` file
23
+
20
24
  ```
21
25
  //= require mousetrap
22
26
  ```
23
27
 
24
- ## Contributing
25
-
26
- 1. Fork it
27
- 2. Create your feature branch (`git checkout -b my-new-feature`)
28
- 3. Commit your changes (`git commit -am 'Added some feature'`)
29
- 4. Push to the branch (`git push origin my-new-feature`)
30
- 5. Create new Pull Request
28
+ Thanks to [ccampbel](https://github.com/ccampbell/mousetrap) for the hard work.
@@ -1,3 +1,3 @@
1
1
  module Mousetrapjs
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/mousetrapjs.gemspec CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/mousetrapjs/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Luiz Eduardo Kowalski"]
6
6
  gem.email = ["luizeduardokowalski@gmail.com"]
7
- gem.description = %q{Gem for Mousetrap js lib}
8
- gem.summary = %q{Gem for Mousetrap js lib}
7
+ gem.description = %q{Gem for Mousetrap, a simple library for handling keyboard shortcuts in Javascript}
8
+ gem.summary = %q{Mousetrap is a simple library for handling keyboard shortcuts in Javascript}
9
9
  gem.homepage = "https://github.com/luizkowalski/mousetrapjs"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -16,7 +16,7 @@
16
16
  * Mousetrap is a simple keyboard shortcut library for Javascript with
17
17
  * no external dependencies
18
18
  *
19
- * @preserve @version 1.1
19
+ * @version 1.1.1
20
20
  * @url craig.is/killing/mice
21
21
  */
22
22
  window.Mousetrap = (function() {
@@ -24,6 +24,10 @@ window.Mousetrap = (function() {
24
24
  /**
25
25
  * mapping of special keycodes to their corresponding keys
26
26
  *
27
+ * everything in this dictionary cannot use keypress events
28
+ * so it has to be here to map to the correct keycodes for
29
+ * keyup/keydown events
30
+ *
27
31
  * @type {Object}
28
32
  */
29
33
  var _MAP = {
@@ -53,7 +57,9 @@ window.Mousetrap = (function() {
53
57
 
54
58
  /**
55
59
  * mapping for special characters so they can support
56
- * keydown and keyup events
60
+ *
61
+ * this dictionary is only used incase you want to bind a
62
+ * keyup or keydown event to one of these keys
57
63
  *
58
64
  * @type {Object}
59
65
  */
@@ -82,6 +88,8 @@ window.Mousetrap = (function() {
82
88
  *
83
89
  * this is so you can use keyup events with these keys
84
90
  *
91
+ * note that this will only work reliably on US keyboards
92
+ *
85
93
  * @type {Object}
86
94
  */
87
95
  _SHIFT_MAP = {
@@ -183,8 +191,8 @@ window.Mousetrap = (function() {
183
191
  /**
184
192
  * loop through to map numbers on the numeric keypad
185
193
  */
186
- for (i = 96; i < 106; ++i) {
187
- _MAP[i] = i - 96;
194
+ for (i = 0; i <= 9; ++i) {
195
+ _MAP[i + 96] = i;
188
196
  }
189
197
 
190
198
  /**
@@ -204,7 +212,7 @@ window.Mousetrap = (function() {
204
212
  }
205
213
 
206
214
  /**
207
- * takes the event and returns the keycode
215
+ * takes the event and returns the key character
208
216
  *
209
217
  * @param {Event} e
210
218
  * @return {string}
@@ -268,14 +276,15 @@ window.Mousetrap = (function() {
268
276
  function _resetSequences(do_not_reset) {
269
277
  do_not_reset = do_not_reset || {};
270
278
 
271
- var active_sequences = false;
279
+ var active_sequences = false,
280
+ key;
272
281
 
273
- for (var key in _sequence_levels) {
274
- if (!do_not_reset[key]) {
275
- _sequence_levels[key] = 0;
282
+ for (key in _sequence_levels) {
283
+ if (do_not_reset[key]) {
284
+ active_sequences = true;
276
285
  continue;
277
286
  }
278
- active_sequences = true;
287
+ _sequence_levels[key] = 0;
279
288
  }
280
289
 
281
290
  if (!active_sequences) {
@@ -329,7 +338,7 @@ window.Mousetrap = (function() {
329
338
  // if this is a keypress event that means that we need to only
330
339
  // look at the character, otherwise check the modifiers as
331
340
  // well
332
- if (action === 'keypress' || _modifiersMatch(modifiers, callback.modifiers)) {
341
+ if (action == 'keypress' || _modifiersMatch(modifiers, callback.modifiers)) {
333
342
 
334
343
  // remove is used so if you change your mind and call bind a
335
344
  // second time with a new function the first one is overwritten
@@ -373,13 +382,38 @@ window.Mousetrap = (function() {
373
382
  }
374
383
 
375
384
  /**
376
- * fires a callback for a matching keycode
385
+ * actually calls the callback function
386
+ *
387
+ * if your callback function returns false this will use the jquery
388
+ * convention - prevent default and stop propogation on the event
389
+ *
390
+ * @param {Function} callback
391
+ * @param {Event} e
392
+ * @returns void
393
+ */
394
+ function _fireCallback(callback, e) {
395
+ if (callback(e) === false) {
396
+ if (e.preventDefault) {
397
+ e.preventDefault();
398
+ }
399
+
400
+ if (e.stopPropagation) {
401
+ e.stopPropagation();
402
+ }
403
+
404
+ e.returnValue = false;
405
+ e.cancelBubble = true;
406
+ }
407
+ }
408
+
409
+ /**
410
+ * handles a character key event
377
411
  *
378
412
  * @param {string} character
379
413
  * @param {Event} e
380
414
  * @returns void
381
415
  */
382
- function _fireCallback(character, e) {
416
+ function _handleCharacter(character, e) {
383
417
 
384
418
  // if this event should not happen stop here
385
419
  if (_stop(e)) {
@@ -404,14 +438,14 @@ window.Mousetrap = (function() {
404
438
 
405
439
  // keep a list of which sequences were matches for later
406
440
  do_not_reset[callbacks[i]['seq']] = 1;
407
- callbacks[i].callback(e);
441
+ _fireCallback(callbacks[i].callback, e);
408
442
  continue;
409
443
  }
410
444
 
411
445
  // if there were no sequence matches but we are still here
412
- // that means this is a regular match so we should fire then break
446
+ // that means this is a regular match so we should fire that
413
447
  if (!processed_sequence_callback && !_inside_sequence) {
414
- callbacks[i].callback(e);
448
+ _fireCallback(callbacks[i].callback, e);
415
449
  }
416
450
  }
417
451
 
@@ -431,7 +465,7 @@ window.Mousetrap = (function() {
431
465
  */
432
466
  function _handleKey(e) {
433
467
 
434
- // add which for key events
468
+ // normalize e.which for key events
435
469
  // @see http://stackoverflow.com/questions/4285627/javascript-keycode-vs-charcode-utter-confusion
436
470
  e.which = typeof e.which == "number" ? e.which : e.keyCode;
437
471
 
@@ -442,12 +476,12 @@ window.Mousetrap = (function() {
442
476
  return;
443
477
  }
444
478
 
445
- if (e.type === 'keyup' && _ignore_next_keyup === character) {
479
+ if (e.type == 'keyup' && _ignore_next_keyup == character) {
446
480
  _ignore_next_keyup = false;
447
481
  return;
448
482
  }
449
483
 
450
- _fireCallback(character, e);
484
+ _handleCharacter(character, e);
451
485
  }
452
486
 
453
487
  /**
@@ -468,7 +502,7 @@ window.Mousetrap = (function() {
468
502
  *
469
503
  * @returns void
470
504
  */
471
- function _resetSequence() {
505
+ function _resetSequenceTimer() {
472
506
  clearTimeout(_reset_timer);
473
507
  _reset_timer = setTimeout(_resetSequences, 1000);
474
508
  }
@@ -515,7 +549,7 @@ window.Mousetrap = (function() {
515
549
 
516
550
  // modifier keys don't work as expected with keypress,
517
551
  // switch to keydown
518
- if (action === 'keypress' && modifiers.length) {
552
+ if (action == 'keypress' && modifiers.length) {
519
553
  action = 'keydown';
520
554
  }
521
555
 
@@ -553,7 +587,7 @@ window.Mousetrap = (function() {
553
587
  var _increaseSequence = function(e) {
554
588
  _inside_sequence = action;
555
589
  ++_sequence_levels[combo];
556
- _resetSequence();
590
+ _resetSequenceTimer();
557
591
  },
558
592
 
559
593
  /**
@@ -564,7 +598,7 @@ window.Mousetrap = (function() {
564
598
  * @returns void
565
599
  */
566
600
  _callbackAndReset = function(e) {
567
- callback(e);
601
+ _fireCallback(callback, e);
568
602
 
569
603
  // we should ignore the next key up if the action is key down
570
604
  // or keypress. this is so if you finish a sequence and
@@ -684,9 +718,9 @@ window.Mousetrap = (function() {
684
718
  }
685
719
 
686
720
  // start!
721
+ _addEvent(document, 'keypress', _handleKey);
687
722
  _addEvent(document, 'keydown', _handleKey);
688
723
  _addEvent(document, 'keyup', _handleKey);
689
- _addEvent(document, 'keypress', _handleKey);
690
724
 
691
725
  return {
692
726
 
@@ -720,7 +754,7 @@ window.Mousetrap = (function() {
720
754
  * the keycombo+action has to be exactly the same as
721
755
  * it was defined in the bind method
722
756
  *
723
- * @todo actually remove this from the _callbacks dictionary instead
757
+ * TODO: actually remove this from the _callbacks dictionary instead
724
758
  * of binding an empty function
725
759
  *
726
760
  * @param {string|Array} keys
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mousetrapjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-11 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Gem for Mousetrap js lib
14
+ description: Gem for Mousetrap, a simple library for handling keyboard shortcuts in
15
+ Javascript
15
16
  email:
16
17
  - luizeduardokowalski@gmail.com
17
18
  executables: []
@@ -50,5 +51,5 @@ rubyforge_project:
50
51
  rubygems_version: 1.8.22
51
52
  signing_key:
52
53
  specification_version: 3
53
- summary: Gem for Mousetrap js lib
54
+ summary: Mousetrap is a simple library for handling keyboard shortcuts in Javascript
54
55
  test_files: []