Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event-move-debug.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('event-move', function(Y) {
  9. /**
  10. * Adds lower level support for "gesturemovestart", "gesturemove" and "gesturemoveend" events, which can be used to create drag/drop
  11. * interactions which work across touch and mouse input devices. They correspond to "touchstart", "touchmove" and "touchend" on a touch input
  12. * device, and "mousedown", "mousemove", "mouseup" on a mouse based input device.
  13. *
  14. * @module event-gestures
  15. * @submodule event-move
  16. */
  17. var UA = Y.UA,
  18. EVENT = ((UA.mobile || UA.android || UA.ios) && UA.touch) ? {
  19. start: "touchstart",
  20. move: "touchmove",
  21. end: "touchend"
  22. } : {
  23. start: "mousedown",
  24. move: "mousemove",
  25. end: "mouseup"
  26. },
  27. START = "start",
  28. MOVE = "move",
  29. END = "end",
  30. GESTURE_MOVE = "gesture" + MOVE,
  31. GESTURE_MOVE_END = GESTURE_MOVE + END,
  32. GESTURE_MOVE_START = GESTURE_MOVE + START,
  33. _MOVE_START_HANDLE = "_msh",
  34. _MOVE_HANDLE = "_mh",
  35. _MOVE_END_HANDLE = "_meh",
  36. _DEL_MOVE_START_HANDLE = "_dmsh",
  37. _DEL_MOVE_HANDLE = "_dmh",
  38. _DEL_MOVE_END_HANDLE = "_dmeh",
  39. _MOVE_START = "_ms",
  40. _MOVE = "_m",
  41. MIN_TIME = "minTime",
  42. MIN_DISTANCE = "minDistance",
  43. PREVENT_DEFAULT = "preventDefault",
  44. BUTTON = "button",
  45. OWNER_DOCUMENT = "ownerDocument",
  46. CURRENT_TARGET = "currentTarget",
  47. TARGET = "target",
  48. NODE_TYPE = "nodeType",
  49. _defArgsProcessor = function(se, args, delegate) {
  50. var iConfig = (delegate) ? 4 : 3,
  51. config = (args.length > iConfig) ? Y.merge(args.splice(iConfig,1)[0]) : {};
  52. if (!(PREVENT_DEFAULT in config)) {
  53. config[PREVENT_DEFAULT] = se.PREVENT_DEFAULT;
  54. }
  55. return config;
  56. },
  57. _getRoot = function(node, subscriber) {
  58. return subscriber._extra.root || (node.get(NODE_TYPE) === 9) ? node : node.get(OWNER_DOCUMENT);
  59. },
  60. _normTouchFacade = function(touchFacade, touch, params) {
  61. touchFacade.pageX = touch.pageX;
  62. touchFacade.pageY = touch.pageY;
  63. touchFacade.screenX = touch.screenX;
  64. touchFacade.screenY = touch.screenY;
  65. touchFacade.clientX = touch.clientX;
  66. touchFacade.clientY = touch.clientY;
  67. touchFacade[TARGET] = touchFacade[TARGET] || touch[TARGET];
  68. touchFacade[CURRENT_TARGET] = touchFacade[CURRENT_TARGET] || touch[CURRENT_TARGET];
  69. touchFacade[BUTTON] = (params && params[BUTTON]) || 1; // default to left (left as per vendors, not W3C which is 0)
  70. },
  71. _prevent = function(e, preventDefault) {
  72. if (preventDefault) {
  73. // preventDefault is a boolean or a function
  74. if (!preventDefault.call || preventDefault(e)) {
  75. e.preventDefault();
  76. }
  77. }
  78. },
  79. define = Y.Event.define;
  80. /**
  81. * Sets up a "gesturemovestart" event, that is fired on touch devices in response to a single finger "touchstart",
  82. * and on mouse based devices in response to a "mousedown". The subscriber can specify the minimum time
  83. * and distance thresholds which should be crossed before the "gesturemovestart" is fired and for the mouse,
  84. * which button should initiate a "gesturemovestart". This event can also be listened for using node.delegate().
  85. *
  86. * <p>It is recommended that you use Y.bind to set up context and additional arguments for your event handler,
  87. * however if you want to pass the context and arguments as additional signature arguments to on/delegate,
  88. * you need to provide a null value for the configuration object, e.g: <code>node.on("gesturemovestart", fn, null, context, arg1, arg2, arg3)</code></p>
  89. *
  90. * @event gesturemovestart
  91. * @for YUI
  92. * @param type {string} "gesturemovestart"
  93. * @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mousedown or touchstart.touches[0]) which contains position co-ordinates.
  94. * @param cfg {Object} Optional. An object which specifies:
  95. *
  96. * <dl>
  97. * <dt>minDistance (defaults to 0)</dt>
  98. * <dd>The minimum distance threshold which should be crossed before the gesturemovestart is fired</dd>
  99. * <dt>minTime (defaults to 0)</dt>
  100. * <dd>The minimum time threshold for which the finger/mouse should be help down before the gesturemovestart is fired</dd>
  101. * <dt>button (no default)</dt>
  102. * <dd>In the case of a mouse input device, if the event should only be fired for a specific mouse button.</dd>
  103. * <dt>preventDefault (defaults to false)</dt>
  104. * <dd>Can be set to true/false to prevent default behavior as soon as the touchstart or mousedown is received (that is before minTime or minDistance thresholds are crossed, and so before the gesturemovestart listener is notified) so that things like text selection and context popups (on touch devices) can be
  105. * prevented. This property can also be set to a function, which returns true or false, based on the event facade passed to it (for example, DragDrop can determine if the target is a valid handle or not before preventing default).</dd>
  106. * </dl>
  107. *
  108. * @return {EventHandle} the detach handle
  109. */
  110. define(GESTURE_MOVE_START, {
  111. on: function (node, subscriber, ce) {
  112. subscriber[_MOVE_START_HANDLE] = node.on(EVENT[START],
  113. this._onStart,
  114. this,
  115. node,
  116. subscriber,
  117. ce);
  118. },
  119. delegate : function(node, subscriber, ce, filter) {
  120. var se = this;
  121. subscriber[_DEL_MOVE_START_HANDLE] = node.delegate(EVENT[START],
  122. function(e) {
  123. se._onStart(e, node, subscriber, ce, true);
  124. },
  125. filter);
  126. },
  127. detachDelegate : function(node, subscriber, ce, filter) {
  128. var handle = subscriber[_DEL_MOVE_START_HANDLE];
  129. if (handle) {
  130. handle.detach();
  131. subscriber[_DEL_MOVE_START_HANDLE] = null;
  132. }
  133. },
  134. detach: function (node, subscriber, ce) {
  135. var startHandle = subscriber[_MOVE_START_HANDLE];
  136. if (startHandle) {
  137. startHandle.detach();
  138. subscriber[_MOVE_START_HANDLE] = null;
  139. }
  140. },
  141. processArgs : function(args, delegate) {
  142. var params = _defArgsProcessor(this, args, delegate);
  143. if (!(MIN_TIME in params)) {
  144. params[MIN_TIME] = this.MIN_TIME;
  145. }
  146. if (!(MIN_DISTANCE in params)) {
  147. params[MIN_DISTANCE] = this.MIN_DISTANCE;
  148. }
  149. return params;
  150. },
  151. _onStart : function(e, node, subscriber, ce, delegate) {
  152. if (delegate) {
  153. node = e[CURRENT_TARGET];
  154. }
  155. var params = subscriber._extra,
  156. fireStart = true,
  157. minTime = params[MIN_TIME],
  158. minDistance = params[MIN_DISTANCE],
  159. button = params.button,
  160. preventDefault = params[PREVENT_DEFAULT],
  161. root = _getRoot(node, subscriber),
  162. startXY;
  163. if (e.touches) {
  164. if (e.touches.length === 1) {
  165. _normTouchFacade(e, e.touches[0], params);
  166. } else {
  167. fireStart = false;
  168. }
  169. } else {
  170. fireStart = (button === undefined) || (button === e.button);
  171. }
  172. Y.log("gesturemovestart: params = button:" + button + ", minTime = " + minTime + ", minDistance = " + minDistance, "event-gestures");
  173. if (fireStart) {
  174. _prevent(e, preventDefault);
  175. if (minTime === 0 || minDistance === 0) {
  176. Y.log("gesturemovestart: No minTime or minDistance. Firing immediately", "event-gestures");
  177. this._start(e, node, ce, params);
  178. } else {
  179. startXY = [e.pageX, e.pageY];
  180. if (minTime > 0) {
  181. Y.log("gesturemovestart: minTime specified. Setup timer.", "event-gestures");
  182. Y.log("gesturemovestart: initialTime for minTime = " + new Date().getTime(), "event-gestures");
  183. params._ht = Y.later(minTime, this, this._start, [e, node, ce, params]);
  184. params._hme = root.on(EVENT[END], Y.bind(function() {
  185. this._cancel(params);
  186. }, this));
  187. }
  188. if (minDistance > 0) {
  189. Y.log("gesturemovestart: minDistance specified. Setup native mouse/touchmove listener to measure distance.", "event-gestures");
  190. Y.log("gesturemovestart: initialXY for minDistance = " + startXY, "event-gestures");
  191. params._hm = root.on(EVENT[MOVE], Y.bind(function(em) {
  192. if (Math.abs(em.pageX - startXY[0]) > minDistance || Math.abs(em.pageY - startXY[1]) > minDistance) {
  193. Y.log("gesturemovestart: minDistance hit.", "event-gestures");
  194. this._start(e, node, ce, params);
  195. }
  196. }, this));
  197. }
  198. }
  199. }
  200. },
  201. _cancel : function(params) {
  202. if (params._ht) {
  203. params._ht.cancel();
  204. params._ht = null;
  205. }
  206. if (params._hme) {
  207. params._hme.detach();
  208. params._hme = null;
  209. }
  210. if (params._hm) {
  211. params._hm.detach();
  212. params._hm = null;
  213. }
  214. },
  215. _start : function(e, node, ce, params) {
  216. if (params) {
  217. this._cancel(params);
  218. }
  219. e.type = GESTURE_MOVE_START;
  220. Y.log("gesturemovestart: Firing start: " + new Date().getTime(), "event-gestures");
  221. node.setData(_MOVE_START, e);
  222. ce.fire(e);
  223. },
  224. MIN_TIME : 0,
  225. MIN_DISTANCE : 0,
  226. PREVENT_DEFAULT : false
  227. });
  228. /**
  229. * Sets up a "gesturemove" event, that is fired on touch devices in response to a single finger "touchmove",
  230. * and on mouse based devices in response to a "mousemove".
  231. *
  232. * <p>By default this event is only fired when the same node
  233. * has received a "gesturemovestart" event. The subscriber can set standAlone to true, in the configuration properties,
  234. * if they want to listen for this event without an initial "gesturemovestart".</p>
  235. *
  236. * <p>By default this event sets up it's internal "touchmove" and "mousemove" DOM listeners on the document element. The subscriber
  237. * can set the root configuration property, to specify which node to attach DOM listeners to, if different from the document.</p>
  238. *
  239. * <p>This event can also be listened for using node.delegate().</p>
  240. *
  241. * <p>It is recommended that you use Y.bind to set up context and additional arguments for your event handler,
  242. * however if you want to pass the context and arguments as additional signature arguments to on/delegate,
  243. * you need to provide a null value for the configuration object, e.g: <code>node.on("gesturemove", fn, null, context, arg1, arg2, arg3)</code></p>
  244. *
  245. * @event gesturemove
  246. * @for YUI
  247. * @param type {string} "gesturemove"
  248. * @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mousemove or touchmove.touches[0]) which contains position co-ordinates.
  249. * @param cfg {Object} Optional. An object which specifies:
  250. * <dl>
  251. * <dt>standAlone (defaults to false)</dt>
  252. * <dd>true, if the subscriber should be notified even if a "gesturemovestart" has not occured on the same node.</dd>
  253. * <dt>root (defaults To Document)</dt>
  254. * <dd>The node to which the internal DOM listeners should be attached.</dd>
  255. * <dt>preventDefault (defaults to false)</dt>
  256. * <dd>Can be set to true/false to prevent default behavior as soon as the touchmove or mousemove is received. As with gesturemovestart, can also be set to function which returns true/false based on the event facade passed to it.</dd>
  257. * </dl>
  258. *
  259. * @return {EventHandle} the detach handle
  260. */
  261. define(GESTURE_MOVE, {
  262. on : function (node, subscriber, ce) {
  263. var root = _getRoot(node, subscriber),
  264. moveHandle = root.on(EVENT[MOVE],
  265. this._onMove,
  266. this,
  267. node,
  268. subscriber,
  269. ce);
  270. subscriber[_MOVE_HANDLE] = moveHandle;
  271. },
  272. delegate : function(node, subscriber, ce, filter) {
  273. var se = this;
  274. subscriber[_DEL_MOVE_HANDLE] = node.delegate(EVENT[MOVE],
  275. function(e) {
  276. se._onMove(e, node, subscriber, ce, true);
  277. },
  278. filter);
  279. },
  280. detach : function (node, subscriber, ce) {
  281. var moveHandle = subscriber[_MOVE_HANDLE];
  282. if (moveHandle) {
  283. moveHandle.detach();
  284. subscriber[_MOVE_HANDLE] = null;
  285. }
  286. },
  287. detachDelegate : function(node, subscriber, ce, filter) {
  288. var handle = subscriber[_DEL_MOVE_HANDLE];
  289. if (handle) {
  290. handle.detach();
  291. subscriber[_DEL_MOVE_HANDLE] = null;
  292. }
  293. },
  294. processArgs : function(args, delegate) {
  295. return _defArgsProcessor(this, args, delegate);
  296. },
  297. _onMove : function(e, node, subscriber, ce, delegate) {
  298. if (delegate) {
  299. node = e[CURRENT_TARGET];
  300. }
  301. var fireMove = subscriber._extra.standAlone || node.getData(_MOVE_START),
  302. preventDefault = subscriber._extra.preventDefault;
  303. Y.log("onMove initial fireMove check:" + fireMove,"event-gestures");
  304. if (fireMove) {
  305. if (e.touches) {
  306. if (e.touches.length === 1) {
  307. _normTouchFacade(e, e.touches[0]);
  308. } else {
  309. fireMove = false;
  310. }
  311. }
  312. if (fireMove) {
  313. _prevent(e, preventDefault);
  314. Y.log("onMove second fireMove check:" + fireMove,"event-gestures");
  315. e.type = GESTURE_MOVE;
  316. ce.fire(e);
  317. }
  318. }
  319. },
  320. PREVENT_DEFAULT : false
  321. });
  322. /**
  323. * Sets up a "gesturemoveend" event, that is fired on touch devices in response to a single finger "touchend",
  324. * and on mouse based devices in response to a "mouseup".
  325. *
  326. * <p>By default this event is only fired when the same node
  327. * has received a "gesturemove" or "gesturemovestart" event. The subscriber can set standAlone to true, in the configuration properties,
  328. * if they want to listen for this event without a preceding "gesturemovestart" or "gesturemove".</p>
  329. *
  330. * <p>By default this event sets up it's internal "touchend" and "mouseup" DOM listeners on the document element. The subscriber
  331. * can set the root configuration property, to specify which node to attach DOM listeners to, if different from the document.</p>
  332. *
  333. * <p>This event can also be listened for using node.delegate().</p>
  334. *
  335. * <p>It is recommended that you use Y.bind to set up context and additional arguments for your event handler,
  336. * however if you want to pass the context and arguments as additional signature arguments to on/delegate,
  337. * you need to provide a null value for the configuration object, e.g: <code>node.on("gesturemoveend", fn, null, context, arg1, arg2, arg3)</code></p>
  338. *
  339. *
  340. * @event gesturemoveend
  341. * @for YUI
  342. * @param type {string} "gesturemoveend"
  343. * @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mouseup or touchend.changedTouches[0]).
  344. * @param cfg {Object} Optional. An object which specifies:
  345. * <dl>
  346. * <dt>standAlone (defaults to false)</dt>
  347. * <dd>true, if the subscriber should be notified even if a "gesturemovestart" or "gesturemove" has not occured on the same node.</dd>
  348. * <dt>root (defaults To Document)</dt>
  349. * <dd>The node to which the internal DOM listeners should be attached.</dd>
  350. * <dt>preventDefault (defaults to false)</dt>
  351. * <dd>Can be set to true/false to prevent default behavior as soon as the touchend or mouseup is received. As with gesturemovestart, can also be set to function which returns true/false based on the event facade passed to it.</dd>
  352. * </dl>
  353. *
  354. * @return {EventHandle} the detach handle
  355. */
  356. define(GESTURE_MOVE_END, {
  357. on : function (node, subscriber, ce) {
  358. var root = _getRoot(node, subscriber),
  359. endHandle = root.on(EVENT[END],
  360. this._onEnd,
  361. this,
  362. node,
  363. subscriber,
  364. ce);
  365. subscriber[_MOVE_END_HANDLE] = endHandle;
  366. },
  367. delegate : function(node, subscriber, ce, filter) {
  368. var se = this;
  369. subscriber[_DEL_MOVE_END_HANDLE] = node.delegate(EVENT[END],
  370. function(e) {
  371. se._onEnd(e, node, subscriber, ce, true);
  372. },
  373. filter);
  374. },
  375. detachDelegate : function(node, subscriber, ce, filter) {
  376. var handle = subscriber[_DEL_MOVE_END_HANDLE];
  377. if (handle) {
  378. handle.detach();
  379. subscriber[_DEL_MOVE_END_HANDLE] = null;
  380. }
  381. },
  382. detach : function (node, subscriber, ce) {
  383. var endHandle = subscriber[_MOVE_END_HANDLE];
  384. if (endHandle) {
  385. endHandle.detach();
  386. subscriber[_MOVE_END_HANDLE] = null;
  387. }
  388. },
  389. processArgs : function(args, delegate) {
  390. return _defArgsProcessor(this, args, delegate);
  391. },
  392. _onEnd : function(e, node, subscriber, ce, delegate) {
  393. if (delegate) {
  394. node = e[CURRENT_TARGET];
  395. }
  396. var fireMoveEnd = subscriber._extra.standAlone || node.getData(_MOVE) || node.getData(_MOVE_START),
  397. preventDefault = subscriber._extra.preventDefault;
  398. if (fireMoveEnd) {
  399. if (e.changedTouches) {
  400. if (e.changedTouches.length === 1) {
  401. _normTouchFacade(e, e.changedTouches[0]);
  402. } else {
  403. fireMoveEnd = false;
  404. }
  405. }
  406. if (fireMoveEnd) {
  407. _prevent(e, preventDefault);
  408. e.type = GESTURE_MOVE_END;
  409. ce.fire(e);
  410. node.clearData(_MOVE_START);
  411. node.clearData(_MOVE);
  412. }
  413. }
  414. },
  415. PREVENT_DEFAULT : false
  416. });
  417. }, '3.4.0' ,{requires:['node-base','event-touch','event-synthetic']});