Instantly share code, notes, and snippets.
The JS required to setup social sharing on a Shopify theme
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
timber.socialSharing = function () { | |
var buttons = timber.cache.shareButtons, | |
permalink = buttons.data('permalink'), | |
shareButtons = buttons.find('a'), | |
socialCounts = buttons.find('span.share-count'); | |
{% if settings.social_sharing_products or settings.social_sharing_blog %} | |
// Get share stats from respective APIs | |
var fbLink = $('.share-facebook'), | |
twitLink = $('.share-twitter'), | |
pinLink = $('.share-pinterest'), | |
googleLink = $('.share-google'), | |
fbShares, twitShares, pinShares, googleShares; | |
if ( fbLink.length ) { | |
$.getJSON('//graph.facebook.com/?id=' + permalink + '&callback=?', function(data) { | |
fbShares = data.shares; | |
if (!fbShares) { | |
fbShares = 0; | |
} | |
fbLink.find('.share-count').text(fbShares).addClass('is-loaded'); | |
}); | |
}; | |
if ( twitLink.length ) { | |
$.getJSON('//cdn.api.twitter.com/1/urls/count.json?url=' + permalink + '&callback=?', function(data) { | |
twitShares = data.count; | |
twitLink.find('.share-count').text(twitShares).addClass('is-loaded'); | |
}); | |
}; | |
if ( pinLink.length ) { | |
$.getJSON('//api.pinterest.com/v1/urls/count.json?url=' + permalink + '&callback=?', function(data) { | |
pinShares = data.count; | |
pinLink.find('.share-count').text(pinShares).addClass('is-loaded'); | |
}); | |
}; | |
if ( googleLink.length ) { | |
// Can't currently get Google+ count with JS, so just pretend it loaded | |
googleLink.find('.share-count').addClass('is-loaded'); | |
}; | |
{% endif %} | |
{% if settings.social_sharing_products or settings.social_sharing_blog %} | |
// Share popups | |
shareButtons.on('click', function(e) { | |
e.preventDefault(); | |
var el = $(this), | |
popup = el.attr('class'), | |
link = el.attr('href'), | |
w = 700, | |
h = 400; | |
// Set popup sizes | |
switch (popup) { | |
case 'share-twitter': | |
h = 300; | |
break; | |
case 'share-fancy': | |
w = 480; | |
h = 720; | |
break; | |
case 'share-google': | |
w = 500; | |
break; | |
} | |
window.open(link, popup, 'width=' + w + ', height=' + h); | |
}); | |
{% endif %} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment