source: Dev/branches/rest-dojo-ui/client/dojox/gesture/tap.js @ 274

Last change on this file since 274 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: 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                        delete data.context;
82                        return;
83                }
84                var target = e.target;
85                this._initTap(data, e);
86                data.tapTimeOut = setTimeout(lang.hitch(this, function(){
87                        if(this._isTap(data, e)){
88                                this.fire(target, {type: "tap.hold"});
89                        }
90                        delete data.context;
91                }), this.holdThreshold);
92        },
93        release: function(/*Object*/data, /*Event*/e){
94                // summary:
95                //              Overwritten, fire matched 'tap' or 'tap.doubletap' during touchend
96                if(!data.context){
97                        clearTimeout(data.tapTimeOut);
98                        return;
99                }
100                if(this._isTap(data, e)){
101                        switch(data.context.c){
102                        case 1:
103                                this.fire(e.target, {type: "tap"});
104                                break;
105                        case 2:
106                                this.fire(e.target, {type: "tap.doubletap"});
107                                break;
108                        }
109                }
110                clearTimeout(data.tapTimeOut);
111        },
112        _initTap: function(/*Object*/data, /*Event*/e){
113                // summary:
114                //              Update the gesture data with new tap info
115                if(!data.context){
116                        data.context = {x: 0, y: 0, t: 0, c: 0};
117                }
118                var ct = new Date().getTime();
119                if(ct - data.context.t <= this.doubleTapTimeout){
120                        data.context.c++;
121                }else{
122                        data.context.c = 1;
123                        data.context.x = e.screenX;
124                        data.context.y = e.screenY;
125                }
126                data.context.t = ct;
127        },
128        _isTap: function(/*Object*/data, /*Event*/e){
129                // summary:
130                //              Check whether it's an valid tap
131                var dx = Math.abs(data.context.x - e.screenX);
132                var dy = Math.abs(data.context.y - e.screenY);
133                return dx <= this.tapRadius && dy <= this.tapRadius;
134        }
135});
136
137// the default tap instance for handy use
138dojox.gesture.tap = new clz();
139// Class for creating a new Tap instance
140dojox.gesture.tap.Tap = clz;
141
142return dojox.gesture.tap;
143
144});
Note: See TracBrowser for help on using the repository browser.