Skip to content

Instantly share code, notes, and snippets.

@jamlfy
Last active August 29, 2015 14:21
Show Gist options
  • Save jamlfy/d0f444f491cd1a359420 to your computer and use it in GitHub Desktop.
Save jamlfy/d0f444f491cd1a359420 to your computer and use it in GitHub Desktop.
Share
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="index.js"></script>
<ANY share="twitter" text="Hey is cool!!" via="@alejonext"/>
angular.module('app', ['ng'])
.directive('share', require('./share.angular.js'));
var MULTI = 1000;
var DEC = 100;
module.exports =function (loco, win, $http){
var share = {
'fb' : {
icon : 'facebook-square',
share: function () {
return 'http://www.facebook.com/sharer.php?u=' + loco.absUrl();
},
count : function () {
return "https://graph.facebook.com/?callback=JSON_CALLBACK&id="+ loco.absUrl();
},
get: function(data) {
return data.shares;
}
},
twitter : {
icon : 'twitter-square',
share: function (text, via) {
return 'http://twitter.com/share?text=' + text + '&url=' + loco.absUrl() + '&via=' + via;
},
count : function () {
return "https://cdn.api.twitter.com/1/urls/count.json?callback=JSON_CALLBACK&url="+ loco.absUrl();
},
get: function(data) {
return data.count;
}
},
google : {
icon : 'google-plus-square',
share: function () {
return 'https://plus.google.com/share?url='+ loco.absUrl();
}
},
tumblr : {
icon : 'tumblr-square',
share: function () {
return 'https://www.tumblr.com/widgets/share/tool?shareSource=legacy&canonicalUrl=&url=' + loco.absUrl();
}
},
reddit : {
icon : 'reddit-square',
share: function () {
return 'https://www.reddit.com/submit?url=' + loco.absUrl();
}
},
linkedin : {
icon : 'linkedin-square',
share: function () {
return 'https://www.linkedin.com/shareArticle?mini=true&url=' + loco.absUrl();
},
count : function () {
return "https://www.linkedin.com/countserv/count/share?callback=JSON_CALLBACK&format=jsonp&url="+ loco.absUrl();
},
get: function(data) {
return data.count;
}
},
pinterest : {
icon : 'pinterest-square',
share: function (text, media) {
return 'http://pinterest.com/pin/create/button/?url=' + loco.absUrl() + '&description=' + text + '&media=' + media ;
},
count : function () {
return "https://api.pinterest.com/v1/urls/count.json?callback=JSON_CALLBACK&url="+ loco.absUrl();
},
get : function (data) {
return data.count;
}
},
xing : {
icon : 'xing-square',
share: function () {
return 'https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url=' + loco.absUrl();
}
},
email : {
icon : 'envelope-o',
share: function (text) {
return 'mailto:?subject=' + text + '&body=' + loco.absUrl();
}
},
adn : {
icon : 'adn',
share: function () {
return 'idont';
}
}
};
return {
restrict: 'A',
scope: {
share : '@'
},
bindToController: true,
link : function (scope, ele, attr) {
scope.count = '';
scope.more = '';
scope.tos = share[ scope.share ];
if(scope.tos.count){
$http.jsonp(scope.tos.count()).success(function (data) {
scope.count = scope.tos.get(data);
if( scope.count >= MULTI ){
scope.count = Math.round( (scope.count / MULTI) * DEC) / DEC;
scope.more = 'k';
if( scope.count >= MULTI ){
scope.count = Math.round( (scope.count / MULTI) * DEC) / DEC;
scope.more = 'M';
}
}
});
}
ele.on('click', function (e) {
var left = (screen.width/2)- 300;
var top = (screen.height/2)- 200;
var newwindow = win.open(scope.tos.share( attr.text, attr.via ),'Share Galeph','location=1,status=1,scrollbars=1,height=400,width=600,top='+top+',left='+left);
if (win.focus) newwindow.focus();
});
},
template : '<i class="fa fa-{{tos.icon}} fa-lg">{{count + more}}</i>',
};
};
module.exports.$inject = [ '$location', '$window', '$http' ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment