Skip to content

Commit

Permalink
Refactor share-buttons.js extract a socialButtonClickHandler function
Browse files Browse the repository at this point in the history
  • Loading branch information
kudashevs committed Aug 11, 2023
1 parent 390da66 commit 4751934
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions resources/assets/js/share-buttons.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
document.addEventListener("DOMContentLoaded", () => {
const popupWidth = 780;
const popupHeight = 550;

let socialButtons = document.querySelectorAll('.social-button');

socialButtons.forEach((button) => {
button.addEventListener('click', clickHandler)
button.addEventListener('click', socialButtonClickHandler)
})
});

function socialButtonClickHandler(e) {
const popupWidth = 780;
const popupHeight = 550;

function clickHandler(e) {
if ((e.target.id || e.target.parentElement.id) === 'clip') {
e.preventDefault();
if (window.clipboardData && window.clipboardData.setData) {
clipboardData.setData("Text", this.href);
} else {
let textArea = document.createElement("textarea");
textArea.value = this.href;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy"); // Security exception may be thrown by some browsers.
textArea.remove();
}
return;
if ((e.target.id || e.target.parentElement.id) === 'clip') {
e.preventDefault();
if (window.clipboardData && window.clipboardData.setData) {
clipboardData.setData("Text", this.href);
} else {
let textArea = document.createElement("textarea");
textArea.value = this.href;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy"); // Security exception may be thrown by some browsers.
textArea.remove();
}
return;
}

let windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
let windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
let windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
let windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

let vPosition = Math.floor((windowWidth - popupWidth) / 2),
hPosition = Math.floor((windowHeight - popupHeight) / 2);
let vPosition = Math.floor((windowWidth - popupWidth) / 2),
hPosition = Math.floor((windowHeight - popupHeight) / 2);

let popup = window.open(this.href, 'social',
'width=' + popupWidth + ',height=' + popupHeight +
',left=' + vPosition + ',top=' + hPosition +
',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1');
let popup = window.open(this.href, 'social',
'width=' + popupWidth + ',height=' + popupHeight +
',left=' + vPosition + ',top=' + hPosition +
',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1');

if (popup) {
popup.focus();
e.preventDefault();
}
if (popup) {
popup.focus();
e.preventDefault();
}
});
}

0 comments on commit 4751934

Please sign in to comment.