|
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 |
|
37 if (e.touches) { |
|
38 |
|
39 /** |
|
40 * Array of individual touch events for touch points that are still in |
|
41 * contact with the touch surface. |
|
42 * |
|
43 * @property touches |
|
44 * @type {DOMEventFacade[]} |
|
45 */ |
|
46 this.touches = []; |
|
47 touchCache = {}; |
|
48 |
|
49 for (i = 0, l = e.touches.length; i < l; ++i) { |
|
50 et = e.touches[i]; |
|
51 touchCache[Y.stamp(et)] = this.touches[i] = new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
52 } |
|
53 } |
|
54 |
|
55 if (e.targetTouches) { |
|
56 |
|
57 /** |
|
58 * Array of individual touch events still in contact with the touch |
|
59 * surface and whose `touchstart` event occurred inside the same taregt |
|
60 * element as the current target element. |
|
61 * |
|
62 * @property targetTouches |
|
63 * @type {DOMEventFacade[]} |
|
64 */ |
|
65 this.targetTouches = []; |
|
66 |
|
67 for (i = 0, l = e.targetTouches.length; i < l; ++i) { |
|
68 et = e.targetTouches[i]; |
|
69 etCached = touchCache && touchCache[Y.stamp(et, true)]; |
|
70 |
|
71 this.targetTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
72 |
|
73 } |
|
74 } |
|
75 |
|
76 if (e.changedTouches) { |
|
77 |
|
78 /** |
|
79 An array of event-specific touch events. |
|
80 |
|
81 For `touchstart`, the touch points that became active with the current |
|
82 event. |
|
83 |
|
84 For `touchmove`, the touch points that have changed since the last |
|
85 event. |
|
86 |
|
87 For `touchend`, the touch points that have been removed from the touch |
|
88 surface. |
|
89 |
|
90 @property changedTouches |
|
91 @type {DOMEventFacade[]} |
|
92 **/ |
|
93 this.changedTouches = []; |
|
94 |
|
95 for (i = 0, l = e.changedTouches.length; i < l; ++i) { |
|
96 et = e.changedTouches[i]; |
|
97 etCached = touchCache && touchCache[Y.stamp(et, true)]; |
|
98 |
|
99 this.changedTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper); |
|
100 |
|
101 } |
|
102 } |
|
103 |
|
104 if (SCALE in e) { |
|
105 this[SCALE] = e[SCALE]; |
|
106 } |
|
107 |
|
108 if (ROTATION in e) { |
|
109 this[ROTATION] = e[ROTATION]; |
|
110 } |
|
111 |
|
112 if (IDENTIFIER in e) { |
|
113 this[IDENTIFIER] = e[IDENTIFIER]; |
|
114 } |
|
115 }; |
|
116 |
|
117 //Adding MSPointer events to whitelisted DOM Events. MSPointer event payloads |
|
118 //have the same properties as mouse events. |
|
119 if (Y.Node.DOM_EVENTS) { |
|
120 Y.mix(Y.Node.DOM_EVENTS, { |
|
121 touchstart:1, |
|
122 touchmove:1, |
|
123 touchend:1, |
|
124 touchcancel:1, |
|
125 gesturestart:1, |
|
126 gesturechange:1, |
|
127 gestureend:1, |
|
128 MSPointerDown:1, |
|
129 MSPointerUp:1, |
|
130 MSPointerMove:1, |
|
131 MSPointerCancel:1, |
|
132 pointerdown:1, |
|
133 pointerup:1, |
|
134 pointermove:1, |
|
135 pointercancel:1 |
|
136 }); |
|
137 } |
|
138 |
|
139 //Add properties to Y.EVENT.GESTURE_MAP based on feature detection. |
|
140 if ((win && ("ontouchstart" in win)) && !(Y.UA.chrome && Y.UA.chrome < 6)) { |
|
141 GESTURE_MAP.start = ["touchstart", "mousedown"]; |
|
142 GESTURE_MAP.end = ["touchend", "mouseup"]; |
|
143 GESTURE_MAP.move = ["touchmove", "mousemove"]; |
|
144 GESTURE_MAP.cancel = ["touchcancel", "mousecancel"]; |
|
145 } |
|
146 |
|
147 else if (win && win.PointerEvent) { |
|
148 GESTURE_MAP.start = "pointerdown"; |
|
149 GESTURE_MAP.end = "pointerup"; |
|
150 GESTURE_MAP.move = "pointermove"; |
|
151 GESTURE_MAP.cancel = "pointercancel"; |
|
152 } |
|
153 |
|
154 else if (win && ("msPointerEnabled" in win.navigator)) { |
|
155 GESTURE_MAP.start = "MSPointerDown"; |
|
156 GESTURE_MAP.end = "MSPointerUp"; |
|
157 GESTURE_MAP.move = "MSPointerMove"; |
|
158 GESTURE_MAP.cancel = "MSPointerCancel"; |
|
159 } |
|
160 |
|
161 else { |
|
162 GESTURE_MAP.start = "mousedown"; |
|
163 GESTURE_MAP.end = "mouseup"; |
|
164 GESTURE_MAP.move = "mousemove"; |
|
165 GESTURE_MAP.cancel = "mousecancel"; |
|
166 } |
|
167 |
|
168 /** |
|
169 * A object literal with keys "start", "end", and "move". The value for each key is a |
|
170 * string representing the event for that environment. For touch environments, the respective |
|
171 * values are "touchstart", "touchend" and "touchmove". Mouse and MSPointer environments are also |
|
172 * supported via feature detection. |
|
173 * |
|
174 * @property _GESTURE_MAP |
|
175 * @type Object |
|
176 * @static |
|
177 */ |
|
178 Y.Event._GESTURE_MAP = GESTURE_MAP; |
|
179 |
|
180 |
|
181 }, '@VERSION@', {"requires": ["node-base"]}); |