|
1 | 1 | (function (window, document) {
|
2 |
| - 'use strict'; |
3 |
| - |
4 |
| - /** |
5 |
| - * Class to display the buttons of social networks. |
6 |
| - * |
7 |
| - * @author Yauheni Pakala <[email protected]> |
8 |
| - * @version 1.0 |
9 |
| - * @license MIT |
10 |
| - */ |
11 |
| - function ShareButtons() { |
12 |
| - |
13 |
| - var FB_LINK_FORMAT = 'https://www.facebook.com/sharer/sharer.php?u=', |
14 |
| - VK_LINK_FORMAT = 'https://vk.com/share.php?url={0}&description={1}. {2}', |
15 |
| - TW_LINK_FORMAT = 'https://twitter.com/intent/tweet?url=', |
16 |
| - MAIL_LINK_FORMAT = 'mailto:?Subject={0}{1}&body={2}{3}', |
17 |
| - |
18 |
| - // from https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript |
19 |
| - stringFormat = function (str, args) { |
20 |
| - return str.replace(/\{(\d+)\}/g, function (m, n) { |
21 |
| - return args[n] || m; |
22 |
| - }); |
23 |
| - }; |
24 |
| - |
25 |
| - /** |
26 |
| - * Method for initialize class for all elements |
27 |
| - */ |
28 |
| - this.init = function () { |
29 |
| - var i, |
30 |
| - share = document.querySelectorAll('.share-btn'); |
31 |
| - |
32 |
| - for (i = share.length; i--;) { |
33 |
| - this.initForElement(share[i]); |
34 |
| - } |
35 |
| - }; |
36 |
| - |
37 |
| - /** |
38 |
| - * Method for initialize class for all elements |
39 |
| - */ |
40 |
| - this.initForElement = function (el) { |
41 |
| - var i, |
42 |
| - a = el.querySelectorAll('a'); |
43 |
| - |
44 |
| - for (i = a.length; i--;) { |
45 |
| - this.prepareLink(a[i], { |
46 |
| - id: '', |
47 |
| - url: this.getUrl(el), |
48 |
| - title: this.getTitle(el), |
49 |
| - desc: this.getDesc(el) |
50 |
| - }); |
51 |
| - } |
52 |
| - }; |
53 |
| - |
54 |
| - /** |
55 |
| - * Method for handling click event to link |
56 |
| - */ |
57 |
| - this.prepareLink = function (el, options) { |
58 |
| - var that = this; |
59 |
| - |
60 |
| - options.id = el.getAttribute('data-id'); |
61 |
| - if (options.id) { |
62 |
| - that.addEventListener(el, 'click', options); |
63 |
| - } |
64 |
| - }; |
65 |
| - |
66 |
| - /** |
67 |
| - * Method for getting url from page or options |
68 |
| - */ |
69 |
| - this.getUrl = function (share) { |
70 |
| - return share.getAttribute('data-url') || location.href || ' '; |
71 |
| - }; |
72 |
| - |
73 |
| - /** |
74 |
| - * Method for getting title from page or options |
75 |
| - */ |
76 |
| - this.getTitle = function (share) { |
77 |
| - return share.getAttribute('data-title') || document.title || ' '; |
78 |
| - }; |
79 |
| - |
80 |
| - /** |
81 |
| - * Method for getting description from page or options |
82 |
| - */ |
83 |
| - this.getDesc = function (share) { |
84 |
| - var metaDesc = document.querySelector('meta[name=description]'); |
85 |
| - return share.getAttribute('data-desc') || (metaDesc && metaDesc.getAttribute('content')) || ' '; |
86 |
| - }; |
87 |
| - |
88 |
| - /** |
89 |
| - * Method for attaching event to the element |
90 |
| - */ |
91 |
| - this.addEventListener = function (el, eventName, opt) { |
92 |
| - var that = this, |
93 |
| - handler = function () { |
94 |
| - that.share(opt.id, opt.url, opt.title, opt.desc); |
95 |
| - }; |
96 |
| - |
97 |
| - if (el.addEventListener) { |
98 |
| - el.addEventListener(eventName, handler); |
99 |
| - } else { |
100 |
| - el.attachEvent('on' + eventName, function () { |
101 |
| - handler.call(el); |
102 |
| - }); |
103 |
| - } |
104 |
| - }; |
105 |
| - |
106 |
| - /** |
107 |
| - * Method for handling chosen links |
108 |
| - */ |
109 |
| - this.share = function (id, urlDef, titleDef, descDef) { |
110 |
| - var url = encodeURIComponent(urlDef), |
111 |
| - desc = encodeURIComponent(descDef), |
112 |
| - title = encodeURIComponent(titleDef), |
113 |
| - text = title || desc || ''; |
114 |
| - |
115 |
| - switch (id) { |
116 |
| - case 'fb': |
117 |
| - this.popupCenter(FB_LINK_FORMAT + url, titleDef); |
118 |
| - break; |
119 |
| - |
120 |
| - case 'vk': |
121 |
| - this.popupCenter(stringFormat(VK_LINK_FORMAT, [url, title, desc]), titleDef); |
122 |
| - break; |
123 |
| - |
124 |
| - case 'tw': |
125 |
| - if (title.length > 0 && desc.length > 0) { |
126 |
| - text = title + ' - ' + desc; |
127 |
| - } |
128 |
| - if (text.length > 0) { |
129 |
| - text = '&text=' + text; |
130 |
| - } |
131 |
| - |
132 |
| - this.popupCenter(TW_LINK_FORMAT + url + text, titleDef); |
133 |
| - break; |
134 |
| - |
135 |
| - case 'mail': |
136 |
| - if (title.length > 0 && desc.length > 0) { |
137 |
| - text = title + ' - ' + desc; |
138 |
| - } |
139 |
| - if (text.length > 0) { |
140 |
| - text = text + ' / '; |
141 |
| - } |
142 |
| - if (title.length > 0) { |
143 |
| - title += ' / '; |
144 |
| - } |
145 |
| - |
146 |
| - location.href = stringFormat(MAIL_LINK_FORMAT, [title, titleDef, text, url]); |
147 |
| - break; |
148 |
| - |
149 |
| - default: |
150 |
| - break; |
151 |
| - } |
152 |
| - }; |
153 |
| - |
154 |
| - /** |
155 |
| - * Method for opening popup window |
156 |
| - */ |
157 |
| - this.popupCenter = function (url, title) { |
158 |
| - var w = 600, |
159 |
| - h = 400, |
160 |
| - dualScreenLeft = typeof window.screenLeft !== 'undefined' ? window.screenLeft : screen.left, |
161 |
| - dualScreenTop = typeof window.screenTop !== 'undefined' ? window.screenTop : screen.top, |
162 |
| - width = window.innerWidth || document.documentElement.clientWidth || screen.width, |
163 |
| - height = window.innerHeight || document.documentElement.clientHeight || screen.height, |
164 |
| - left = ((width / 2) - (w / 2)) + dualScreenLeft, |
165 |
| - top = ((height / 3) - (h / 3)) + dualScreenTop, |
166 |
| - windowFormat = 'resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width={0},height={1},top={2},left={3}', |
167 |
| - newWindow = window.open(url, '', stringFormat(windowFormat, [w, h, top, left])); |
168 |
| - |
169 |
| - if (newWindow !== null && newWindow.focus) { |
170 |
| - newWindow.focus(); |
171 |
| - } |
172 |
| - }; |
173 |
| - } |
174 |
| - |
175 |
| - // start |
176 |
| - new ShareButtons().init(); |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /** |
| 5 | + * Class to display the buttons of social networks. |
| 6 | + * |
| 7 | + * @author Yauheni Pakala <https://wcoder.github.io> |
| 8 | + * @version 1.0 |
| 9 | + * @license MIT |
| 10 | + */ |
| 11 | + function ShareButtons() { |
| 12 | + |
| 13 | + var FB_LINK_FORMAT = 'https://www.facebook.com/sharer/sharer.php?u=', |
| 14 | + VK_LINK_FORMAT = 'https://vk.com/share.php?url={0}&description={1}. {2}', |
| 15 | + TW_LINK_FORMAT = 'https://twitter.com/intent/tweet?url=', |
| 16 | + MAIL_LINK_FORMAT = 'mailto:?Subject={0}{1}&body={2}{3}', |
| 17 | + |
| 18 | + // from https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript |
| 19 | + stringFormat = function (str, args) { |
| 20 | + return str.replace(/\{(\d+)\}/g, function (m, n) { |
| 21 | + return args[n] || m; |
| 22 | + }); |
| 23 | + }; |
| 24 | + |
| 25 | + /** |
| 26 | + * Method for initialize class for all elements |
| 27 | + */ |
| 28 | + this.init = function () { |
| 29 | + var i, share = document.querySelectorAll('.share-btn'); |
| 30 | + |
| 31 | + for (i = share.length; i--;) { |
| 32 | + this.initForElement(share[i]); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + /** |
| 37 | + * Method for initialize class for all elements |
| 38 | + */ |
| 39 | + this.initForElement = function (el) { |
| 40 | + var i, a = el.querySelectorAll('a'); |
| 41 | + |
| 42 | + for (i = a.length; i--;) { |
| 43 | + this.prepareLink(a[i], { |
| 44 | + id: '', |
| 45 | + url: this.getUrl(el), |
| 46 | + title: this.getTitle(el), |
| 47 | + desc: this.getDesc(el) |
| 48 | + }); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * Method for handling click event to link |
| 54 | + */ |
| 55 | + this.prepareLink = function (el, options) { |
| 56 | + var that = this; |
| 57 | + |
| 58 | + options.id = el.getAttribute('data-id'); |
| 59 | + if (options.id) { |
| 60 | + that.addEventListener(el, 'click', options); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * Method for getting url from page or options |
| 66 | + */ |
| 67 | + this.getUrl = function (share) { |
| 68 | + return share.getAttribute('data-url') || location.href || ' '; |
| 69 | + }; |
| 70 | + |
| 71 | + /** |
| 72 | + * Method for getting title from page or options |
| 73 | + */ |
| 74 | + this.getTitle = function (share) { |
| 75 | + return share.getAttribute('data-title') || document.title || ' '; |
| 76 | + }; |
| 77 | + |
| 78 | + /** |
| 79 | + * Method for getting description from page or options |
| 80 | + */ |
| 81 | + this.getDesc = function (share) { |
| 82 | + var metaDesc = document.querySelector('meta[name=description]'); |
| 83 | + return share.getAttribute('data-desc') || (metaDesc && metaDesc.getAttribute('content')) || ' '; |
| 84 | + }; |
| 85 | + |
| 86 | + /** |
| 87 | + * Method for attaching event to the element |
| 88 | + */ |
| 89 | + this.addEventListener = function (el, eventName, opt) { |
| 90 | + var that = this, |
| 91 | + handler = function () { |
| 92 | + that.share(opt.id, opt.url, opt.title, opt.desc); |
| 93 | + }; |
| 94 | + |
| 95 | + if (el.addEventListener) { |
| 96 | + el.addEventListener(eventName, handler); |
| 97 | + } else { |
| 98 | + el.attachEvent('on' + eventName, function () { |
| 99 | + handler.call(el); |
| 100 | + }); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + /** |
| 105 | + * Method for handling chosen links |
| 106 | + */ |
| 107 | + this.share = function (id, urlDef, titleDef, descDef) { |
| 108 | + var url = encodeURIComponent(urlDef), |
| 109 | + desc = encodeURIComponent(descDef), |
| 110 | + title = encodeURIComponent(titleDef), |
| 111 | + text = title || desc || ''; |
| 112 | + |
| 113 | + switch (id) { |
| 114 | + case 'fb': |
| 115 | + this.popupCenter(FB_LINK_FORMAT + url, titleDef); |
| 116 | + break; |
| 117 | + |
| 118 | + case 'vk': |
| 119 | + this.popupCenter(stringFormat(VK_LINK_FORMAT, [url, title, desc]), titleDef); |
| 120 | + break; |
| 121 | + |
| 122 | + case 'tw': |
| 123 | + if (title.length > 0 && desc.length > 0) { |
| 124 | + text = title + ' - ' + desc; |
| 125 | + } |
| 126 | + if (text.length > 0) { |
| 127 | + text = '&text=' + text; |
| 128 | + } |
| 129 | + |
| 130 | + this.popupCenter(TW_LINK_FORMAT + url + text, titleDef); |
| 131 | + break; |
| 132 | + |
| 133 | + case 'mail': |
| 134 | + if (title.length > 0 && desc.length > 0) { |
| 135 | + text = title + ' - ' + desc; |
| 136 | + } |
| 137 | + if (text.length > 0) { |
| 138 | + text = text + ' / '; |
| 139 | + } |
| 140 | + if (title.length > 0) { |
| 141 | + title += ' / '; |
| 142 | + } |
| 143 | + |
| 144 | + location.href = stringFormat(MAIL_LINK_FORMAT, [title, titleDef, text, url]); |
| 145 | + break; |
| 146 | + |
| 147 | + default: |
| 148 | + break; |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + /** |
| 153 | + * Method for opening popup window |
| 154 | + */ |
| 155 | + this.popupCenter = function (url, title) { |
| 156 | + var w = 600, |
| 157 | + h = 400, |
| 158 | + dualScreenLeft = typeof window.screenLeft !== 'undefined' ? window.screenLeft : screen.left, |
| 159 | + dualScreenTop = typeof window.screenTop !== 'undefined' ? window.screenTop : screen.top, |
| 160 | + width = window.innerWidth || document.documentElement.clientWidth || screen.width, |
| 161 | + height = window.innerHeight || document.documentElement.clientHeight || screen.height, |
| 162 | + left = ((width / 2) - (w / 2)) + dualScreenLeft, |
| 163 | + top = ((height / 3) - (h / 3)) + dualScreenTop, |
| 164 | + windowFormat = 'resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width={0},height={1},top={2},left={3}', |
| 165 | + newWindow = window.open(url, '', stringFormat(windowFormat, [w, h, top, left])); |
| 166 | + |
| 167 | + if (newWindow !== null && newWindow.focus) { |
| 168 | + newWindow.focus(); |
| 169 | + } |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + // start |
| 174 | + new ShareButtons().init(); |
177 | 175 |
|
178 | 176 | }(window, document));
|
0 commit comments