[483] | 1 | define([ |
---|
| 2 | "dojo/_base/kernel", |
---|
| 3 | "dojo/_base/declare", |
---|
| 4 | "./Base", |
---|
| 5 | "../main" |
---|
| 6 | ], function(kernel, declare, Base, dojox){ |
---|
| 7 | // module: |
---|
| 8 | // dojox/gesture/swipe |
---|
| 9 | |
---|
| 10 | /*===== |
---|
| 11 | dojox.gesture.swipe = { |
---|
| 12 | // summary: |
---|
| 13 | // This module provides swipe gestures including: |
---|
| 14 | // |
---|
| 15 | // ####dojox.gesture.swipe |
---|
| 16 | // |
---|
| 17 | // A series of 'swipe' will be fired during touchmove, this will mostly |
---|
| 18 | // be used to keep sliding the Dom target based on the swiped distance(dx, dy). |
---|
| 19 | // |
---|
| 20 | // ####dojox.gesture.swipe.end |
---|
| 21 | // |
---|
| 22 | // Fired when a swipe is ended so that an bounce animation may be applied |
---|
| 23 | // to the dom target sliding to the final position. |
---|
| 24 | // |
---|
| 25 | // Following information will be included in the fired swipe events: |
---|
| 26 | // |
---|
| 27 | // 1. type: 'swipe'|'swipe.end' |
---|
| 28 | // 2. time: an integer indicating the delta time(in milliseconds) |
---|
| 29 | // 3. dx: delta distance on X axis, dx less than 0 - moving left, dx larger than 0 - moving right |
---|
| 30 | // 4. dy: delta distance on Y axis, dy less than 0 - moving up, dY larger than 0 - moving down |
---|
| 31 | // |
---|
| 32 | // Note - dx and dy can also be used together for a hybrid swipe(both vertically and horizontally) |
---|
| 33 | // |
---|
| 34 | // example: |
---|
| 35 | // A. Used with dojo.connect() |
---|
| 36 | // | dojo.connect(node, dojox.gesture.swipe, function(e){}); |
---|
| 37 | // | dojo.connect(node, dojox.gesture.swipe.end, function(e){}); |
---|
| 38 | // |
---|
| 39 | // B. Used with dojo.on |
---|
| 40 | // | define(['dojo/on', 'dojox/gesture/swipe'], function(on, swipe){ |
---|
| 41 | // | on(node, swipe, function(e){}); |
---|
| 42 | // | on(node, swipe.end, function(e){}); |
---|
| 43 | // |
---|
| 44 | // C. Used with dojox.gesture.swipe.* directly |
---|
| 45 | // | dojox.gesture.swipe(node, function(e){}); |
---|
| 46 | // | dojox.gesture.swipe.end(node, function(e){}); |
---|
| 47 | }; |
---|
| 48 | =====*/ |
---|
| 49 | |
---|
| 50 | kernel.experimental("dojox.gesture.swipe"); |
---|
| 51 | |
---|
| 52 | // Declare an internal anonymous class which will only be exported |
---|
| 53 | // by module return value e.g. dojox.gesture.swipe.Swipe |
---|
| 54 | var clz = declare(/*===== "dojox.gesture.swipe", =====*/Base, { |
---|
| 55 | |
---|
| 56 | // defaultEvent: [readonly] String |
---|
| 57 | // Default event - 'swipe' |
---|
| 58 | defaultEvent: "swipe", |
---|
| 59 | |
---|
| 60 | // subEvents: [readonly] Array |
---|
| 61 | // List of sub events, used by |
---|
| 62 | // being combined with defaultEvent as 'swipe.end' |
---|
| 63 | subEvents: ["end"], |
---|
| 64 | |
---|
| 65 | press: function(/*Object*/data, /*Event*/e){ |
---|
| 66 | // summary: |
---|
| 67 | // Overwritten, set initial swipe info |
---|
| 68 | if(e.touches && e.touches.length >= 2){ |
---|
| 69 | //currently only support single-touch swipe |
---|
| 70 | delete data.context; |
---|
| 71 | return; |
---|
| 72 | } |
---|
| 73 | if(!data.context){ |
---|
| 74 | data.context = {x: 0, y: 0, t: 0}; |
---|
| 75 | } |
---|
| 76 | data.context.x = e.screenX; |
---|
| 77 | data.context.y = e.screenY; |
---|
| 78 | data.context.t = new Date().getTime(); |
---|
| 79 | this.lock(e.currentTarget); |
---|
| 80 | }, |
---|
| 81 | move: function(/*Object*/data, /*Event*/e){ |
---|
| 82 | // summary: |
---|
| 83 | // Overwritten, fire matched 'swipe' during touchmove |
---|
| 84 | this._recognize(data, e, "swipe"); |
---|
| 85 | }, |
---|
| 86 | release: function(/*Object*/data, /*Event*/e){ |
---|
| 87 | // summary: |
---|
| 88 | // Overwritten, fire matched 'swipe.end' when touchend |
---|
| 89 | this._recognize(data, e, "swipe.end"); |
---|
| 90 | delete data.context; |
---|
| 91 | this.unLock(); |
---|
| 92 | }, |
---|
| 93 | cancel: function(data, e){ |
---|
| 94 | // summary: |
---|
| 95 | // Overwritten |
---|
| 96 | delete data.context; |
---|
| 97 | this.unLock(); |
---|
| 98 | }, |
---|
| 99 | _recognize: function(/*Object*/data, /*Event*/e, /*String*/type){ |
---|
| 100 | // summary: |
---|
| 101 | // Recognize and fire appropriate gesture events |
---|
| 102 | if(!data.context){ |
---|
| 103 | return; |
---|
| 104 | } |
---|
| 105 | var info = this._getSwipeInfo(data, e); |
---|
| 106 | if(!info){ |
---|
| 107 | // no swipe happened |
---|
| 108 | return; |
---|
| 109 | } |
---|
| 110 | info.type = type; |
---|
| 111 | this.fire(e.target, info); |
---|
| 112 | }, |
---|
| 113 | _getSwipeInfo: function(/*Object*/data, /*Event*/e){ |
---|
| 114 | // summary: |
---|
| 115 | // Calculate swipe information - time, dx and dy |
---|
| 116 | var dx, dy, info = {}, startData = data.context; |
---|
| 117 | |
---|
| 118 | info.time = new Date().getTime() - startData.t; |
---|
| 119 | |
---|
| 120 | dx = e.screenX - startData.x; |
---|
| 121 | dy = e.screenY - startData.y; |
---|
| 122 | |
---|
| 123 | if(dx === 0 && dy === 0){ |
---|
| 124 | // no swipes happened |
---|
| 125 | return null; |
---|
| 126 | } |
---|
| 127 | info.dx = dx; |
---|
| 128 | info.dy = dy; |
---|
| 129 | return info; |
---|
| 130 | } |
---|
| 131 | }); |
---|
| 132 | |
---|
| 133 | // the default swipe instance for handy use |
---|
| 134 | dojox.gesture.swipe = new clz(); |
---|
| 135 | // Class for creating a new Swipe instance |
---|
| 136 | dojox.gesture.swipe.Swipe = clz; |
---|
| 137 | |
---|
| 138 | return dojox.gesture.swipe; |
---|
| 139 | |
---|
| 140 | }); |
---|