<commit_before>define([
  'underscore',
  'backbone',
  'underscore-keys'
], function(_, Backbone) {

  var delegateEvents = Backbone.View.prototype.delegateEvents;

  _.extend(Backbone.View.prototype, {

    delegateEvents: function(events){
      if (!(events || (events = this.events))) return;

      _.each(events, function(method, eventConfig){

        var eventParts = eventConfig.split(' '),
            event = eventParts[0],
            selector = eventParts[1] || '';

        if (_isSpecificKeyEvent(event)) {
          this._bindSpecificKeyEvent(selector, event, method);
          delete events[eventConfig];
        }

      }, this);
      delegateEvents.call(this, events);
    },
    _bindSpecificKeyEvent: function(selector, event, method) {
      var eventParts = _getEventParts(event),
          data = {
            key: _.getKeycode(eventParts.key),
            method: method
          },
          router = $.proxy(_keyEventRouter, this);

      if (selector === '') {
        this.$el.bind(eventParts.event, data, router);
      } else {
        this.$el.delegate(selector, eventParts.event, data, router);
      }
    }
  });

  //|---------|
  //| HELPERS |
  //|---------|

  function _isSpecificKeyEvent(event){
    if (!_isEventWithOption(event)) return false;

    var eventParts = _getEventParts(event);
    return _.isKeyEvent(eventParts.event) && _.isKey(eventParts.key);
  }

  function _isEventWithOption(event){
    return (event.indexOf(':') !== -1);
  }

  function _getEventParts(event) {
    var parts = event.split(':');
    return {
      event: parts[0],
      key: parts[1]
    };
  }

  function _getEventMethod(){
    return _.toArray(arguments).join('_');
  }

  function _keyEventRouter(e) {
    if (e.data.key === e.which) {
      this[e.data.method].apply(this);
    }
  }

  return Backbone;
<commit_msg>Added jsDoc documentation, Made AMD format optional<commit_after>/**
 * |--------------------|
 * | Backbone-Keyevents |
 * |--------------------|
 *  Backbone-Keyevents is freely distributable under the MIT license.
 *
 *  <a href="https://github.com/chalbert/Backbone-Keyevents">More details & documentation</a>
 *
 * @author <NAME>
 *
 * @requires _
 * @requires Backbone
 * @requires Underscore-Keys - https://github.com/chalbert/Underscore-Keys
 */

(function(factory){
  'use strict';

  if (typeof define === 'function' && define.amd) {
    define(['underscore', 'backbone', 'underscore-keys'], factory);
  } else {
    factory(_, Backbone);
  }

})(function (_, Backbone){
  'use strict';

  /**
   * @borrows Backbone.View#delegateEvents
   */
  var delegateEvents = Backbone.View.prototype.delegateEvents,

      /**
       * @lends BackboneKeyevents
       */
      BackboneKeyevents = {

        /**
         * Extends Backbone event delegation to add specific-key mapping
         *
         * @param {Object} events Backbone.View events object
         */

        delegateEvents: function(events){
          if (!(events || (events = this.events))) return;

          _.each(events, function(method, eventConfig){

            var eventParts = eventConfig.split(' '),
                event = eventParts[0],
                selector = eventParts[1] || '';

            if (this.isSpecificKeyEvent(event)) {
              this.bindSpecificKeyEvent(selector, event, method);
              delete events[eventConfig];
            }

          }, this);
          delegateEvents.call(this, events);
        },

        /**
         * Bind or delegate the key event
         *
         * @param {String} selector jQuery selector
         * @param {String} event Event to bind to
         * @param {String} method Method name
         */

        bindSpecificKeyEvent: function(selector, event, method) {
          var eventParts = this.getEventParts(event),
              data = {
                key: _.getKeycode(eventParts.key),
                method: method
              },
              router = $.proxy(this.keyEventRouter, this);

          if (selector === '') {
            this.$el.bind(eventParts.event, data, router);
          } else {
            this.$el.delegate(selector, eventParts.event, data, router);
          }
        },

        /**
         * Returns True if the passed event is a specific key event
         *
         * @param {String} event The event to test
         * @returns {Boolean} True is the event is a specific key event
         */

        isSpecificKeyEvent: function (event){
          if (!this.isEventWithOption(event)) return false;

          var eventParts = this.getEventParts(event);
          return _.isKeyEvent(eventParts.event) && _.isKey(eventParts.key);
        },

        /**
         * Return True if the passed event has options
         *
         * @param {String} event The event to test
         * @return {Boolean} return True if the event has options
         */

        isEventWithOption: function(event){
          return (event.indexOf(':') !== -1);
        },

        /**
         * Returns the components of the key event
         *
         * @param {String} event
         * @returns {Object} Parts of the events
         */

        getEventParts: function(event) {
          var parts = event.split(':');
          return {
            event: parts[0],
            key: parts[1]
          };
        },

        /**
         * Route keyevent to method
         *
         * @event
         * @param {Object} e Event object
         */

        keyEventRouter: function(e) {
          if (e.data.key === e.which) {
            this[e.data.method].apply(this);
          }
        }
      };

  _.extend(Backbone.View.prototype, BackboneKeyevents);

  return Backbone;

{'fraction_non_alphanumeric': 0.13845615263434272, 'fraction_numerical': 0.001750393838613688, 'mean_word_length': 1.945360824742268, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5861209', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1572, 'n_words': 363}