mousetrapjs 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e244036ef071e3d6222f608a02a948661138a11
4
- data.tar.gz: a1ca76a8c83b8df9cce0ab86d3538591dd4c5e56
3
+ metadata.gz: a05b6045de34fc7a0ad351cfbde3c00199238455
4
+ data.tar.gz: 8c923714b26069877b456fffbfd66b5fbefa4485
5
5
  SHA512:
6
- metadata.gz: d09f19f0fbb6d71efc1cfe9a324bec24a7ece344d05f948770fb19c1b370deaab8899cf257595dbdde6161ae560546b3b602ef2a7f884ded80eda41a0b914da9
7
- data.tar.gz: 117a6788596f94ab0513fbfb12a61699abd52ddf62acf5189d0f59cd38ee5fec6e99edd79b5de8b05b1d106faff6f40b493dc88f01584dc76cf38042e7597706
6
+ metadata.gz: 1c147ad6eeb0ab7b660fdaa2ff6d968ef4245f3bad8fda3f0281221bb3cb44e13ca21f4585869a3e02d98ed582c34d5325d8cdde16801d17383f52be4f89cf7e
7
+ data.tar.gz: d3a340ed7919ea2731f88493e229a6815ac0ae8b861e6a087870377d39ff52a9fa49b64c5fdb207e9c0fb66fffb961e6ae7688d614f3d9cdb8d2cc2466c7622e
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in mousetrapjs.gemspec
4
3
  gemspec
@@ -1,3 +1,3 @@
1
1
  module Mousetrapjs
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.1"
3
3
  end
@@ -17,7 +17,7 @@
17
17
  * Mousetrap is a simple keyboard shortcut library for Javascript with
18
18
  * no external dependencies
19
19
  *
20
- * @version 1.4.0
20
+ * @version 1.4.1
21
21
  * @url craig.is/killing/mice
22
22
  */
23
23
  (function() {
@@ -180,7 +180,7 @@
180
180
  *
181
181
  * @type {boolean|string}
182
182
  */
183
- _sequenceType = false;
183
+ _nextExpectedAction = false;
184
184
 
185
185
  /**
186
186
  * loop through the f keys, f1 to f19 and add them to the map
@@ -291,7 +291,7 @@
291
291
  }
292
292
 
293
293
  if (!activeSequences) {
294
- _sequenceType = false;
294
+ _nextExpectedAction = false;
295
295
  }
296
296
  }
297
297
 
@@ -302,11 +302,12 @@
302
302
  * @param {string} character
303
303
  * @param {Array} modifiers
304
304
  * @param {Event|Object} e
305
- * @param {boolean=} remove - should we remove any matches
305
+ * @param {string=} sequenceName - name of the sequence we are looking for
306
306
  * @param {string=} combination
307
+ * @param {number=} level
307
308
  * @returns {Array}
308
309
  */
309
- function _getMatches(character, modifiers, e, remove, combination) {
310
+ function _getMatches(character, modifiers, e, sequenceName, combination, level) {
310
311
  var i,
311
312
  callback,
312
313
  matches = [],
@@ -327,9 +328,9 @@
327
328
  for (i = 0; i < _callbacks[character].length; ++i) {
328
329
  callback = _callbacks[character][i];
329
330
 
330
- // if this is a sequence but it is not at the right level
331
- // then move onto the next match
332
- if (callback.seq && _sequenceLevels[callback.seq] != callback.level) {
331
+ // if a sequence name is not specified, but this is a sequence at
332
+ // the wrong level then move onto the next match
333
+ if (!sequenceName && callback.seq && _sequenceLevels[callback.seq] != callback.level) {
333
334
  continue;
334
335
  }
335
336
 
@@ -348,9 +349,14 @@
348
349
  // firefox will fire a keypress if meta or control is down
349
350
  if ((action == 'keypress' && !e.metaKey && !e.ctrlKey) || _modifiersMatch(modifiers, callback.modifiers)) {
350
351
 
351
- // remove is used so if you change your mind and call bind a
352
- // second time with a new function the first one is overwritten
353
- if (remove && callback.combo == combination) {
352
+ // when you bind a combination or sequence a second time it
353
+ // should overwrite the first one. if a sequenceName or
354
+ // combination is specified in this call it does just that
355
+ //
356
+ // @todo make deleting its own method?
357
+ var deleteCombo = !sequenceName && callback.combo == combination;
358
+ var deleteSequence = sequenceName && callback.seq == sequenceName && callback.level == level;
359
+ if (deleteCombo || deleteSequence) {
354
360
  _callbacks[character].splice(i, 1);
355
361
  }
356
362
 
@@ -490,7 +496,7 @@
490
496
  // modifier keys are ignored because you can have a sequence
491
497
  // that contains modifiers such as "enter ctrl+space" and in most
492
498
  // cases the modifier key will be pressed before the next key
493
- if (e.type == _sequenceType && !_isModifier(character)) {
499
+ if (e.type == _nextExpectedAction && !_isModifier(character)) {
494
500
  _resetSequences(doNotReset);
495
501
  }
496
502
  }
@@ -611,90 +617,89 @@
611
617
  // and setting the level to 0
612
618
  _sequenceLevels[combo] = 0;
613
619
 
614
- // if there is no action pick the best one for the first key
615
- // in the sequence
616
- if (!action) {
617
- action = _pickBestAction(keys[0], []);
618
- }
619
-
620
620
  /**
621
621
  * callback to increase the sequence level for this sequence and reset
622
622
  * all other sequences that were active
623
623
  *
624
- * @param {Event} e
625
- * @returns void
624
+ * @param {string} nextAction
625
+ * @returns {Function}
626
626
  */
627
- var _increaseSequence = function() {
628
- _sequenceType = action;
627
+ function _increaseSequence(nextAction) {
628
+ return function() {
629
+ _nextExpectedAction = nextAction;
629
630
  ++_sequenceLevels[combo];
630
631
  _resetSequenceTimer();
631
- },
632
-
633
- /**
634
- * wraps the specified callback inside of another function in order
635
- * to reset all sequence counters as soon as this sequence is done
636
- *
637
- * @param {Event} e
638
- * @returns void
639
- */
640
- _callbackAndReset = function(e) {
641
- _fireCallback(callback, e, combo);
642
-
643
- // we should ignore the next key up if the action is key down
644
- // or keypress. this is so if you finish a sequence and
645
- // release the key the final key will not trigger a keyup
646
- if (action !== 'keyup') {
647
- _ignoreNextKeyup = _characterFromEvent(e);
648
- }
632
+ };
633
+ }
634
+
635
+ /**
636
+ * wraps the specified callback inside of another function in order
637
+ * to reset all sequence counters as soon as this sequence is done
638
+ *
639
+ * @param {Event} e
640
+ * @returns void
641
+ */
642
+ function _callbackAndReset(e) {
643
+ _fireCallback(callback, e, combo);
644
+
645
+ // we should ignore the next key up if the action is key down
646
+ // or keypress. this is so if you finish a sequence and
647
+ // release the key the final key will not trigger a keyup
648
+ if (action !== 'keyup') {
649
+ _ignoreNextKeyup = _characterFromEvent(e);
650
+ }
649
651
 
650
- // weird race condition if a sequence ends with the key
651
- // another sequence begins with
652
- setTimeout(_resetSequences, 10);
653
- },
654
- i;
652
+ // weird race condition if a sequence ends with the key
653
+ // another sequence begins with
654
+ setTimeout(_resetSequences, 10);
655
+ }
655
656
 
656
657
  // loop through keys one at a time and bind the appropriate callback
657
658
  // function. for any key leading up to the final one it should
658
659
  // increase the sequence. after the final, it should reset all sequences
659
- for (i = 0; i < keys.length; ++i) {
660
- _bindSingle(keys[i], i < keys.length - 1 ? _increaseSequence : _callbackAndReset, action, combo, i);
660
+ //
661
+ // if an action is specified in the original bind call then that will
662
+ // be used throughout. otherwise we will pass the action that the
663
+ // next key in the sequence should match. this allows a sequence
664
+ // to mix and match keypress and keydown events depending on which
665
+ // ones are better suited to the key provided
666
+ for (var i = 0; i < keys.length; ++i) {
667
+ var isFinal = i + 1 === keys.length;
668
+ var wrappedCallback = isFinal ? _callbackAndReset : _increaseSequence(action || _getKeyInfo(keys[i + 1]).action);
669
+ _bindSingle(keys[i], wrappedCallback, action, combo, i);
661
670
  }
662
671
  }
663
672
 
664
673
  /**
665
- * binds a single keyboard combination
674
+ * Converts from a string key combination to an array
666
675
  *
667
- * @param {string} combination
668
- * @param {Function} callback
669
- * @param {string=} action
670
- * @param {string=} sequenceName - name of sequence if part of sequence
671
- * @param {number=} level - what part of the sequence the command is
672
- * @returns void
676
+ * @param {string} combination like "command+shift+l"
677
+ * @return {Array}
673
678
  */
674
- function _bindSingle(combination, callback, action, sequenceName, level) {
675
-
676
- // store a direct mapped reference for use with Mousetrap.trigger
677
- _directMap[combination + ':' + action] = callback;
679
+ function _keysFromString(combination) {
680
+ if (combination === '+') {
681
+ return ['+'];
682
+ }
678
683
 
679
- // make sure multiple spaces in a row become a single space
680
- combination = combination.replace(/\s+/g, ' ');
684
+ return combination.split('+');
685
+ }
681
686
 
682
- var sequence = combination.split(' '),
683
- i,
687
+ /**
688
+ * Gets info for a specific key combination
689
+ *
690
+ * @param {string} combination key combination ("command+s" or "a" or "*")
691
+ * @param {string=} action
692
+ * @returns {Object}
693
+ */
694
+ function _getKeyInfo(combination, action) {
695
+ var keys,
684
696
  key,
685
- keys,
697
+ i,
686
698
  modifiers = [];
687
699
 
688
- // if this pattern is a sequence of keys then run through this method
689
- // to reprocess each pattern one key at a time
690
- if (sequence.length > 1) {
691
- _bindSequence(combination, sequence, callback, action);
692
- return;
693
- }
694
-
695
700
  // take the keys from this pattern and figure out what the actual
696
701
  // pattern is all about
697
- keys = combination === '+' ? ['+'] : combination.split('+');
702
+ keys = _keysFromString(combination);
698
703
 
699
704
  for (i = 0; i < keys.length; ++i) {
700
705
  key = keys[i];
@@ -722,14 +727,49 @@
722
727
  // we will try to pick the best event for it
723
728
  action = _pickBestAction(key, modifiers, action);
724
729
 
730
+ return {
731
+ key: key,
732
+ modifiers: modifiers,
733
+ action: action
734
+ };
735
+ }
736
+
737
+ /**
738
+ * binds a single keyboard combination
739
+ *
740
+ * @param {string} combination
741
+ * @param {Function} callback
742
+ * @param {string=} action
743
+ * @param {string=} sequenceName - name of sequence if part of sequence
744
+ * @param {number=} level - what part of the sequence the command is
745
+ * @returns void
746
+ */
747
+ function _bindSingle(combination, callback, action, sequenceName, level) {
748
+
749
+ // store a direct mapped reference for use with Mousetrap.trigger
750
+ _directMap[combination + ':' + action] = callback;
751
+
752
+ // make sure multiple spaces in a row become a single space
753
+ combination = combination.replace(/\s+/g, ' ');
754
+
755
+ var sequence = combination.split(' '),
756
+ info;
757
+
758
+ // if this pattern is a sequence of keys then run through this method
759
+ // to reprocess each pattern one key at a time
760
+ if (sequence.length > 1) {
761
+ _bindSequence(combination, sequence, callback, action);
762
+ return;
763
+ }
764
+
765
+ info = _getKeyInfo(combination, action);
766
+
725
767
  // make sure to initialize array if this is the first time
726
768
  // a callback is added for this key
727
- if (!_callbacks[key]) {
728
- _callbacks[key] = [];
729
- }
769
+ _callbacks[info.key] = _callbacks[info.key] || [];
730
770
 
731
771
  // remove an existing match if there is one
732
- _getMatches(key, modifiers, {type: action}, !sequenceName, combination);
772
+ _getMatches(info.key, info.modifiers, {type: info.action}, sequenceName, combination, level);
733
773
 
734
774
  // add this call back to the array
735
775
  // if it is a sequence put it at the beginning
@@ -737,10 +777,10 @@
737
777
  //
738
778
  // this is important because the way these are processed expects
739
779
  // the sequence ones to come first
740
- _callbacks[key][sequenceName ? 'unshift' : 'push']({
780
+ _callbacks[info.key][sequenceName ? 'unshift' : 'push']({
741
781
  callback: callback,
742
- modifiers: modifiers,
743
- action: action,
782
+ modifiers: info.modifiers,
783
+ action: info.action,
744
784
  seq: sequenceName,
745
785
  level: level,
746
786
  combo: combination
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mousetrapjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Eduardo Kowalski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-20 00:00:00.000000000 Z
11
+ date: 2013-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Gem for Mousetrap, a simple library for handling keyboard shortcuts in
14
14
  Javascript