Skip to content

Instantly share code, notes, and snippets.

@karlclement
Forked from cshold/supply-social-sharing-js
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save karlclement/82246d3be0677ab887e5 to your computer and use it in GitHub Desktop.

Select an option

Save karlclement/82246d3be0677ab887e5 to your computer and use it in GitHub Desktop.

Revisions

  1. @cshold cshold created this gist Jun 11, 2014.
    72 changes: 72 additions & 0 deletions supply-social-sharing-js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    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 %}
    }