drag.js.orig 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. $axure.internal(function($ax) {
  2. var widgetDragInfo = new Object();
  3. var _drag = {};
  4. $ax.drag = _drag;
  5. $ax.drag.GetWidgetDragInfo = function() {
  6. return $.extend({}, widgetDragInfo);
  7. };
  8. $ax.drag.StartDragWidget = function(event, id) {
  9. $ax.setjBrowserEvent(jQuery.Event(event));
  10. if(event.donotdrag) return;
  11. var x, y;
  12. var tg;
  13. if($.browser.msie) {
  14. x = window.event.clientX + window.document.documentElement.scrollLeft + window.document.body.scrollLeft;
  15. y = window.event.clientY + window.document.documentElement.scrollTop + window.document.body.scrollTop;
  16. tg = window.event.srcElement;
  17. } else {
  18. if(event.changedTouches) {
  19. x = event.changedTouches[0].pageX;
  20. y = event.changedTouches[0].pageY;
  21. } else {
  22. x = event.pageX;
  23. y = event.pageY;
  24. event.preventDefault();
  25. }
  26. tg = event.target;
  27. }
  28. widgetDragInfo.hasStarted = false;
  29. widgetDragInfo.widgetId = id;
  30. widgetDragInfo.cursorStartX = x;
  31. widgetDragInfo.cursorStartY = y;
  32. widgetDragInfo.lastX = x;
  33. widgetDragInfo.lastY = y;
  34. widgetDragInfo.currentX = x;
  35. widgetDragInfo.currentY = y;
  36. widgetDragInfo.movedWidgets = new Object();
  37. widgetDragInfo.startTime = (new Date()).getTime();
  38. widgetDragInfo.targetWidget = tg;
  39. if($.browser.msie) {
  40. window.document.attachEvent("onmousemove", _dragWidget);
  41. window.document.attachEvent("onmouseup", _stopDragWidget);
  42. } else {
  43. window.document.addEventListener("mousemove", _dragWidget, true);
  44. window.document.addEventListener("mouseup", _stopDragWidget, true);
  45. window.document.addEventListener("touchmove", _dragWidget, true);
  46. window.document.addEventListener("touchend", _stopDragWidget, true);
  47. }
  48. $ax.legacy.SuppressBubble(event);
  49. };
  50. var _dragWidget = function(event) {
  51. $ax.setjBrowserEvent(jQuery.Event(event));
  52. var x, y;
  53. if($.browser.msie) {
  54. x = window.event.clientX + window.document.documentElement.scrollLeft + window.document.body.scrollLeft;
  55. y = window.event.clientY + window.document.documentElement.scrollTop + window.document.body.scrollTop;
  56. } else {
  57. if(event.changedTouches) {
  58. x = event.changedTouches[0].pageX;
  59. y = event.changedTouches[0].pageY;
  60. //allow scroll (defaults) if only swipe events have cases and delta x is less than 5px and not blocking scrolling
  61. var deltaX = x - widgetDragInfo.currentX;
  62. var target = window.document.getElementById(widgetDragInfo.widgetId);
  63. if($ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onDrag") || $ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onSwipeUp") ||
  64. $ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onSwipeDown") || (deltaX * deltaX) > 25
  65. || ($ax.document.configuration.preventScroll && $ax.legacy.GetScrollable(target) == window.document.body)) {
  66. event.preventDefault();
  67. }
  68. } else {
  69. x = event.pageX;
  70. y = event.pageY;
  71. }
  72. }
  73. widgetDragInfo.xDelta = x - widgetDragInfo.currentX;
  74. widgetDragInfo.yDelta = y - widgetDragInfo.currentY;
  75. widgetDragInfo.lastX = widgetDragInfo.currentX;
  76. widgetDragInfo.lastY = widgetDragInfo.currentY;
  77. widgetDragInfo.currentX = x;
  78. widgetDragInfo.currentY = y;
  79. widgetDragInfo.currentTime = (new Date()).getTime();
  80. $ax.legacy.SuppressBubble(event);
  81. if(!widgetDragInfo.hasStarted) {
  82. widgetDragInfo.hasStarted = true;
  83. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDragStart");
  84. widgetDragInfo.oldBodyCursor = window.document.body.style.cursor;
  85. window.document.body.style.cursor = 'move';
  86. var widget = window.document.getElementById(widgetDragInfo.widgetId);
  87. widgetDragInfo.oldCursor = widget.style.cursor;
  88. widget.style.cursor = 'move';
  89. }
  90. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDrag");
  91. };
  92. var _suppressClickAfterDrag = function(event) {
  93. _removeSuppressEvents();
  94. $ax.legacy.SuppressBubble(event);
  95. };
  96. var _removeSuppressEvents = function() {
  97. if($.browser.msie) {
  98. window.event.srcElement.detachEvent("onclick", _suppressClickAfterDrag);
  99. widgetDragInfo.targetWidget.detachEvent("onmousemove", _removeSuppressEvents);
  100. } else {
  101. window.document.removeEventListener("click", _suppressClickAfterDrag, true);
  102. window.document.removeEventListener("mousemove", _removeSuppressEvents, true);
  103. }
  104. };
  105. var _stopDragWidget = function(event) {
  106. $ax.setjBrowserEvent(jQuery.Event(event));
  107. var tg;
  108. if($.browser.msie) {
  109. window.document.detachEvent("onmousemove", _dragWidget);
  110. window.document.detachEvent("onmouseup", _stopDragWidget);
  111. tg = window.event.srcElement;
  112. } else {
  113. window.document.removeEventListener("mousemove", _dragWidget, true);
  114. window.document.removeEventListener("mouseup", _stopDragWidget, true);
  115. window.document.removeEventListener("touchmove", _dragWidget, true);
  116. window.document.removeEventListener("touchend", _stopDragWidget, true);
  117. tg = event.target;
  118. }
  119. if(widgetDragInfo.hasStarted) {
  120. widgetDragInfo.currentTime = (new Date()).getTime();
  121. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDragDrop");
  122. if($ax.globalVariableProvider.getVariableValue('totaldragx') < -30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
  123. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeLeft");
  124. }
  125. if($ax.globalVariableProvider.getVariableValue('totaldragx') > 30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
  126. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeRight");
  127. }
  128. var totalDragY = $ax.globalVariableProvider.getVariableValue('totaldragy');
  129. if(totalDragY < -30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
  130. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeUp");
  131. }
  132. if(totalDragY > 30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
  133. $ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeDown");
  134. }
  135. window.document.body.style.cursor = widgetDragInfo.oldBodyCursor;
  136. var widget = window.document.getElementById(widgetDragInfo.widgetId);
  137. widget.style.cursor = widgetDragInfo.oldCursor;
  138. if(widgetDragInfo.targetWidget == tg && !event.changedTouches) {
  139. // suppress the click after the drag on desktop browsers
  140. if($.browser.msie && widgetDragInfo.targetWidget) {
  141. widgetDragInfo.targetWidget.attachEvent("onclick", _suppressClickAfterDrag);
  142. } else {
  143. window.document.addEventListener("click", _suppressClickAfterDrag, true);
  144. }
  145. if($.browser.msie && widgetDragInfo.targetWidget) {
  146. widgetDragInfo.targetWidget.attachEvent("onmousemove", _removeSuppressEvents);
  147. } else {
  148. window.document.addEventListener("mousemove", _removeSuppressEvents, true);
  149. }
  150. }
  151. }
  152. widgetDragInfo.hasStarted = false;
  153. widgetDragInfo.movedWidgets = new Object();
  154. return false;
  155. };
  156. $ax.drag.GetDragX = function() {
  157. if(widgetDragInfo.hasStarted) return widgetDragInfo.xDelta;
  158. return 0;
  159. };
  160. $ax.drag.GetDragY = function() {
  161. if(widgetDragInfo.hasStarted) return widgetDragInfo.yDelta;
  162. return 0;
  163. };
  164. $ax.drag.GetTotalDragX = function() {
  165. if(widgetDragInfo.hasStarted) return widgetDragInfo.currentX - widgetDragInfo.cursorStartX;
  166. return 0;
  167. };
  168. $ax.drag.GetTotalDragY = function() {
  169. if(widgetDragInfo.hasStarted) return widgetDragInfo.currentY - widgetDragInfo.cursorStartY;
  170. return 0;
  171. };
  172. $ax.drag.GetDragTime = function() {
  173. if(widgetDragInfo.hasStarted) return widgetDragInfo.currentTime - widgetDragInfo.startTime;
  174. return 600000;
  175. };
  176. // $ax.drag.GetCursorRectangles = function() {
  177. // var rects = new Object();
  178. // rects.lastRect = new Rectangle($ax.lastMouseLocation.x, $ax.lastMouseLocation.y, 1, 1);
  179. // rects.currentRect = new Rectangle($ax.mouseLocation.x, $ax.mouseLocation.y, 1, 1);
  180. // return rects;
  181. // };
  182. // $ax.drag.GetWidgetRectangles = function(id) {
  183. // var widget = window.document.getElementById(id);
  184. // var rects = new Object();
  185. // rects.lastRect = new Rectangle($ax.legacy.getAbsoluteLeft(widget), $ax.legacy.getAbsoluteTop(widget), Number($('#' + id).css('width').replace("px", "")), Number($('#' + id).css('height').replace("px", "")));
  186. // rects.currentRect = rects.lastRect;
  187. // return rects;
  188. // };
  189. // $ax.drag.IsEntering = function(movingRects, targetRects) {
  190. // return !movingRects.lastRect.IntersectsWith(targetRects.currentRect) && movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  191. // };
  192. // $ax.drag.IsLeaving = function(movingRects, targetRects) {
  193. // return movingRects.lastRect.IntersectsWith(targetRects.currentRect) && !movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  194. // };
  195. // function IsOver(movingRects, targetRects) {
  196. // return movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  197. // }
  198. // function IsNotOver(movingRects, targetRects) {
  199. // return !IsOver(movingRects, targetRects);
  200. // }
  201. $ax.drag.LogMovedWidgetForDrag = function(id) {
  202. if(widgetDragInfo.hasStarted) {
  203. var widget = $('#' + id);
  204. var y = Number(widget.css('top').replace("px", ""));
  205. var x = Number(widget.css('left').replace("px", ""));
  206. var movedWidgets = widgetDragInfo.movedWidgets;
  207. if(!movedWidgets[id]) {
  208. movedWidgets[id] = new Location(x, y);
  209. }
  210. }
  211. };
  212. var Location = function(x, y) {
  213. this.x = x;
  214. this.y = y;
  215. };
  216. $ax.drag.location = Location;
  217. var Rectangle = $ax.drag.Rectangle = function(x, y, width, height) {
  218. this.x = x;
  219. this.y = y;
  220. this.width = width;
  221. this.height = height;
  222. this.right = x + width;
  223. this.bottom = y + height;
  224. };
  225. Rectangle.prototype.IntersectsWith = function(rect) {
  226. if(rect.length) {
  227. for(var i = 0; i < rect.length; i++) if(this.IntersectsWith(rect[i])) return true;
  228. return false;
  229. }
  230. return this.x < rect.right && this.right > rect.x && this.y < rect.bottom && this.bottom > rect.y;
  231. };
  232. Rectangle.prototype.Move = function(x, y) {
  233. return new Rectangle(x, y, this.width, this.height);
  234. };
  235. });