|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Creating an Arrow Event for DOM Subscription</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: Creating an Arrow Event for DOM Subscription</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#demo { |
|
|
30 |
position: relative; |
|
|
31 |
} |
|
|
32 |
#homebase { |
|
|
33 |
margin-left: 100px; |
|
|
34 |
position: absolute; |
|
|
35 |
left: 0; |
|
|
36 |
top: 40px; |
|
|
37 |
height: 150px; |
|
|
38 |
width: 200px; |
|
|
39 |
} |
|
|
40 |
.robot { |
|
|
41 |
height: 210px; |
|
|
42 |
width: 180px; |
|
|
43 |
position: absolute; |
|
|
44 |
top: 0px; |
|
|
45 |
left: 0; |
|
|
46 |
outline-style: none; |
|
|
47 |
opacity: 0.5; |
|
|
48 |
filter: alpha(opacity=50); |
|
|
49 |
} |
|
|
50 |
.yui3-focused { |
|
|
51 |
opacity: 1; |
|
|
52 |
z-index: 1; |
|
|
53 |
filter: alpha(opacity=100); |
|
|
54 |
} |
|
|
55 |
#B { |
|
|
56 |
left: 125px; |
|
|
57 |
} |
|
|
58 |
#demo input { |
|
|
59 |
margin-left: 4em; |
|
|
60 |
} |
|
|
61 |
#demo label { |
|
|
62 |
font-size: 87%; |
|
|
63 |
color: #555; |
|
|
64 |
} |
|
|
65 |
#detach { |
|
|
66 |
margin-top: 150px; |
|
|
67 |
} |
|
|
68 |
</style> |
|
|
69 |
|
|
|
70 |
<div class="intro"> |
|
|
71 |
<p> |
|
|
72 |
This example will illustrate how to use the synthetic event creation |
|
|
73 |
API. We'll create an <code>arrow</code> event that fires in response |
|
|
74 |
to the user pressing the arrow keys (up, down, left, right) and adds a |
|
|
75 |
<code>direction</code> property to the generated event. |
|
|
76 |
</p> |
|
|
77 |
|
|
|
78 |
<p>Subscribing to this new event will look like this:</p> |
|
|
79 |
<pre class="code prettyprint">node.on("arrow", onArrowHandler);</pre> |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
<p> |
|
|
83 |
Support will also be added for delegation, allowing a single subscriber |
|
|
84 |
from a node higher up the DOM tree, to listen for the new event |
|
|
85 |
emanating from its descendant elements. |
|
|
86 |
</p> |
|
|
87 |
|
|
|
88 |
<pre class="code prettyprint">containerNode.delegate("arrow", onArrowHandler, ".robot");</pre> |
|
|
89 |
|
|
|
90 |
<p> |
|
|
91 |
This example is not applicable to touch devices, since they don't have arrow keys. |
|
|
92 |
</p> |
|
|
93 |
</div> |
|
|
94 |
|
|
|
95 |
<div class="example yui3-skin-sam"> |
|
|
96 |
<div id="demo"> |
|
|
97 |
<p>Step 1. <button type="button" id="attach" tabindex="1">subscribe</button> to the <code>arrow</code> event.<br> |
|
|
98 |
<input type="checkbox" id="delegate" value="1" tabindex="1"> |
|
|
99 |
<label for="delegate">Use a delegated subscription</label></p> |
|
|
100 |
<p>Step 2. Click on a toaster-bot and move it around with the arrow keys.</p> |
|
|
101 |
|
|
|
102 |
<div id="homebase"> |
|
|
103 |
<img id="A" class="robot" tabindex="3" src="../assets/event/toast-8b-left.png" /> |
|
|
104 |
<img id="B" class="robot" tabindex="3" src="../assets/event/toast-8b-right.png" /> |
|
|
105 |
</div> |
|
|
106 |
|
|
|
107 |
<button type="button" id="detach" tabindex="4">Detach subscriptions</button> |
|
|
108 |
</div> |
|
|
109 |
|
|
|
110 |
<script> |
|
|
111 |
YUI({ filter: 'raw' }).use('node', 'event-synthetic', 'transition', function (Y) { |
|
|
112 |
Y.Event.define("arrow", { |
|
|
113 |
// Webkit and IE repeat keydown when you hold down arrow keys. |
|
|
114 |
// Opera links keypress to page scroll; others keydown. |
|
|
115 |
// Firefox prevents page scroll via preventDefault() on either |
|
|
116 |
// keydown or keypress. |
|
|
117 |
_event: (Y.UA.webkit || Y.UA.ie) ? 'keydown' : 'keypress', |
|
|
118 |
|
|
|
119 |
_keys: { |
|
|
120 |
'37': 'left', |
|
|
121 |
'38': 'up', |
|
|
122 |
'39': 'right', |
|
|
123 |
'40': 'down' |
|
|
124 |
}, |
|
|
125 |
|
|
|
126 |
_keyHandler: function (e, notifier) { |
|
|
127 |
if (this._keys[e.keyCode]) { |
|
|
128 |
e.direction = this._keys[e.keyCode]; |
|
|
129 |
notifier.fire(e); |
|
|
130 |
} |
|
|
131 |
}, |
|
|
132 |
|
|
|
133 |
on: function (node, sub, notifier) { |
|
|
134 |
sub._detacher = node.on(this._event, this._keyHandler, |
|
|
135 |
this, notifier); |
|
|
136 |
}, |
|
|
137 |
|
|
|
138 |
detach: function (node, sub, notifier) { |
|
|
139 |
sub._detacher.detach(); |
|
|
140 |
}, |
|
|
141 |
|
|
|
142 |
delegate: function (node, sub, notifier, filter) { |
|
|
143 |
sub._delegateDetacher = node.delegate(this._event, this._keyHandler, |
|
|
144 |
filter, this, notifier); |
|
|
145 |
}, |
|
|
146 |
|
|
|
147 |
detachDelegate: function (node, sub, notifier) { |
|
|
148 |
sub._delegateDetacher.detach(); |
|
|
149 |
} |
|
|
150 |
}); |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
var robotA = Y.one('#A'), |
|
|
154 |
robotB = Y.one('#B'), |
|
|
155 |
subs, |
|
|
156 |
moving = false; |
|
|
157 |
|
|
|
158 |
robotA.setData('x', parseInt(robotA.getStyle('left'), 10)); |
|
|
159 |
robotA.setData('y', parseInt(robotA.getStyle('top'), 10)); |
|
|
160 |
robotB.setData('x', parseInt(robotB.getStyle('left'), 10)); |
|
|
161 |
robotB.setData('y', parseInt(robotB.getStyle('top'), 10)); |
|
|
162 |
|
|
|
163 |
// create variables for image path/filenames |
|
|
164 |
// Use 8bit pngs for IE |
|
|
165 |
var imgBits = (Y.UA.ie && Y.UA.ie < 9) ? '-8b-' : '-24b-', |
|
|
166 |
imgNamePre = '../assets/event/toast' + imgBits, |
|
|
167 |
|
|
|
168 |
imgUp = imgNamePre + 'up.png', |
|
|
169 |
imgDown = imgNamePre + 'down.png', |
|
|
170 |
imgLeft = imgNamePre + 'left.png', |
|
|
171 |
imgRight = imgNamePre + 'right.png'; |
|
|
172 |
|
|
|
173 |
Y.one('#A').setAttribute('src', imgLeft); |
|
|
174 |
Y.one('#B').setAttribute('src', imgRight); |
|
|
175 |
|
|
|
176 |
function move(e) { |
|
|
177 |
// to prevent page scrolling |
|
|
178 |
e.preventDefault(); |
|
|
179 |
if(moving){ |
|
|
180 |
return; // Don't move during a transition (a move) |
|
|
181 |
}else{ |
|
|
182 |
moving = true; // During moving, block other arrow keys from moving |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
var xy = this.getData(), |
|
|
186 |
imgWidth, |
|
|
187 |
imgHeight, |
|
|
188 |
// var scale is used to make the image size and distance moved |
|
|
189 |
// proportional to the Y position of the image |
|
|
190 |
scale = (150 + xy.y) / 150, |
|
|
191 |
moveXDistance = 40, |
|
|
192 |
moveYDistance = 20; |
|
|
193 |
|
|
|
194 |
switch (e.direction) { |
|
|
195 |
case 'up': |
|
|
196 |
if(xy.y < -100){ |
|
|
197 |
moving = false; |
|
|
198 |
return; // Stop moving when image gets too small |
|
|
199 |
} |
|
|
200 |
xy.y -= Math.round(moveYDistance * scale); |
|
|
201 |
e.target.setAttribute('src', imgUp); |
|
|
202 |
break; |
|
|
203 |
case 'down': |
|
|
204 |
if(xy.y > 90){ |
|
|
205 |
moving = false; |
|
|
206 |
return; // Stop moving when image gets too big |
|
|
207 |
} |
|
|
208 |
xy.y += Math.round(moveYDistance * scale); |
|
|
209 |
e.target.setAttribute('src', imgDown); |
|
|
210 |
break; |
|
|
211 |
case 'left': |
|
|
212 |
xy.x -= Math.round(moveXDistance * scale); |
|
|
213 |
e.target.setAttribute('src', imgLeft); |
|
|
214 |
break; |
|
|
215 |
case 'right': |
|
|
216 |
xy.x += Math.round(moveXDistance * scale); |
|
|
217 |
e.target.setAttribute('src', imgRight); |
|
|
218 |
break; |
|
|
219 |
} |
|
|
220 |
scale = 150 / (150 - xy.y); // calculate scale with new Y dimentions |
|
|
221 |
imgWidth = Math.round(scale * 180) + 'px'; |
|
|
222 |
imgHeight = Math.round(scale * 210) + 'px'; |
|
|
223 |
this.transition({ |
|
|
224 |
top : (xy.y + 'px'), |
|
|
225 |
left: (xy.x + 'px'), |
|
|
226 |
width: imgWidth, |
|
|
227 |
height: imgHeight, |
|
|
228 |
duration: .8 |
|
|
229 |
}, function(){ |
|
|
230 |
moving = false; // now that move is done, allow arrow keys to move again |
|
|
231 |
}); |
|
|
232 |
|
|
|
233 |
this.setData('x', xy.x); |
|
|
234 |
this.setData('y', xy.y); |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
function detachSubs() { |
|
|
238 |
if (subs) { |
|
|
239 |
subs.detach(); |
|
|
240 |
subs = null; |
|
|
241 |
Y.all('.robot').removeClass('yui3-focused'); |
|
|
242 |
} |
|
|
243 |
} |
|
|
244 |
|
|
|
245 |
Y.one("#attach").on("click", function (e) { |
|
|
246 |
detachSubs(); |
|
|
247 |
|
|
|
248 |
if (Y.one("#delegate").get('checked')) { |
|
|
249 |
subs = Y.one('#demo').delegate('arrow', move, '.robot'); |
|
|
250 |
} else { |
|
|
251 |
subs = new Y.EventHandle([ |
|
|
252 |
robotA.on("arrow", move), |
|
|
253 |
robotB.on("arrow", move) |
|
|
254 |
]); |
|
|
255 |
} |
|
|
256 |
}); |
|
|
257 |
|
|
|
258 |
Y.one("#detach").on("click", detachSubs); |
|
|
259 |
Y.all('.robot').on('focus', function(e){ |
|
|
260 |
if (subs) { |
|
|
261 |
Y.all('.robot').removeClass('yui3-focused'); |
|
|
262 |
e.target.addClass('yui3-focused'); |
|
|
263 |
} |
|
|
264 |
}); |
|
|
265 |
|
|
|
266 |
Y.all('.robot').on('click', function(e){ |
|
|
267 |
e.target.focus(); |
|
|
268 |
}); |
|
|
269 |
|
|
|
270 |
}); |
|
|
271 |
</script> |
|
|
272 |
|
|
|
273 |
</div> |
|
|
274 |
|
|
|
275 |
<h2><code>on</code>, <code>fire</code>, and <code>detach</code></h2> |
|
|
276 |
|
|
|
277 |
<p> |
|
|
278 |
The three interesting moments in the lifecycle of a DOM event subscription |
|
|
279 |
are |
|
|
280 |
</p> |
|
|
281 |
|
|
|
282 |
<ol> |
|
|
283 |
<li>The event is subscribed to</li> |
|
|
284 |
<li>The event is fired</li> |
|
|
285 |
<li>The event is unsubscribed from</li> |
|
|
286 |
</ol> |
|
|
287 |
|
|
|
288 |
<p> |
|
|
289 |
Create a new synthetic DOM event with `Y.Event.define( <em>name</em>, |
|
|
290 |
<em>config</em> )`. Define the implementation logic for the |
|
|
291 |
<code>on</code> and <code>detach</code> moments in the configuration. |
|
|
292 |
Typically the condition triggering the event firing is set up in the |
|
|
293 |
<code>on</code> phase. |
|
|
294 |
</p> |
|
|
295 |
|
|
|
296 |
<pre class="code prettyprint">Y.Event.define("arrow", { |
|
|
297 |
on: function (node, sub, notifier) { |
|
|
298 |
// what happens when a subscription is made |
|
|
299 |
|
|
|
300 |
// if (condition) { |
|
|
301 |
notifier.fire(); // subscribers executed |
|
|
302 |
// } |
|
|
303 |
}, |
|
|
304 |
|
|
|
305 |
detach: function (node, sub, notifier) { |
|
|
306 |
// what happens when a subscription is removed |
|
|
307 |
} |
|
|
308 |
});</pre> |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
<p> |
|
|
312 |
In the case of arrow handling, the trigger is simply a key event with a |
|
|
313 |
<code>keyCode</code> between 37 and 40. There are a few browser quirks with arrow |
|
|
314 |
handling that warrant listening to <code>keydown</code> for some browsers and |
|
|
315 |
<code>keypress</code> for others, so we'll take care of that transparently for <code>arrow</code> |
|
|
316 |
subscribers. |
|
|
317 |
</p> |
|
|
318 |
|
|
|
319 |
<pre class="code prettyprint">Y.Event.define("arrow", { |
|
|
320 |
on: function (node, sub, notifier) { |
|
|
321 |
var directions = { |
|
|
322 |
37: 'left', |
|
|
323 |
38: 'up', |
|
|
324 |
39: 'right', |
|
|
325 |
40: 'down' |
|
|
326 |
}; |
|
|
327 |
|
|
|
328 |
// Webkit and IE repeat keydown when you hold down arrow keys. |
|
|
329 |
// Opera links keypress to page scroll; others keydown. |
|
|
330 |
// Firefox prevents page scroll via preventDefault() on either |
|
|
331 |
// keydown or keypress. |
|
|
332 |
// Bummer to sniff, but can't test the repeating behavior, and a |
|
|
333 |
// feature test for the scrolling would more than double the code size. |
|
|
334 |
var eventName = (Y.UA.webkit || Y.UA.ie) ? 'keydown' : 'keypress'; |
|
|
335 |
|
|
|
336 |
// To make detaching the associated DOM event easy, store the detach |
|
|
337 |
// handle from the DOM subscription on the synthethic subscription |
|
|
338 |
// object. |
|
|
339 |
sub._detacher = node.on(eventName, function (e) { |
|
|
340 |
// Only notify subscribers if one of the arrow keys was pressed |
|
|
341 |
if (directions[e.keyCode]) { |
|
|
342 |
// Add the extra property |
|
|
343 |
e.direction = directions[e.keyCode]; |
|
|
344 |
|
|
|
345 |
// Firing the notifier event executes the arrow subscribers |
|
|
346 |
// Pass along the key event, which will be renamed "arrow" |
|
|
347 |
notifier.fire(e); |
|
|
348 |
} |
|
|
349 |
}); |
|
|
350 |
}, |
|
|
351 |
|
|
|
352 |
detach: function (node, sub, notifier) { |
|
|
353 |
// Detach the key event subscription using the stored detach handle |
|
|
354 |
sub._detacher.detach(); |
|
|
355 |
} |
|
|
356 |
} );</pre> |
|
|
357 |
|
|
|
358 |
|
|
|
359 |
<h2>Add Delegation Support</h2> |
|
|
360 |
<p> |
|
|
361 |
Since the <code>arrow</code> event is simply a filtered <code>keydown</code> or <code>keypress</code> event, |
|
|
362 |
no special handling needs to be done for delegate subscriptions. We will |
|
|
363 |
extract the key event handler and use it for both <code>on("arrow", ...)</code> and |
|
|
364 |
<code>delegate("arrow", ...)</code> subscriptions. |
|
|
365 |
</p> |
|
|
366 |
|
|
|
367 |
<pre class="code prettyprint">Y.Event.define("arrow", { |
|
|
368 |
// Webkit and IE repeat keydown when you hold down arrow keys. |
|
|
369 |
// Opera links keypress to page scroll; others keydown. |
|
|
370 |
// Firefox prevents page scroll via preventDefault() on either |
|
|
371 |
// keydown or keypress. |
|
|
372 |
_event: (Y.UA.webkit || Y.UA.ie) ? 'keydown' : 'keypress', |
|
|
373 |
|
|
|
374 |
_keys: { |
|
|
375 |
'37': 'left', |
|
|
376 |
'38': 'up', |
|
|
377 |
'39': 'right', |
|
|
378 |
'40': 'down' |
|
|
379 |
}, |
|
|
380 |
|
|
|
381 |
_keyHandler: function (e, notifier) { |
|
|
382 |
if (this._keys[e.keyCode]) { |
|
|
383 |
e.direction = this._keys[e.keyCode]; |
|
|
384 |
notifier.fire(e); |
|
|
385 |
} |
|
|
386 |
}, |
|
|
387 |
|
|
|
388 |
on: function (node, sub, notifier) { |
|
|
389 |
// Use the extended subscription signature to set the 'this' object |
|
|
390 |
// in the callback and pass the notifier as a second parameter to |
|
|
391 |
// _keyHandler |
|
|
392 |
sub._detacher = node.on(this._event, this._keyHandler, |
|
|
393 |
this, notifier); |
|
|
394 |
}, |
|
|
395 |
|
|
|
396 |
detach: function (node, sub, notifier) { |
|
|
397 |
sub._detacher.detach(); |
|
|
398 |
}, |
|
|
399 |
|
|
|
400 |
// Note the delegate handler receives a fourth parameter, the filter |
|
|
401 |
// passed (e.g.) container.delegate('click', callback, '.HERE'); |
|
|
402 |
// The filter could be either a string or a function. |
|
|
403 |
delegate: function (node, sub, notifier, filter) { |
|
|
404 |
sub._delegateDetacher = node.delegate(this._event, this._keyHandler, |
|
|
405 |
filter, this, notifier); |
|
|
406 |
}, |
|
|
407 |
|
|
|
408 |
// Delegate uses a separate detach function to facilitate undoing more |
|
|
409 |
// complex wiring created in the delegate logic above. Not needed here. |
|
|
410 |
detachDelegate: function (node, sub, notifier) { |
|
|
411 |
sub._delegateDetacher.detach(); |
|
|
412 |
} |
|
|
413 |
});</pre> |
|
|
414 |
|
|
|
415 |
|
|
|
416 |
<h2>Use it</h2> |
|
|
417 |
<p> |
|
|
418 |
Subscribe to the new event or detach the event as you would any other DOM |
|
|
419 |
event. |
|
|
420 |
</p> |
|
|
421 |
|
|
|
422 |
<pre class="code prettyprint">function move(e) { |
|
|
423 |
// to prevent page scrolling |
|
|
424 |
e.preventDefault(); |
|
|
425 |
|
|
|
426 |
// See full code listing to show the data set up |
|
|
427 |
var xy = this.getData(); |
|
|
428 |
|
|
|
429 |
switch (e.direction) { |
|
|
430 |
case 'up': xy.y -= 10; break; |
|
|
431 |
case 'down': xy.y += 10; break; |
|
|
432 |
case 'left': xy.x -= 10; break; |
|
|
433 |
case 'right': xy.x += 10; break; |
|
|
434 |
} |
|
|
435 |
|
|
|
436 |
this.transition({ |
|
|
437 |
top : (xy.y + 'px'), |
|
|
438 |
left: (xy.x + 'px'), |
|
|
439 |
duration: .2 |
|
|
440 |
}); |
|
|
441 |
} |
|
|
442 |
|
|
|
443 |
// Subscribe using node.on("arrow", ...); |
|
|
444 |
Y.one("#A").on("arrow", move), |
|
|
445 |
Y.one("#B").on("arrow", move) |
|
|
446 |
|
|
|
447 |
// OR using container.delegate("arrow", ...); |
|
|
448 |
subs = Y.one('#demo').delegate('arrow', move, '.robot');</pre> |
|
|
449 |
|
|
|
450 |
|
|
|
451 |
<h2>Bonus Step: to the Gallery!</h2> |
|
|
452 |
<p> |
|
|
453 |
Synthetic events are perfect candidates for Gallery modules. There are a |
|
|
454 |
number already hosted there, and there are plenty of UI interaction |
|
|
455 |
patterns that would benefit from being encapsulated in synthetic |
|
|
456 |
events. |
|
|
457 |
</p> |
|
|
458 |
|
|
|
459 |
<p> |
|
|
460 |
The <code>arrow</code> event in this example is also |
|
|
461 |
<a href="http://yuilibrary.com/gallery/show/event-arrow">in the gallery</a>, |
|
|
462 |
but with additional functionality. Check out |
|
|
463 |
<a href="https://github.com/lsmith/yui3-gallery/blob/master/build/gallery-event-arrow/gallery-event-arrow-debug.js">its source</a> |
|
|
464 |
to see what you can do with synthetic events. |
|
|
465 |
</p> |
|
|
466 |
|
|
|
467 |
<h2>Full Code Listing</h2> |
|
|
468 |
<pre class="code prettyprint"><div id="demo"> |
|
|
469 |
<p>Step 1. <button type="button" id="attach" tabindex="1">subscribe</button> to the <code>arrow</code> event.<br> |
|
|
470 |
<input type="checkbox" id="delegate" value="1" tabindex="1"> |
|
|
471 |
<label for="delegate">Use a delegated subscription</label></p> |
|
|
472 |
<p>Step 2. Click on a toaster-bot and move it around with the arrow keys.</p> |
|
|
473 |
|
|
|
474 |
<div id="homebase"> |
|
|
475 |
<img id="A" class="robot" tabindex="3" src="../assets/event/toast-8b-left.png" /> |
|
|
476 |
<img id="B" class="robot" tabindex="3" src="../assets/event/toast-8b-right.png" /> |
|
|
477 |
</div> |
|
|
478 |
|
|
|
479 |
<button type="button" id="detach" tabindex="4">Detach subscriptions</button> |
|
|
480 |
</div> |
|
|
481 |
|
|
|
482 |
<script> |
|
|
483 |
YUI({ filter: 'raw' }).use('node', 'event-synthetic', 'transition', function (Y) { |
|
|
484 |
Y.Event.define("arrow", { |
|
|
485 |
// Webkit and IE repeat keydown when you hold down arrow keys. |
|
|
486 |
// Opera links keypress to page scroll; others keydown. |
|
|
487 |
// Firefox prevents page scroll via preventDefault() on either |
|
|
488 |
// keydown or keypress. |
|
|
489 |
_event: (Y.UA.webkit || Y.UA.ie) ? 'keydown' : 'keypress', |
|
|
490 |
|
|
|
491 |
_keys: { |
|
|
492 |
'37': 'left', |
|
|
493 |
'38': 'up', |
|
|
494 |
'39': 'right', |
|
|
495 |
'40': 'down' |
|
|
496 |
}, |
|
|
497 |
|
|
|
498 |
_keyHandler: function (e, notifier) { |
|
|
499 |
if (this._keys[e.keyCode]) { |
|
|
500 |
e.direction = this._keys[e.keyCode]; |
|
|
501 |
notifier.fire(e); |
|
|
502 |
} |
|
|
503 |
}, |
|
|
504 |
|
|
|
505 |
on: function (node, sub, notifier) { |
|
|
506 |
sub._detacher = node.on(this._event, this._keyHandler, |
|
|
507 |
this, notifier); |
|
|
508 |
}, |
|
|
509 |
|
|
|
510 |
detach: function (node, sub, notifier) { |
|
|
511 |
sub._detacher.detach(); |
|
|
512 |
}, |
|
|
513 |
|
|
|
514 |
delegate: function (node, sub, notifier, filter) { |
|
|
515 |
sub._delegateDetacher = node.delegate(this._event, this._keyHandler, |
|
|
516 |
filter, this, notifier); |
|
|
517 |
}, |
|
|
518 |
|
|
|
519 |
detachDelegate: function (node, sub, notifier) { |
|
|
520 |
sub._delegateDetacher.detach(); |
|
|
521 |
} |
|
|
522 |
}); |
|
|
523 |
|
|
|
524 |
|
|
|
525 |
var robotA = Y.one('#A'), |
|
|
526 |
robotB = Y.one('#B'), |
|
|
527 |
subs, |
|
|
528 |
moving = false; |
|
|
529 |
|
|
|
530 |
robotA.setData('x', parseInt(robotA.getStyle('left'), 10)); |
|
|
531 |
robotA.setData('y', parseInt(robotA.getStyle('top'), 10)); |
|
|
532 |
robotB.setData('x', parseInt(robotB.getStyle('left'), 10)); |
|
|
533 |
robotB.setData('y', parseInt(robotB.getStyle('top'), 10)); |
|
|
534 |
|
|
|
535 |
// create variables for image path/filenames |
|
|
536 |
// Use 8bit pngs for IE |
|
|
537 |
var imgBits = (Y.UA.ie && Y.UA.ie < 9) ? '-8b-' : '-24b-', |
|
|
538 |
imgNamePre = '../assets/event/toast' + imgBits, |
|
|
539 |
|
|
|
540 |
imgUp = imgNamePre + 'up.png', |
|
|
541 |
imgDown = imgNamePre + 'down.png', |
|
|
542 |
imgLeft = imgNamePre + 'left.png', |
|
|
543 |
imgRight = imgNamePre + 'right.png'; |
|
|
544 |
|
|
|
545 |
Y.one('#A').setAttribute('src', imgLeft); |
|
|
546 |
Y.one('#B').setAttribute('src', imgRight); |
|
|
547 |
|
|
|
548 |
function move(e) { |
|
|
549 |
// to prevent page scrolling |
|
|
550 |
e.preventDefault(); |
|
|
551 |
if(moving){ |
|
|
552 |
return; // Don't move during a transition (a move) |
|
|
553 |
}else{ |
|
|
554 |
moving = true; // During moving, block other arrow keys from moving |
|
|
555 |
} |
|
|
556 |
|
|
|
557 |
var xy = this.getData(), |
|
|
558 |
imgWidth, |
|
|
559 |
imgHeight, |
|
|
560 |
// var scale is used to make the image size and distance moved |
|
|
561 |
// proportional to the Y position of the image |
|
|
562 |
scale = (150 + xy.y) / 150, |
|
|
563 |
moveXDistance = 40, |
|
|
564 |
moveYDistance = 20; |
|
|
565 |
|
|
|
566 |
switch (e.direction) { |
|
|
567 |
case 'up': |
|
|
568 |
if(xy.y < -100){ |
|
|
569 |
moving = false; |
|
|
570 |
return; // Stop moving when image gets too small |
|
|
571 |
} |
|
|
572 |
xy.y -= Math.round(moveYDistance * scale); |
|
|
573 |
e.target.setAttribute('src', imgUp); |
|
|
574 |
break; |
|
|
575 |
case 'down': |
|
|
576 |
if(xy.y > 90){ |
|
|
577 |
moving = false; |
|
|
578 |
return; // Stop moving when image gets too big |
|
|
579 |
} |
|
|
580 |
xy.y += Math.round(moveYDistance * scale); |
|
|
581 |
e.target.setAttribute('src', imgDown); |
|
|
582 |
break; |
|
|
583 |
case 'left': |
|
|
584 |
xy.x -= Math.round(moveXDistance * scale); |
|
|
585 |
e.target.setAttribute('src', imgLeft); |
|
|
586 |
break; |
|
|
587 |
case 'right': |
|
|
588 |
xy.x += Math.round(moveXDistance * scale); |
|
|
589 |
e.target.setAttribute('src', imgRight); |
|
|
590 |
break; |
|
|
591 |
} |
|
|
592 |
scale = 150 / (150 - xy.y); // calculate scale with new Y dimentions |
|
|
593 |
imgWidth = Math.round(scale * 180) + 'px'; |
|
|
594 |
imgHeight = Math.round(scale * 210) + 'px'; |
|
|
595 |
this.transition({ |
|
|
596 |
top : (xy.y + 'px'), |
|
|
597 |
left: (xy.x + 'px'), |
|
|
598 |
width: imgWidth, |
|
|
599 |
height: imgHeight, |
|
|
600 |
duration: .8 |
|
|
601 |
}, function(){ |
|
|
602 |
moving = false; // now that move is done, allow arrow keys to move again |
|
|
603 |
}); |
|
|
604 |
|
|
|
605 |
this.setData('x', xy.x); |
|
|
606 |
this.setData('y', xy.y); |
|
|
607 |
} |
|
|
608 |
|
|
|
609 |
function detachSubs() { |
|
|
610 |
if (subs) { |
|
|
611 |
subs.detach(); |
|
|
612 |
subs = null; |
|
|
613 |
Y.all('.robot').removeClass('yui3-focused'); |
|
|
614 |
} |
|
|
615 |
} |
|
|
616 |
|
|
|
617 |
Y.one("#attach").on("click", function (e) { |
|
|
618 |
detachSubs(); |
|
|
619 |
|
|
|
620 |
if (Y.one("#delegate").get('checked')) { |
|
|
621 |
subs = Y.one('#demo').delegate('arrow', move, '.robot'); |
|
|
622 |
} else { |
|
|
623 |
subs = new Y.EventHandle([ |
|
|
624 |
robotA.on("arrow", move), |
|
|
625 |
robotB.on("arrow", move) |
|
|
626 |
]); |
|
|
627 |
} |
|
|
628 |
}); |
|
|
629 |
|
|
|
630 |
Y.one("#detach").on("click", detachSubs); |
|
|
631 |
Y.all('.robot').on('focus', function(e){ |
|
|
632 |
if (subs) { |
|
|
633 |
Y.all('.robot').removeClass('yui3-focused'); |
|
|
634 |
e.target.addClass('yui3-focused'); |
|
|
635 |
} |
|
|
636 |
}); |
|
|
637 |
|
|
|
638 |
Y.all('.robot').on('click', function(e){ |
|
|
639 |
e.target.focus(); |
|
|
640 |
}); |
|
|
641 |
|
|
|
642 |
}); |
|
|
643 |
</script></pre> |
|
|
644 |
|
|
|
645 |
|
|
|
646 |
</div> |
|
|
647 |
</div> |
|
|
648 |
</div> |
|
|
649 |
|
|
|
650 |
<div class="yui3-u-1-4"> |
|
|
651 |
<div class="sidebar"> |
|
|
652 |
|
|
|
653 |
|
|
|
654 |
|
|
|
655 |
<div class="sidebox"> |
|
|
656 |
<div class="hd"> |
|
|
657 |
<h2 class="no-toc">Examples</h2> |
|
|
658 |
</div> |
|
|
659 |
|
|
|
660 |
<div class="bd"> |
|
|
661 |
<ul class="examples"> |
|
|
662 |
|
|
|
663 |
|
|
|
664 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
665 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
666 |
</li> |
|
|
667 |
|
|
|
668 |
|
|
|
669 |
|
|
|
670 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
671 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
672 |
</li> |
|
|
673 |
|
|
|
674 |
|
|
|
675 |
|
|
|
676 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
677 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
678 |
</li> |
|
|
679 |
|
|
|
680 |
|
|
|
681 |
|
|
|
682 |
|
|
|
683 |
|
|
|
684 |
|
|
|
685 |
|
|
|
686 |
|
|
|
687 |
|
|
|
688 |
|
|
|
689 |
|
|
|
690 |
|
|
|
691 |
</ul> |
|
|
692 |
</div> |
|
|
693 |
</div> |
|
|
694 |
|
|
|
695 |
|
|
|
696 |
|
|
|
697 |
<div class="sidebox"> |
|
|
698 |
<div class="hd"> |
|
|
699 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
700 |
</div> |
|
|
701 |
|
|
|
702 |
<div class="bd"> |
|
|
703 |
<ul class="examples"> |
|
|
704 |
|
|
|
705 |
|
|
|
706 |
|
|
|
707 |
|
|
|
708 |
|
|
|
709 |
|
|
|
710 |
|
|
|
711 |
|
|
|
712 |
<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."> |
|
|
713 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
714 |
</li> |
|
|
715 |
|
|
|
716 |
|
|
|
717 |
|
|
|
718 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
719 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
720 |
</li> |
|
|
721 |
|
|
|
722 |
|
|
|
723 |
|
|
|
724 |
<li data-description="Example Photo Browser application."> |
|
|
725 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
726 |
</li> |
|
|
727 |
|
|
|
728 |
|
|
|
729 |
|
|
|
730 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
731 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
732 |
</li> |
|
|
733 |
|
|
|
734 |
|
|
|
735 |
|
|
|
736 |
<li data-description="Use IO to request data over HTTP."> |
|
|
737 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
738 |
</li> |
|
|
739 |
|
|
|
740 |
|
|
|
741 |
</ul> |
|
|
742 |
</div> |
|
|
743 |
</div> |
|
|
744 |
|
|
|
745 |
</div> |
|
|
746 |
</div> |
|
|
747 |
</div> |
|
|
748 |
</div> |
|
|
749 |
|
|
|
750 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
751 |
<script>prettyPrint();</script> |
|
|
752 |
|
|
|
753 |
<script> |
|
|
754 |
YUI.Env.Tests = { |
|
|
755 |
examples: [], |
|
|
756 |
project: '../assets', |
|
|
757 |
assets: '../assets/event', |
|
|
758 |
name: 'synth-example', |
|
|
759 |
title: 'Creating an Arrow Event for DOM Subscription', |
|
|
760 |
newWindow: '', |
|
|
761 |
auto: false |
|
|
762 |
}; |
|
|
763 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
764 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
765 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
766 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
767 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
768 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
769 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
770 |
YUI.Env.Tests.examples.push('get'); |
|
|
771 |
|
|
|
772 |
</script> |
|
|
773 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
774 |
|
|
|
775 |
|
|
|
776 |
|
|
|
777 |
</body> |
|
|
778 |
</html> |