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

From Team Fortress Wiki
Jump to: navigation, search
m
m
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
+
$.fn.addTouch = function(){
var clickms = 250;
+
    this.each(function(i,el){
var lastTouchDown = -1;
+
      $(el).bind('touchstart touchmove touchend touchcancel',function(){
// /hai
+
        //we pass the original event object because the jQuery event
 +
        //object is normalized to w3c specs and does not provide the TouchList
 +
        handleTouch(event);
 +
      });
 +
    });
  
 +
    var handleTouch = function(event)
 +
    {
 +
      var touches = event.changedTouches,
 +
              first = touches[0],
 +
              type = '';
  
 +
      switch(event.type)
 +
      {
 +
        case 'touchstart':
 +
          type = 'mousedown';
 +
          break;
  
function touchHandler(event) {
+
        case 'touchmove':
    var touch = event.changedTouches[0];
+
          type = 'mousemove';
 +
          event.preventDefault();
 +
          break;
  
 +
        case 'touchend':
 +
          type = 'mouseup';
 +
          break;
  
// hai
+
        default:
var d = new Date();
+
          return;
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, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
 +
      first.target.dispatchEvent(simulatedEvent);
 +
    };
 +
  };
  
    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);
+
$('.viewer-3d').addTouch();
    event.preventDefault();
 
}
 
 
 
function touchinit() {
 
    document.addEventListener("touchstart", touchHandler, true);
 
    document.addEventListener("touchmove", touchHandler, true);
 
    document.addEventListener("touchend", touchHandler, true);
 
    document.addEventListener("touchcancel", touchHandler, true);
 
}
 
 
 
$(document).ready(function() {
 
  touchinit();
 
});
 

Revision as of 05:26, 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');

$.fn.addTouch = function(){
    this.each(function(i,el){
      $(el).bind('touchstart touchmove touchend touchcancel',function(){
        //we pass the original event object because the jQuery event
        //object is normalized to w3c specs and does not provide the TouchList
        handleTouch(event);
      });
    });

    var handleTouch = function(event)
    {
      var touches = event.changedTouches,
              first = touches[0],
              type = '';

      switch(event.type)
      {
        case 'touchstart':
          type = 'mousedown';
          break;

        case 'touchmove':
          type = 'mousemove';
          event.preventDefault();
          break;

        case 'touchend':
          type = 'mouseup';
          break;

        default:
          return;
      }

      var simulatedEvent = document.createEvent('MouseEvent');
      simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
      first.target.dispatchEvent(simulatedEvent);
    };
  };


$('.viewer-3d').addTouch();