source: Dev/trunk/src/client/dojox/gesture/tap.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.1 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/declare",
4        "dojo/_base/lang",
5        "./Base",
6        "../main"
7], function(kernel, declare, lang, Base, dojox){
8// module:
9//              dojox/gesture/tap
10       
11/*=====
12        dojox.gesture.tap = {
13                // summary:
14                //              This module provides tap gesture event handlers:
15                //
16                //              1. dojox.gesture.tap: 'tap' event
17                //
18                //              2. dojox.gesture.tap.hold: 'tap.hold' event
19                //
20                //              3. dojox.gesture.tap.doubletap: 'tap.doubletap' event
21                //
22                // example:
23                //              A. Used with dojo.connect()
24                //              |       dojo.connect(node, dojox.gesture.tap, function(e){});
25                //              |       dojo.connect(node, dojox.gesture.tap.hold, function(e){});
26                //              |       dojo.connect(node, dojox.gesture.tap.doubletap, function(e){});
27                //
28                //              B. Used with dojo.on
29                //              |       define(['dojo/on', 'dojox/gesture/tap'], function(on, tap){
30                //              |               on(node, tap, function(e){});
31                //              |               on(node, tap.hold, function(e){});
32                //              |               on(node, tap.doubletap, function(e){});
33                //
34                //              C. Used with dojox.gesture.tap.* directly
35                //              |       dojox.gesture.tap(node, function(e){});
36                //              |       dojox.gesture.tap.hold(node, function(e){});
37                //              |       dojox.gesture.tap.doubletap(node, function(e){});
38                //
39                //              Though there is always a default gesture instance after being required, e.g
40                //              |       require(['dojox/gesture/tap'], function(){...});
41                //
42                //              It's possible to create a new one with different parameter setting:
43                //              |       var myTap = new dojox.gesture.tap.Tap({holdThreshold: 300});
44                //              |       dojo.connect(node, myTap, function(e){});
45                //              |       dojo.connect(node, myTap.hold, function(e){});
46                //              |       dojo.connect(node, myTap.doubletap, function(e){});
47        };
48=====*/
49
50kernel.experimental("dojox.gesture.tap");
51
52// Declare an internal anonymous class which will only be exported
53// by module return value e.g. dojox.gesture.tap.Tap
54var clz = declare(/*===== "dojox.gesture.tap", =====*/Base, {
55        // defaultEvent: [readonly] String
56        //              Default event - 'tap'
57        defaultEvent: "tap",
58
59        // subEvents: [readonly] Array
60        //              List of sub events, used by being
61        //              combined with defaultEvent as 'tap.hold', 'tap.doubletap'.
62        subEvents: ["hold", "doubletap"],
63
64        // holdThreshold: Integer
65        //              Threshold(in milliseconds) for 'tap.hold'
66        holdThreshold: 500,
67
68        // holdThreshold: Integer
69        //              Timeout (in milliseconds) for 'tap.doubletap'
70        doubleTapTimeout: 250,
71
72        // tapRadius: Integer
73        //              Valid tap radius from previous touch point
74        tapRadius: 10,
75
76        press: function(/*Object*/data, /*Event*/e){
77                // summary:
78                //              Overwritten, record initial tap info and register a timeout checker for 'tap.hold'
79                if(e.touches && e.touches.length >= 2){
80                        //tap gesture is only for single touch
81                        clearTimeout(data.tapTimeOut);
82                        delete data.context;
83                        return;
84                }
85                var target = e.target;
86                this._initTap(data, e);
87                data.tapTimeOut = setTimeout(lang.hitch(this, function(){
88                        if(this._isTap(data, e)){
89                                this.fire(target, {type: "tap.hold"});
90                        }
91                        delete data.context;
92                }), this.holdThreshold);
93        },
94        release: function(/*Object*/data, /*Event*/e){
95                // summary:
96                //              Overwritten, fire matched 'tap' or 'tap.doubletap' during touchend
97                if(!data.context){
98                        clearTimeout(data.tapTimeOut);
99                        return;
100                }
101                if(this._isTap(data, e)){
102                        switch(data.context.c){
103                        case 1:
104                                this.fire(e.target, {type: "tap"});
105                                break;
106                        case 2:
107                                this.fire(e.target, {type: "tap.doubletap"});
108                                break;
109                        }
110                }
111                clearTimeout(data.tapTimeOut);
112        },
113        _initTap: function(/*Object*/data, /*Event*/e){
114                // summary:
115                //              Update the gesture data with new tap info
116                if(!data.context){
117                        data.context = {x: 0, y: 0, t: 0, c: 0};
118                }
119                var ct = new Date().getTime();
120                if(ct - data.context.t <= this.doubleTapTimeout){
121                        data.context.c++;
122                }else{
123                        data.context.c = 1;
124                        data.context.x = e.screenX;
125                        data.context.y = e.screenY;
126                }
127                data.context.t = ct;
128        },
129        _isTap: function(/*Object*/data, /*Event*/e){
130                // summary:
131                //              Check whether it's an valid tap
132                var dx = Math.abs(data.context.x - e.screenX);
133                var dy = Math.abs(data.context.y - e.screenY);
134                return dx <= this.tapRadius && dy <= this.tapRadius;
135        }
136});
137
138// the default tap instance for handy use
139dojox.gesture.tap = new clz();
140// Class for creating a new Tap instance
141dojox.gesture.tap.Tap = clz;
142
143return dojox.gesture.tap;
144
145});
Note: See TracBrowser for help on using the repository browser.