vendor/assets/javascripts/mousetrap.js in mousetrap-rails-0.0.8 vs vendor/assets/javascripts/mousetrap.js in mousetrap-rails-0.0.9

- old
+ new

@@ -14,11 +14,11 @@ * limitations under the License. * * Mousetrap is a simple keyboard shortcut library for Javascript with * no external dependencies * - * @version 1.2.2 + * @version 1.3.0 * @url craig.is/killing/mice */ (function() { /** @@ -146,41 +146,41 @@ /** * direct map of string combinations to callbacks used for trigger() * * @type {Object} */ - _direct_map = {}, + _directMap = {}, /** * keeps track of what level each sequence is at since multiple * sequences can start out with the same sequence * * @type {Object} */ - _sequence_levels = {}, + _sequenceLevels = {}, /** * variable to store the setTimeout call * * @type {null|number} */ - _reset_timer, + _resetTimer, /** * temporary state where we will ignore the next keyup * * @type {boolean|string} */ - _ignore_next_keyup = false, + _ignoreNextKeyup = false, /** * are we currently inside of a sequence? * type of action ("keyup" or "keydown" or "keypress") or false * * @type {boolean|string} */ - _sequence_type = false; + _sequenceType = false; /** * loop through the f keys, f1 to f19 and add them to the map * programatically */ @@ -250,29 +250,29 @@ } /** * resets all sequence counters except for the ones passed in * - * @param {Object} do_not_reset + * @param {Object} doNotReset * @returns void */ - function _resetSequences(do_not_reset, max_level) { - do_not_reset = do_not_reset || {}; + function _resetSequences(doNotReset, maxLevel) { + doNotReset = doNotReset || {}; - var active_sequences = false, + var activeSequences = false, key; - for (key in _sequence_levels) { - if (do_not_reset[key] && _sequence_levels[key] > max_level) { - active_sequences = true; + for (key in _sequenceLevels) { + if (doNotReset[key] && _sequenceLevels[key] > maxLevel) { + activeSequences = true; continue; } - _sequence_levels[key] = 0; + _sequenceLevels[key] = 0; } - if (!active_sequences) { - _sequence_type = false; + if (!activeSequences) { + _sequenceType = false; } } /** * finds all callbacks that match based on the keycode, modifiers, @@ -306,11 +306,11 @@ 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 && _sequence_levels[callback.seq] != callback.level) { + if (callback.seq && _sequenceLevels[callback.seq] != callback.level) { continue; } // if the action we are looking for doesn't match the action we got // then we should keep going @@ -407,47 +407,47 @@ * @returns void */ function _handleCharacter(character, e) { var callbacks = _getMatches(character, _eventModifiers(e), e), i, - do_not_reset = {}, - max_level = 0, - processed_sequence_callback = false; + doNotReset = {}, + maxLevel = 0, + processedSequenceCallback = false; // loop through matching callbacks for this key event for (i = 0; i < callbacks.length; ++i) { // fire for all sequence callbacks // this is because if for example you have multiple sequences // bound such as "g i" and "g t" they both need to fire the // callback for matching g cause otherwise you can only ever // match the first one if (callbacks[i].seq) { - processed_sequence_callback = true; + processedSequenceCallback = true; // as we loop through keep track of the max // any sequence at a lower level will be discarded - max_level = Math.max(max_level, callbacks[i].level); + maxLevel = Math.max(maxLevel, callbacks[i].level); // keep a list of which sequences were matches for later - do_not_reset[callbacks[i].seq] = 1; + doNotReset[callbacks[i].seq] = 1; _fireCallback(callbacks[i].callback, e, callbacks[i].combo); continue; } // if there were no sequence matches but we are still here // that means this is a regular match so we should fire that - if (!processed_sequence_callback && !_sequence_type) { + if (!processedSequenceCallback && !_sequenceType) { _fireCallback(callbacks[i].callback, e, callbacks[i].combo); } } // if you are inside of a sequence and the key you are pressing // is not a modifier key then we should reset all sequences // that were not matched by this key event - if (e.type == _sequence_type && !_isModifier(character)) { - _resetSequences(do_not_reset, max_level); + if (e.type == _sequenceType && !_isModifier(character)) { + _resetSequences(doNotReset, maxLevel); } } /** * handles a keydown event @@ -468,12 +468,12 @@ // no character found then stop if (!character) { return; } - if (e.type == 'keyup' && _ignore_next_keyup == character) { - _ignore_next_keyup = false; + if (e.type == 'keyup' && _ignoreNextKeyup == character) { + _ignoreNextKeyup = false; return; } _handleCharacter(character, e); } @@ -495,12 +495,12 @@ * to press the next key before you have to start over * * @returns void */ function _resetSequenceTimer() { - clearTimeout(_reset_timer); - _reset_timer = setTimeout(_resetSequences, 1000); + clearTimeout(_resetTimer); + _resetTimer = setTimeout(_resetSequences, 1000); } /** * reverses the map lookup so that we can look for specific keys * to see what can and can't use keypress @@ -561,11 +561,11 @@ */ function _bindSequence(combo, keys, callback, action) { // start off by adding a sequence level record for this combination // and setting the level to 0 - _sequence_levels[combo] = 0; + _sequenceLevels[combo] = 0; // if there is no action pick the best one for the first key // in the sequence if (!action) { action = _pickBestAction(keys[0], []); @@ -577,12 +577,12 @@ * * @param {Event} e * @returns void */ var _increaseSequence = function(e) { - _sequence_type = action; - ++_sequence_levels[combo]; + _sequenceType = action; + ++_sequenceLevels[combo]; _resetSequenceTimer(); }, /** * wraps the specified callback inside of another function in order @@ -596,11 +596,11 @@ // we should ignore the next key up if the action is key down // or keypress. this is so if you finish a sequence and // release the key the final key will not trigger a keyup if (action !== 'keyup') { - _ignore_next_keyup = _characterFromEvent(e); + _ignoreNextKeyup = _characterFromEvent(e); } // weird race condition if a sequence ends with the key // another sequence begins with setTimeout(_resetSequences, 10); @@ -619,16 +619,19 @@ * binds a single keyboard combination * * @param {string} combination * @param {Function} callback * @param {string=} action - * @param {string=} sequence_name - name of sequence if part of sequence + * @param {string=} sequenceName - name of sequence if part of sequence * @param {number=} level - what part of the sequence the command is * @returns void */ - function _bindSingle(combination, callback, action, sequence_name, level) { + function _bindSingle(combination, callback, action, sequenceName, level) { + // store a direct mapped reference for use with Mousetrap.trigger + _directMap[combination + ':' + action] = callback; + // make sure multiple spaces in a row become a single space combination = combination.replace(/\s+/g, ' '); var sequence = combination.split(' '), i, @@ -678,23 +681,23 @@ if (!_callbacks[key]) { _callbacks[key] = []; } // remove an existing match if there is one - _getMatches(key, modifiers, {type: action}, !sequence_name, combination); + _getMatches(key, modifiers, {type: action}, !sequenceName, combination); // add this call back to the array // if it is a sequence put it at the beginning // if not put it at the end // // this is important because the way these are processed expects // the sequence ones to come first - _callbacks[key][sequence_name ? 'unshift' : 'push']({ + _callbacks[key][sequenceName ? 'unshift' : 'push']({ callback: callback, modifiers: modifiers, action: action, - seq: sequence_name, + seq: sequenceName, level: level, combo: combination }); } @@ -732,49 +735,47 @@ * @param {Function} callback * @param {string=} action - 'keypress', 'keydown', or 'keyup' * @returns void */ bind: function(keys, callback, action) { - _bindMultiple(keys instanceof Array ? keys : [keys], callback, action); - _direct_map[keys + ':' + action] = callback; + keys = keys instanceof Array ? keys : [keys]; + _bindMultiple(keys, callback, action); return this; }, /** * unbinds an event to mousetrap * * the unbinding sets the callback function of the specified key combo * to an empty function and deletes the corresponding key in the - * _direct_map dict. + * _directMap dict. * - * the keycombo+action has to be exactly the same as - * it was defined in the bind method - * * TODO: actually remove this from the _callbacks dictionary instead * of binding an empty function * + * the keycombo+action has to be exactly the same as + * it was defined in the bind method + * * @param {string|Array} keys * @param {string} action * @returns void */ unbind: function(keys, action) { - if (_direct_map[keys + ':' + action]) { - delete _direct_map[keys + ':' + action]; - this.bind(keys, function() {}, action); - } - return this; + return Mousetrap.bind(keys, function() {}, action); }, /** * triggers an event that has already been bound * * @param {string} keys * @param {string=} action * @returns void */ trigger: function(keys, action) { - _direct_map[keys + ':' + action](); + if (_directMap[keys + ':' + action]) { + _directMap[keys + ':' + action](); + } return this; }, /** * resets the library back to its initial state. this is useful @@ -783,10 +784,10 @@ * * @returns void */ reset: function() { _callbacks = {}; - _direct_map = {}; + _directMap = {}; return this; }, /** * should we stop this event before firing off callbacks