5
<title>Example: Photo Browser</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>
15
<h1>Example: Photo Browser</h1>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
25
<p>This example uses DD and <a href="http://developer.yahoo.com/yql/">YQL</a> to build a Photo Browser application. This example was part of the YUI 3 presentation by <a href="http://twiiter.com/davglass">@davglass</a> at <a href="http://openhacklondon.pbworks.com/">Open Hack : London</a></p>
28
<div class="example newwindow">
29
<a href="photo-browser-example.html" target="_blank" class="button">
30
View Example in New Window
35
<h3 id="drag-and-drop">Drag and Drop</h3>
36
<p>In this example, Drag and Drop is heavily customized by using "event bubbling" and "custom proxies".</p>
37
<p>When you see <code>Y.DD.DDM.on</code> in the code, you are seeing the built-in "event bubbling".</p>
38
<p>The DD <code>dragNode</code> is the proxy node, we add some styles to it allowing it to look the way we want.</p>
41
<p>Here is the Flickr YQL query used in this example.</p>
43
<pre class="code prettyprint">SELECT * FROM flickr.photos.search(100) WHERE
44
(text="yuiconf")
46
AND (media = "photos")
47
AND (api_key = "1895311ec0d2e23431a6407f3e8dffcc")</pre>
49
<p><em>Note: You need to get your own API key, please do not use ours.</em></p>
51
<h3 id="slider-and-stylesheet">Slider and StyleSheet</h3>
52
<p>In this example, we will use the Slider control to dynamically manipulate a CSS Style Rule.</p>
53
<p>First, we need to create the slider and render it.</p>
55
<pre class="code prettyprint">//Create and render the slider
56
var sl = new Y.Slider({
57
length: '200px', value: 40, max: 70, min: 5
58
}).render('.horiz_slider');</pre>
61
<p>Now, we listen for the Slider's <code>valueChange</code> event. This event is fired when the value of the Slider has changed.</p>
62
<p>Next we use the StyleSheet utility to dynamically change a style rule to resize the images.
63
The style rule that we want to change is <code>#yui-main .yui-g ul li</code>. When the Slider's value changes, we will take the value and divide it by 2, then use that as the percentage width of the li.
64
This will give us the effect we want (resizing images) without touching all the images via the DOM.
67
<pre class="code prettyprint">//Listen for the change
68
sl.after('valueChange',function (e) {
69
//Insert a dynamic stylesheet rule:
70
var sheet = new Y.StyleSheet('image_slider');
71
sheet.set('#yui-main .yui-g ul li', {
72
width: (e.newVal / 2) + '%'
77
<h3 id="event-delegation">Event Delegation</h3>
78
<p>This listener listens for all <code>mouseup</code> events on the <code>document</code> and it will only fire when the target element matches the <code>*</code> selector (which should be all elements).</p>
79
<p>This way we can remove all the <code>selected</code> CSS classes from all the images in the browser when a <code>mouseup</code> occurs, only if the shift key was not pressed. We can then check to determine if the mouseup came from one of the images. If it has, add the selected class back to it.</p>
81
<pre class="code prettyprint">//Listen for all mouseups on the document (selecting/deselecting images)
82
Y.delegate('mouseup' , function(e) {
84
//No shift key - remove all selected images
85
wrapper.all('img.selected').removeClass('selected');
87
//Check if the target is an image and select it.
88
if (e.target.test('#yui-main .yui-g ul li img')) {
89
e.target.addClass('selected');
91
}, document, '*');</pre>
94
<p>This listener, listens for all <code>click</code> events on the album list <code>#photoList li</code>.
95
First, it stops the click, so the href is not followed. Next, it removes all the <code>selected</code> classes from the list. Then, it adds the <code>selected</code> class to the item that was clicked on.</p>
96
<p>After that UI setup, it uses Selectors to change the view of the images in the browser.
97
First, it checks if we are viewing "all" or a "sub album". If all is selected, it removes the <code>hidden</code> class from all the images.
98
If it was an album, it adds the <code>hidden</code> class to all the images, then selects all the images with the class of its <code>id</code>, then it removes the hidden class from them.
100
<p>Basically, it hides all the images, then determines the ones it needs to show and removes the <code>hidden</code> class from them.</p>
102
<pre class="code prettyprint">//Listen for all clicks on the '#photoList li' selector
103
Y.delegate('click', function(e) {
104
//Prevent the click
106
//Remove all the selected items
107
e.currentTarget.get('parentNode').all('li.selected').removeClass('selected');
108
//Add the selected class to the one that one clicked
109
e.currentTarget.addClass('selected');
110
//The "All Photos" link was clicked
111
if (e.currentTarget.hasClass('all')) {
112
//Remove all the hidden classes
113
wrapper.all('li').removeClass('hidden');
115
//Another "album" was clicked, get its id
116
var c = e.target.get('id');
117
//Hide all items by adding the hidden class
118
wrapper.all('li').addClass('hidden');
119
//Now, find all the items with the class name the same as the album id
120
//and remove the hidden class
121
wrapper.all('li.' + c).removeClass('hidden');
123
}, document, '#photoList li');</pre>
127
<h3 id="full-source">Full Source</h3>
128
<p>Here is the full commented JavaScript source for this example.</p>
130
<pre class="code prettyprint">YUI().use('yql', 'node', 'anim', 'dd', 'dd-plugin', 'dd-drop-plugin', 'slider', 'stylesheet', 'event-delegate', function(Y) {
131
//Get a reference to the wrapper to use later and add a loading class to it.
132
var wrapper = Y.one('#yui-main .yui-g ul').addClass('loading');
133
//Set it's height to the height of the viewport so we can scroll it.
134
wrapper.setStyle('height', (wrapper.get('winHeight') - 50 )+ 'px');
135
Y.on('windowresize', function() { wrapper.setStyle('height', (wrapper.get('winHeight') - 50 )+ 'px'); });
136
//Make the YQL query.
137
Y.YQL('SELECT * FROM flickr.photos.search(100) WHERE (text="yuiconf") AND (safe_search = 1) AND (media = "photos") AND (api_key = "1895311ec0d2e23431a6407f3e8dffcc")', function(e) {
138
if (e.query && e.query.results) {
139
var photos = e.query.results.photo;
140
//Walk the returned photos array
141
Y.each(photos, function(v, k) {
142
//Create our URL
143
var url = 'http:/'+'/static.flickr.com/' + v.server + '/' + v.id + '_' + v.secret + '_m.jpg',
144
//Create the image and the LI
145
li = Y.Node.create('<li class="loading"><img src="' + url + '" title="' + v.title + '"></li>'),
146
//Get the image from the LI
147
img = li.get('firstChild');
148
//Append the li to the wrapper
149
wrapper.appendChild(li);
150
//This little hack moves the tall images to the bottom of the list
151
//So they float better ;)
152
img.on('load', function() {
153
//Is the height longer than the width?
154
var c = ((this.get('height') > this.get('width')) ? 'tall' : 'wide');
156
if (c === 'tall') {
157
//Move it to the end of the list.
158
this.get('parentNode.parentNode').removeChild(this.get('parentNode'));
159
wrapper.appendChild(this.get('parentNode'));
161
this.get('parentNode').removeClass('loading');
164
//Get all the newly added li's
165
wrapper.all('li').each(function(node) {
166
//Plugin the Drag plugin
167
this.plug(Y.Plugin.Drag, {
170
//Plug the Proxy into the DD object
171
this.dd.plug(Y.Plugin.DDProxy, {
174
borderStyle: 'none'
177
//Create and render the slider
178
var sl = new Y.Slider({
179
length: '200px', value: 40, max: 70, min: 5
180
}).render('.horiz_slider');
181
//Listen for the change
182
sl.after('valueChange',function (e) {
183
//Insert a dynamic stylesheet rule:
184
var sheet = new Y.StyleSheet('image_slider');
185
sheet.set('#yui-main .yui-g ul li', {
186
width: (e.newVal / 2) + '%'
189
//Remove the DDM as a bubble target..
190
sl._dd.removeTarget(Y.DD.DDM);
191
//Remove the wrappers loading class
192
wrapper.removeClass('loading');
193
Y.one('#ft').removeClass('loading');
196
//Listen for all mouseup's on the document (selecting/deselecting images)
197
Y.delegate('mouseup', function(e) {
199
//No shift key - remove all selected images
200
wrapper.all('img.selected').removeClass('selected');
202
//Check if the target is an image and select it.
203
if (e.target.test('#yui-main .yui-g ul li img')) {
204
e.target.addClass('selected');
206
}, document, '*');
208
//Listen for all clicks on the '#photoList li' selector
209
Y.delegate('click', function(e) {
210
//Prevent the click
212
//Remove all the selected items
213
e.currentTarget.get('parentNode').all('li.selected').removeClass('selected');
214
//Add the selected class to the one that one clicked
215
e.currentTarget.addClass('selected');
216
//The "All Photos" link was clicked
217
if (e.currentTarget.hasClass('all')) {
218
//Remove all the hidden classes
219
wrapper.all('li').removeClass('hidden');
221
//Another "album" was clicked, get it's id
222
var c = e.currentTarget.get('id');
223
//Hide all items by adding the hidden class
224
wrapper.all('li').addClass('hidden');
225
//Now, find all the items with the class name the same as the album id
226
//and remove the hidden class
227
wrapper.all('li.' + c).removeClass('hidden');
229
}, document, '#photoList li');
231
//Stop the drag with the escape key
232
Y.one(document).on('keyup', function(e) {
233
//The escape key was pressed
234
if ((e.keyCode === 27) || (e.charCode === 27)) {
235
//We have an active Drag
236
if (Y.DD.DDM.activeDrag) {
237
//Stop the drag
238
Y.DD.DDM.activeDrag.stopDrag();
242
//On the drag:mouseDown add the selected class
243
Y.DD.DDM.on('drag:mouseDown', function(e) {
244
var img = e.target.get('node').one('img');
245
//If it's a gesture event, then we need to act differently
246
if (Y.DD.Drag.START_EVENT.indexOf('gesture') === 0) {
247
if (img.hasClass('selected')) {
248
img.removeClass('selected');
250
img.addClass('selected');
253
img.removeClass('selected');
256
//On drag start, get all the selected elements
257
//Add the count to the proxy element and offset it to the cursor.
258
Y.DD.DDM.on('drag:start', function(e) {
259
var img = e.target.get('node').one('img').addClass('selected');
260
//How many items are selected
261
var count = wrapper.all('img.selected').size();
262
//Set the style on the proxy node
263
e.target.get('dragNode').setStyles({
264
height: '25px', width: '25px'
265
}).set('innerHTML', '<span>' + count + '</span>');
266
//Offset the dragNode
267
e.target.deltaXY = [25, 5];
269
//We dropped on a drop target
270
Y.DD.DDM.on('drag:drophit', function(e) {
271
//get the images that are selected.
272
var imgs = wrapper.all('img.selected'),
273
//The xy position of the item we dropped on
274
toXY = e.drop.get('node').getXY();
276
imgs.each(function(node) {
277
//Clone the image, position it on top of the original and animate it to the drop target
278
node.get('parentNode').addClass(e.drop.get('node').get('id'));
279
var n = node.cloneNode().set('id', '').setStyle('position', 'absolute');
280
Y.one('body').appendChild(n);
281
n.setXY(node.getXY());
285
height: 20, width: 20, opacity: 0,
286
top: toXY[1], left: toXY[0]
289
width: node.get('offsetWidth'),
290
height: node.get('offsetHeight')
295
//Update the count
296
var count = wrapper.all('li.' + e.drop.get('node').get('id')).size();
297
e.drop.get('node').one('span').set('innerHTML', '(' + count + ')');
299
//Add drop support to the albums
300
Y.all('#photoList li').each(function(node) {
301
if (!node.hasClass('all')) {
302
//make all albums Drop Targets except the all photos.
303
node.plug(Y.Plugin.Drop);
312
<div class="yui3-u-1-4">
313
<div class="sidebar">
315
<div id="toc" class="sidebox">
317
<h2 class="no-toc">Table of Contents</h2>
323
<a href="#drag-and-drop">Drag and Drop</a>
326
<a href="#yql">YQL</a>
329
<a href="#slider-and-stylesheet">Slider and StyleSheet</a>
332
<a href="#event-delegation">Event Delegation</a>
335
<a href="#full-source">Full Source</a>
343
<div class="sidebox">
345
<h2 class="no-toc">Examples</h2>
349
<ul class="examples">
352
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
353
<a href="simple-drag.html">Simple Drag</a>
358
<li data-description="How to apply the Drag Plugin to a node.">
359
<a href="drag-plugin.html">Drag - Node plugin</a>
364
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
365
<a href="proxy-drag.html">Drag - Proxy</a>
370
<li data-description="How to constrain a draggable Node to another Node's region.">
371
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
376
<li data-description="Using interaction groups, this example demonstrates how to tie into the Drag & Drop Utility's interesting moments to provide visual affordances for the current drag operation.">
377
<a href="groups-drag.html">Drag - Interaction Groups</a>
382
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
383
<a href="shim-drag.html">Using the Drag Shim</a>
388
<li data-description="How to use the Drop Target events to code your application.">
389
<a href="drop-code.html">Using Drop Based Coding</a>
394
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
395
<a href="winscroll.html">Window Scrolling</a>
400
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
401
<a href="delegate.html">Drag Delegation</a>
406
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
407
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
412
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
413
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
418
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
419
<a href="list-drag.html">List Reorder w/Bubbling</a>
424
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
425
<a href="scroll-list.html">List Reorder w/Scrolling</a>
430
<li data-description="How to make an animated node a Drop target.">
431
<a href="anim-drop.html">Animated Drop Targets</a>
436
<li data-description="Example Photo Browser application.">
437
<a href="photo-browser.html">Photo Browser</a>
442
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
443
<a href="portal-drag.html">Portal Style Example</a>
457
<div class="sidebox">
459
<h2 class="no-toc">Examples That Use This Component</h2>
463
<ul class="examples">
498
<li data-description="Working with multiple YUI instances.">
499
<a href="../yui/yui-multi.html">Multiple Instances</a>
504
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
505
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
518
<script src="../assets/vendor/prettify/prettify-min.js"></script>
519
<script>prettyPrint();</script>