Skip to content

Instantly share code, notes, and snippets.

@mrzmyr
Last active December 30, 2015 00:29
Show Gist options
  • Save mrzmyr/7749827 to your computer and use it in GitHub Desktop.
Save mrzmyr/7749827 to your computer and use it in GitHub Desktop.
Share directives for google plus, facebook and twitter incl. popup service.
<a
href="javascript:void(0)"
facebook-share
app-id="{{ appId }}"
image="{{ image }}"
url="{{ url }}"
title="{{ title }}"
description="{{ description }}"
>
Share with Facebook
</a>
app.directive('facebookShare', ['PopupService', function (PopupService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var queryArr = [];
var params = {
caption: attrs.title,
description: attrs.description,
link: attrs.url,
picture: attrs.image,
display: 'popup',
redirect_uri: attrs.url,
app_id: attrs.appId
};
for(var key in params) {
queryArr.push(key + '=' + encodeURIComponent(params[key]));
}
var url = 'https://www.facebook.com/dialog/feed?' + queryArr.join('&');
element.click(function () {
PopupService.open(url, 'Facebook');
});
}
};
}]);
app.directive('googlePlusShare', ['PopupService', function (PopupService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var url = ' https://plus.google.com/share?url=' + encodeURIComponent(attrs.url);
element.click(function () {
PopupService.open(url, 'Google Plus');
});
}
};
}]);
app.service('PopupService', [function () {
this.open = function (url, title, options) {
var queryArr = [];
options = options || {};
options = angular.extend({
width: 550,
height: 300,
resizable: 1
}, options);
options.left = (screen.width / 2) - (options.width / 2);
options.top = (screen.height / 2) - (options.height / 2);
for(var key in options) {
queryArr.push(key + '=' + options[key]);
}
window.open(url, title, queryArr.join(','));
};
}]);
app.directive('twitterShare', ['PopupService', function (PopupService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var url = 'http://twitter.com/share?url=' + encodeURIComponent(attrs.url) + '&text=' + encodeURIComponent(attrs.text) + '&count=none&via=' + attrs.via;
element.click(function () {
PopupService.open(url, 'Twitter');
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment