Created
February 4, 2014 16:16
-
-
Save clarkieryan/8806858 to your computer and use it in GitHub Desktop.
Simple JQuery gallery using the FB API.
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
You need to add in the dependancies for fancybox http://fancybox.net/ | |
And ensure that JQuery is included to the module, | |
Facebook part | |
You need an appID which you can register for via developer.facebook.com | |
You can use what's called a channel which allows for a caching some scripts. | |
You need to find the pageID of the page you are calling | |
You can set all of these are the start of the script. | |
Any questions drop me an email |
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
window.fbAsyncInit = function() { | |
var appID = ""; | |
var channelURL = ""; | |
var pageID = ""; | |
// init the FB JS SDK | |
FB.init({ | |
appId: appID, // App ID from the App Dashboard | |
channelUrl: channelURL, // Need to sort this out. | |
status: true, // check the login status upon init? | |
cookie: true, // set sessions cookies to allow your server to access the session? | |
xfbml: true // parse XFBML tags on this page? | |
}); | |
if (typeof facebookInit == 'function') { | |
facebookInit(); | |
} | |
}; | |
// Load the SDK's source Asynchronously | |
// Note that the debug version is being actively developed and might | |
// contain some type checks that are overly strict. | |
// Please report such bugs using the bugs tool. | |
(function(d, debug) { | |
var js, id = 'facebook-jssdk', | |
ref = d.getElementsByTagName('script')[0]; | |
if (d.getElementById(id)) { | |
return; | |
} | |
js = d.createElement('script'); | |
js.id = id; | |
js.async = true; | |
js.src = "http://connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; | |
ref.parentNode.insertBefore(js, ref); | |
}(document, /*debug*/ false)); | |
//Code to run | |
var facebookInit = function() { | |
getAlbums(); | |
} | |
var getAlbums = function() { | |
FB.api('/'+pageID+'/albums?fields=name,id,cover_photo', function(response) { | |
$.each(response.data, function(index, value) { | |
var amount = index +1; | |
FB.api('/' + value.cover_photo, function(image) { | |
console.log(image); | |
if (image.source == undefined || value.name.substr(-1) === "." || value.name === "Timeline Photos" || value.name === "Cover Photos" ||value.name === "Profile Pictures" || value.name === "Mobile Uploads") { | |
console.log("No cover photo set for that album/Album not allowed"); | |
} else { | |
if(response.data.length == amount){ | |
$("#albums").append('<div class="photo" data-albumId="' + value.id + '">' + '<img src="../wp-content/themes/therainbow/includes/timthumb.php?src=' + image.source + '&h=310&w=310" onload="unhidePhotos("#photos", "#albums")"/>' + '<div class="photoInfo">' + value.name + '</div></div>'); | |
} else { | |
$("#albums").append('<div class="photo" data-albumId="' + value.id + '">' + '<img src="../wp-content/themes/therainbow/includes/timthumb.php?src=' + image.source + '&h=310&w=310" />' + '<div class="photoInfo">' + value.name + '</div></div>'); | |
} | |
} | |
}); | |
}); | |
}); | |
} | |
var getAlbumPhotos = function(album) { | |
//If the user has already loaded the album dont load it again. | |
//Create the element | |
$("#photos").append('<div class="gallery" data-photoalbumId="' + album + '"></div>'); | |
//Do the call | |
FB.api('/' + album + '/photos?fields=source,name', function(response) { | |
$.each(response.data, function(index, value) { | |
var amount = index + 1; | |
if (response.data.length == amount) { | |
$('#photos').find('[data-photoalbumId="' + album + '"]').append('<a class="photo" href="' + value.source + '" rel="group" >' + '<img src="../wp-content/themes/therainbow/includes/timthumb.php?src=' + value.source + '&h=182&w=182" onload="unhidePhotos(\'#albums\', \'#photos\')"/></a>'); | |
} else { | |
$('#photos').find('[data-photoalbumId="' + album + '"]').append('<a class="photo" href="' + value.source + '" rel="group" >' + '<img src="../wp-content/themes/therainbow/includes/timthumb.php?src=' + value.source + '&h=182&w=182" /></a>'); | |
} | |
}); | |
}); | |
} | |
var unhidePhotos = function() { | |
var interval = window.setInterval(function() { | |
var opacity = $('.overlay').css('opacity') | |
if (opacity == '1') { | |
window.clearInterval(interval); | |
$('body').scrollTop(0); | |
$('#albums').css('display', 'none'); | |
$('#photos').css('display', 'block'); | |
$('.overlay').fadeOut(1400); | |
} | |
}, 500); | |
} | |
//On document ready | |
$(document).ready(function() { | |
//Add the listener | |
$("#albums").on('click', '.photo', function() { | |
$('.overlay').fadeIn(1500); | |
getAlbumPhotos($(this).data("albumid")); | |
//setTimeout(unhidePhotos, 2500); | |
}); | |
$('.container').on('click', '#backButton', function() { | |
console.log("Back button pressed"); | |
$('#photos').css('display', 'none'); | |
$('#photos').find('.gallery').css('display', 'none'); | |
$("#albums").css('display', 'block'); | |
}); | |
//init fancybox | |
$('.photo[rel=group]').fancybox({ | |
padding: 10, | |
openEffect: 'none', | |
nextEasing: 'swing', | |
prevEasing: 'none', | |
prevEffect: 'none', | |
nextEffect: 'none', | |
}); | |
}); | |
var toggleLoading = function(div) { | |
//If the div is pre-loaded show it and then invoke the fade | |
if ($(div).length == 0) { | |
$('body').scrollTop(0); | |
$('#albums').css('display', 'none'); | |
$('#photos').css('display', 'block'); | |
$('.overlay').fadeOut("slow"); | |
} else { | |
} | |
} |
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
<!--- Loads in the Javascript API Library --> | |
<div id="fb-root"></div> | |
<script>(function(d, s, id) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); js.id = id; | |
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk'));</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment