forked from Julienh/Sharrre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.sharrre.js
700 lines (670 loc) · 30.1 KB
/
jquery.sharrre.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*!
* Sharrre iQuery Plugin, v1.3.4b
* Mod: Annexare Studio, v1.1
*
* Author: Julien Hany
* License: MIT http://en.wikipedia.org/wiki/MIT_License or GPLv2 http://en.wikipedia.org/wiki/GNU_General_Public_License
*/
;(function ( $, window, document, undefined ) {
/* Defaults
================================================== */
var pluginName = 'sharrre',
defaults = {
className: 'sharrre',
share: {
googlePlus: false,
facebook: false,
twitter: false,
digg: false,
delicious: false,
stumbleupon: false,
linkedin: false,
pinterest: false,
vk: false
},
shareTotal: 0,
template: '',
title: '',
url: document.location.href,
text: document.title,
urlCurl: 'sharrre.php', //PHP script for google plus...
count: {}, //counter by social network
total: 0, //total of sharing
shorterTotal: true, //show total by k or M when number is to big
enableHover: true, //disable if you want to personalize hover event with callback
enableCounter: true, //disable if you just want use buttons
enableTracking: false, //tracking with google analitycs
hover: function(){}, //personalize hover event with this callback function
hide: function(){}, //personalize hide event with this callback function
click: function(){}, //personalize click event with this callback function
render: function(){}, //personalize render event with this callback function
buttons: { //settings for buttons
googlePlus : { //http://www.google.com/webmasters/+1/button/
url: '', //if you need to personnalize button url
urlCount: false, //if you want to use personnalize button url on global counter
size: 'medium',
lang: 'en-US',
annotation: ''
},
facebook: { //http://developers.facebook.com/docs/reference/plugins/like/
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
action: 'like',
layout: 'button_count',
width: '',
send: 'false',
faces: 'false',
colorscheme: '',
font: '',
lang: 'en_US'
},
twitter: { //http://twitter.com/about/resources/tweetbutton
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
count: 'horizontal',
hashtags: '',
via: '',
related: '',
username: false, // if string, gets number of followers instead of tweets
lang: 'en'
},
digg: { //http://about.digg.com/downloads/button/smart
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
type: 'DiggCompact'
},
delicious: {
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
size: 'medium' //medium or tall
},
stumbleupon: { //http://www.stumbleupon.com/badges/
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
layout: '1'
},
linkedin: { //http://developer.linkedin.com/plugins/share-button
url: '', //if you need to personalize url button
urlCount: false, //if you want to use personnalize button url on global counter
counter: ''
},
pinterest: { //http://pinterest.com/about/goodies/
url: '', //if you need to personalize url button
media: '',
description: '',
layout: 'horizontal'
},
vk: {
apiId: 0, // VK.com App/Website ID
counted: 0,
bId: 1, // button ID on the page
height: 22, // button height: 18, 20, 22, 24
// pageTitle
// pageDescription
pageUrl: '', // if you need to personalize url button
// pageImage
// text: '', // 140 chars max
type: 'full', // button, full, mini, vertical
verb: 0, // 0 like, 1 recommend
width: 350 // only for type = full
}
}
},
/* Json URL to get count number
================================================== */
urlJson = {
googlePlus: "",
//using FQL method by Sire
//facebook: "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27{url}%27&callback=?",
//using Graph
facebook: "http://graph.facebook.com/?id={url}&callback=?",
//facebook : "http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&urls={url}&format=json"
twitter: "https://cdn.api.twitter.com/1/urls/count.json?url={url}&callback=?",
//twitter: "https://cdn.api.twitter.com/1/users/show.json?screen_name={text}&include_entities=true&callback=?",
digg: "http://services.digg.com/2.0/story.getInfo?links={url}&type=javascript&callback=?",
delicious: 'http://feeds.delicious.com/v2/json/urlinfo/data?url={url}&callback=?',
//stumbleupon: "http://www.stumbleupon.com/services/1.01/badge.getinfo?url={url}&format=jsonp&callback=?",
stumbleupon: "",
linkedin: "http://www.linkedin.com/countserv/count/share?format=jsonp&url={url}&callback=?",
pinterest: "",
vk: ""
},
/* Load share buttons asynchronously
================================================== */
loadButton = {
googlePlus : function(self){
var sett = self.options.buttons.googlePlus;
//$(self.element).find('.buttons').append('<div class="button googleplus"><g:plusone size="'+self.options.buttons.googlePlus.size+'" href="'+self.options.url+'"></g:plusone></div>');
$(self.element).find('.buttons').append('<div class="button googleplus"><div class="g-plusone" data-size="'+sett.size+'" data-href="'+(sett.url !== '' ? sett.url : self.options.url)+'" data-annotation="'+sett.annotation+'"></div></div>');
window.___gcfg = {
lang: self.options.buttons.googlePlus.lang
};
var loading = 0;
if(typeof gapi === 'undefined' && loading == 0){
loading = 1;
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = '//apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
}
else{
gapi.plusone.go();
}
},
facebook : function(self){
var sett = self.options.buttons.facebook;
$(self.element).find('.buttons').append('<div class="button facebook"><div id="fb-root"></div><div class="fb-like" data-href="'+(sett.url !== '' ? sett.url : self.options.url)+'" data-send="'+sett.send+'" data-layout="'+sett.layout+'" data-width="'+sett.width+'" data-show-faces="'+sett.faces+'" data-action="'+sett.action+'" data-colorscheme="'+sett.colorscheme+'" data-font="'+sett.font+'" data-via="'+sett.via+'"></div></div>');
var loading = 0;
if(typeof FB === 'undefined' && loading == 0){
loading = 1;
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = '//connect.facebook.net/'+sett.lang+'/all.js#xfbml=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
else{
FB.XFBML.parse();
}
},
twitter : function(self){
var sett = self.options.buttons.twitter;
$(self.element).find('.buttons').append(
sett.username
? ('<div class="button twitter" data-text="' + sett.username + '"><a href="https://twitter.com/' + sett.username + '" class="twitter-follow-button" data-text="' + sett.username + '" data-show-count="false">Follow @' + sett.username + '</a></div>')
: ('<div class="button twitter"><a href="https://twitter.com/share" class="twitter-share-button" data-url="'+(sett.url !== '' ? sett.url : self.options.url)+'" data-count="'+sett.count+'" data-text="'+self.options.text+'" data-via="'+sett.via+'" data-hashtags="'+sett.hashtags+'" data-related="'+sett.related+'" data-lang="'+sett.lang+'">Tweet</a></div>')
);
var loading = 0;
if(typeof twttr === 'undefined' && loading == 0){
loading = 1;
(function() {
var twitterScriptTag = document.createElement('script');
twitterScriptTag.type = 'text/javascript';
twitterScriptTag.async = true;
twitterScriptTag.src = '//platform.twitter.com/widgets.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(twitterScriptTag, s);
})();
}
else{
$.ajax({ url: '//platform.twitter.com/widgets.js', dataType: 'script', cache:true}); //http://stackoverflow.com/q/6536108
}
},
digg : function(self){
var sett = self.options.buttons.digg;
$(self.element).find('.buttons').append('<div class="button digg"><a class="DiggThisButton '+sett.type+'" rel="nofollow external" href="http://digg.com/submit?url='+encodeURIComponent((sett.url !== '' ? sett.url : self.options.url))+'"></a></div>');
var loading = 0;
if(typeof __DBW === 'undefined' && loading == 0){
loading = 1;
(function() {
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = '//widgets.digg.com/buttons.js';
s1.parentNode.insertBefore(s, s1);
})();
}
},
delicious : function(self){
if(self.options.buttons.delicious.size == 'tall'){//tall
var css = 'width:50px;',
cssCount = 'height:35px;width:50px;font-size:15px;line-height:35px;',
cssShare = 'height:18px;line-height:18px;margin-top:3px;';
}
else{//medium
var css = 'width:93px;',
cssCount = 'float:right;padding:0 3px;height:20px;width:26px;line-height:20px;',
cssShare = 'float:left;height:20px;line-height:20px;';
}
var count = self.shorterTotal(self.options.count.delicious);
if(typeof count === "undefined"){
count = 0;
}
$(self.element).find('.buttons').append(
'<div class="button delicious"><div style="'+css+'font:12px Arial,Helvetica,sans-serif;cursor:pointer;color:#666666;display:inline-block;float:none;height:20px;line-height:normal;margin:0;padding:0;text-indent:0;vertical-align:baseline;">'+
'<div style="'+cssCount+'background-color:#fff;margin-bottom:5px;overflow:hidden;text-align:center;border:1px solid #ccc;border-radius:3px;">'+count+'</div>'+
'<div style="'+cssShare+'display:block;padding:0;text-align:center;text-decoration:none;width:50px;background-color:#7EACEE;border:1px solid #40679C;border-radius:3px;color:#fff;">'+
'<img src="http://www.delicious.com/static/img/delicious.small.gif" height="10" width="10" alt="Delicious" /> Add</div></div></div>');
$(self.element).find('.delicious').on('click', function(){
self.openPopup('delicious');
});
},
stumbleupon : function(self){
var sett = self.options.buttons.stumbleupon;
$(self.element).find('.buttons').append('<div class="button stumbleupon"><su:badge layout="'+sett.layout+'" location="'+(sett.url !== '' ? sett.url : self.options.url)+'"></su:badge></div>');
var loading = 0;
if(typeof STMBLPN === 'undefined' && loading == 0){
loading = 1;
(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//platform.stumbleupon.com/1/widgets.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
s = window.setTimeout(function(){
if(typeof STMBLPN !== 'undefined'){
STMBLPN.processWidgets();
clearInterval(s);
}
},500);
}
else{
STMBLPN.processWidgets();
}
},
linkedin : function(self){
var sett = self.options.buttons.linkedin;
$(self.element).find('.buttons').append('<div class="button linkedin"><script type="in/share" data-url="'+(sett.url !== '' ? sett.url : self.options.url)+'" data-counter="'+sett.counter+'"></script></div>');
var loading = 0;
if(typeof window.IN === 'undefined' && loading == 0){
loading = 1;
(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//platform.linkedin.com/in.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
}
else{
window.IN.init();
}
},
pinterest : function(self){
var sett = self.options.buttons.pinterest;
$(self.element).find('.buttons').append('<div class="button pinterest"><a href="http://pinterest.com/pin/create/button/?url='+(sett.url !== '' ? sett.url : self.options.url)+'&media='+sett.media+'&description='+sett.description+'" class="pin-it-button" count-layout="'+sett.layout+'">Pin It</a></div>');
(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//assets.pinterest.com/js/pinit.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
},
vk : function(self) {
var sett = self.options.buttons.vk;
$(self.element).find('.buttons').append('<div class="button vk"><div id="vk-like-' + sett.bId + '" class="vk-button" data-url="'+(sett.pageUrl !== '' ? sett.pageUrl : self.options.url)+'" data-pageUrl="'+(sett.pageUrl !== '' ? sett.pageUrl : self.options.url)+'"></div></div>');
var loading = 0,
vkButton = function() {
VK.Widgets.Like("vk-like-" + sett.bId++, sett);
/*if (this.options.buttons[name].counted == 0) {
// first try: JS API
url = '';
VK.api('likes.getList', {type: 'sitepage', page_url: (this.options.buttons[name].pageUrl !== '' ? this.options.buttons[name].pageUrl : self.options.url)}, function(r) {
console.log('VK likes.getList: ', r);
if (r.error) {
this.options.buttons[name].counted = -1;
this.getSocialJson(name);
}
});
} else {
// else: temporary PHP solution
console.log('VK likes PHP.');
}*/
VK.Observer.subscribe('widgets.like.liked', function(counted) {
self.updateCounter('vk');
if (self.options.enableTracking) {
_gaq.push(['_trackSocial', 'vkontakte', 'like']);
}
});
VK.Observer.subscribe('widgets.like.unliked', function(counted) {
self.updateCounter('vk', -1);
if (self.options.enableTracking) {
_gaq.push(['_trackSocial', 'vkontakte', 'unlike']);
}
});
},
vkInit = function() {
VK.init({apiId: sett.apiId, onlyWidgets: true});
vkButton();
};
if(typeof VK === 'undefined' && loading == 0){
loading = 1;
(function() {
window.vkAsyncInit = vkInit;
var vjs = document.createElement('script');
vjs.type = 'text/javascript';
vjs.async = true;
vjs.src = '//vk.com/js/api/openapi.js?63';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(vjs, s);
})();
} else {
vkInit();
}
}
},
/* Tracking for Google Analytics
================================================== */
tracking = {
googlePlus: function(){},
facebook: function( self ){
fb = window.setInterval(function(){
if (typeof FB !== 'undefined') {
FB.Event.subscribe('edge.create', function(targetUrl) {
self.updateCounter('facebook');
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
self.updateCounter('facebook', -1);
_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
});
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
});
clearInterval(fb);
}
},1000);
},
twitter: function( self ){
//console.log('twitter');
tw = window.setInterval(function(){
if (typeof twttr !== 'undefined') {
twttr.events.bind('tweet', function(event) {
if (event) {
self.updateCounter('twitter');
_gaq.push(['_trackSocial', 'twitter', 'tweet']);
}
});
//console.log('ok');
clearInterval(tw);
}
},1000);
},
digg: function(){
//if somenone find a solution, mail me !
/*$(this.element).find('.digg').on('click', function(){
_gaq.push(['_trackSocial', 'digg', 'add']);
});*/
},
delicious: function(){},
stumbleupon: function(){},
linkedin: function(){
function LinkedInShare() {
_gaq.push(['_trackSocial', 'linkedin', 'share']);
}
},
pinterest: function(){
//if somenone find a solution, mail me !
},
vk: function(){
// subscribed after init()
}
},
/* Popup for each social network
================================================== */
popup = {
googlePlus: function(opt){
window.open("https://plus.google.com/share?hl="+opt.buttons.googlePlus.lang+"&url="+encodeURIComponent((opt.buttons.googlePlus.url !== '' ? opt.buttons.googlePlus.url : opt.url)), "", "toolbar=0, status=0, width=900, height=500");
},
facebook: function(opt){
window.open("http://www.facebook.com/sharer/sharer.php?u="+encodeURIComponent((opt.buttons.facebook.url !== '' ? opt.buttons.facebook.url : opt.url))+"&t="+opt.text+"", "", "toolbar=0, status=0, width=900, height=500");
},
twitter: function(opt){
window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(opt.text)+"&url="+encodeURIComponent((opt.buttons.twitter.url !== '' ? opt.buttons.twitter.url : opt.url))+(opt.buttons.twitter.via !== '' ? '&via='+opt.buttons.twitter.via : ''), "", "toolbar=0, status=0, width=650, height=360");
},
digg: function(opt){
window.open("http://digg.com/tools/diggthis/submit?url="+encodeURIComponent((opt.buttons.digg.url !== '' ? opt.buttons.digg.url : opt.url))+"&title="+opt.text+"&related=true&style=true", "", "toolbar=0, status=0, width=650, height=360");
},
delicious: function(opt){
window.open('http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url))+'&title='+opt.text, 'delicious', 'toolbar=no,width=550,height=550');
},
stumbleupon: function(opt){
window.open('http://www.stumbleupon.com/badge/?url='+encodeURIComponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url)), 'stumbleupon', 'toolbar=no,width=550,height=550');
},
linkedin: function(opt){
window.open('https://www.linkedin.com/cws/share?url='+encodeURIComponent((opt.buttons.delicious.url !== '' ? opt.buttons.delicious.url : opt.url))+'&token=&isFramed=true', 'linkedin', 'toolbar=no,width=550,height=550');
},
pinterest: function(opt){
window.open('http://pinterest.com/pin/create/button/?url='+encodeURIComponent((opt.buttons.pinterest.url !== '' ? opt.buttons.pinterest.url : opt.url))+'&media='+encodeURIComponent(opt.buttons.pinterest.media)+'&description='+opt.buttons.pinterest.description, 'pinterest', 'toolbar=no,width=700,height=300');
},
vk: function(opt) {
// TODO: [VK] Popup
console.log('VK Popup', opt);
}
};
/* Plugin constructor
================================================== */
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( true, {}, defaults, options);
this.options.share = options.share; //simple solution to allow order of buttons
this._defaults = defaults;
this._name = pluginName;
this.init();
};
/* Initialization method
================================================== */
Plugin.prototype.init = function () {
var self = this;
if(this.options.urlCurl !== ''){
urlJson.googlePlus = this.options.urlCurl + '?url={url}&type=googlePlus'; // PHP script for GooglePlus...
urlJson.stumbleupon = this.options.urlCurl + '?url={url}&type=stumbleupon'; // PHP script for Stumbleupon...
urlJson.pinterest = this.options.urlCurl + '?url={url}&type=pinterest'; // PHP script for Pinterest...
urlJson.vk = this.options.urlCurl + '?url={url}&type=vk&app={app}'; // PHP script for VK...
}
$(this.element).addClass(this.options.className); //add class
//HTML5 Custom data
if(typeof $(this.element).data('title') !== 'undefined'){
this.options.title = $(this.element).attr('data-title');
}
if(typeof $(this.element).data('url') !== 'undefined'){
this.options.url = $(this.element).data('url');
}
if(typeof $(this.element).data('text') !== 'undefined'){
this.options.text = $(this.element).data('text');
}
//how many social website have been selected
$.each(this.options.share, function(name, val) {
if(val === true){
self.options.shareTotal ++;
}
});
if(self.options.enableCounter === true){ //if for some reason you don't need counter
//get count of social share that have been selected
$.each(this.options.share, function(name, val) {
if(val === true){
//self.getSocialJson(name);
try {
self.getSocialJson(name);
} catch(e){
}
}
});
}
else if(self.options.template !== ''){ //for personalized button (with template)
this.options.render(this, this.options);
}
else{ // if you want to use official button like example 3 or 5
this.loadButtons();
}
//add hover event
$(this.element).hover(function(){
//load social button if enable and 1 time
if($(this).find('.buttons').length === 0 && self.options.enableHover === true){
self.loadButtons();
}
self.options.hover(self, self.options);
}, function(){
self.options.hide(self, self.options);
});
//click event
$(this.element).click(function(){
self.options.click(self, self.options);
return false;
});
};
/* loadButtons methode
================================================== */
Plugin.prototype.loadButtons = function () {
var self = this;
$(this.element).append('<div class="buttons"></div>');
$.each(self.options.share, function(name, val) {
if(val == true){
loadButton[name](self);
if(self.options.enableTracking === true){ //add tracking
tracking[name](self);
}
}
});
};
/* getSocialJson methode
================================================== */
Plugin.prototype.getSocialJson = function (name) {
var self = this,
count = 0,
url = urlJson[name].replace('{url}', encodeURIComponent(this.options.url));
if ('twitter' == name && this.options.buttons[name].username) {
url = urlJson[name] = "https://cdn.api.twitter.com/1/users/show.json?screen_name="
+ this.options.buttons[name].username + "&include_entities=true&callback=?";
} else if ('vk' == name) {
url = url.replace('{app}', this.options.buttons[name].apiId);
}
if (this.options.buttons[name].urlCount === true && this.options.buttons[name].url !== ''){
url = urlJson[name].replace('{url}', this.options.buttons[name].url);
}
//console.log('name : ' + name + ' - url : '+url); //debug
if(url != '' && self.options.urlCurl !== ''){ //urlCurl = '' if you don't want to used PHP script but used social button
$.getJSON(url, function(json){
if(typeof json.count !== "undefined"){ //GooglePlus, Stumbleupon, Twitter and Digg
var temp = json.count + '';
temp = temp.replace('\u00c2\u00a0', ''); //remove google plus special chars
count += parseInt(temp, 10);
}
//get the FB total count (shares, likes and more)
else if(json.data && json.data.length > 0 && typeof json.data[0].total_count !== "undefined"){ //Facebook total count
count += parseInt(json.data[0].total_count, 10);
}
else if(typeof json.likes !== "undefined"){ //Facebook Fan page
count += parseInt(json.likes, 10); //changed shares to likes to use with fanPage url
self.options.buttons[name].url = json.link;
}
else if(typeof json.shares !== "undefined"){ //Facebook
count += parseInt(json.shares, 10);
}
else if(typeof json[0] !== "undefined"){ //Delicious
count += parseInt(json[0].total_posts, 10);
}
else if(typeof json[0] !== "undefined"){ //Stumbleupon
}
else if(typeof json.followers_count !== "undefined"){ //Twitter Followers
count += parseInt(json.followers_count, 10);
}
self.options.count[name] = count;
self.options.total += count;
self.renderer();
self.rendererPerso();
//console.log(json); //debug
})
.error(function() {
self.options.count[name] = 0;
self.rendererPerso();
});
}
else{
self.renderer();
self.options.count[name] = 0;
self.rendererPerso();
}
};
/* launch render methode
================================================== */
Plugin.prototype.rendererPerso = function () {
//check if this is the last social website to launch render
var shareCount = 0;
for (e in this.options.count) { shareCount++; }
if(shareCount === this.options.shareTotal){
this.options.render(this, this.options);
}
};
/* render methode
================================================== */
Plugin.prototype.renderer = function () {
var total = this.options.total,
template = this.options.template;
if(this.options.shorterTotal === true){ //format number like 1.2k or 5M
total = this.shorterTotal(total);
}
if(template !== ''){ //if there is a template
template = template.replace('{total}', total);
$(this.element).html(template);
}
else{ //template by defaults
$(this.element).html(
'<div class="box"><a class="count" href="#">' + total + '</a>' +
(this.options.title !== '' ? '<a class="share" href="#">' + this.options.title + '</a>' : '') +
'</div>'
);
}
};
/* format total numbers like 1.2k or 5M
================================================== */
Plugin.prototype.shorterTotal = function (num) {
if (num >= 1e6){
num = (num / 1e6).toFixed(2) + "M"
} else if (num >= 1e3){
num = (num / 1e3).toFixed(1) + "k"
}
return num;
};
/* Methode for open popup
================================================== */
Plugin.prototype.openPopup = function (site) {
popup[site](this.options); //open
if(this.options.enableTracking === true){ //tracking!
var tracking = {
googlePlus: {site: 'Google', action: '+1'},
facebook: {site: 'facebook', action: 'like'},
twitter: {site: 'twitter', action: 'tweet'},
digg: {site: 'digg', action: 'add'},
delicious: {site: 'delicious', action: 'add'},
stumbleupon: {site: 'stumbleupon', action: 'add'},
linkedin: {site: 'linkedin', action: 'share'},
pinterest: {site: 'pinterest', action: 'pin'},
vk: {site: 'vk', action: 'like'}
};
_gaq.push(['_trackSocial', tracking[site].site, tracking[site].action]);
}
};
/* Methode for add +1 to a counter
================================================== */
Plugin.prototype.simulateClick = function () {
var html = $(this.element).html();
$(this.element).html(html.replace(this.options.total, this.options.total+1));
};
/* Methode for add +1 to a counter
================================================== */
Plugin.prototype.update = function (url, text) {
if(url !== ''){
this.options.url = url;
}
if(text !== ''){
this.options.text = text;
}
};
// Changes global counter DOM element value
Plugin.prototype.updateCounter = function ( name, change ) {
if ( typeof change === 'undefined' ) {
change = 1;
}
this.options.count[name] += change;
this.options.total += change;
$(this.element).find('a.count:first').text(this.options.total);
}
/* A really lightweight plugin wrapper around the constructor, preventing against multiple instantiations
================================================== */
$.fn[pluginName] = function ( options ) {
var args = arguments;
if (options === undefined || typeof options === 'object') {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
return this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
}
});
}
};
})(jQuery, window, document);