1/** 2* jQuery.UI.iPad plugin 3* Copyright (c) 2010 Stephen von Takach 4* licensed under MIT. 5* Date: 27/8/2010 6* 7* Project Home: 8* http://code.google.com/p/jquery-ui-for-ipad-and-iphone/ 9*/ 10 11$(function() { 12 // 13 // Extend jQuery feature detection 14 // 15 $.extend($.support, { 16 touch: "ontouchend" in document 17 }); 18 19 // 20 // Hook up touch events 21 // 22 if ($.support.touch) { 23 var obj = document.getElementsByClassName('QapTcha'); 24 for(i=0; i<obj.length;i++){ 25 obj[i].addEventListener("touchstart", iPadTouchHandler, false); 26 obj[i].addEventListener("touchmove", iPadTouchHandler, false); 27 obj[i].addEventListener("touchend", iPadTouchHandler, false); 28 obj[i].addEventListener("touchcancel", iPadTouchHandler, false); 29 }} 30}); 31 32 33var lastTap = null; // Holds last tapped element (so we can compare for double tap) 34var tapValid = false; // Are we still in the .6 second window where a double tap can occur 35var tapTimeout = null; // The timeout reference 36 37function cancelTap() { 38 tapValid = false; 39} 40 41 42var rightClickPending = false; // Is a right click still feasible 43var rightClickEvent = null; // the original event 44var holdTimeout = null; // timeout reference 45var cancelMouseUp = false; // prevents a click from occuring as we want the context menu 46 47 48function cancelHold() { 49 if (rightClickPending) { 50 window.clearTimeout(holdTimeout); 51 rightClickPending = false; 52 rightClickEvent = null; 53 } 54} 55 56function startHold(event) { 57 if (rightClickPending) 58 return; 59 60 rightClickPending = true; // We could be performing a right click 61 rightClickEvent = (event.changedTouches)[0]; 62 holdTimeout = window.setTimeout("doRightClick();", 800); 63} 64 65 66function doRightClick() { 67 rightClickPending = false; 68 69 // 70 // We need to mouse up (as we were down) 71 // 72 var first = rightClickEvent, 73 simulatedEvent = document.createEvent("MouseEvent"); 74 simulatedEvent.initMouseEvent("mouseup", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 75 false, false, false, false, 0, null); 76 first.target.dispatchEvent(simulatedEvent); 77 78 // 79 // emulate a right click 80 // 81 simulatedEvent = document.createEvent("MouseEvent"); 82 simulatedEvent.initMouseEvent("mousedown", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 83 false, false, false, false, 2, null); 84 first.target.dispatchEvent(simulatedEvent); 85 86 // 87 // Show a context menu 88 // 89 simulatedEvent = document.createEvent("MouseEvent"); 90 simulatedEvent.initMouseEvent("contextmenu", true, true, window, 1, first.screenX + 50, first.screenY + 5, first.clientX + 50, first.clientY + 5, 91 false, false, false, false, 2, null); 92 first.target.dispatchEvent(simulatedEvent); 93 94 95 // 96 // Note:: I don't mouse up the right click here however feel free to add if required 97 // 98 99 100 cancelMouseUp = true; 101 rightClickEvent = null; // Release memory 102} 103 104 105// 106// mouse over event then mouse down 107// 108function iPadTouchStart(event) { 109 var touches = event.changedTouches, 110 first = touches[0], 111 type = "mouseover", 112 simulatedEvent = document.createEvent("MouseEvent"); 113 // 114 // Mouse over first - I have live events attached on mouse over 115 // 116 simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 117 false, false, false, false, 0, null); 118 first.target.dispatchEvent(simulatedEvent); 119 120 type = "mousedown"; 121 simulatedEvent = document.createEvent("MouseEvent"); 122 123 simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 124 false, false, false, false, 0, null); 125 first.target.dispatchEvent(simulatedEvent); 126 127 128 if (!tapValid) { 129 lastTap = first.target; 130 tapValid = true; 131 tapTimeout = window.setTimeout("cancelTap();", 600); 132 startHold(event); 133 } 134 else { 135 window.clearTimeout(tapTimeout); 136 137 // 138 // If a double tap is still a possibility and the elements are the same 139 // Then perform a double click 140 // 141 if (first.target == lastTap) { 142 lastTap = null; 143 tapValid = false; 144 145 type = "click"; 146 simulatedEvent = document.createEvent("MouseEvent"); 147 148 simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 149 false, false, false, false, 0/*left*/, null); 150 first.target.dispatchEvent(simulatedEvent); 151 152 type = "dblclick"; 153 simulatedEvent = document.createEvent("MouseEvent"); 154 155 simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 156 false, false, false, false, 0/*left*/, null); 157 first.target.dispatchEvent(simulatedEvent); 158 } 159 else { 160 lastTap = first.target; 161 tapValid = true; 162 tapTimeout = window.setTimeout("cancelTap();", 600); 163 startHold(event); 164 } 165 } 166} 167 168function iPadTouchHandler(event) { 169 var type = "", 170 button = 0; /*left*/ 171 172 if (event.touches.length > 1) 173 return; 174 175 switch (event.type) { 176 case "touchstart": 177 if ($(event.changedTouches[0].target).is("select")) { 178 return; 179 } 180 iPadTouchStart(event);
180 /*We need to trigger two events here to support one touch drag and drop*/ 181 event.preventDefault(); 182 return false; 183 break; 184 185 case "touchmove": 186 cancelHold(); 187 type = "mousemove"; 188 event.preventDefault(); 189 break; 190 191 case "touchend": 192 if (cancelMouseUp) { 193 cancelMouseUp = false; 194 event.preventDefault(); 195 return false; 196 } 197 cancelHold(); 198 type = "mouseup"; 199 break; 200 201 default: 202 return; 203 } 204 205 var touches = event.changedTouches, 206 first = touches[0], 207 simulatedEvent = document.createEvent("MouseEvent"); 208 209 simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 210 false, false, false, false, button, null); 211 212 first.target.dispatchEvent(simulatedEvent); 213 214 if (type == "mouseup" && tapValid && first.target == lastTap) { // This actually emulates the ipads default behaviour (which we prevented) 215 simulatedEvent = document.createEvent("MouseEvent"); // This check avoids click being emulated on a double tap 216 217 simulatedEvent.initMouseEvent("click", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, 218 false, false, false, false, button, null); 219 220 first.target.dispatchEvent(simulatedEvent); 221 } 222} 223 224
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.