desktopBrowsers.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //desktopBrowsers contributed by Carlos Ouro @ Badoo
  2. //translates desktop browsers events to touch events and prevents defaults
  3. //It can be used independently in other apps but it is required for using the touchLayer in the desktop
  4. ;(function ($) {
  5. var cancelClickMove=false;
  6. var preventAll = function(e)
  7. {
  8. e.preventDefault();
  9. e.stopPropagation();
  10. }
  11. var redirectMouseToTouch = function(type, originalEvent, newTarget)
  12. {
  13. var theTarget = newTarget ? newTarget : originalEvent.target;
  14. //stop propagation, and remove default behavior for everything but INPUT, TEXTAREA & SELECT fields
  15. if (theTarget.tagName.toUpperCase().indexOf("SELECT") == -1 &&
  16. theTarget.tagName.toUpperCase().indexOf("TEXTAREA") == -1 &&
  17. theTarget.tagName.toUpperCase().indexOf("INPUT") == -1) //SELECT, TEXTAREA & INPUT
  18. {
  19. // by luofei , 为了兼容iscroll 去掉原生事件的取消监听
  20. // preventAll(originalEvent);
  21. }
  22. var touchevt = document.createEvent("Event");
  23. touchevt.initEvent(type, true, true);
  24. if(type!='touchend'){
  25. touchevt.touches = new Array();
  26. touchevt.touches[0] = new Object();
  27. touchevt.touches[0].pageX = originalEvent.pageX;
  28. touchevt.touches[0].pageY = originalEvent.pageY;
  29. //target
  30. touchevt.touches[0].target = theTarget;
  31. touchevt.changedTouches = touchevt.touches; //for jqtouch
  32. touchevt.targetTouches = touchevt.touches; //for jqtouch
  33. }
  34. //target
  35. touchevt.target = theTarget;
  36. touchevt.mouseToTouch = true;
  37. theTarget.dispatchEvent(touchevt);
  38. }
  39. var mouseDown = false,
  40. lastTarget = null,firstMove=false;
  41. if(!window.navigator.msPointerEnabled){
  42. document.addEventListener("mousedown", function(e)
  43. {
  44. mouseDown = true;
  45. lastTarget = e.target;
  46. if(e.target.nodeName.toLowerCase()=="a"&&e.target.href.toLowerCase()=="javascript:;")
  47. e.target.href="#";
  48. redirectMouseToTouch("touchstart", e);
  49. firstMove = true;
  50. cancelClickMove=false;
  51. }, true);
  52. document.addEventListener("mouseup", function(e)
  53. {
  54. if(!mouseDown) return;
  55. redirectMouseToTouch("touchend", e, lastTarget); //bind it to initial mousedown target
  56. lastTarget = null;
  57. mouseDown = false;
  58. }, true);
  59. document.addEventListener("mousemove", function(e)
  60. {
  61. if (!mouseDown) return;
  62. if(firstMove) return firstMove=false
  63. redirectMouseToTouch("touchmove", e);
  64. e.preventDefault();
  65. cancelClickMove=true;
  66. }, true);
  67. }
  68. else { //Win8
  69. document.addEventListener("MSPointerDown", function(e)
  70. {
  71. mouseDown = true;
  72. lastTarget = e.target;
  73. if(e.target.nodeName.toLowerCase()=="a"&&e.target.href.toLowerCase()=="javascript:;")
  74. e.target.href="#";
  75. redirectMouseToTouch("touchstart", e);
  76. firstMove = true;
  77. cancelClickMove=false;
  78. // e.preventDefault();e.stopPropagation();
  79. }, true);
  80. document.addEventListener("MSPointerUp", function(e)
  81. {
  82. if(!mouseDown) return;
  83. redirectMouseToTouch("touchend", e, lastTarget); //bind it to initial mousedown target
  84. lastTarget = null;
  85. mouseDown = false;
  86. // e.preventDefault();e.stopPropagation();
  87. }, true);
  88. document.addEventListener("MSPointerMove", function(e)
  89. {
  90. if (!mouseDown) return;
  91. if(firstMove) return firstMove=false
  92. redirectMouseToTouch("touchmove", e);
  93. e.preventDefault();
  94. //e.stopPropagation();
  95. cancelClickMove=true;
  96. }, true);
  97. }
  98. //prevent all mouse events which dont exist on touch devices
  99. document.addEventListener("drag", preventAll, true);
  100. document.addEventListener("dragstart", preventAll, true);
  101. document.addEventListener("dragenter", preventAll, true);
  102. document.addEventListener("dragover", preventAll, true);
  103. document.addEventListener("dragleave", preventAll, true);
  104. document.addEventListener("dragend", preventAll, true);
  105. document.addEventListener("drop", preventAll, true);
  106. document.addEventListener("selectstart", preventAll, true);
  107. document.addEventListener("click", function(e)
  108. {
  109. if(!e.mouseToTouch&&e.target==lastTarget){
  110. preventAll(e);
  111. }
  112. if(cancelClickMove)
  113. {
  114. preventAll(e);
  115. cancelClickMove=false;
  116. }
  117. }, true);
  118. window.addEventListener("resize",function(){
  119. var touchevt = document.createEvent("Event");
  120. touchevt.initEvent("orientationchange", true, true);
  121. document.dispatchEvent(touchevt);
  122. },false);
  123. })(jQuery);