|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Supporting A Swipe Left Gesture</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Supporting A Swipe Left Gesture</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p> |
|
|
30 |
This example shows how you can support a "swipeleft" gesture, built on |
|
|
31 |
top of the synthetic "gesturemove" events, which work not only on touch |
|
|
32 |
devices, but also on mouse based input devices. |
|
|
33 |
</p> |
|
|
34 |
</div> |
|
|
35 |
|
|
|
36 |
<div class="example newwindow"> |
|
|
37 |
<a href="swipe-example-content.html" target="_blank" class="button"> |
|
|
38 |
View Example in New Window |
|
|
39 |
</a> |
|
|
40 |
</div> |
|
|
41 |
|
|
|
42 |
<h2>Modules Used</h2> |
|
|
43 |
|
|
|
44 |
<p>For the example, the two core modules we'll use are:</p> |
|
|
45 |
|
|
|
46 |
<dl> |
|
|
47 |
<dt>The <code>event-move</code> module</dt> |
|
|
48 |
<dd> |
|
|
49 |
Provides the <code>gesturemovestart</code>, <code>gesturemove</code> and <code>gesturemoveend</code> |
|
|
50 |
low-level gesture events. These events are fired whenever the user |
|
|
51 |
performs a move gesture (mouse button/finger down, mouse/finger move, |
|
|
52 |
mouse button/finger up) with the mouse or their finger. |
|
|
53 |
</dd> |
|
|
54 |
<dt>The <code>transitions</code> module</dt> |
|
|
55 |
<dd> |
|
|
56 |
Provides transitions support, leveraging CSS transitions under the hood |
|
|
57 |
where supported. |
|
|
58 |
</dd> |
|
|
59 |
</dl> |
|
|
60 |
|
|
|
61 |
<p>The YUI use statement for the example is shown below:</p> |
|
|
62 |
|
|
|
63 |
<pre class="code prettyprint">YUI().use("node-base", "node-event-delegate", "transition", "event-move", function(Y) { |
|
|
64 |
... |
|
|
65 |
});</pre> |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
<h2>Delegating Gesture Move Events</h2> |
|
|
69 |
|
|
|
70 |
<p> |
|
|
71 |
The basic idea for the example is to listen for a <code>gesturemovestart</code> on a |
|
|
72 |
list item, and when we get one, store its position, and then listen for a |
|
|
73 |
gesturemoveend. If the gesturemoveend occurs more than X pixels to the left |
|
|
74 |
of the start, then we've identified a "swipeleft" gesture. Future versions |
|
|
75 |
of the library will package such logic into a higher level gesture event |
|
|
76 |
(ala event-flick). |
|
|
77 |
</p> |
|
|
78 |
|
|
|
79 |
<p> |
|
|
80 |
For this example, since we're dealing with a list of items, rather than |
|
|
81 |
attach individual listeners to each <li> in the list, we use |
|
|
82 |
<code>delegate</code> on the parent <ul> element, which leads to better |
|
|
83 |
performance and avoids having to add/remove listeners each time the |
|
|
84 |
contents of the list change. The <code>gesturemovestart</code>, <code>gesturemove</code> and |
|
|
85 |
<code>gesturemoveend</code> are synthetic events, and can all be used with <code>delegate</code>, |
|
|
86 |
just like any DOM event. |
|
|
87 |
</p> |
|
|
88 |
|
|
|
89 |
<p> |
|
|
90 |
We set up a delegate listener on the <ul> which listens for the |
|
|
91 |
<code>gesturemovestart</code> event (<code>gesturemovestart</code> abstracts |
|
|
92 |
<code>mousedown</code>/<code>touchstart</code> events under the hood). The delegate listener is |
|
|
93 |
set up to be notified when the target of the <code>gesturemovestart</code> is an |
|
|
94 |
<code><li></code> |
|
|
95 |
</p> |
|
|
96 |
|
|
|
97 |
<pre class="code prettyprint">Y.Node.one("#swipe").delegate("gesturemovestart", function(e) { |
|
|
98 |
|
|
|
99 |
... |
|
|
100 |
|
|
|
101 |
}, "li", { // filter for "li" |
|
|
102 |
preventDefault:true |
|
|
103 |
});</pre> |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
<p> |
|
|
107 |
The <code>gesturemovestart</code> event supports a configuration object passed as an |
|
|
108 |
additional subscription argument, which can be used to set minimum distance |
|
|
109 |
and minimum time thresholds at which to fire the start event. The |
|
|
110 |
configuration also supports the ability to prevent the default behavior |
|
|
111 |
before the minimum time or distance thresholds kick in, which is what we do |
|
|
112 |
above by passing <code>preventDefault:true</code>. |
|
|
113 |
</p> |
|
|
114 |
|
|
|
115 |
<p> |
|
|
116 |
The advantage of the gesture synthetic events is that the developer can use |
|
|
117 |
the same API without having to worry about whether or not the browser |
|
|
118 |
platform is touch based or mouse based. |
|
|
119 |
</p> |
|
|
120 |
|
|
|
121 |
<h2>Gesture Move End</h2> |
|
|
122 |
|
|
|
123 |
<p> |
|
|
124 |
As mentioned above, the <code>gesturemovestart</code> listener is notified whenever a |
|
|
125 |
<code>mousedown</code> or <code>touchstart</code> is received on a list item. The listener has |
|
|
126 |
access to: |
|
|
127 |
</p> |
|
|
128 |
|
|
|
129 |
<dl> |
|
|
130 |
<dt><code>e.currentTarget</code></dt> |
|
|
131 |
<dd>The list item targeted.</dd> |
|
|
132 |
<dt><code>e.target</code></dt> |
|
|
133 |
<dd> |
|
|
134 |
The element clicked on (it may be an element inside the targeted |
|
|
135 |
list item, the span for example). |
|
|
136 |
</dd> |
|
|
137 |
<dt><code>e.container</code></dt> |
|
|
138 |
<dd> |
|
|
139 |
The element to which the delegate listener is attached (the ul in |
|
|
140 |
this case). |
|
|
141 |
</dd> |
|
|
142 |
</dl> |
|
|
143 |
|
|
|
144 |
<p> |
|
|
145 |
The event facade also has the page coordinates for the <code>mousedown</code> or |
|
|
146 |
<code>touchstart</code> event. We use the list item's <code>setData</code> method, to store the |
|
|
147 |
<code>pageX</code> position for the start event, so we can compare it when we get the |
|
|
148 |
<code>gesturemoveend</code> event. This way it's stored on the node instance, and we |
|
|
149 |
don't need to pass it along separately to the <code>gesturemoveend</code> event, or |
|
|
150 |
store it globally. |
|
|
151 |
</p> |
|
|
152 |
|
|
|
153 |
<p> |
|
|
154 |
<code>getData</code>, <code>setData</code> and <code>clearData</code> are useful methods to store ad-hoc |
|
|
155 |
node centric data. |
|
|
156 |
</p> |
|
|
157 |
|
|
|
158 |
<pre class="code prettyprint">Y.Node.one("#swipe").delegate("gesturemovestart", function(e) { |
|
|
159 |
|
|
|
160 |
var item = e.currentTarget, |
|
|
161 |
target = e.target, |
|
|
162 |
container = e.container, |
|
|
163 |
|
|
|
164 |
... |
|
|
165 |
|
|
|
166 |
item.setData("swipeStart", e.pageX); |
|
|
167 |
|
|
|
168 |
item.once("gesturemoveend", function(e) { |
|
|
169 |
|
|
|
170 |
var swipeStart = item.getData("swipeStart"), |
|
|
171 |
swipeEnd = e.pageX, |
|
|
172 |
isSwipeLeft = (swipeStart - swipeEnd) > MIN_SWIPE; |
|
|
173 |
|
|
|
174 |
if (isSwipeLeft) { |
|
|
175 |
item.one(".myapp-delete").removeClass("myapp-hidden"); |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
}); |
|
|
179 |
|
|
|
180 |
... |
|
|
181 |
|
|
|
182 |
});</pre> |
|
|
183 |
|
|
|
184 |
|
|
|
185 |
<p> |
|
|
186 |
When we get the <code>gesturemovestart</code> event, we set up a listener for the |
|
|
187 |
<code>gesturemoveend</code> event, so we can determine the end of the gesture, and |
|
|
188 |
figure out if the user swiped left. Since we don't want to set up a new |
|
|
189 |
listener every time we get a <code>gesturemovestart</code> we use <code>once</code> to set up the |
|
|
190 |
<code>gesturemoveend</code> listener. <code>once</code> will detach the listener after it's been |
|
|
191 |
invoked. Again, since <code>gesturemoveend</code> is a synthetic event, it works with |
|
|
192 |
<code>once</code> just like any other DOM event. |
|
|
193 |
</p> |
|
|
194 |
|
|
|
195 |
<p> |
|
|
196 |
In the <code>gesturemoveend</code> listener we check the page position of the event, |
|
|
197 |
and if it's far enough to the left of the start position, we display the |
|
|
198 |
"Delete" button by removing the hidden class which it contains. |
|
|
199 |
</p> |
|
|
200 |
|
|
|
201 |
<h2>Transitions</h2> |
|
|
202 |
|
|
|
203 |
<p> |
|
|
204 |
To hide and remove the item when the delete button is pressed, we use the |
|
|
205 |
<code>transition</code> method, to animate its opacity and height down to 0. Under the |
|
|
206 |
hood transition will use CSS transition support where available (WebKit) |
|
|
207 |
and set up timer based animation where not (IE). As with the gesture event |
|
|
208 |
support, the developer gets to use the same API without having to worry |
|
|
209 |
about the browser environment. |
|
|
210 |
</p> |
|
|
211 |
|
|
|
212 |
<pre class="code prettyprint">item = target.get("parentNode"); |
|
|
213 |
|
|
|
214 |
item.transition({ |
|
|
215 |
duration:0.3, |
|
|
216 |
opacity:0, |
|
|
217 |
height:0 |
|
|
218 |
}, function() { |
|
|
219 |
this.remove(); |
|
|
220 |
});</pre> |
|
|
221 |
|
|
|
222 |
|
|
|
223 |
<p> |
|
|
224 |
The second argument to transition above is a callback function, which is |
|
|
225 |
invoked when the transition is complete. We use this support to remove the |
|
|
226 |
item from the DOM. |
|
|
227 |
</p> |
|
|
228 |
|
|
|
229 |
<h2>Full Code Listing</h2> |
|
|
230 |
|
|
|
231 |
<pre class="code prettyprint">YUI().use('node-base','node-event-delegate', 'transition', 'event-move', function (Y) { |
|
|
232 |
|
|
|
233 |
var MIN_SWIPE = 10; |
|
|
234 |
|
|
|
235 |
Y.all(".myexample-hidden").removeClass("myexample-hidden"); |
|
|
236 |
|
|
|
237 |
Y.one("#swipe").delegate("gesturemovestart", function(e) { |
|
|
238 |
|
|
|
239 |
var item = e.currentTarget, |
|
|
240 |
target = e.target, |
|
|
241 |
container = e.container, |
|
|
242 |
isDeleteButton = target.hasClass("myapp-delete"); |
|
|
243 |
|
|
|
244 |
// Prevent Text Selection in IE |
|
|
245 |
item.once("selectstart", function(e) { |
|
|
246 |
e.preventDefault(); |
|
|
247 |
}); |
|
|
248 |
|
|
|
249 |
if (!isDeleteButton) { |
|
|
250 |
|
|
|
251 |
container.all(".myapp-delete").addClass("myapp-hidden"); |
|
|
252 |
|
|
|
253 |
item.setData("swipeStart", e.pageX); |
|
|
254 |
|
|
|
255 |
item.once("gesturemoveend", function(e) { |
|
|
256 |
|
|
|
257 |
var swipeStart = item.getData("swipeStart"), |
|
|
258 |
swipeEnd = e.pageX, |
|
|
259 |
isSwipeLeft = (swipeStart - swipeEnd) > MIN_SWIPE; |
|
|
260 |
|
|
|
261 |
if (isSwipeLeft) { |
|
|
262 |
item.one(".myapp-delete").removeClass("myapp-hidden"); |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
}); |
|
|
266 |
} else { |
|
|
267 |
item = target.get("parentNode"); |
|
|
268 |
|
|
|
269 |
if (item.get("id") != "kitkat" || confirm("Seriously? The KitKats?")) { |
|
|
270 |
item.transition({ |
|
|
271 |
duration:0.3, |
|
|
272 |
opacity:0, |
|
|
273 |
height:0 |
|
|
274 |
}, function() { |
|
|
275 |
this.remove(); |
|
|
276 |
}); |
|
|
277 |
} |
|
|
278 |
} |
|
|
279 |
|
|
|
280 |
}, "li", { |
|
|
281 |
preventDefault:true |
|
|
282 |
}); |
|
|
283 |
});</pre> |
|
|
284 |
|
|
|
285 |
</div> |
|
|
286 |
</div> |
|
|
287 |
</div> |
|
|
288 |
|
|
|
289 |
<div class="yui3-u-1-4"> |
|
|
290 |
<div class="sidebar"> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<div class="sidebox"> |
|
|
295 |
<div class="hd"> |
|
|
296 |
<h2 class="no-toc">Examples</h2> |
|
|
297 |
</div> |
|
|
298 |
|
|
|
299 |
<div class="bd"> |
|
|
300 |
<ul class="examples"> |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
304 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
305 |
</li> |
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
310 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
311 |
</li> |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
316 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
317 |
</li> |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
</ul> |
|
|
331 |
</div> |
|
|
332 |
</div> |
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
<div class="sidebox"> |
|
|
337 |
<div class="hd"> |
|
|
338 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
339 |
</div> |
|
|
340 |
|
|
|
341 |
<div class="bd"> |
|
|
342 |
<ul class="examples"> |
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
352 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
353 |
</li> |
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
358 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
359 |
</li> |
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
<li data-description="Example Photo Browser application."> |
|
|
364 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
365 |
</li> |
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
370 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
371 |
</li> |
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
<li data-description="Use IO to request data over HTTP."> |
|
|
376 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
377 |
</li> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
</ul> |
|
|
381 |
</div> |
|
|
382 |
</div> |
|
|
383 |
|
|
|
384 |
</div> |
|
|
385 |
</div> |
|
|
386 |
</div> |
|
|
387 |
</div> |
|
|
388 |
|
|
|
389 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
390 |
<script>prettyPrint();</script> |
|
|
391 |
|
|
|
392 |
<script> |
|
|
393 |
YUI.Env.Tests = { |
|
|
394 |
examples: [], |
|
|
395 |
project: '../assets', |
|
|
396 |
assets: '../assets/event', |
|
|
397 |
name: 'swipe-example', |
|
|
398 |
title: 'Supporting A Swipe Left Gesture', |
|
|
399 |
newWindow: 'true', |
|
|
400 |
auto: false |
|
|
401 |
}; |
|
|
402 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
403 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
404 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
405 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
406 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
407 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
408 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
409 |
YUI.Env.Tests.examples.push('get'); |
|
|
410 |
|
|
|
411 |
</script> |
|
|
412 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
413 |
|
|
|
414 |
|
|
|
415 |
|
|
|
416 |
</body> |
|
|
417 |
</html> |