Skip to content

Instantly share code, notes, and snippets.

@nu7hatch
Created July 25, 2013 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nu7hatch/6078989 to your computer and use it in GitHub Desktop.
Save nu7hatch/6078989 to your computer and use it in GitHub Desktop.
var fillSocialCounters = function() {
$('a.socialBtn').fillSocialCounter()
}
$(document).ready(function() {
var text = $('.post h1.title').text()
, url = document.location.href
$('a[rel="facebook"]').facebookButton({ text: text, url: url })
$('a[rel="twitter"]').twitterButton({ text: text, url: url, via: "nu7hatch" })
$('a[rel="plusone"]').plusoneButton({ url: url })
$('a[rel="reddit"]').redditButton({ url: url, text: text, sr: "golang" })
})
;(function($) {
var increaseCounter = function(link) {
$counter = $(link).find('.count');
$counter.html(parseInt($counter.html()) + 1);
}
$.fn.fillSocialCounter = function() {
this.each(function(_, link) {
var $link = $(link)
, $counter = $link.find('.count')
, rel = $link.attr('rel');
$counter.html(egoistat.points(rel));
});
}
$.fn.facebookButton = function(opts) {
$window = $(window);
opts = !!opts ? opts : {};
var text = !!opts.text ? opts.text : document.title
, url = !!opts.url ? opts.url : document.location.href;
var facebookUrl = 'http://www.facebook.com/sharer.php?';
facebookUrl += 'u=' + encodeURIComponent(url);
facebookUrl += '&t=' + encodeURIComponent(text);
this.attr('href', facebookUrl).click(function(e) {
e.preventDefault();
var self = this
, width = 480
, height = 350
, left = ($window.width() - width) / 2
, top = ($window.height() - height) / 2
, opts = 'status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left
, popup = window.open(this.href, 'facebook', opts);
$(popup).unload(function() {
mixpanel.track("Page shared", { "Network": "Facebook" });
increaseCounter(self);
});
});
}
$.fn.plusoneButton = function(opts) {
$window = $(window);
opts = !!opts ? opts : {};
var text = !!opts.text ? opts.text : document.title
, url = !!opts.url ? opts.url : document.location.href;
var plusoneUrl = 'https://plusone.google.com/_/+1/confirm?'
plusoneUrl += 'hl=en-US'
plusoneUrl += '&url=' + encodeURIComponent(url);
this.attr('href', plusoneUrl).click(function(e) {
e.preventDefault();
var self = this
, width = 460
, height = 450
, left = ($window.width() - width) / 2
, top = ($window.height() - height) / 2
, opts = 'scrollbars=Yes,status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left
, popup = window.open(this.href, 'plusone', opts);
$(popup).unload(function() {
mixpanel.track("Page shared", { "Network": "Google+" });
increaseCounter(self);
});
});
}
$.fn.twitterButton = function(opts) {
$window = $(window);
opts = !!opts ? opts : {};
var text = !!opts.text ? opts.text : document.title
, url = !!opts.url ? opts.url : document.location.href
, via = opts.via;
var tweetUrl = 'http://twitter.com/share?';
tweetUrl += 'url=' + encodeURIComponent(url);
tweetUrl += '&text=' + encodeURIComponent(text);
if (!!via) tweetUrl += '&via=' + via;
this.attr('href', tweetUrl).click(function(e) {
e.preventDefault();
var self = this
, width = 575
, height = 300
, left = ($window.width() - width) / 2
, top = ($window.height() - height) / 2
, opts = 'status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left
, popup = window.open(this.href, 'twitter', opts);
$(popup).unload(function() {
mixpanel.track("Page shared", { "Network": "Twitter" });
increaseCounter(self);
});
});
}
$.fn.redditButton = function(opts) {
opts = !!opts ? opts : {}
var redditUrl = 'http://www.reddit.com/submit?'
redditUrl += 'url=' + encodeURIComponent(opts.url)
redditUrl += '&title=' + encodeURIComponent(opts.text)
this.attr('href', redditUrl).click(function(e) {
e.preventDefault()
window.location = redditUrl
mixpanel.track("Page shared", { "Network": "Reddit" });
increaseCounter(this);
})
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment