Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
joshwnj committed Sep 3, 2012
1 parent c281d96 commit c016258
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
20 changes: 20 additions & 0 deletions index.html
@@ -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>
22 changes: 22 additions & 0 deletions photos.css
@@ -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;
}
85 changes: 85 additions & 0 deletions photos.js
@@ -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));

0 comments on commit c016258

Please sign in to comment.