diff options
author | Craig Campbell <iamcraigcampbell@gmail.com> | 2013-05-28 23:45:59 -0400 |
---|---|---|
committer | Craig Campbell <iamcraigcampbell@gmail.com> | 2013-05-28 23:45:59 -0400 |
commit | 0fc451957902cb3744725c527d8a9dfdae1c3bcf (patch) | |
tree | 6cc33d2e85c3d8ddf6022310f913899316d05cd3 | |
parent | eee5c190f377b0f02f3897db143cedd79320d9a2 (diff) | |
download | mousetrap-origin/spring-cleaning.zip mousetrap-origin/spring-cleaning.tar.gz mousetrap-origin/spring-cleaning.tar.bz2 |
Sequence rebind should remove previous callbackorigin/spring-cleaning
Fixes #120
-rw-r--r-- | mousetrap.js | 24 | ||||
-rw-r--r-- | tests/test.mousetrap.js | 2 |
2 files changed, 16 insertions, 10 deletions
diff --git a/mousetrap.js b/mousetrap.js index 1916712..51d0bb7 100644 --- a/mousetrap.js +++ b/mousetrap.js @@ -302,11 +302,12 @@ * @param {string} character * @param {Array} modifiers * @param {Event|Object} e - * @param {boolean=} remove - should we remove any matches + * @param {string=} sequenceName - name of the sequence we are looking for * @param {string=} combination + * @param {number=} level * @returns {Array} */ - function _getMatches(character, modifiers, e, remove, combination) { + function _getMatches(character, modifiers, e, sequenceName, combination, level) { var i, callback, matches = [], @@ -327,9 +328,9 @@ for (i = 0; i < _callbacks[character].length; ++i) { callback = _callbacks[character][i]; - // if this is a sequence but it is not at the right level - // then move onto the next match - if (callback.seq && _sequenceLevels[callback.seq] != callback.level) { + // if a sequence name is not specified, but this is a sequence at + // the wrong level then move onto the next match + if (!sequenceName && callback.seq && _sequenceLevels[callback.seq] != callback.level) { continue; } @@ -348,9 +349,14 @@ // firefox will fire a keypress if meta or control is down if ((action == 'keypress' && !e.metaKey && !e.ctrlKey) || _modifiersMatch(modifiers, callback.modifiers)) { - // remove is used so if you change your mind and call bind a - // second time with a new function the first one is overwritten - if (remove && callback.combo == combination) { + // when you bind a combination or sequence a second time it + // should overwrite the first one. if a sequenceName or + // combination is specified in this call it does just that + // + // @todo make deleting its own method? + var deleteCombo = !sequenceName && callback.combo == combination; + var deleteSequence = sequenceName && callback.seq == sequenceName && callback.level == level; + if (deleteCombo || deleteSequence) { _callbacks[character].splice(i, 1); } @@ -763,7 +769,7 @@ _callbacks[info.key] = _callbacks[info.key] || []; // remove an existing match if there is one - _getMatches(info.key, info.modifiers, {type: info.action}, !sequenceName, combination); + _getMatches(info.key, info.modifiers, {type: info.action}, sequenceName, combination, level); // add this call back to the array // if it is a sequence put it at the beginning diff --git a/tests/test.mousetrap.js b/tests/test.mousetrap.js index 06d7cf8..2a4c698 100644 --- a/tests/test.mousetrap.js +++ b/tests/test.mousetrap.js @@ -375,7 +375,7 @@ describe('Mousetrap.bind', function() { expect(spy2.callCount).to.equal(1, '"a option b" should fire'); }); - it.skip('rebinding same sequence should override previous', function() { + it('rebinding same sequence should override previous', function() { var spy1 = sinon.spy(); var spy2 = sinon.spy(); Mousetrap.bind('a b c', spy1); |