mousetrapjs 0.1.2.2 → 0.1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 695bbc0800be132445f64021f08b14450ed51408
4
+ data.tar.gz: d87c345754c3ecbff2005b5fcde86c66a690acf7
5
+ SHA512:
6
+ metadata.gz: 60ac93071cd1eee948d5bde94115b2af5ce75cc4b0fcfebdd64555e726217b5106cd2f16cc65daa10624ccb10d451243530e94ba69764ae54c490d5cabd8d1f4
7
+ data.tar.gz: 43609f8b5a37550e9711c3f1f7d0554186b22efa0aa673f9e7cdaceefd2547c23c187f24c3ad299f3899d1f70c44275fcf588c5af739b87fd992d19e9f78f44b
@@ -1,3 +1,3 @@
1
1
  module Mousetrapjs
2
- VERSION = "0.1.2.2"
2
+ VERSION = "0.1.3.2"
3
3
  end
@@ -1,5 +1,6 @@
1
+ /*global define:false */
1
2
  /**
2
- * Copyright 2012 Craig Campbell
3
+ * Copyright 2013 Craig Campbell
3
4
  *
4
5
  * Licensed under the Apache License, Version 2.0 (the "License");
5
6
  * you may not use this file except in compliance with the License.
@@ -16,7 +17,7 @@
16
17
  * Mousetrap is a simple keyboard shortcut library for Javascript with
17
18
  * no external dependencies
18
19
  *
19
- * @version 1.2.2
20
+ * @version 1.3.2
20
21
  * @url craig.is/killing/mice
21
22
  */
22
23
  (function() {
@@ -148,7 +149,7 @@
148
149
  *
149
150
  * @type {Object}
150
151
  */
151
- _direct_map = {},
152
+ _directMap = {},
152
153
 
153
154
  /**
154
155
  * keeps track of what level each sequence is at since multiple
@@ -156,21 +157,21 @@
156
157
  *
157
158
  * @type {Object}
158
159
  */
159
- _sequence_levels = {},
160
+ _sequenceLevels = {},
160
161
 
161
162
  /**
162
163
  * variable to store the setTimeout call
163
164
  *
164
165
  * @type {null|number}
165
166
  */
166
- _reset_timer,
167
+ _resetTimer,
167
168
 
168
169
  /**
169
170
  * temporary state where we will ignore the next keyup
170
171
  *
171
172
  * @type {boolean|string}
172
173
  */
173
- _ignore_next_keyup = false,
174
+ _ignoreNextKeyup = false,
174
175
 
175
176
  /**
176
177
  * are we currently inside of a sequence?
@@ -178,7 +179,7 @@
178
179
  *
179
180
  * @type {boolean|string}
180
181
  */
181
- _sequence_type = false;
182
+ _sequenceType = false;
182
183
 
183
184
  /**
184
185
  * loop through the f keys, f1 to f19 and add them to the map
@@ -222,7 +223,22 @@
222
223
 
223
224
  // for keypress events we should return the character as is
224
225
  if (e.type == 'keypress') {
225
- return String.fromCharCode(e.which);
226
+ var character = String.fromCharCode(e.which);
227
+
228
+ // if the shift key is not pressed then it is safe to assume
229
+ // that we want the character to be lowercase. this means if
230
+ // you accidentally have caps lock on then your key bindings
231
+ // will continue to work
232
+ //
233
+ // the only side effect that might not be desired is if you
234
+ // bind something like 'A' cause you want to trigger an
235
+ // event when capital A is pressed caps lock will no longer
236
+ // trigger the event. shift+a will though.
237
+ if (!e.shiftKey) {
238
+ character = character.toLowerCase();
239
+ }
240
+
241
+ return character;
226
242
  }
227
243
 
228
244
  // for non keypress events the special maps are needed
@@ -235,6 +251,10 @@
235
251
  }
236
252
 
237
253
  // if it is not in the special map
254
+
255
+ // with keydown and keyup events the character seems to always
256
+ // come in as an uppercase character whether you are pressing shift
257
+ // or not. we should make sure it is always lowercase for comparisons
238
258
  return String.fromCharCode(e.which).toLowerCase();
239
259
  }
240
260
 
@@ -252,25 +272,25 @@
252
272
  /**
253
273
  * resets all sequence counters except for the ones passed in
254
274
  *
255
- * @param {Object} do_not_reset
275
+ * @param {Object} doNotReset
256
276
  * @returns void
257
277
  */
258
- function _resetSequences(do_not_reset, max_level) {
259
- do_not_reset = do_not_reset || {};
278
+ function _resetSequences(doNotReset, maxLevel) {
279
+ doNotReset = doNotReset || {};
260
280
 
261
- var active_sequences = false,
281
+ var activeSequences = false,
262
282
  key;
263
283
 
264
- for (key in _sequence_levels) {
265
- if (do_not_reset[key] && _sequence_levels[key] > max_level) {
266
- active_sequences = true;
284
+ for (key in _sequenceLevels) {
285
+ if (doNotReset[key] && _sequenceLevels[key] > maxLevel) {
286
+ activeSequences = true;
267
287
  continue;
268
288
  }
269
- _sequence_levels[key] = 0;
289
+ _sequenceLevels[key] = 0;
270
290
  }
271
291
 
272
- if (!active_sequences) {
273
- _sequence_type = false;
292
+ if (!activeSequences) {
293
+ _sequenceType = false;
274
294
  }
275
295
  }
276
296
 
@@ -308,7 +328,7 @@
308
328
 
309
329
  // if this is a sequence but it is not at the right level
310
330
  // then move onto the next match
311
- if (callback.seq && _sequence_levels[callback.seq] != callback.level) {
331
+ if (callback.seq && _sequenceLevels[callback.seq] != callback.level) {
312
332
  continue;
313
333
  }
314
334
 
@@ -403,15 +423,16 @@
403
423
  * handles a character key event
404
424
  *
405
425
  * @param {string} character
426
+ * @param {Array} modifiers
406
427
  * @param {Event} e
407
428
  * @returns void
408
429
  */
409
- function _handleCharacter(character, e) {
410
- var callbacks = _getMatches(character, _eventModifiers(e), e),
430
+ function _handleKey(character, modifiers, e) {
431
+ var callbacks = _getMatches(character, modifiers, e),
411
432
  i,
412
- do_not_reset = {},
413
- max_level = 0,
414
- processed_sequence_callback = false;
433
+ doNotReset = {},
434
+ maxLevel = 0,
435
+ processedSequenceCallback = false;
415
436
 
416
437
  // loop through matching callbacks for this key event
417
438
  for (i = 0; i < callbacks.length; ++i) {
@@ -422,21 +443,21 @@
422
443
  // callback for matching g cause otherwise you can only ever
423
444
  // match the first one
424
445
  if (callbacks[i].seq) {
425
- processed_sequence_callback = true;
446
+ processedSequenceCallback = true;
426
447
 
427
448
  // as we loop through keep track of the max
428
449
  // any sequence at a lower level will be discarded
429
- max_level = Math.max(max_level, callbacks[i].level);
450
+ maxLevel = Math.max(maxLevel, callbacks[i].level);
430
451
 
431
452
  // keep a list of which sequences were matches for later
432
- do_not_reset[callbacks[i].seq] = 1;
453
+ doNotReset[callbacks[i].seq] = 1;
433
454
  _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
434
455
  continue;
435
456
  }
436
457
 
437
458
  // if there were no sequence matches but we are still here
438
459
  // that means this is a regular match so we should fire that
439
- if (!processed_sequence_callback && !_sequence_type) {
460
+ if (!processedSequenceCallback && !_sequenceType) {
440
461
  _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
441
462
  }
442
463
  }
@@ -444,8 +465,8 @@
444
465
  // if you are inside of a sequence and the key you are pressing
445
466
  // is not a modifier key then we should reset all sequences
446
467
  // that were not matched by this key event
447
- if (e.type == _sequence_type && !_isModifier(character)) {
448
- _resetSequences(do_not_reset, max_level);
468
+ if (e.type == _sequenceType && !_isModifier(character)) {
469
+ _resetSequences(doNotReset, maxLevel);
449
470
  }
450
471
  }
451
472
 
@@ -455,7 +476,7 @@
455
476
  * @param {Event} e
456
477
  * @returns void
457
478
  */
458
- function _handleKey(e) {
479
+ function _handleKeyEvent(e) {
459
480
 
460
481
  // normalize e.which for key events
461
482
  // @see http://stackoverflow.com/questions/4285627/javascript-keycode-vs-charcode-utter-confusion
@@ -470,12 +491,12 @@
470
491
  return;
471
492
  }
472
493
 
473
- if (e.type == 'keyup' && _ignore_next_keyup == character) {
474
- _ignore_next_keyup = false;
494
+ if (e.type == 'keyup' && _ignoreNextKeyup == character) {
495
+ _ignoreNextKeyup = false;
475
496
  return;
476
497
  }
477
498
 
478
- _handleCharacter(character, e);
499
+ Mousetrap.handleKey(character, _eventModifiers(e), e);
479
500
  }
480
501
 
481
502
  /**
@@ -497,8 +518,8 @@
497
518
  * @returns void
498
519
  */
499
520
  function _resetSequenceTimer() {
500
- clearTimeout(_reset_timer);
501
- _reset_timer = setTimeout(_resetSequences, 1000);
521
+ clearTimeout(_resetTimer);
522
+ _resetTimer = setTimeout(_resetSequences, 1000);
502
523
  }
503
524
 
504
525
  /**
@@ -563,7 +584,7 @@
563
584
 
564
585
  // start off by adding a sequence level record for this combination
565
586
  // and setting the level to 0
566
- _sequence_levels[combo] = 0;
587
+ _sequenceLevels[combo] = 0;
567
588
 
568
589
  // if there is no action pick the best one for the first key
569
590
  // in the sequence
@@ -578,9 +599,9 @@
578
599
  * @param {Event} e
579
600
  * @returns void
580
601
  */
581
- var _increaseSequence = function(e) {
582
- _sequence_type = action;
583
- ++_sequence_levels[combo];
602
+ var _increaseSequence = function() {
603
+ _sequenceType = action;
604
+ ++_sequenceLevels[combo];
584
605
  _resetSequenceTimer();
585
606
  },
586
607
 
@@ -598,7 +619,7 @@
598
619
  // or keypress. this is so if you finish a sequence and
599
620
  // release the key the final key will not trigger a keyup
600
621
  if (action !== 'keyup') {
601
- _ignore_next_keyup = _characterFromEvent(e);
622
+ _ignoreNextKeyup = _characterFromEvent(e);
602
623
  }
603
624
 
604
625
  // weird race condition if a sequence ends with the key
@@ -621,11 +642,14 @@
621
642
  * @param {string} combination
622
643
  * @param {Function} callback
623
644
  * @param {string=} action
624
- * @param {string=} sequence_name - name of sequence if part of sequence
645
+ * @param {string=} sequenceName - name of sequence if part of sequence
625
646
  * @param {number=} level - what part of the sequence the command is
626
647
  * @returns void
627
648
  */
628
- function _bindSingle(combination, callback, action, sequence_name, level) {
649
+ function _bindSingle(combination, callback, action, sequenceName, level) {
650
+
651
+ // store a direct mapped reference for use with Mousetrap.trigger
652
+ _directMap[combination + ':' + action] = callback;
629
653
 
630
654
  // make sure multiple spaces in a row become a single space
631
655
  combination = combination.replace(/\s+/g, ' ');
@@ -680,7 +704,7 @@
680
704
  }
681
705
 
682
706
  // remove an existing match if there is one
683
- _getMatches(key, modifiers, {type: action}, !sequence_name, combination);
707
+ _getMatches(key, modifiers, {type: action}, !sequenceName, combination);
684
708
 
685
709
  // add this call back to the array
686
710
  // if it is a sequence put it at the beginning
@@ -688,11 +712,11 @@
688
712
  //
689
713
  // this is important because the way these are processed expects
690
714
  // the sequence ones to come first
691
- _callbacks[key][sequence_name ? 'unshift' : 'push']({
715
+ _callbacks[key][sequenceName ? 'unshift' : 'push']({
692
716
  callback: callback,
693
717
  modifiers: modifiers,
694
718
  action: action,
695
- seq: sequence_name,
719
+ seq: sequenceName,
696
720
  level: level,
697
721
  combo: combination
698
722
  });
@@ -713,9 +737,9 @@
713
737
  }
714
738
 
715
739
  // start!
716
- _addEvent(document, 'keypress', _handleKey);
717
- _addEvent(document, 'keydown', _handleKey);
718
- _addEvent(document, 'keyup', _handleKey);
740
+ _addEvent(document, 'keypress', _handleKeyEvent);
741
+ _addEvent(document, 'keydown', _handleKeyEvent);
742
+ _addEvent(document, 'keyup', _handleKeyEvent);
719
743
 
720
744
  var Mousetrap = {
721
745
 
@@ -734,8 +758,8 @@
734
758
  * @returns void
735
759
  */
736
760
  bind: function(keys, callback, action) {
737
- _bindMultiple(keys instanceof Array ? keys : [keys], callback, action);
738
- _direct_map[keys + ':' + action] = callback;
761
+ keys = keys instanceof Array ? keys : [keys];
762
+ _bindMultiple(keys, callback, action);
739
763
  return this;
740
764
  },
741
765
 
@@ -744,24 +768,20 @@
744
768
  *
745
769
  * the unbinding sets the callback function of the specified key combo
746
770
  * to an empty function and deletes the corresponding key in the
747
- * _direct_map dict.
748
- *
749
- * the keycombo+action has to be exactly the same as
750
- * it was defined in the bind method
771
+ * _directMap dict.
751
772
  *
752
773
  * TODO: actually remove this from the _callbacks dictionary instead
753
774
  * of binding an empty function
754
775
  *
776
+ * the keycombo+action has to be exactly the same as
777
+ * it was defined in the bind method
778
+ *
755
779
  * @param {string|Array} keys
756
780
  * @param {string} action
757
781
  * @returns void
758
782
  */
759
783
  unbind: function(keys, action) {
760
- if (_direct_map[keys + ':' + action]) {
761
- delete _direct_map[keys + ':' + action];
762
- this.bind(keys, function() {}, action);
763
- }
764
- return this;
784
+ return Mousetrap.bind(keys, function() {}, action);
765
785
  },
766
786
 
767
787
  /**
@@ -772,7 +792,9 @@
772
792
  * @returns void
773
793
  */
774
794
  trigger: function(keys, action) {
775
- _direct_map[keys + ':' + action]();
795
+ if (_directMap[keys + ':' + action]) {
796
+ _directMap[keys + ':' + action]({}, keys);
797
+ }
776
798
  return this;
777
799
  },
778
800
 
@@ -785,7 +807,7 @@
785
807
  */
786
808
  reset: function() {
787
809
  _callbacks = {};
788
- _direct_map = {};
810
+ _directMap = {};
789
811
  return this;
790
812
  },
791
813
 
@@ -796,7 +818,7 @@
796
818
  * @param {Element} element
797
819
  * @return {boolean}
798
820
  */
799
- stopCallback: function(e, element, combo) {
821
+ stopCallback: function(e, element) {
800
822
 
801
823
  // if the element has the class "mousetrap" then no need to stop
802
824
  if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
@@ -805,7 +827,12 @@
805
827
 
806
828
  // stop for input, select, and textarea
807
829
  return element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA' || (element.contentEditable && element.contentEditable == 'true');
808
- }
830
+ },
831
+
832
+ /**
833
+ * exposes _handleKey publicly so it can be overwritten by extensions
834
+ */
835
+ handleKey: _handleKey
809
836
  };
810
837
 
811
838
  // expose mousetrap to the global object
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mousetrapjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2.2
5
- prerelease:
4
+ version: 0.1.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Luiz Eduardo Kowalski
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-16 00:00:00.000000000 Z
11
+ date: 2013-04-12 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Gem for Mousetrap, a simple library for handling keyboard shortcuts in
15
14
  Javascript
@@ -30,26 +29,25 @@ files:
30
29
  - vendor/assets/javascripts/mousetrap.js
31
30
  homepage: https://github.com/luizkowalski/mousetrapjs
32
31
  licenses: []
32
+ metadata: {}
33
33
  post_install_message:
34
34
  rdoc_options: []
35
35
  require_paths:
36
36
  - lib
37
37
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
38
  requirements:
40
- - - ! '>='
39
+ - - '>='
41
40
  - !ruby/object:Gem::Version
42
41
  version: '0'
43
42
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
43
  requirements:
46
- - - ! '>='
44
+ - - '>='
47
45
  - !ruby/object:Gem::Version
48
46
  version: '0'
49
47
  requirements: []
50
48
  rubyforge_project:
51
- rubygems_version: 1.8.23
49
+ rubygems_version: 2.0.0
52
50
  signing_key:
53
- specification_version: 3
51
+ specification_version: 4
54
52
  summary: Mousetrap is a simple library for handling keyboard shortcuts in Javascript
55
53
  test_files: []