|
1 YUI.add('event-touch', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 Adds touch event facade normalization properties (touches, changedTouches, targetTouches etc.) to the DOM event facade. Adds |
|
5 touch events to the DOM events whitelist. |
|
6 |
|
7 @example |
|
8 YUI().use('event-touch', function (Y) { |
|
9 Y.one('#myDiv').on('touchstart', function(e) { |
|
10 ... |
|
11 }); |
|
12 }); |
|
13 @module event |
|
14 @submodule event-touch |
|
15 */ |
|
16 var SCALE = "scale", |
|
17 ROTATION = "rotation", |
|
18 IDENTIFIER = "identifier", |
|
19 win = Y.config.win, |
|
20 GESTURE_MAP = {}; |
|
21 |
|
22 /** |
|
23 * Adds touch event facade normalization properties to the DOM event facade |
|
24 * |
|
25 * @method _touch |
|
26 * @for DOMEventFacade |
|
27 * @private |
|
28 * @param ev {Event} the DOM event |
|
29 * @param currentTarget {HTMLElement} the element the listener was attached to |
|
30 * @param wrapper {CustomEvent} the custom event wrapper for this DOM event |
|
31 */ |
|
32 Y.DOMEventFacade.prototype._touch = function(e, currentTarget, wrapper) { |
|
33 |
|
34 var i,l, etCached, et,touchCache; |
|
35 |
|
36 Y.log("Calling facade._touch() with e = " + e, "debug", "event-touch"); |
|
37 |
|
38 if (e.touches) { |
|
39 Y.log("Found e.touches. Replicating on facade", "info", "event-touch"); |
|
40 |
|
41 /** |
|
42 * Array of individual touch events for touch points that are still in |
|
43 * contact with the touch surface. |
|
44 * |
|
45 * @property touches |
|
46 * @type {DOMEventFacade[]} |
|
47 */ |
|
48 this.touches = []; |
|
49 touchCache = {}; |
|
50 |
|
51 for (i = 0, l = e.touches.length; i < l; ++i) { |
|
52 et = e.touches[i]; |
|
53 touchCache[Y.stamp(et)] = this.touches[i] = new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
54 } |
|
55 } |
|
56 |
|
57 if (e.targetTouches) { |
|
58 Y.log("Found e.targetTouches. Replicating on facade", "info", "event-touch"); |
|
59 |
|
60 /** |
|
61 * Array of individual touch events still in contact with the touch |
|
62 * surface and whose `touchstart` event occurred inside the same taregt |
|
63 * element as the current target element. |
|
64 * |
|
65 * @property targetTouches |
|
66 * @type {DOMEventFacade[]} |
|
67 */ |
|
68 this.targetTouches = []; |
|
69 |
|
70 for (i = 0, l = e.targetTouches.length; i < l; ++i) { |
|
71 et = e.targetTouches[i]; |
|
72 etCached = touchCache && touchCache[Y.stamp(et, true)]; |
|
73 |
|
74 this.targetTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
75 |
|
76 if (etCached) { Y.log("Found native event in touches. Using same facade in targetTouches", "info", "event-touch"); } |
|
77 } |
|
78 } |
|
79 |
|
80 if (e.changedTouches) { |
|
81 Y.log("Found e.changedTouches. Replicating on facade", "info", "event-touch"); |
|
82 |
|
83 /** |
|
84 An array of event-specific touch events. |
|
85 |
|
86 For `touchstart`, the touch points that became active with the current |
|
87 event. |
|
88 |
|
89 For `touchmove`, the touch points that have changed since the last |
|
90 event. |
|
91 |
|
92 For `touchend`, the touch points that have been removed from the touch |
|
93 surface. |
|
94 |
|
95 @property changedTouches |
|
96 @type {DOMEventFacade[]} |
|
97 **/ |
|
98 this.changedTouches = []; |
|
99 |
|
100 for (i = 0, l = e.changedTouches.length; i < l; ++i) { |
|
101 et = e.changedTouches[i]; |
|
102 etCached = touchCache && touchCache[Y.stamp(et, true)]; |
|
103 |
|
104 this.changedTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
105 |
|
106 if (etCached) { Y.log("Found native event in touches. Using same facade in changedTouches", "info", "event-touch"); } |
|
107 } |
|
108 } |
|
109 |
|
110 if (SCALE in e) { |
|
111 this[SCALE] = e[SCALE]; |
|
112 } |
|
113 |
|
114 if (ROTATION in e) { |
|
115 this[ROTATION] = e[ROTATION]; |
|
116 } |
|
117 |
|
118 if (IDENTIFIER in e) { |
|
119 this[IDENTIFIER] = e[IDENTIFIER]; |
|
120 } |
|
121 }; |
|
122 |
|
123 //Adding MSPointer events to whitelisted DOM Events. MSPointer event payloads |
|
124 //have the same properties as mouse events. |
|
125 if (Y.Node.DOM_EVENTS) { |
|
126 Y.mix(Y.Node.DOM_EVENTS, { |
|
127 touchstart:1, |
|
128 touchmove:1, |
|
129 touchend:1, |
|
130 touchcancel:1, |
|
131 gesturestart:1, |
|
132 gesturechange:1, |
|
133 gestureend:1, |
|
134 MSPointerDown:1, |
|
135 MSPointerUp:1, |
|
136 MSPointerMove:1, |
|
137 MSPointerCancel:1, |
|
138 pointerdown:1, |
|
139 pointerup:1, |
|
140 pointermove:1, |
|
141 pointercancel:1 |
|
142 }); |
|
143 } |
|
144 |
|
145 //Add properties to Y.EVENT.GESTURE_MAP based on feature detection. |
|
146 if ((win && ("ontouchstart" in win)) && !(Y.UA.chrome && Y.UA.chrome < 6)) { |
|
147 GESTURE_MAP.start = ["touchstart", "mousedown"]; |
|
148 GESTURE_MAP.end = ["touchend", "mouseup"]; |
|
149 GESTURE_MAP.move = ["touchmove", "mousemove"]; |
|
150 GESTURE_MAP.cancel = ["touchcancel", "mousecancel"]; |
|
151 } |
|
152 |
|
153 else if (win && win.PointerEvent) { |
|
154 GESTURE_MAP.start = "pointerdown"; |
|
155 GESTURE_MAP.end = "pointerup"; |
|
156 GESTURE_MAP.move = "pointermove"; |
|
157 GESTURE_MAP.cancel = "pointercancel"; |
|
158 } |
|
159 |
|
160 else if (win && ("msPointerEnabled" in win.navigator)) { |
|
161 GESTURE_MAP.start = "MSPointerDown"; |
|
162 GESTURE_MAP.end = "MSPointerUp"; |
|
163 GESTURE_MAP.move = "MSPointerMove"; |
|
164 GESTURE_MAP.cancel = "MSPointerCancel"; |
|
165 } |
|
166 |
|
167 else { |
|
168 GESTURE_MAP.start = "mousedown"; |
|
169 GESTURE_MAP.end = "mouseup"; |
|
170 GESTURE_MAP.move = "mousemove"; |
|
171 GESTURE_MAP.cancel = "mousecancel"; |
|
172 } |
|
173 |
|
174 /** |
|
175 * A object literal with keys "start", "end", and "move". The value for each key is a |
|
176 * string representing the event for that environment. For touch environments, the respective |
|
177 * values are "touchstart", "touchend" and "touchmove". Mouse and MSPointer environments are also |
|
178 * supported via feature detection. |
|
179 * |
|
180 * @property _GESTURE_MAP |
|
181 * @type Object |
|
182 * @static |
|
183 */ |
|
184 Y.Event._GESTURE_MAP = GESTURE_MAP; |
|
185 |
|
186 |
|
187 }, '@VERSION@', {"requires": ["node-base"]}); |