[256] | 1 | define(["./_base/kernel", "./on", "./has", "./mouse"], function(dojo, on, has, mouse){ |
---|
| 2 | // module: |
---|
| 3 | // dojo/touch |
---|
| 4 | |
---|
| 5 | /*===== |
---|
| 6 | dojo.touch = { |
---|
| 7 | // summary: |
---|
| 8 | // This module provides unified touch event handlers by exporting |
---|
| 9 | // press, move, release and cancel which can also run well on desktop. |
---|
| 10 | // Based on http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html |
---|
| 11 | // |
---|
| 12 | // example: |
---|
| 13 | // 1. Used with dojo.connect() |
---|
| 14 | // | dojo.connect(node, dojo.touch.press, function(e){}); |
---|
| 15 | // | dojo.connect(node, dojo.touch.move, function(e){}); |
---|
| 16 | // | dojo.connect(node, dojo.touch.release, function(e){}); |
---|
| 17 | // | dojo.connect(node, dojo.touch.cancel, function(e){}); |
---|
| 18 | // |
---|
| 19 | // 2. Used with dojo.on |
---|
| 20 | // | define(["dojo/on", "dojo/touch"], function(on, touch){ |
---|
| 21 | // | on(node, touch.press, function(e){}); |
---|
| 22 | // | on(node, touch.move, function(e){}); |
---|
| 23 | // | on(node, touch.release, function(e){}); |
---|
| 24 | // | on(node, touch.cancel, function(e){}); |
---|
| 25 | // |
---|
| 26 | // 3. Used with dojo.touch.* directly |
---|
| 27 | // | dojo.touch.press(node, function(e){}); |
---|
| 28 | // | dojo.touch.move(node, function(e){}); |
---|
| 29 | // | dojo.touch.release(node, function(e){}); |
---|
| 30 | // | dojo.touch.cancel(node, function(e){}); |
---|
| 31 | |
---|
| 32 | press: function(node, listener){ |
---|
| 33 | // summary: |
---|
| 34 | // Register a listener to 'touchstart'|'mousedown' for the given node |
---|
| 35 | // node: Dom |
---|
| 36 | // Target node to listen to |
---|
| 37 | // listener: Function |
---|
| 38 | // Callback function |
---|
| 39 | // returns: |
---|
| 40 | // A handle which will be used to remove the listener by handle.remove() |
---|
| 41 | }, |
---|
| 42 | move: function(node, listener){ |
---|
| 43 | // summary: |
---|
| 44 | // Register a listener to 'touchmove'|'mousemove' for the given node |
---|
| 45 | // node: Dom |
---|
| 46 | // Target node to listen to |
---|
| 47 | // listener: Function |
---|
| 48 | // Callback function |
---|
| 49 | // returns: |
---|
| 50 | // A handle which will be used to remove the listener by handle.remove() |
---|
| 51 | }, |
---|
| 52 | release: function(node, listener){ |
---|
| 53 | // summary: |
---|
| 54 | // Register a listener to 'touchend'|'mouseup' for the given node |
---|
| 55 | // node: Dom |
---|
| 56 | // Target node to listen to |
---|
| 57 | // listener: Function |
---|
| 58 | // Callback function |
---|
| 59 | // returns: |
---|
| 60 | // A handle which will be used to remove the listener by handle.remove() |
---|
| 61 | }, |
---|
| 62 | cancel: function(node, listener){ |
---|
| 63 | // summary: |
---|
| 64 | // Register a listener to 'touchcancel'|'mouseleave' for the given node |
---|
| 65 | // node: Dom |
---|
| 66 | // Target node to listen to |
---|
| 67 | // listener: Function |
---|
| 68 | // Callback function |
---|
| 69 | // returns: |
---|
| 70 | // A handle which will be used to remove the listener by handle.remove() |
---|
| 71 | } |
---|
| 72 | }; |
---|
| 73 | =====*/ |
---|
| 74 | |
---|
| 75 | function _handle(/*String - press | move | release | cancel*/type){ |
---|
| 76 | return function(node, listener){//called by on(), see dojo.on |
---|
| 77 | return on(node, type, listener); |
---|
| 78 | }; |
---|
| 79 | } |
---|
| 80 | var touch = has("touch"); |
---|
| 81 | //device neutral events - dojo.touch.press|move|release|cancel |
---|
| 82 | dojo.touch = { |
---|
| 83 | press: _handle(touch ? "touchstart": "mousedown"), |
---|
| 84 | move: _handle(touch ? "touchmove": "mousemove"), |
---|
| 85 | release: _handle(touch ? "touchend": "mouseup"), |
---|
| 86 | cancel: touch ? _handle("touchcancel") : mouse.leave |
---|
| 87 | }; |
---|
| 88 | return dojo.touch; |
---|
| 89 | }); |
---|