social-share-button
Version:
Sharing button behaviour.
269 lines (211 loc) • 8.01 kB
JavaScript
import Domodule from 'domodule';
import { on, findOne } from 'domassist';
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
var 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',
bluesky: 'https://bsky.app/intent/compose',
whatsapp: 'https://api.whatsapp.com/send'
};
var SocialShareButton =
/*#__PURE__*/
function (_Domodule) {
_inheritsLoose(SocialShareButton, _Domodule);
function SocialShareButton() {
return _Domodule.apply(this, arguments) || this;
}
var _proto = SocialShareButton.prototype;
_proto.postInit = function postInit() {
var _this = this;
var setupMethod = this.options.net + "Setup";
var 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', function (event) {
event.preventDefault();
_this[shareMethod]();
});
}
};
_proto.getShareUrl = function getShareUrl() {
var 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;
};
_proto.twitterShare = function twitterShare() {
SocialShareButton.openWindow(this.el.href, '260', '500', 'twitterWindow');
};
_proto.blueSkyShare = function blueSkyShare() {
SocialShareButton.openWindow(this.el.href, '260', '500', 'blueskyWindow');
};
_proto.linkedinShare = function linkedinShare() {
SocialShareButton.openWindow(this.el.href, '520', '570', 'linkedinWindow');
};
_proto.pinterestShare = function pinterestShare() {
SocialShareButton.openWindow(this.el.href, '600', '600', 'pinterestWindow');
};
_proto.redditShare = function redditShare() {
SocialShareButton.openWindow(this.el.href, '600', '600', 'redditWindow');
};
_proto.whatsappShare = function whatsappShare() {
SocialShareButton.openWindow(this.el.href, '550', '400', 'whatsappWindow');
};
_proto.facebookShare = function facebookShare() {
if (typeof window.FB !== 'undefined' && typeof window.FB.ui !== 'undefined') {
var 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');
}
};
_proto.facebookSetup = function facebookSetup() {
var 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('&');
};
_proto.emailSetup = function emailSetup() {
var title = encodeURIComponent(this.options.subject || document.title);
var 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;
};
_proto.linkedinSetup = function linkedinSetup() {
// linkedin only allows the url param for security reasons:
this.el.href = BASE_URLS.linkedin + "?mini=true&url=" + encodeURIComponent(this.getShareUrl());
};
_proto.twitterSetup = function twitterSetup() {
var shareText = this.options.text || SocialShareButton.getMeta('text', 'twi');
var shareTag = this.options.tags || SocialShareButton.getMeta('hashtag', 'twi');
var shareVia = this.options.via || SocialShareButton.getMeta('author', 'twi');
var shareImage = this.options.image || SocialShareButton.getMeta('image', 'twi');
var 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('&');
};
_proto.blueskySetup = function blueskySetup() {
var shareText = this.options.text || SocialShareButton.getMeta('text', 'twi');
var params = [];
var fullText = shareText + "\n" + this.getShareUrl();
if (shareText) {
params.push("text=" + encodeURIComponent(fullText));
}
this.el.href = BASE_URLS.bluesky + "?" + params.join('&');
};
_proto.pinterestSetup = function pinterestSetup() {
var shareTitle = this.options.title || SocialShareButton.getMeta('title');
var shareMedia = this.options.media || SocialShareButton.getMeta('image');
var 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('&');
};
_proto.redditSetup = function redditSetup() {
var shareTitle = this.options.title;
var params = ["url=" + encodeURIComponent(this.getShareUrl())];
if (shareTitle) {
params.push("title=" + encodeURIComponent(shareTitle));
}
this.el.href = BASE_URLS.reddit + "?" + params.join('&');
};
_proto.whatsappSetup = function whatsappSetup() {
var shareText = this.options.text || SocialShareButton.getMeta('text');
var params = [];
if (shareText) {
params.push("text=" + encodeURIComponent(shareText));
}
this.el.href = BASE_URLS.whatsapp + "?" + params.join('&');
};
SocialShareButton.getMeta = function getMeta(tag, prop) {
if (prop === void 0) {
prop = 'og';
}
var meta = findOne("meta[property=\"" + prop + ":" + tag + "\"]");
return meta ? meta.getAttribute('content') : null;
};
SocialShareButton.openWindow = function 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);
};
_createClass(SocialShareButton, [{
key: "required",
get: function get() {
return {
options: ['net']
};
}
}, {
key: "defaults",
get: function get() {
return {
relative: false,
baseUrl: false
};
}
}]);
return SocialShareButton;
}(Domodule);
Domodule.register('SocialShareButton', SocialShareButton);
export default SocialShareButton;
//# sourceMappingURL=social-share-button.js.map