{"size": 2458, "ext": "js", "max_stars_repo_path": "backbone.hammer.js", "max_stars_repo_name": "EnotionZ/backbone.hammer", "max_stars_repo_stars_event_min_datetime": "2017-06-26T22:09:30.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-27T22:39:27.000Z", "max_issues_repo_path": "backbone.hammer.js", "max_issues_repo_name": "michaelBenin/backbone.hammer", "max_issues_repo_issues_event_min_datetime": "2015-01-05T16:47:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-14T20:40:22.000Z", "max_forks_repo_path": "backbone.hammer.js", "max_forks_repo_name": "michaelBenin/backbone.hammer", "max_forks_repo_forks_event_min_datetime": "2015-06-02T20:08:36.000Z", "max_forks_repo_forks_event_max_datetime": "2017-03-07T23:19:00.000Z"}
{"max_stars_count": 7, "max_issues_count": 246, "max_forks_count": 6, "avg_line_length": 28.9176470588, "max_line_length": 94, "alphanum_fraction": 0.6090317331}
(function(root, factory) {
  // Set up Backbone appropriately for the environment.
  if (typeof define === 'function' && define.amd) {
    // AMD
    define(['underscore', 'backbone', 'jquery-hammerjs'], function(_, Backbone) {
      factory(_, Backbone);
    });
  } else if (typeof exports !== 'undefined') {
    var _ = require('underscore');
    var Backbone = require('backbone');
    require('jquery-hammerjs');
    factory(_, Backbone);
  } else {
    // Browser globals
    factory(root._, root.Backbone);
  }
}(this, function(_, Backbone) {
  var $ = Backbone.$;

  if( !$.fn.hammer ){
    throw new Error('Hammer jQuery plugin not loaded.');
  }

  var delegateEventSplitter = /^(\S+)\s*(.*)$/;
  var viewOptions = ['hammerEvents', 'hammerOptions'];

  var View = Backbone.View;
  var delegateEvents = View.prototype.delegateEvents;
  var undelegateEvents = View.prototype.undelegateEvents;

  Backbone.View = View.extend({
    constructor: function(options){
      options = options || {};
      _.extend(this, _.pick(options, viewOptions));
      return View.apply(this, arguments);
    },

    _hammered: false,

    undelegateEvents: function(){
      this.undelegateHammerEvents();
      return undelegateEvents.apply(this, arguments);
    },

    undelegateHammerEvents: function(){
      if (this._hammered) {
        this.hammer().off('.hammerEvents' + this.cid);
      }
      return this;
    },

    delegateEvents: function(){
      delegateEvents.apply(this, arguments);
      this.delegateHammerEvents();
      return this;
    },

    delegateHammerEvents: function(events){
      var options = _.defaults(_.result(this, 'hammerOptions') || {}, Backbone.hammerOptions);
      if (!(events || (events = _.result(this, 'hammerEvents')))) return this;
      for(var key in events) {
        var method = events[key];
        if (!_.isFunction(method)) method = this[events[key]];
        if (!method) continue;

        var match = key.match(delegateEventSplitter);
        var eventName = match[1], selector = match[2];
        eventName += '.hammerEvents' + this.cid;
        method = _.bind(method, this);
        if (selector === '') {
          this.hammer(options).on(eventName, method);
        } else {
          this.hammer(options).on(eventName, selector, method);
        }
      }
      return this;
    },

    hammer: function(options){
      this._hammered = true;
      return this.$el.hammer(options);
    }
  });
}));
