Difference between revisions of "User:Moussekateer/vector.js"

From Team Fortress Wiki
Jump to: navigation, search
m
m (Undo edit by Moussekateer (Talk) (1381917))
Line 3: Line 3:
 
importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/wikifi_toolbox.js', 'text/javascript');
 
importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/wikifi_toolbox.js', 'text/javascript');
  
 +
// hai
 +
var clickms = 100;
 +
var lastTouchDown = -1;
 +
// /hai
  
/*!
 
* jQuery UI Touch Punch 0.2.2
 
*
 
* Copyright 2011, Dave Furfero
 
* Dual licensed under the MIT or GPL Version 2 licenses.
 
*
 
* Depends:
 
*  jquery.ui.widget.js
 
*  jquery.ui.mouse.js
 
*/
 
(function ($) {
 
  
  // Detect touch support
 
  $.support.touch = 'ontouchend' in document;
 
  
  // Ignore browsers without touch support
+
function touchHandler(event) {
  if (!$.support.touch) {
+
     var touch = event.changedTouches[0];
     return;
 
  }
 
  
  var mouseProto = $.ui.mouse.prototype,
 
      _mouseInit = mouseProto._mouseInit,
 
      touchHandled;
 
  
  /**
+
// hai
  * Simulate a mouse event based on a corresponding touch event
+
var d = new Date();
  * @param {Object} event A touch event
+
switch(event.type)
  * @param {String} simulatedType The corresponding mouse event
+
{
  */
+
    case "touchstart": type = "mousedown"; lastTouchDown = d.getTime(); break;
  function simulateMouseEvent (event, simulatedType) {
+
    case "touchmove": type="mousemove"; lastTouchDown = -1; break;       
 +
    case "touchend": if(lastTouchDown > -1 && (d.getTime() - lastTouchDown) < clickms){lastTouchDown = -1; type="click"; break;} type="mouseup"; break;
 +
    default: return;
 +
}
 +
// /hai
  
    // Ignore multi-touch events
 
    if (event.originalEvent.touches.length > 1) {
 
      return;
 
    }
 
  
 +
    var simulatedEvent = document.createEvent("MouseEvent");
 +
        simulatedEvent.initMouseEvent(type, true, true, window, 1,
 +
        touch.screenX, touch.screenY,
 +
        touch.clientX, touch.clientY, false,
 +
        false, false, false, 0, null);
 +
 +
    touch.target.dispatchEvent(simulatedEvent);
 
     event.preventDefault();
 
     event.preventDefault();
 +
}
  
    var touch = event.originalEvent.changedTouches[0],
+
function init() {
        simulatedEvent = document.createEvent('MouseEvents');
+
     document.addEventListener("touchstart", touchHandler, true);
      
+
     document.addEventListener("touchmove", touchHandler, true);
    // Initialize the simulated mouse event using the touch event's coordinates
+
     document.addEventListener("touchend", touchHandler, true);
    simulatedEvent.initMouseEvent(
+
     document.addEventListener("touchcancel", touchHandler, true);
      simulatedType,   // type
+
}
      true,             // bubbles                   
 
      true,            // cancelable               
 
      window,          // view                     
 
      1,                // detail                   
 
      touch.screenX,    // screenX                   
 
      touch.screenY,    // screenY                   
 
      touch.clientX,    // clientX                   
 
      touch.clientY,    // clientY                   
 
      false,            // ctrlKey                   
 
      false,            // altKey                   
 
      false,            // shiftKey                 
 
      false,            // metaKey                   
 
      0,                // button                   
 
      null              // relatedTarget             
 
    );
 
 
 
     // Dispatch the simulated event to the target element
 
    event.target.dispatchEvent(simulatedEvent);
 
  }
 
 
 
  /**
 
  * Handle the jQuery UI widget's touchstart events
 
  * @param {Object} event The widget element's touchstart event
 
  */
 
  mouseProto._touchStart = function (event) {
 
 
 
    var self = this;
 
 
 
    // Ignore the event if another widget is already being handled
 
    if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
 
      return;
 
    }
 
 
 
    // Set the flag to prevent other widgets from inheriting the touch event
 
    touchHandled = true;
 
 
 
    // Track movement to determine if interaction was a click
 
    self._touchMoved = false;
 
 
 
    // Simulate the mouseover event
 
    simulateMouseEvent(event, 'mouseover');
 
 
 
    // Simulate the mousemove event
 
    simulateMouseEvent(event, 'mousemove');
 
 
 
     // Simulate the mousedown event
 
    simulateMouseEvent(event, 'mousedown');
 
  };
 
 
 
  /**
 
  * Handle the jQuery UI widget's touchmove events
 
  * @param {Object} event The document's touchmove event
 
  */
 
  mouseProto._touchMove = function (event) {
 
 
 
    // Ignore event if not handled
 
    if (!touchHandled) {
 
      return;
 
    }
 
 
 
    // Interaction was not a click
 
    this._touchMoved = true;
 
 
 
    // Simulate the mousemove event
 
    simulateMouseEvent(event, 'mousemove');
 
  };
 
 
 
  /**
 
  * Handle the jQuery UI widget's touchend events
 
  * @param {Object} event The document's touchend event
 
  */
 
  mouseProto._touchEnd = function (event) {
 
 
 
    // Ignore event if not handled
 
    if (!touchHandled) {
 
      return;
 
    }
 
 
 
    // Simulate the mouseup event
 
    simulateMouseEvent(event, 'mouseup');
 
 
 
    // Simulate the mouseout event
 
    simulateMouseEvent(event, 'mouseout');
 
 
 
     // If the touch interaction did not move, it should trigger a click
 
    if (!this._touchMoved) {
 
 
 
      // Simulate the click event
 
      simulateMouseEvent(event, 'click');
 
    }
 
 
 
    // Unset the flag to allow other widgets to inherit the touch event
 
    touchHandled = false;
 
  };
 
 
 
  /**
 
  * A duck punch of the $.ui.mouse _mouseInit method to support touch events.
 
  * This method extends the widget with bound touch event handlers that
 
  * translate touch events to mouse events and pass them to the widget's
 
  * original mouse event handling methods.
 
  */
 
  mouseProto._mouseInit = function () {
 
   
 
    var self = this;
 
 
 
    // Delegate the touch handlers to the widget's element
 
    self.element
 
      .bind('touchstart', $.proxy(self, '_touchStart'))
 
      .bind('touchmove', $.proxy(self, '_touchMove'))
 
      .bind('touchend', $.proxy(self, '_touchEnd'));
 
 
 
    // Call the original $.ui.mouse init method
 
    _mouseInit.call(self);
 
  };
 
 
 
})(jQuery);
 
  
 
$(document).ready(function() {
 
$(document).ready(function() {
   $('.viewer-3d').draggable();
+
   touchinit();
 
});
 
});

Revision as of 04:48, 16 June 2013

importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/spambot_killer.js', 'text/javascript');

importScriptURI('https://raw.github.com/Moussekateer/TFWiki-scripts/master/wikifi_toolbox.js', 'text/javascript');

// hai
var clickms = 100;
var lastTouchDown = -1;
// /hai



function touchHandler(event) {
    var touch = event.changedTouches[0];


// hai
var d = new Date();
switch(event.type)
{
    case "touchstart": type = "mousedown"; lastTouchDown = d.getTime(); break;
    case "touchmove": type="mousemove"; lastTouchDown = -1; break;        
    case "touchend": if(lastTouchDown > -1 && (d.getTime() - lastTouchDown) < clickms){lastTouchDown = -1; type="click"; break;} type="mouseup"; break;
    default: return;
}
// /hai


    var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent(type, true, true, window, 1,
        touch.screenX, touch.screenY,
        touch.clientX, touch.clientY, false,
        false, false, false, 0, null);

    touch.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() {
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);
}

$(document).ready(function() {
  touchinit();
});