-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor share-buttons.js extract a socialButtonClickHandler function
- Loading branch information
Showing
1 changed file
with
31 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} |