-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharer.js
144 lines (112 loc) · 4.52 KB
/
Sharer.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
"use strict";
function Sharer (options) {
options = options || {};
var self = this;
var def = {
"vk": "https://vk.com/share.php",
"fb": "https://www.facebook.com/dialog/share",
"tw": "https://twitter.com/intent/tweet"
};
this.share_vk = function (e) {
var node = e.currentTarget;
var has_title = node.hasAttribute("data-title");
var has_text = node.hasAttribute("data-text");
var has_picture = node.hasAttribute("data-picture");
var has_url = node.hasAttribute("data-url");
var qs = "";
if (has_url) {
qs += "?url=" + encodeURIComponent(node.getAttribute("data-url"));
} else {
qs += "?url=" + window.location.href;
}
if (has_title) {
qs += "&title=" + encodeURIComponent(node.getAttribute("data-title"));
}
if (has_text) {
qs += "&description=" + encodeURIComponent(node.getAttribute("data-text"));
}
if (has_picture) {
qs += "&image=" + encodeURIComponent(node.getAttribute("data-picture"));
}
this._openPopup(def.vk + qs, "vk");
}
this.share_fb = function (e) {
var node = e.currentTarget;
var meta_app_id = document.querySelector("meta[property='fb:app_id']");
var qs = "?display=page";
var has_title = node.hasAttribute("data-title");
var has_text = node.hasAttribute("data-text");
var has_picture = node.hasAttribute("data-picture");
var has_url = node.hasAttribute("data-url");
if (meta_app_id) {
qs += "&app_id=" + meta_app_id.getAttribute("content");
}
if (has_title) {
qs += "&title=" + encodeURIComponent(node.getAttribute("data-title"));
}
if (has_text) {
qs += "&description=" + encodeURIComponent(node.getAttribute("data-text"));
}
if (has_picture) {
qs += "&picture=" + encodeURIComponent(node.getAttribute("data-picture"));
}
if (has_url) {
qs += "&href=" + encodeURIComponent(node.getAttribute("data-url"));
} else {
qs += "&href=" + window.location.href;
}
this._openPopup(def.fb + qs, "fb");
}
this.share_tw = function (e) {
var node = e.currentTarget;
var meta_title = document.querySelector("meta[property='og:title']");
var meta_creator = document.querySelector("meta[name='twitter:creator']");
var has_text = node.hasAttribute("data-text");
var text = document.title;
var url = window.location.href,
qs = "?url=" + url,
via;
if (meta_title) {
text = meta_title.getAttribute("content");
}
if (has_text) {
text = node.getAttribute("data-text");
}
if (meta_creator) {
via = meta_creator.getAttribute("content").replace('@', '');
}
if (text) {
qs += "&text=«" + encodeURIComponent(text) + "»";
}
if (via) {
qs += "&via=" + via;
}
this._openPopup(def.tw + qs, "tw");
}
this._openPopup = function (url, name) {
var w = 800;
var h = 600;
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, name + ' Share', 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
if (window.focus) {
newWindow.focus();
}
}
this.bindEvents = function () {
var self = this;
Object.keys(def).forEach(function (network) {
var selector = "[data-share='" + network + "']";
var nodes = document.querySelectorAll(selector);
for (var i = 0, l = nodes.length; i < l; i++) {
nodes[i].addEventListener("click", self["share_" + network].bind(self));
}
});
}
this.run = this.bindEvents.bind(this);
};
module.exports = Sharer;