Skip to content

Instantly share code, notes, and snippets.

@cshold
Created June 11, 2014 19:03
Show Gist options
  • Save cshold/66446a15f75a059adea2 to your computer and use it in GitHub Desktop.
Save cshold/66446a15f75a059adea2 to your computer and use it in GitHub Desktop.
The JS required to setup social sharing on a Shopify theme
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