Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
initial commit
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Photos</title> | ||
<link href="photos.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<h1>Photos</h1> | ||
|
||
<div id="photos"></div> | ||
|
||
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> | ||
<script src="photos.js"></script> | ||
<script> | ||
setupPhotos(['squirrel', 'cat'], function (err, result) { | ||
if (err) { return console.error(err); } | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
body { | ||
font: 16px/1 Helvetica, sans-serif; | ||
} | ||
|
||
|
||
#photos { | ||
float: left; | ||
width: 100%; | ||
} | ||
|
||
.photo { | ||
float: left; | ||
margin: 0 2px 2px 0; | ||
overflow: hidden; | ||
height: 160px; | ||
} | ||
|
||
.photo, | ||
.photo img { | ||
min-width: 160px; | ||
max-width: 240px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*global jQuery*/ | ||
|
||
var setupPhotos = (function ($) { | ||
function each (items, callback) { | ||
var i; | ||
for (i = 0; i < items.length; i += 1) { | ||
setTimeout(callback.bind(this, items[i]), 0); | ||
} | ||
} | ||
|
||
function flatten (items) { | ||
return items.reduce(function (a, b) { | ||
return a.concat(b); | ||
}); | ||
} | ||
|
||
function loadPhotosByTag (tag, max, callback) { | ||
var photos = []; | ||
var callback_name = 'callback_' + Math.floor(Math.random() * 100000); | ||
|
||
window[callback_name] = function (data) { | ||
delete window[callback_name]; | ||
var i; | ||
for (i = 0; i < max; i += 1) { | ||
photos.push(data.items[i].media.m); | ||
} | ||
callback(null, photos); | ||
}; | ||
|
||
$.ajax({ | ||
url: 'http://api.flickr.com/services/feeds/photos_public.gne', | ||
data: { | ||
tags: tag, | ||
lang: 'en-us', | ||
format: 'json', | ||
jsoncallback: callback_name | ||
}, | ||
dataType: 'jsonp' | ||
}); | ||
} | ||
|
||
function loadAllPhotos (tags, max, callback) { | ||
var results = []; | ||
function handleResult (err, photos) { | ||
if (err) { return callback(err); } | ||
|
||
results.push(photos); | ||
if (results.length === tags.length) { | ||
callback(null, flatten(results)); | ||
} | ||
} | ||
|
||
each(tags, function (tag) { | ||
loadPhotosByTag(tag, max, handleResult); | ||
}); | ||
} | ||
|
||
function renderPhoto (photo) { | ||
var img = new Image(); | ||
img.src = photo; | ||
return img; | ||
} | ||
|
||
function imageAppender (id) { | ||
var holder = document.getElementById(id); | ||
return function (img) { | ||
var elm = document.createElement('div'); | ||
elm.className = 'photo'; | ||
elm.appendChild(img); | ||
holder.appendChild(elm); | ||
}; | ||
} | ||
|
||
// ---- | ||
|
||
var max_per_tag = 5; | ||
return function setup (tags, callback) { | ||
loadAllPhotos(tags, max_per_tag, function (err, items) { | ||
if (err) { return callback(err); } | ||
|
||
each(items.map(renderPhoto), imageAppender('photos')); | ||
callback(); | ||
}); | ||
}; | ||
}(jQuery)); |