docbrown/app/assets/javascripts/initializers/initializeCommentDropdown.js
Elliot 6a1f560f65
[deploy] Fix dropdowns breaking after adding comment (#10976)
What was wrong?
`initializeCommentDropdown` is called when a new comment is added.
`initializeCommentDropdown` calls `addDropdownListener(dropdown)` on all
`.dropbtn` elements. Elements with the `.dropbtn` class already exist on
the page so an extra listener is added to it whenever a comment is
added.

How was it fixed?
Whenever a dropdown listener is added to an element with the
`.dropbtn` class, an attribute is set on that element so we know it
already has a dropdown listener. When we add listeners to elements with
the `.dropbtn` class in the future, we don't add them to elements with
the attribute.
2020-10-21 08:15:10 -04:00

135 lines
3.9 KiB
JavaScript

/* global Runtime */
function initializeCommentDropdown() {
const announcer = document.getElementById('article-copy-link-announcer');
function removeClass(className) {
return (element) => element.classList.remove(className);
}
function getAllByClassName(className) {
return Array.from(document.getElementsByClassName(className));
}
function showAnnouncer() {
const { activeElement } = document;
const input =
activeElement.localName === 'clipboard-copy'
? activeElement.querySelector('input')
: document.getElementById('article-copy-link-input');
input.focus();
input.setSelectionRange(0, input.value.length);
announcer.hidden = false;
}
function hideAnnouncer() {
if (announcer) {
announcer.hidden = true;
}
}
function copyText() {
const inputValue = document.getElementById('article-copy-link-input').value;
Runtime.copyToClipboard(inputValue).then(() => {
showAnnouncer();
});
}
function shouldCloseDropdown(event) {
return !(
event.target.matches('.dropdown-icon') ||
event.target.matches('.dropbtn') ||
event.target.matches('clipboard-copy') ||
document.getElementById('article-copy-icon').contains(event.target) ||
event.target.parentElement.classList.contains('dropdown-link-row')
);
}
function removeClickListener() {
// disabling this rule because `removeEventListener` needs
// a reference to the specific handler. The function is hoisted.
// eslint-disable-next-line no-use-before-define
document.removeEventListener('click', outsideClickListener);
}
function removeCopyListener() {
const clipboardCopyElement = document.getElementsByTagName(
'clipboard-copy',
)[0];
if (clipboardCopyElement) {
clipboardCopyElement.removeEventListener('click', copyText);
}
}
function removeAllShowing() {
getAllByClassName('crayons-dropdown').forEach(removeClass('block'));
}
function outsideClickListener(event) {
if (shouldCloseDropdown(event)) {
removeAllShowing();
hideAnnouncer();
removeClickListener();
}
}
function dropdownFunction(e) {
const button = e.currentTarget;
const dropdownContent = button.parentElement.getElementsByClassName(
'crayons-dropdown',
)[0];
if (!dropdownContent) {
return;
}
// Android native apps have enhanced sharing capabilities for Articles
const articleShowMoreClicked = button.id === 'article-show-more-button';
if (articleShowMoreClicked && Runtime.isNativeAndroid('shareText')) {
AndroidBridge.shareText(location.href);
return;
}
finalizeAbuseReportLink(
dropdownContent.querySelector('.report-abuse-link-wrapper'),
);
if (dropdownContent.classList.contains('block')) {
dropdownContent.classList.remove('block');
removeClickListener();
removeCopyListener();
hideAnnouncer();
} else {
removeAllShowing();
dropdownContent.classList.add('block');
const clipboardCopyElement = document.getElementsByTagName(
'clipboard-copy',
)[0];
document.addEventListener('click', outsideClickListener);
if (clipboardCopyElement) {
clipboardCopyElement.addEventListener('click', copyText);
}
}
}
function finalizeAbuseReportLink(reportAbuseLink) {
// Add actual link location (SEO doesn't like these "useless" links, so adding in here instead of in HTML)
if (!reportAbuseLink) {
return;
}
reportAbuseLink.innerHTML = `<a href="${reportAbuseLink.dataset.path}" class="crayons-link crayons-link--block">Report Abuse</a>`;
}
function addDropdownListener(dropdown) {
if (!dropdown.getAttribute('has-dropdown-listener')) {
dropdown.addEventListener('click', dropdownFunction);
dropdown.setAttribute('has-dropdown-listener', 'true');
}
}
setTimeout(function addListeners() {
getAllByClassName('dropbtn').forEach(addDropdownListener);
}, 100);
}