-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp-share.js
286 lines (231 loc) · 10.8 KB
/
bsp-share.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
/**
* Default BSP Share Plugin
*
* This allows you turn some HTML markup into a share bar, utilizing the currently known sharing URLs for the most
* popular sharing services. It's customizable, in that you can pass in a custom sharing service, urls,
* popup sizes, etc as required.
*
* The most basic use example is designating a piece of the DOM with some elements that have classes named
* for each of the supported services.
* 1) You can leave off any services if you do not want those.
* 2) You also need to pass in the facebook app id.
* 3) The plugin also drops on an icon class for you, so if you are not using the default 'fa' class, you will want to pass in an override
* 4) Lastly, if you need link text vs just an icon, you can pass in an optional linkText in each of the serviceProps throught the data-bsp-share-options pattern
*
* <div class="bsp-sharing" data-bsp-share data-bsp-share-options='{"iconClass":"icon", "serviceProps":{"facebook":{"appId":"645138725541385"}}}'>
* <div class="bsp-facebook-share"></div>
* <div class="bsp-twitter-share"></div>
* <div class="bsp-google-share"></div>
* <div class="bsp-pinterest-share"></div>
* <div class="bsp-linkedin-share"></div>
* </div>
*
* The plugin allows you to customize just about everything, and below are a couple of examples of share-options that you can pass in
*
* 1) Instead of using the meta tags, pass in your own title and description
* data-bsp-share-options='{"title": "My Custom Title", "description": "My Custom Description"}'
*
* 2) Add in a tracking URL to track Google Shares
* data-bsp-share-options='{"serviceProps":{"google":{"trackingUrl":"http://asdf.com"}}}'
*
* 3) Instead of the default "bsp-service-share" class name, put in your own namespace for the share div classes and links
* ex: <div class="asdf-facebook-share"></div> and your a will be .asdf-share-link if you need to style them
* data-bsp-share-options='{"serviceClassBefore":"asdf-", "sharingClass": "asdf-share-link"}''
*
*/
import $ from 'jquery';
import bsp_utils from 'bsp-utils';
var module = {
serviceProps: {
'facebook' : {
'baseUrl' : 'https://www.facebook.com/dialog/feed?',
'appId' : '',
'trackingUrl' : '',
'width' : 1000,
'height' : 400,
'linkText' : ''
},
'google' : {
'baseUrl' : 'https://plus.google.com/share?',
'trackingUrl' : '',
'width' : 1000,
'height' : 400,
'linkText' : ''
},
'linkedin' : {
'baseUrl' : 'https://www.linkedin.com/shareArticle?',
'trackingUrl' : '',
'width' : 1000,
'height' : 600,
'linkText' : ''
},
'pinterest' : {
'baseUrl' : 'http://pinterest.com/pin/create/bookmarklet/?',
'trackingUrl' : '',
'width' : 1000,
'height' : 400,
'linkText' : ''
},
'tumblr' : {
'baseUrl' : 'https://www.tumblr.com/share?',
'trackingUrl' : '',
'width' : 1000,
'height' : 400,
'linkText' : ''
},
'twitter' : {
'baseUrl' : 'https://twitter.com/intent/tweet?',
'trackingUrl' : '',
'width' : 1000,
'height' : 300,
'linkText' : ''
}
},
init: function($el, options) {
var self = this;
var serviceProps;
// pull in the options from the plugin
self.$el = $el;
self.options = options;
// we have our own default service props. Pull in the overrides from the plugin, which we then
// use to extend our own defaults, to create the final serviceProps object that we use
serviceProps = options.serviceProps;
options.serviceProps = $.extend(true, self.serviceProps, serviceProps);
self._createLinks();
self._createClickHandler();
},
/**
* Public API to share to a specific service.
* This is preferable over someone trying to click on of our links manually if they want to use our plugin vs creating one of their own
*
* Example:
* var bspShare = $('.bsp-sharing[data-bsp-share]').data('bsp-share'); // pulls in the instance
* bspShare.share('facebook');
*/
share: function(service){
var self = this;
var $serviceLink = self.$el.find('[data-service=' + service + ']');
self._openSharePopup($serviceLink);
self._trackShare($serviceLink);
},
_createLinks: function() {
var self = this;
var services = self.options.services;
var $shareLink;
var currentService;
// we go through the services, and create the actual a elements that will be clicked on by the user
for (var i = 0; i < services.length; i++) {
currentService = services[i];
$shareLink = $('<a>').addClass(self.options.sharingClass)
.addClass(self.options.iconClass)
.addClass(self.options.iconClass + '-' + currentService);
$shareLink.attr('data-shareHeight', self.options.serviceProps[currentService].height);
$shareLink.attr('data-shareWidth', self.options.serviceProps[currentService].width);
$shareLink.attr('data-service', currentService);
$shareLink.attr('href', self._createShareURL(currentService));
$shareLink.attr('target', '_blank');
$shareLink.attr('title', self.options.sharingText + ' ' + currentService);
$shareLink.html(self.options.serviceProps[currentService].linkText);
$shareLink.appendTo(self.$el.find('.' + self.options.serviceClassBefore + currentService + self.options.serviceClassAfter));
}
},
_createShareURL: function(service) {
let self = this;
let serviceProps = self.options.serviceProps[service];
let shareUrl = self.options.serviceProps[service].baseUrl;
let caption = encodeURIComponent(serviceProps.caption || self.options.caption);
let description = encodeURIComponent(serviceProps.description || self.options.description);
let image = encodeURIComponent(self.options.image);
let title = encodeURIComponent(serviceProps.title || self.options.title);
let url = encodeURIComponent(self.options.url);
let redirectUrl = encodeURIComponent(self.options.redirectUrl);
// each service gets a custom URL based on the options that were passed in
switch (service) {
case 'facebook':
shareUrl += 'app_id=' + self.options.serviceProps[service].appId + '&' +
'link=' + url + '&' +
'caption=' + caption + '&' +
'description=' + description + '&' +
// this prevents facebook's popup from also showing the shared page
// and staying open w/ that 2nd instance ...
// 'redirect_uri=' + redirectUrl + '&' +
'picture=' + image;
break;
case 'google':
shareUrl += 'url=' + url;
break;
case 'linkedin':
shareUrl += 'summary=' + description + '&' +
'ro=' + 'false' + '&' +
'title=' + title + '&' +
'mini=' + 'true' + '&' +
'url=' + url;
break;
case 'pinterest':
shareUrl += 'url=' + url + '&' +
'title=' + title + '&' +
'description=' + description + '&' +
'media=' + image;
break;
case 'tumblr':
shareUrl += 'v=3&u=' + url + '&' +
't=' + title;
break;
case 'twitter':
shareUrl += 'original_referer=' + encodeURIComponent(location.href) + '&' +
'text=' + title + '&' +
'url=' + url;
break;
}
return shareUrl;
},
_createClickHandler: function() {
var self = this;
self.$el.on('click', '.' + self.options.sharingClass, function(event) {
event.preventDefault();
event.stopPropagation();
self._openSharePopup($(this));
self._trackShare($(this));
});
},
_openSharePopup: function($link) {
var self = this;
var width = $link.attr('data-shareWidth');
var height = $link.attr('data-shareHeight');
var left = (screen.width/2)-(width/2);
var top = (screen.height/2)-(height/2);
window.open($link.attr('href'), 'share', 'width='+width+', height='+height+', top='+top+', left=' + left + ' toolbar=1, resizable=0');
},
_trackShare: function() {
var self = this;
if (self.options.trackingUrl) {
$.ajax({
url: self.options.trackingUrl
});
}
}
};
var thePlugin = {
'_defaultOptions': {
'services' : ['facebook', 'google', 'linkedin', 'pinterest', 'tumblr', 'twitter'],
'caption' : $('meta[property="og:caption"]').attr('content') || '',
'description' : $('meta[property="og:description"]').attr('content') || '',
'image' : $('meta[property="og:image"]').attr('content') || '',
'title' : document.title || '',
'url' : window.location.protocol + '//' + window.location.hostname + window.location.pathname,
'redirectUrl' : window.location.protocol + '//' + window.location.hostname + window.location.pathname,
'sharingClass' : 'bsp-share-link',
'serviceClassBefore' : 'bsp-',
'serviceClassAfter' : '-share',
'sharingText' : 'Share on',
'iconClass' : 'fa'
},
'_each': function(item) {
var options = this.option(item);
var moduleInstance = Object.create(module);
moduleInstance.init($(item), options);
// expose the instance of the module on the item if anyone else needs to use the public API
$(item).data('bsp-share', moduleInstance);
}
};
export default bsp_utils.plugin(false, 'bsp', 'share', thePlugin);