~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/docs/event/touch.html

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html>
2
 
<html lang="en">
3
 
<head>
4
 
    <meta charset="utf-8">
5
 
    <title>Touch Events and Abstractions</title>
6
 
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
 
    <link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
 
    <link rel="stylesheet" href="../assets/css/main.css">
9
 
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
 
    <script src="../../build/yui/yui-min.js"></script>
11
 
</head>
12
 
<body>
13
 
 
14
 
<div id="doc">
15
 
    <h1>Touch Events and Abstractions</h1>
16
 
 
17
 
    
18
 
        <a href="#toc" class="jump">Jump to Table of Contents</a>
19
 
    
20
 
 
21
 
    <div class="yui3-g">
22
 
        <div class="yui3-u-3-4">
23
 
            <div id="main">
24
 
                <div class="content"><div class="intro">
25
 
    <p>The <code>event-touch</code> module extends the <a
26
 
    href="index.html#appendixA">whitelist of DOM events</a> to include the
27
 
    native touch and gesture events and adds  relevant information to event
28
 
    facades.</p>
29
 
 
30
 
    <p>The <code>event-move</code> module adds a set of abstract events that adapt to
31
 
    the client environment to handle either touch or mouse input.</p>
32
 
 
33
 
    <p>The <code>event-flick</code> module adds a "flick" event on top of the gesture
34
 
    abstraction layer to capture a user flicking an element with mouse or
35
 
    finger.</p>
36
 
 
37
 
    <p>The <code>event-gestures</code> module is a rollup of these three, but will
38
 
    potentially roll in more gesture based events in the future.</p>
39
 
</div>
40
 
 
41
 
<h2 id="using-touch-events">Using Touch Events</h2>
42
 
 
43
 
<p>YUI DOM event support also extends to touch events. To use touch events in your application, you will need to include the <code>event-touch</code> module in your use statement.</p>
44
 
 
45
 
<p>The set of common low-level touch events, which exist on most touch-enabled OSes are supported:</p>
46
 
 
47
 
<ul>
48
 
    <li><code>touchstart</code></li>
49
 
    <li><code>touchmove</code></li>
50
 
    <li><code>touchend</code></li>
51
 
    <li><code>touchcancel</code></li>
52
 
</ul>
53
 
 
54
 
<p>Additionally, the iOS specific touch events, <code>gesturestart</code>,
55
 
<code>gesturechange</code> and <code>gestureend</code>, are also supported.
56
 
YUI doesn't replicate support for these events on other touch OSes currently,
57
 
due to their lack of DOM level multi-touch support. At the point at which they
58
 
do expose multi-touch information in the lower level touch events, we can build
59
 
in cross-platform support for multi-touch gestures.</p>
60
 
 
61
 
<pre class="code prettyprint">node.on(&#x27;touchstart&#x60;, function () {
62
 
    this.addClass(&#x27;detached&#x27;);
63
 
});</pre>
64
 
 
65
 
 
66
 
<h4 id="the-touch-event-facade">The Touch Event Facade</h4>
67
 
 
68
 
<p>The event facade passed to touch events has the common set of touch specific
69
 
array properties available:</p>
70
 
 
71
 
<ul>
72
 
    <li><code>touches</code></li>
73
 
    <li><code>changedTouches</code></li>
74
 
    <li><code>touchTargets</code></li>
75
 
</ul>
76
 
 
77
 
<p>These event objects in these arrays are also normalized, just the same as
78
 
the event object pass to any other DOM listener.</p>
79
 
 
80
 
<p>The event object for the iOS gesture events have <code>scale</code> and
81
 
<code>rotation</code> properties, the same as the native event object.</p>
82
 
 
83
 
<h2 id="move">Cross-Device Gesture Support</h2>
84
 
 
85
 
<p>The <code>event-move</code> module provides the following events that
86
 
<em>roughly</em> relate to the associated touch or mouse event, depending on the client
87
 
device:</p>
88
 
 
89
 
<table>
90
 
<thead>
91
 
    <tr>
92
 
        <th>Abstract event</th>
93
 
        <th>Mouse event</th>
94
 
        <th>Touch event</th>
95
 
    </th>
96
 
</thead>
97
 
<tbody>
98
 
    <tr>
99
 
        <td><strong><code>gesturemovestart</code></strong></td>
100
 
        <td><code>mousedown</code></td>
101
 
        <td><code>touchstart</code></td>
102
 
    </tr>
103
 
    <tr>
104
 
        <td><strong><code>gesturemove</code></strong></td>
105
 
        <td><code>mousemove</code></td>
106
 
        <td><code>touchmove</code></td>
107
 
    </tr>
108
 
    <tr>
109
 
        <td><strong><code>gesturemoveend</code></strong></td>
110
 
        <td><code>mouseup</code></td>
111
 
        <td><code>touchend</code></td>
112
 
    </tr>
113
 
</tbody>
114
 
</table>
115
 
 
116
 
<p>I say "roughly" because the gesture events aim to encapsulate common
117
 
interactions rather than just serving as a relay to other events.  Where this
118
 
comes out is in the additional configuration that can be included in the
119
 
subscription as a third argument.</p>
120
 
 
121
 
<pre class="code prettyprint">&#x2F;&#x2F; Notify me when the user puts a finger down, or mouses down on
122
 
&#x2F;&#x2F; the car node
123
 
car.on(&quot;gesturemovestart&quot;, alignForMove, {
124
 
 
125
 
    &#x2F;&#x2F; fire the event only after 300ms of continuous contact...
126
 
    minTime: 300,
127
 
 
128
 
    &#x2F;&#x2F; ...or if the finger&#x2F;mouse moves more than 3px
129
 
    minDistance: 3
130
 
});
131
 
 
132
 
&#x2F;&#x2F; Move the car node when the user moves the finger or mouse.
133
 
&#x2F;&#x2F; Note the &#x60;this&#x60; override parameter is shifted to account for
134
 
&#x2F;&#x2F; the configuration param
135
 
car.on(&quot;gesturemove&quot;, car.move, null, car);
136
 
 
137
 
 
138
 
&#x2F;&#x2F; Notify me when the user lifts their finger, or lets go of
139
 
&#x2F;&#x2F; the mouse button (only if a gesturemovestart was received
140
 
&#x2F;&#x2F; on the node).
141
 
car.on(&quot;gesturemoveend&quot;, screechingHalt);</pre>
142
 
 
143
 
 
144
 
<p>The complete set of configuration parameters for the gesture events is <a href="http://yuilibrary.com/yui/docs/api/module_event-move.html#event_gesturemove">in the API docs</a>.</p>
145
 
 
146
 
<h4 id="related-events">Related Events</h4>
147
 
 
148
 
<p>The three gesture events are related to each other.  They are notifications
149
 
for the start, progress, and end of the same gesture. <code>gesturemove</code> and
150
 
<code>gesturemoveend</code> subscriptions won't execute unless a <code>gesturemovestart</code>
151
 
happens.</p>
152
 
 
153
 
<p>If you need them to fire separately, such as when attaching and detaching
154
 
subscribers dynamically, the <code>gesturemove</code> and <code>gesturemoveend</code> events can be
155
 
subscribed to individually by configuring <code>standAlone: true</code></p>
156
 
 
157
 
<pre class="code prettyprint">&#x2F;&#x2F; Doesn&#x27;t require an associated &#x60;gesturemovestart&#x60; subscription
158
 
Y.one(&quot;doc&quot;).on(&quot;gesturemove&quot;, function(e) {...}, {
159
 
    standAlone:true
160
 
});</pre>
161
 
 
162
 
 
163
 
<p>Under the hood, the DOM listeners which monitor mousemove/touchmove and
164
 
mouseup/touchend are bound to the <code>document</code> by default. The node only provides
165
 
the shared context to relate the three events.</p>
166
 
 
167
 
 
168
 
<h2 id="flick">Flick Gesture Event</h2>
169
 
 
170
 
<p>The flick gesture event is fired whenever the user initiates a flick gesture (with a finger or the mouse) on the node where the listener is attached.</p>
171
 
 
172
 
<pre class="code prettyprint">myNode.on(&quot;flick&quot;, function(e) {
173
 
 
174
 
  &#x2F;&#x2F; Some of the flick specific data on the event facade
175
 
 
176
 
  var flick = e.flick,
177
 
      velocity = flick.velocity,
178
 
      distance = flick.distance,
179
 
      axis = flick.axis,
180
 
      startX = flick.start.pageX,
181
 
      startY = flick.start.pageY,
182
 
 
183
 
  &#x2F;&#x2F; The event object itself is the event object for
184
 
  &#x2F;&#x2F; the event which concludes the flick (the mouseup or touchend)
185
 
 
186
 
      endX = e.pageX,
187
 
      endY = e.pageY,
188
 
      endTarget = e.target;
189
 
 
190
 
});</pre>
191
 
 
192
 
 
193
 
<p>Like with the supporting gesture events, when subscribing to
194
 
<code>flick</code>, you can also pass additional configuration to control
195
 
when and how the flick subscriber is notified.</p>
196
 
 
197
 
<pre class="code prettyprint">&#x2F;&#x2F; Custom config, with no context or extra args
198
 
myNode.on(&quot;flick&quot;, flickHandler, {
199
 
 
200
 
    &#x2F;&#x2F; only notify me if the flick covered
201
 
    &#x2F;&#x2F; more than 20px and was faster than 0.8 px&#x2F;ms
202
 
    minDistance: 20,
203
 
    minVelocity: 0.8,
204
 
 
205
 
    &#x2F;&#x2F; prevent the default behavior for the
206
 
    &#x2F;&#x2F; underlying mouse&#x2F;touch events
207
 
    preventDefault: true
208
 
});
209
 
 
210
 
&#x2F;&#x2F; Another option to avoid confusion when specifying the &#x60;this&#x60;
211
 
&#x2F;&#x2F; override or bound arguments for events with custom signatures
212
 
&#x2F;&#x2F; is to use Y.bind
213
 
myNode.on(&quot;flick&quot;, Y.bind(o.flickHandler, o, arg1), {
214
 
    minDistance: 20,
215
 
    minVelocity: 0.8,
216
 
    preventDefault: true
217
 
});
218
 
 
219
 
&#x2F;&#x2F; Alternately, make sure to account for the additional subscription
220
 
&#x2F;&#x2F; parameter by passing null if there is no configuration.
221
 
myNode.on(&quot;flick&quot;, o.flickHandler, null, o, arg1);</pre>
222
 
 
223
 
 
224
 
<p>The complete set of configuration parameters for the <code>flick</code> event is <a href="http://yuilibrary.com/yui/docs/api/module_event-flick.html#event_flick">in the API docs</a>.</p>
225
 
 
226
 
<h2 id="caveats">Caveats</h2>
227
 
 
228
 
<ol>
229
 
    <li>The <code>flick</code> event doesn't (yet) support delegation.</li>
230
 
    <li>
231
 
        The <code>delegate()</code> signature for the gesture events is
232
 
        <code>node.delegate('gesturemove', callback, <em>filter</em>,
233
 
        <em>gestureConfig</em>)</code>, reversing the order of the delegation
234
 
        filter and extra params from the <a href="hover.html"><code>hover</code></a> and
235
 
        <a href="key.html"><code>key</code></a> events.  This may be changed in a future
236
 
        release.
237
 
    </li>
238
 
</ol>
239
 
 
240
 
</div>
241
 
            </div>
242
 
        </div>
243
 
 
244
 
        <div class="yui3-u-1-4">
245
 
            <div class="sidebar">
246
 
                
247
 
                    <div id="toc" class="sidebox">
248
 
                        <div class="hd">
249
 
                            <h2 class="no-toc">Table of Contents</h2>
250
 
                        </div>
251
 
 
252
 
                        <div class="bd">
253
 
                            <ul class="toc">
254
 
<li>
255
 
<a href="#using-touch-events">Using Touch Events</a>
256
 
<ul class="toc">
257
 
<li>
258
 
<a href="#the-touch-event-facade">The Touch Event Facade</a>
259
 
</li>
260
 
</ul>
261
 
</li>
262
 
<li>
263
 
<a href="#move">Cross-Device Gesture Support</a>
264
 
<ul class="toc">
265
 
<li>
266
 
<a href="#related-events">Related Events</a>
267
 
</li>
268
 
</ul>
269
 
</li>
270
 
<li>
271
 
<a href="#flick">Flick Gesture Event</a>
272
 
</li>
273
 
<li>
274
 
<a href="#caveats">Caveats</a>
275
 
</li>
276
 
</ul>
277
 
                        </div>
278
 
                    </div>
279
 
                
280
 
 
281
 
                
282
 
                    <div class="sidebox">
283
 
                        <div class="hd">
284
 
                            <h2 class="no-toc">Examples</h2>
285
 
                        </div>
286
 
 
287
 
                        <div class="bd">
288
 
                            <ul class="examples">
289
 
                                
290
 
                                    
291
 
                                        <li data-description="Use the Event Utility to attach simple DOM event handlers.">
292
 
                                            <a href="basic-example.html">Simple DOM Events</a>
293
 
                                        </li>
294
 
                                    
295
 
                                
296
 
                                    
297
 
                                        <li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed.">
298
 
                                            <a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a>
299
 
                                        </li>
300
 
                                    
301
 
                                
302
 
                                    
303
 
                                        <li data-description="Supporting cross-device swipe gestures, using the event-move gesture events">
304
 
                                            <a href="swipe-example.html">Supporting A Swipe Left Gesture</a>
305
 
                                        </li>
306
 
                                    
307
 
                                
308
 
                                    
309
 
                                
310
 
                                    
311
 
                                
312
 
                                    
313
 
                                
314
 
                                    
315
 
                                
316
 
                                    
317
 
                                
318
 
                            </ul>
319
 
                        </div>
320
 
                    </div>
321
 
                
322
 
 
323
 
                
324
 
                    <div class="sidebox">
325
 
                        <div class="hd">
326
 
                            <h2 class="no-toc">Examples That Use This Component</h2>
327
 
                        </div>
328
 
 
329
 
                        <div class="bd">
330
 
                            <ul class="examples">
331
 
                                
332
 
                                    
333
 
                                
334
 
                                    
335
 
                                
336
 
                                    
337
 
                                
338
 
                                    
339
 
                                        <li data-description="Shows how to extend the base widget class, to create your own Widgets.">
340
 
                                            <a href="../widget/widget-extend.html">Extending the Base Widget Class</a>
341
 
                                        </li>
342
 
                                    
343
 
                                
344
 
                                    
345
 
                                        <li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event&#x27;s delegation support and mouseenter event, along with the Overlay widget and Node&#x27;s support for the WAI-ARIA Roles and States.">
346
 
                                            <a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
347
 
                                        </li>
348
 
                                    
349
 
                                
350
 
                                    
351
 
                                        <li data-description="Use IO to request data over HTTP.">
352
 
                                            <a href="../io/get.html">HTTP GET to request data</a>
353
 
                                        </li>
354
 
                                    
355
 
                                
356
 
                                    
357
 
                                        <li data-description="Example Photo Browser application.">
358
 
                                            <a href="../dd/photo-browser.html">Photo Browser</a>
359
 
                                        </li>
360
 
                                    
361
 
                                
362
 
                                    
363
 
                                        <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
364
 
                                            <a href="../dd/portal-drag.html">Portal Style Example</a>
365
 
                                        </li>
366
 
                                    
367
 
                                
368
 
                            </ul>
369
 
                        </div>
370
 
                    </div>
371
 
                
372
 
            </div>
373
 
        </div>
374
 
    </div>
375
 
</div>
376
 
 
377
 
<script src="../assets/vendor/prettify/prettify-min.js"></script>
378
 
<script>prettyPrint();</script>
379
 
 
380
 
</body>
381
 
</html>