-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathjquery.prettySocial.js
148 lines (127 loc) · 5.25 KB
/
jquery.prettySocial.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
/**
* jQuery prettySocial: Use custom social share buttons
* Author: Sonny T. <[email protected]>, sonnyt.com
*/
(function ($) {
'use strict';
$.fn.prettySocial = function () {
/**
* Supported social sites
* @type {Object}
*/
var _sites = {
pinterest: {
url: 'http://pinterest.com/pin/create/button/?url={{url}}&media={{media}}&description={{description}}',
popup: {
width: 685,
height: 500
}
},
facebook: {
url: 'https://www.facebook.com/sharer/sharer.php?s=100&p[title]={{title}}&p[summary]={{description}}&p[url]={{url}}&p[images][0]={{media}}',
popup: {
width: 626,
height: 436
}
},
twitter: {
url: 'https://twitter.com/share?url={{url}}&via={{via}}&text={{description}}',
popup: {
width: 685,
height: 500
}
},
googleplus: {
url: 'https://plus.google.com/share?url={{url}}',
popup: {
width: 600,
height: 600
}
},
linkedin: {
url: 'https://www.linkedin.com/shareArticle?mini=true&url={{url}}&title={{title}}&summary={{description}}+&source={{via}}',
popup: {
width: 600,
height: 600
}
}
},
/**
* Pop-up window
* This method is only used on desktop browsers
* @param {Object} site Selected social site
* @param {String} url Fixed URL to open
*/
_popup = function (site, url) {
// center window
var left = (window.innerWidth/2) - (site.popup.width/2),
top = (window.innerHeight/2) - (site.popup.height/2);
return window.open(url, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + site.popup.width + ', height=' + site.popup.height + ', top=' + top + ', left=' + left);
},
/**
* Prepare link based on social sites
* @param {Object} site Selected social site
* @param {Object} link Link with variables
* @return {String} Polished link based on the social site template
*/
_linkFix = function (site, link) {
// replace template url
var url = site.url.replace(/{{url}}/g, encodeURIComponent(link.url))
.replace(/{{title}}/g, encodeURIComponent(link.title))
.replace(/{{description}}/g, encodeURIComponent(link.description))
.replace(/{{media}}/g, encodeURIComponent(link.media))
.replace(/{{via}}/g, encodeURIComponent(link.via));
return url;
};
return this.each(function() {
// declare $(this) as variable
var $this = $(this);
// link type
var type = $this.data('type'),
// set site
site = _sites[type] || null;
// check if social site is selected
if (!site) {
$.error('Social site is not set.');
}
// gather link info
var link = {
url: $this.data('url') || '',
title: $this.data('title') || '',
description: $this.data('description') || '',
media: $this.data('media') || '',
via: $this.data('via') || ''
};
// prepare link
var url = _linkFix(site, link);
// if not, set click trigger
if (navigator.userAgent.match(/Android|IEMobile|BlackBerry|iPhone|iPad|iPod|Opera Mini/i)) {
$this
.bind('touchstart', function (e) {
if(e.originalEvent.touches.length > 1) {
return;
}
$this.data('touchWithoutScroll', true);
})
.bind('touchmove', function () {
$this.data('touchWithoutScroll', false);
return;
}).bind('touchend', function (e) {
e.preventDefault();
var touchWithoutScroll = $this.data('touchWithoutScroll');
if (e.originalEvent.touches.length > 1 || !touchWithoutScroll) {
return;
}
// call popup window
_popup(site, url);
});
} else {
$this.bind('click', function (e) {
e.preventDefault();
// call popup window
_popup(site, url);
});
}
});
};
})(jQuery);