SocialShare.js
145 lines
| 1 | /** |
| 2 | * SocialShare - jQuery plugin |
| 3 | */ |
| 4 | (function ($) { |
| 5 | |
| 6 | function get_class_list(elem){ |
| 7 | if(elem.classList){ |
| 8 | return elem.classList; |
| 9 | }else{ |
| 10 | return $(elem).attr('class').match(/\S+/gi); |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | $.fn.ShareLink = function(options){ |
| 15 | var defaults = { |
| 16 | title: '', |
| 17 | text: '', |
| 18 | image: '', |
| 19 | url: window.location.href, |
| 20 | class_prefix: 's_' |
| 21 | }; |
| 22 | |
| 23 | var options = $.extend({}, defaults, options); |
| 24 | |
| 25 | var class_prefix_length = options.class_prefix.length; |
| 26 | |
| 27 | var templates = { |
| 28 | twitter: 'https://twitter.com/intent/tweet?url={url}&text={text}', |
| 29 | pinterest: 'https://www.pinterest.com/pin/create/button/?media={image}&url={url}&description={text}', |
| 30 | facebook: 'https://www.facebook.com/sharer/sharer.php?u={url}', |
| 31 | linkedin: 'https://www.linkedin.com/shareArticle?url={url}&title={title}', |
| 32 | reddit: 'https://www.reddit.com/submit?url={url}&title={title}', |
| 33 | whatsapp: 'https://api.whatsapp.com/send?text={url} {title}', |
| 34 | email: 'mailto:?subject={title}&body={url}' |
| 35 | } |
| 36 | |
| 37 | function link(network){ |
| 38 | var url = templates[network]; |
| 39 | url = url.replace(/{url}/g, encodeURIComponent(options.url)); |
| 40 | url = url.replace(/{title}/g, encodeURIComponent(options.title)); |
| 41 | url = url.replace(/{text}/g, encodeURIComponent(options.text)); |
| 42 | url = url.replace(/{image}/g, encodeURIComponent(options.image)); |
| 43 | return url; |
| 44 | } |
| 45 | |
| 46 | return this.each(function(i, elem){ |
| 47 | var classlist = get_class_list(elem); |
| 48 | for(var i = 0; i < classlist.length; i++){ |
| 49 | var cls = classlist[i]; |
| 50 | if(cls.substr(0, class_prefix_length) == options.class_prefix && templates[cls.substr(class_prefix_length)]){ |
| 51 | var final_link = link(cls.substr(class_prefix_length)); |
| 52 | $(elem).attr('href', final_link).click(function(){ |
| 53 | if($(this).attr('href').indexOf('http://') === -1 && $(this).attr('href').indexOf('https://') === -1){ |
| 54 | return window.open($(this).attr('href')) && false; |
| 55 | } |
| 56 | var screen_width = screen.width; |
| 57 | var screen_height = screen.height; |
| 58 | var popup_width = options.width ? options.width : (screen_width - (screen_width*0.2)); |
| 59 | var popup_height = options.height ? options.height : (screen_height - (screen_height*0.2)); |
| 60 | var left = (screen_width/2)-(popup_width/2); |
| 61 | var top = (screen_height/2)-(popup_height/2); |
| 62 | var parameters = 'toolbar=0,status=0,width=' + popup_width + ',height=' + popup_height + ',top=' + top + ',left=' + left; |
| 63 | return window.open($(this).attr('href'), '', parameters) && false; |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | $.fn.ShareCounter = function(options){ |
| 71 | var defaults = { |
| 72 | url: window.location.href, |
| 73 | class_prefix: 'c_', |
| 74 | display_counter_from: 0, |
| 75 | increment: false |
| 76 | }; |
| 77 | |
| 78 | var options = $.extend({}, defaults, options); |
| 79 | |
| 80 | var class_prefix_length = options.class_prefix.length |
| 81 | |
| 82 | var social = { |
| 83 | 'linkedin': linkedin, |
| 84 | 'facebook': facebook, |
| 85 | 'pinterest': pinterest |
| 86 | } |
| 87 | |
| 88 | return this.each(function(i, elem){ |
| 89 | var classlist = get_class_list(elem); |
| 90 | for(var i = 0; i < classlist.length; i++){ |
| 91 | var cls = classlist[i]; |
| 92 | if(cls.substr(0, class_prefix_length) == options.class_prefix && social[cls.substr(class_prefix_length)]){ |
| 93 | social[cls.substr(class_prefix_length)](options.url, function(count){ |
| 94 | count = parseInt(count); |
| 95 | if (count >= options.display_counter_from){ |
| 96 | var current_value = parseInt($(elem).text()); |
| 97 | if(options.increment && !isNaN(current_value)){ |
| 98 | count += current_value; |
| 99 | } |
| 100 | $(elem).text(count); |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | function linkedin(url, callback){ |
| 108 | $.ajax({ |
| 109 | type: 'GET', |
| 110 | dataType: 'jsonp', |
| 111 | url: 'https://www.linkedin.com/countserv/count/share', |
| 112 | data: {'url': url, 'format': 'jsonp'} |
| 113 | }) |
| 114 | .done(function(data){callback(data.count)}) |
| 115 | .fail(function(){callback(0)}) |
| 116 | } |
| 117 | |
| 118 | function facebook(url, callback){ |
| 119 | $.ajax({ |
| 120 | type: 'GET', |
| 121 | dataType: 'jsonp', |
| 122 | url: 'https://graph.facebook.com', |
| 123 | data: {'id': url} |
| 124 | }) |
| 125 | .done(function (data){ |
| 126 | if(data.share != undefined && data.share.share_count != undefined){ |
| 127 | callback(data.share.share_count) |
| 128 | } |
| 129 | }) |
| 130 | .fail(function(){callback(0)}) |
| 131 | } |
| 132 | |
| 133 | function pinterest(url, callback){ |
| 134 | $.ajax({ |
| 135 | type: 'GET', |
| 136 | dataType: 'jsonp', |
| 137 | url: 'https://api.pinterest.com/v1/urls/count.json', |
| 138 | data: {'url': url} |
| 139 | }) |
| 140 | .done(function(data){callback(data.count)}) |
| 141 | .fail(function(){callback(0)}) |
| 142 | } |
| 143 | } |
| 144 | })(jQuery); |
| 145 |