source: Dev/branches/rest-dojo-ui/client/dojox/gesture/swipe.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.9 KB
Line 
1define([
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                //              1. 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                //              2. 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                //
29                //              2. time: an integer indicating the delta time(in milliseconds)
30                //
31                //              3. dx: delta distance on X axis, dx less than 0 - moving left, dx larger than 0 - moving right
32                //
33                //              4. dy: delta distance on Y axis, dy less than 0 - moving up, dY larger than 0 - moving down
34                //
35                //              Note - dx and dy can also be used together for a hybrid swipe(both vertically and horizontally)
36                //
37                // example:
38                //              A. Used with dojo.connect()
39                //              |       dojo.connect(node, dojox.gesture.swipe, function(e){});
40                //              |       dojo.connect(node, dojox.gesture.swipe.end, function(e){});
41                //
42                //              B. Used with dojo.on
43                //              |       define(['dojo/on', 'dojox/gesture/swipe'], function(on, swipe){
44                //              |               on(node, swipe, function(e){});
45                //              |               on(node, swipe.end, function(e){});
46                //
47                //              C. Used with dojox.gesture.swipe.* directly
48                //              |       dojox.gesture.swipe(node, function(e){});
49                //              |       dojox.gesture.swipe.end(node, function(e){});
50        };
51=====*/
52
53kernel.experimental("dojox.gesture.swipe");
54
55// Declare an internal anonymous class which will only be exported
56// by module return value e.g. dojox.gesture.swipe.Swipe
57var clz = declare(/*===== "dojox.gesture.swipe", =====*/Base, {
58
59        // defaultEvent: [readonly] String
60        //              Default event - 'swipe'
61        defaultEvent: "swipe",
62
63        // subEvents: [readonly] Array
64        //              List of sub events, used by
65        //              being combined with defaultEvent as 'swipe.end'
66        subEvents: ["end"],
67
68        press: function(/*Object*/data, /*Event*/e){
69                // summary:
70                //              Overwritten, set initial swipe info
71                if(e.touches && e.touches.length >= 2){
72                        //currently only support single-touch swipe
73                        delete data.context;
74                        return;
75                }
76                if(!data.context){
77                        data.context = {x: 0, y: 0, t: 0};
78                }
79                data.context.x = e.screenX;
80                data.context.y = e.screenY;
81                data.context.t = new Date().getTime();
82                this.lock(e.currentTarget);
83        },
84        move: function(/*Object*/data, /*Event*/e){
85                // summary:
86                //              Overwritten, fire matched 'swipe' during touchmove
87                this._recognize(data, e, "swipe");
88        },
89        release: function(/*Object*/data, /*Event*/e){
90                // summary:
91                //              Overwritten, fire matched 'swipe.end' when touchend
92                this._recognize(data, e, "swipe.end");         
93                delete data.context;
94                this.unLock();
95        },
96        cancel: function(data, e){
97                // summary:
98                //              Overwritten
99                delete data.context;
100                this.unLock();
101        },
102        _recognize: function(/*Object*/data, /*Event*/e, /*String*/type){
103                // summary:
104                //              Recognize and fire appropriate gesture events
105                if(!data.context){
106                        return;
107                }
108                var info = this._getSwipeInfo(data, e);
109                if(!info){
110                        // no swipe happened
111                        return;
112                }
113                info.type = type;
114                this.fire(e.target, info);
115        },
116        _getSwipeInfo: function(/*Object*/data, /*Event*/e){
117                // summary:
118                //              Calculate swipe information - time, dx and dy
119                var dx, dy, info = {}, startData = data.context;
120               
121                info.time = new Date().getTime() - startData.t;
122               
123                dx = e.screenX - startData.x;
124                dy = e.screenY - startData.y;
125               
126                if(dx === 0 && dy === 0){
127                        // no swipes happened
128                        return null;
129                }
130                info.dx = dx;
131                info.dy = dy;
132                return info;
133        }
134});
135
136// the default swipe instance for handy use
137dojox.gesture.swipe = new clz();
138// Class for creating a new Swipe instance
139dojox.gesture.swipe.Swipe = clz;
140
141return dojox.gesture.swipe;
142
143});
Note: See TracBrowser for help on using the repository browser.