Blame jquery/jquery.ui-0.2.3.touch-punch.js

Packit Service 50c9f2
/*!
Packit Service 50c9f2
 * jQuery UI Touch Punch 0.2.3
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright 2011–2014, Dave Furfero
Packit Service 50c9f2
 * Dual licensed under the MIT or GPL Version 2 licenses.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Depends:
Packit Service 50c9f2
 *  jquery.ui.widget.js
Packit Service 50c9f2
 *  jquery.ui.mouse.js
Packit Service 50c9f2
 */
Packit Service 50c9f2
(function ($) {
Packit Service 50c9f2
Packit Service 50c9f2
  // Detect touch support
Packit Service 50c9f2
  $.support.touch = 'ontouchend' in document;
Packit Service 50c9f2
Packit Service 50c9f2
  // Ignore browsers without touch support
Packit Service 50c9f2
  if (!$.support.touch) {
Packit Service 50c9f2
    return;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  var mouseProto = $.ui.mouse.prototype,
Packit Service 50c9f2
      _mouseInit = mouseProto._mouseInit,
Packit Service 50c9f2
      _mouseDestroy = mouseProto._mouseDestroy,
Packit Service 50c9f2
      touchHandled;
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * Simulate a mouse event based on a corresponding touch event
Packit Service 50c9f2
   * @param {Object} event A touch event
Packit Service 50c9f2
   * @param {String} simulatedType The corresponding mouse event
Packit Service 50c9f2
   */
Packit Service 50c9f2
  function simulateMouseEvent (event, simulatedType) {
Packit Service 50c9f2
Packit Service 50c9f2
    // Ignore multi-touch events
Packit Service 50c9f2
    if (event.originalEvent.touches.length > 1) {
Packit Service 50c9f2
      return;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    event.preventDefault();
Packit Service 50c9f2
Packit Service 50c9f2
    var touch = event.originalEvent.changedTouches[0],
Packit Service 50c9f2
        simulatedEvent = document.createEvent('MouseEvents');
Packit Service 50c9f2
    
Packit Service 50c9f2
    // Initialize the simulated mouse event using the touch event's coordinates
Packit Service 50c9f2
    simulatedEvent.initMouseEvent(
Packit Service 50c9f2
      simulatedType,    // type
Packit Service 50c9f2
      true,             // bubbles                    
Packit Service 50c9f2
      true,             // cancelable                 
Packit Service 50c9f2
      window,           // view                       
Packit Service 50c9f2
      1,                // detail                     
Packit Service 50c9f2
      touch.screenX,    // screenX                    
Packit Service 50c9f2
      touch.screenY,    // screenY                    
Packit Service 50c9f2
      touch.clientX,    // clientX                    
Packit Service 50c9f2
      touch.clientY,    // clientY                    
Packit Service 50c9f2
      false,            // ctrlKey                    
Packit Service 50c9f2
      false,            // altKey                     
Packit Service 50c9f2
      false,            // shiftKey                   
Packit Service 50c9f2
      false,            // metaKey                    
Packit Service 50c9f2
      0,                // button                     
Packit Service 50c9f2
      null              // relatedTarget              
Packit Service 50c9f2
    );
Packit Service 50c9f2
Packit Service 50c9f2
    // Dispatch the simulated event to the target element
Packit Service 50c9f2
    event.target.dispatchEvent(simulatedEvent);
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * Handle the jQuery UI widget's touchstart events
Packit Service 50c9f2
   * @param {Object} event The widget element's touchstart event
Packit Service 50c9f2
   */
Packit Service 50c9f2
  mouseProto._touchStart = function (event) {
Packit Service 50c9f2
Packit Service 50c9f2
    var self = this;
Packit Service 50c9f2
Packit Service 50c9f2
    // Ignore the event if another widget is already being handled
Packit Service 50c9f2
    if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
Packit Service 50c9f2
      return;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    // Set the flag to prevent other widgets from inheriting the touch event
Packit Service 50c9f2
    touchHandled = true;
Packit Service 50c9f2
Packit Service 50c9f2
    // Track movement to determine if interaction was a click
Packit Service 50c9f2
    self._touchMoved = false;
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mouseover event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mouseover');
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mousemove event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mousemove');
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mousedown event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mousedown');
Packit Service 50c9f2
  };
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * Handle the jQuery UI widget's touchmove events
Packit Service 50c9f2
   * @param {Object} event The document's touchmove event
Packit Service 50c9f2
   */
Packit Service 50c9f2
  mouseProto._touchMove = function (event) {
Packit Service 50c9f2
Packit Service 50c9f2
    // Ignore event if not handled
Packit Service 50c9f2
    if (!touchHandled) {
Packit Service 50c9f2
      return;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    // Interaction was not a click
Packit Service 50c9f2
    this._touchMoved = true;
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mousemove event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mousemove');
Packit Service 50c9f2
  };
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * Handle the jQuery UI widget's touchend events
Packit Service 50c9f2
   * @param {Object} event The document's touchend event
Packit Service 50c9f2
   */
Packit Service 50c9f2
  mouseProto._touchEnd = function (event) {
Packit Service 50c9f2
Packit Service 50c9f2
    // Ignore event if not handled
Packit Service 50c9f2
    if (!touchHandled) {
Packit Service 50c9f2
      return;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mouseup event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mouseup');
Packit Service 50c9f2
Packit Service 50c9f2
    // Simulate the mouseout event
Packit Service 50c9f2
    simulateMouseEvent(event, 'mouseout');
Packit Service 50c9f2
Packit Service 50c9f2
    // If the touch interaction did not move, it should trigger a click
Packit Service 50c9f2
    if (!this._touchMoved) {
Packit Service 50c9f2
Packit Service 50c9f2
      // Simulate the click event
Packit Service 50c9f2
      simulateMouseEvent(event, 'click');
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    // Unset the flag to allow other widgets to inherit the touch event
Packit Service 50c9f2
    touchHandled = false;
Packit Service 50c9f2
  };
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * A duck punch of the $.ui.mouse _mouseInit method to support touch events.
Packit Service 50c9f2
   * This method extends the widget with bound touch event handlers that
Packit Service 50c9f2
   * translate touch events to mouse events and pass them to the widget's
Packit Service 50c9f2
   * original mouse event handling methods.
Packit Service 50c9f2
   */
Packit Service 50c9f2
  mouseProto._mouseInit = function () {
Packit Service 50c9f2
    
Packit Service 50c9f2
    var self = this;
Packit Service 50c9f2
Packit Service 50c9f2
    // Delegate the touch handlers to the widget's element
Packit Service 50c9f2
    self.element.bind({
Packit Service 50c9f2
      touchstart: $.proxy(self, '_touchStart'),
Packit Service 50c9f2
      touchmove: $.proxy(self, '_touchMove'),
Packit Service 50c9f2
      touchend: $.proxy(self, '_touchEnd')
Packit Service 50c9f2
    });
Packit Service 50c9f2
Packit Service 50c9f2
    // Call the original $.ui.mouse init method
Packit Service 50c9f2
    _mouseInit.call(self);
Packit Service 50c9f2
  };
Packit Service 50c9f2
Packit Service 50c9f2
  /**
Packit Service 50c9f2
   * Remove the touch event handlers
Packit Service 50c9f2
   */
Packit Service 50c9f2
  mouseProto._mouseDestroy = function () {
Packit Service 50c9f2
    
Packit Service 50c9f2
    var self = this;
Packit Service 50c9f2
Packit Service 50c9f2
    // Delegate the touch handlers to the widget's element
Packit Service 50c9f2
    self.element.unbind({
Packit Service 50c9f2
      touchstart: $.proxy(self, '_touchStart'),
Packit Service 50c9f2
      touchmove: $.proxy(self, '_touchMove'),
Packit Service 50c9f2
      touchend: $.proxy(self, '_touchEnd')
Packit Service 50c9f2
    });
Packit Service 50c9f2
Packit Service 50c9f2
    // Call the original $.ui.mouse destroy method
Packit Service 50c9f2
    _mouseDestroy.call(self);
Packit Service 50c9f2
  };
Packit Service 50c9f2
Packit Service 50c9f2
})(jQuery);