Files
Bookshelf/Old/bd-murphy-author/assets/js/social-share.js
2026-10-18 19:01:01 -07:00

113 lines
3.6 KiB
JavaScript

// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* Social Share JavaScript
*/
(function() {
'use strict';
var shareContainer = document.querySelector('.social-share-buttons');
if (!shareContainer) {
return;
}
if (navigator.share) {
var webShareBtns = shareContainer.querySelectorAll('.share-btn[data-action="webshare"]');
webShareBtns.forEach(function(btn) {
btn.addEventListener('click', function(e) {
e.preventDefault();
var url = btn.dataset.url;
var title = btn.dataset.title;
navigator.share({
title: title,
url: url
}).catch(function(err) {
console.log('Error sharing:', err);
});
});
});
}
var copyBtn = shareContainer.querySelector('.share-copy');
if (copyBtn) {
copyBtn.addEventListener('click', function() {
var url = copyBtn.dataset.url;
navigator.clipboard.writeText(url).then(function() {
showToast('Link copied to clipboard!');
}).catch(function(err) {
var textArea = document.createElement('textarea');
textArea.value = url;
textArea.style.position = 'fixed';
textArea.style.left = '-9999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
showToast('Link copied to clipboard!');
} catch (err) {
showToast('Failed to copy link');
}
document.body.removeChild(textArea);
});
});
}
function showToast(message) {
var toast = document.createElement('div');
toast.className = 'share-toast';
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(function() {
toast.classList.add('show');
}, 10);
setTimeout(function() {
toast.classList.remove('show');
setTimeout(function() {
document.body.removeChild(toast);
}, 300);
}, 2000);
}
var shareBtn = document.querySelector('.share-btn:not(.share-copy)');
if (shareBtn) {
shareBtn.addEventListener('click', function() {
var btn = this;
var url = btn.href;
if (navigator.share) {
navigator.share({
title: document.title,
url: window.location.href
}).catch(function() {});
}
});
}
var formatBtns = document.querySelectorAll('.format-btn');
formatBtns.forEach(function(btn) {
btn.addEventListener('click', function() {
var format = btn.dataset.format;
var container = btn.closest('.book-entry, .book-page-content');
container.querySelectorAll('.format-btn').forEach(function(b) {
b.classList.remove('active');
});
btn.classList.add('active');
var retailerLinks = container.querySelectorAll('.book-retailer-links .retailer-link');
retailerLinks.forEach(function(link) {
if (link.dataset.format === format || link.dataset.formats && link.dataset.formats.includes(format)) {
link.style.display = '';
} else {
link.style.display = 'none';
}
});
});
});
})();