1
+ ( function ( ) {
2
+ 'use strict' ;
3
+
4
+ var FB_LINK = 'https://www.facebook.com/sharer/sharer.php?u=' ;
5
+ var VK_LINK = 'https://vk.com/share.php?url={0}&description={1}. {2}' ;
6
+ var TW_LINK = 'https://twitter.com/intent/tweet?url=' ;
7
+ var GP_LINK = 'https://plus.google.com/share?url=' ;
8
+ var IN_LINK = 'https://www.linkedin.com/shareArticle?mini=true&url=' ;
9
+
10
+ // from http://evgeniy.pakalo.name/post/49
11
+ var _F = function ( str , args ) {
12
+ return str . replace ( / \{ ( \d + ) \} / g, function ( m , n ) {
13
+ return args [ n ] ? args [ n ] : m ;
14
+ } ) ;
15
+ } ;
16
+
17
+ function ShareButtons ( ) {
18
+ var width = 600 ,
19
+ height = 400 ;
20
+
21
+ this . init = function ( ) {
22
+ var _this = this ,
23
+ share = document . querySelectorAll ( '.share-btn' ) ;
24
+ for ( var i = 0 , l = share . length ; i < l ; i ++ ) {
25
+ var url = share [ i ] . getAttribute ( 'data-url' ) || location . href ,
26
+ title = share [ i ] . getAttribute ( 'data-title' ) || document . title ,
27
+ desc = share [ i ] . getAttribute ( 'data-desc' ) || ' ' ,
28
+ el = share [ i ] . querySelectorAll ( 'a' ) ;
29
+
30
+ for ( var a = 0 , al = el . length ; a < al ; a ++ ) {
31
+ var id = el [ a ] . getAttribute ( 'data-id' ) ;
32
+ if ( id ) {
33
+ _this . addEventListener ( el [ a ] , 'click' , {
34
+ id : id ,
35
+ url : url ,
36
+ title : title ,
37
+ desc : desc
38
+ } ) ;
39
+ }
40
+ }
41
+ }
42
+ } ;
43
+
44
+ this . addEventListener = function ( el , eventName , opt ) {
45
+ var _this = this ,
46
+ handler = function ( ) {
47
+ _this . share ( opt . id , opt . url , opt . title , opt . desc ) ;
48
+ } ;
49
+
50
+ if ( el . addEventListener ) {
51
+ el . addEventListener ( eventName , handler ) ;
52
+ } else {
53
+ el . attachEvent ( 'on' + eventName , function ( ) {
54
+ handler . call ( el ) ;
55
+ } ) ;
56
+ }
57
+ } ;
58
+
59
+ this . share = function ( id , url , title , desc ) {
60
+ url = encodeURIComponent ( url ) ;
61
+ desc = encodeURIComponent ( desc ) ;
62
+ title = encodeURIComponent ( title ) ;
63
+
64
+ switch ( id ) {
65
+ case 'fb' :
66
+ this . popupCenter ( FB_LINK + url , this . title , this . width , this . height ) ;
67
+ break ;
68
+
69
+ case 'vk' :
70
+ var url = _F ( VK_LINK , [ url , title , desc ] ) ;
71
+
72
+ this . popupCenter ( url , this . title , this . width , this . height ) ;
73
+ break ;
74
+
75
+ case 'tw' :
76
+ var text = title || desc || '' ;
77
+ if ( title . length > 0 && desc . length > 0 ) {
78
+ text = title + ' - ' + desc ;
79
+ }
80
+ if ( text . length > 0 ) {
81
+ text = '&text=' + text ;
82
+ }
83
+
84
+ this . popupCenter ( TW_LINK + url + text , this . title , this . width , this . height ) ;
85
+ break ;
86
+
87
+ case 'gp' :
88
+ this . popupCenter ( GP_LINK + url , this . title , this . width , this . height ) ;
89
+ break ;
90
+
91
+ case 'in' :
92
+ this . popupCenter ( IN_LINK + url , this . title , this . width , this . height ) ;
93
+ break ;
94
+
95
+ case 'mail' :
96
+ var text = title || desc || '' ;
97
+ if ( title . length > 0 && desc . length > 0 ) {
98
+ text = title + ' - ' + desc ;
99
+ }
100
+ if ( text . length > 0 ) {
101
+ text = text + ' / ' ;
102
+ }
103
+ if ( title . length > 0 ) {
104
+ title = title + ' / ' ;
105
+ }
106
+
107
+ var mail = 'mailto:?Subject=' + title + this . title + '&body=' + text + url ;
108
+ this . newTab ( mail ) ;
109
+ break ;
110
+
111
+ default :
112
+ break ;
113
+ } ;
114
+ } ;
115
+
116
+ this . newTab = function ( url ) {
117
+ var win = window . open ( url , '_blank' ) ;
118
+ win . focus ( ) ;
119
+ } ;
120
+
121
+ this . popupCenter = function ( url , title , w , h ) {
122
+ var dualScreenLeft = window . screenLeft !== undefined
123
+ ? window . screenLeft
124
+ : screen . left ;
125
+ var dualScreenTop = window . screenTop !== undefined
126
+ ? window . screenTop
127
+ : screen . top ;
128
+
129
+ var width = window . innerWidth
130
+ ? window . innerWidth
131
+ : document . documentElement . clientWidth
132
+ ? document . documentElement . clientWidth
133
+ : screen . width ;
134
+ var height = window . innerHeight
135
+ ? window . innerHeight
136
+ : document . documentElement . clientHeight
137
+ ? document . documentElement . clientHeight
138
+ : screen . height ;
139
+
140
+ var left = ( ( width / 2 ) - ( w / 2 ) ) + dualScreenLeft ;
141
+ var top = ( ( height / 3 ) - ( h / 3 ) ) + dualScreenTop ;
142
+
143
+ var newWindow = window . open ( url , title , 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left ) ;
144
+
145
+ if ( window . focus ) {
146
+ newWindow . focus ( ) ;
147
+ }
148
+ }
149
+ } ;
150
+
151
+
152
+
153
+ // start
154
+ var sb = new ShareButtons ( ) ;
155
+ sb . init ( ) ;
156
+
157
+ } ( ) ) ;
158
+
159
+
0 commit comments