Skip to content

Commit

Permalink
catch up with master
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGermuska committed Aug 21, 2015
2 parents f7ef3a4 + c2e25c9 commit 294c21c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
53 changes: 38 additions & 15 deletions application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(document).ready(function() {
});

function playPuppies(puppies) {
var lastPuppy;
var puppies_seen = [];
var keycodes = {
spacebar: 32,
left_arrow: 37,
Expand All @@ -15,45 +15,68 @@ function playPuppies(puppies) {
return puppies[Math.floor(Math.random() * puppyCount)];
}

function makeURL(newPup) {
return 'http://i.imgur.com/' + newPup + '.mp4';
}

function newPuppy() {

if (puppies_seen.length == puppies.length) {
console.log("Ran out of puppies. What should I do?");
var current_pup = puppies_seen[puppies_seen.length - 1];
puppies_seen = [current_pup]; // for now reset but don't repeat what we have
}

var newPup = pickUpPuppy();
while (lastPuppy == newPup) {
var newPup = pickUpPuppy();

while (puppies_seen.indexOf(newPup) != -1) {
newPup = pickUpPuppy();
}

lastPuppy = newPup;
return newPup;
}
puppies_seen.push(newPup);
return newPup;

function makeURL(newPup) {
return 'http://i.imgur.com/' + newPup + '.mp4';
}

function loadPuppy(newPup) {
function showPuppy(newPup) {
var url = makeURL(newPup);
$('#puppy > source').attr('src', url);
$('.permalink a').attr('href', url);
$('#puppy').load();
history.pushState({pup: newPup}, '', '#' + newPup);

}

function loadPuppy(pup) {
showPuppy(pup)
history.pushState({pup: pup}, '', '#' + pup);
}

function initialPuppy(pup) {
showPuppy(pup);
history.replaceState({pup: pup}, '', '#' + pup);
}

$(document).keyup(function(evt) {
if (evt.keyCode == keycodes.spacebar || evt.keyCode == keycodes.right_arrow) { // right arrow should probably go forward, but not sure how to know we're at the end of the history stack
if (evt.keyCode == keycodes.spacebar) {
var newPup = newPuppy();
loadPuppy(newPup);
} else if (evt.keyCode == keycodes.left_arrow) {
history.back();
} else if (evt.keyCode == keycodes.right_arrow) {
history.forward();
}
});

window.addEventListener('popstate', function(e) {
loadPuppy(e.state.pup);
if (e.state && e.state.pup) {
showPuppy(e.state.pup);
}
})

var initial_pup = window.location.hash.substring(1);
if (puppies.indexOf(initial_pup) == -1) {
initial_pup = newPuppy();
if (puppies.indexOf(initial_pup) != -1) {
initialPuppy(initial_pup);
} else {
initialPuppy(newPuppy());
}
loadPuppy(initial_pup);
}
3 changes: 2 additions & 1 deletion puppies.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@
"85Pd59R",
"SI34zqi",
"qqXt0XG",
"sSgql8d"
"sSgql8d",
"HblQhgb"
]

0 comments on commit 294c21c

Please sign in to comment.