-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
218 lines (169 loc) · 5.59 KB
/
index.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
/* global FB */
import Domodule from 'domodule';
import { findOne, on } from 'domassist';
const BASE_URLS = {
facebook: 'https://www.facebook.com/sharer/sharer.php',
linkedin: 'https://www.linkedin.com/shareArticle',
pinterest: 'https://pinterest.com/pin/create/button',
reddit: 'https://reddit.com/submit',
twitter: 'https://twitter.com/intent/tweet',
whatsapp: 'https://api.whatsapp.com/send'
};
export default class SocialShareButton extends Domodule {
postInit() {
const setupMethod = `${this.options.net}Setup`;
const shareMethod = `${this.options.net}Share`;
if (!this[shareMethod] && !this[setupMethod]) {
throw new Error(`Sharing method for ${this.options.net} is not implemented`);
}
if (this[setupMethod]) {
this[setupMethod]();
}
if (this[shareMethod]) {
on(this.el, 'click', event => {
event.preventDefault();
this[shareMethod]();
});
}
}
get required() {
return {
options: ['net']
};
}
get defaults() {
return {
relative: false,
baseUrl: false
};
}
getShareUrl() {
let url = window.location.href;
if (this.options.baseUrl) {
if (this.options.relative) {
url = `${window.location.origin}${window.location.pathname}${this.options.baseUrl}`;
} else {
url = this.options.baseUrl;
}
}
return url;
}
twitterShare() {
SocialShareButton.openWindow(this.el.href, '260', '500', 'twitterWindow');
}
linkedinShare() {
SocialShareButton.openWindow(this.el.href, '520', '570', 'linkedinWindow');
}
pinterestShare() {
SocialShareButton.openWindow(this.el.href, '600', '600', 'pinterestWindow');
}
redditShare() {
SocialShareButton.openWindow(this.el.href, '600', '600', 'redditWindow');
}
whatsappShare() {
SocialShareButton.openWindow(this.el.href, '550', '400', 'whatsappWindow');
}
facebookShare() {
if (typeof window.FB !== 'undefined' &&
typeof window.FB.ui !== 'undefined') {
const object = {
method: 'share',
href: this.getShareUrl()
};
if (this.options.tag) {
object.hashtag = `#${this.options.tag}`;
}
if (this.options.text) {
object.quote = this.options.text;
}
FB.ui(object);
} else {
SocialShareButton.openWindow(this.el.href, '440', '600', 'facebookWindow');
}
}
facebookSetup() {
const params = [`u=${this.getShareUrl()}`];
if (this.options.tag) {
params.push(`hashtag=${encodeURIComponent(`#${this.options.tag}`)}`);
}
if (this.options.text) {
params.push(`quote=${encodeURIComponent(this.options.text)}`);
}
this.el.href = `${BASE_URLS.facebook}?${params.join('&')}`;
}
emailSetup() {
const title = encodeURIComponent(this.options.subject || document.title);
let body = this.options.body || 'Check this out #url';
body = body.replace(/#url/gi, this.getShareUrl());
body = encodeURIComponent(body);
this.el.href = `mailto:?subject=${title}&body=${body}`;
}
linkedinSetup() {
// linkedin only allows the url param for security reasons:
this.el.href = `${BASE_URLS.linkedin}?mini=true&url=${encodeURIComponent(this.getShareUrl())}`;
}
twitterSetup() {
const shareText = this.options.text || SocialShareButton.getMeta('text', 'twi');
const shareTag = this.options.tags || SocialShareButton.getMeta('hashtag', 'twi');
const shareVia = this.options.via || SocialShareButton.getMeta('author', 'twi');
const shareImage = this.options.image || SocialShareButton.getMeta('image', 'twi');
const params = [];
params.push(`url=${encodeURIComponent(this.getShareUrl())}`);
if (shareText) {
params.push(`text=${encodeURIComponent(shareText)}`);
}
if (shareTag && shareTag !== 'none') {
params.push(`hashtags=${encodeURIComponent(shareTag)}`);
}
if (shareVia && shareVia !== 'none') {
params.push(`via=${encodeURIComponent(shareVia)}`);
}
if (shareImage) {
params.push(`media=${encodeURIComponent(shareImage)}`);
}
this.el.href = `${BASE_URLS.twitter}?${params.join('&')}`;
}
pinterestSetup() {
const shareTitle = this.options.title || SocialShareButton.getMeta('title');
const shareMedia = this.options.media || SocialShareButton.getMeta('image');
const params = [
`url=${encodeURIComponent(this.getShareUrl())}`
];
if (shareTitle) {
params.push(`description=${encodeURIComponent(shareTitle)}`);
}
if (shareMedia) {
params.push(`media=${encodeURIComponent(shareMedia)}`);
}
this.el.href = `${BASE_URLS.pinterest}?${params.join('&')}`;
}
redditSetup() {
const shareTitle = this.options.title;
const params = [
`url=${encodeURIComponent(this.getShareUrl())}`
];
if (shareTitle) {
params.push(`title=${encodeURIComponent(shareTitle)}`);
}
this.el.href = `${BASE_URLS.reddit}?${params.join('&')}`;
}
whatsappSetup() {
const shareText = this.options.text || SocialShareButton.getMeta('text');
const params = [];
if (shareText) {
params.push(`text=${encodeURIComponent(shareText)}`);
}
this.el.href = `${BASE_URLS.whatsapp}?${params.join('&')}`;
}
static getMeta(tag, prop = 'og') {
const meta = findOne(`meta[property="${prop}:${tag}"]`);
return meta ? meta.getAttribute('content') : null;
}
static openWindow(url, height, width, key) {
window.open(
url,
key,
`menubar=no,toolbar=no,left=200,top=200,resizable=yes,scrollbars=no,height=${height},width=${width}`);
}
}
Domodule.register('SocialShareButton', SocialShareButton);