Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Ui / Message / Share.js
1 /**
2 * Provides buttons to share a page through multiple social community sites.
3 *
4 * @author Marcel Werk
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Ui/Message/Share
8 */
9 define(['EventHandler'], function(EventHandler) {
10 "use strict";
11
12 /**
13 * @exports WoltLabSuite/Core/Ui/Message/Share
14 */
15 return {
16 _pageDescription: '',
17 _pageUrl: '',
18
19 init: function() {
20 var title = elBySel('meta[property="og:title"]');
21 if (title !== null) this._pageDescription = encodeURIComponent(title.content);
22 var url = elBySel('meta[property="og:url"]');
23 if (url !== null) this._pageUrl = encodeURIComponent(url.content);
24
25 elBySelAll('.jsMessageShareButtons', null, (function(container) {
26 container.classList.remove('jsMessageShareButtons');
27
28 var providers = {
29 facebook: {
30 link: elBySel('.jsShareFacebook', container),
31 share: (function() { this._share('facebook', 'https://www.facebook.com/sharer.php?u={pageURL}&t={text}', true); }).bind(this)
32 },
33 google: {
34 link: elBySel('.jsShareGoogle', container),
35 share: (function() { this._share('google', 'https://plus.google.com/share?url={pageURL}', false); }).bind(this)
36 },
37 reddit: {
38 link: elBySel('.jsShareReddit', container),
39 share: (function() { this._share('reddit', 'https://ssl.reddit.com/submit?url={pageURL}', false); }).bind(this)
40 },
41 twitter: {
42 link: elBySel('.jsShareTwitter', container),
43 share: (function() { this._share('twitter', 'https://twitter.com/share?url={pageURL}&text={text}', false); }).bind(this)
44 },
45 linkedIn: {
46 link: elBySel('.jsShareLinkedIn', container),
47 share: (function() { this._share('linkedIn', 'https://www.linkedin.com/cws/share?url={pageURL}', false); }).bind(this)
48 },
49 pinterest: {
50 link: elBySel('.jsSharePinterest', container),
51 share: (function() { this._share('pinterest', 'https://www.pinterest.com/pin/create/link/?url={pageURL}&description={text}', false); }).bind(this)
52 },
53 xing: {
54 link: elBySel('.jsShareXing', container),
55 share: (function() { this._share('xing', 'https://www.xing.com/social_plugins/share?url={pageURL}', false); }).bind(this)
56 },
57 whatsApp: {
58 link: elBySel('.jsShareWhatsApp', container),
59 share: (function() {
60 window.location.href = 'https://api.whatsapp.com/send?text=' + this._pageDescription + '%20' + this._pageUrl;
61 }).bind(this)
62 }
63 };
64
65 EventHandler.fire('com.woltlab.wcf.message.share', 'shareProvider', {
66 container: container,
67 providers: providers,
68 pageDescription: this._pageDescription,
69 pageUrl: this._pageUrl
70 });
71
72 for (var provider in providers) {
73 if (providers.hasOwnProperty(provider)) {
74 if (providers[provider].link !== null) {
75 providers[provider].link.addEventListener(WCF_CLICK_EVENT, providers[provider].share);
76 }
77 }
78 }
79 }).bind(this));
80 },
81
82 _share: function(objectName, url, appendURL) {
83 window.open(url.replace(/\{pageURL}/, this._pageUrl).replace(/\{text}/, this._pageDescription + (appendURL ? "%20" + this._pageUrl : "")), objectName, 'height=600,width=600');
84 }
85 };
86 });