Skip to content

Instantly share code, notes, and snippets.

@munro
Created July 30, 2014 22:15
Show Gist options
  • Save munro/46d75ff20879772ac649 to your computer and use it in GitHub Desktop.
Save munro/46d75ff20879772ac649 to your computer and use it in GitHub Desktop.
<a share="facebook" data-url="http://www.getringer.com/" class="btn btn-primary">Share to Facebook</a>
<span share="facebook" data-url="http://www.getringer.com/">Shares: {{share_count | humanCount}}</a>
function humanifyCount(number) {
var count = number.toString();
var decimal = '';
if (count.length > 6) {
if (count.slice(-6,-5) !== '0') {
decimal = '.'+count.slice(-6,-5);
}
count = count.slice(0, -6);
count = count + decimal + 'M';
} else if (count.length > 3) {
if (count.slice(-3,-2) !== '0') {
decimal = '.' + count.slice(-3, -2);
}
count = count.slice(0, -3);
count = count + decimal + 'k';
}
return count;
}
getRingerApp.filter('humanCount', function() {
return function (number) {
return typeof number === 'number' ? humanifyCount(number) : '';
};
});
getRingerApp.directive('share', function ($compile, $http) {
return {
restrict: 'AE',
link: function (scope, element, attrs) {
var url, popup;
$http.get('https://api.facebook.com/method/links.getStats?urls=' + encodeURIComponent(attrs.url) + '&format=json').success(function (data) {
scope.share_count = data[0].share_count;
scope.like_count = data[0].like_count;
scope.click_count = data[0].click_count;
scope.total_count = data[0].total_count;
});
element.on('click', function () {
if (attrs.share === 'facebook') {
url = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(attrs.url) + '&display=popup';
popup = window.open(url, 'share', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,height=300,width=611,top=' + ((screen.height / 2) - (300 / 2)) + ',left=' + ((screen.width / 2) - (611 / 2)));
if (window.focus) {
popup.focus();
}
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment