docbrown/app/assets/javascripts/initializers/initializeCommentDropdown.js
ludwiczakpawel 16f4d7d46a
[deploy] Article Design update (#8234)
* flare tag line height

* .

* init

* widgets

* widgets lists

* new tabs on home

* instantclick

* .

* rethinking css

* .

* .

* empty space

* init

* campaign widget

* .

* merge

* .

* update layout

* fix sidebars on home page

* .

* fix onboarding x

* test

* spec

* test

* toolbar fix

* more

* test

* better handling ads

* .

* spec

* card border

* .

* .

* actions bar

* cleanup

* animation

* test

* button width

* .

* better responsiveness and author boxes

* meta info

* padding

* better animation

* optimize videos

* fixes

* spec

* .

* codeblocks in comments

* whoops

* Use .present? correctly as it preloads items

* sticky nav

* Update app/views/articles/_sticky_nav.html.erb

I don't know what I'm doing but @benhalpern says so! e8c0f337a5 (r440248874)

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>

* Update app/views/articles/show.html.erb

I don't know what I'm doing but @benhalpern says so! e8c0f337a5 (r440247802)

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>

* little fixes

* Update app/assets/stylesheets/article-show.scss

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>

* pawel updating ruby code......

* actually better merge

* semantic updates

* .

Co-authored-by: rhymes <rhymesete@gmail.com>
Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
2020-06-17 09:57:57 -04:00

139 lines
3.7 KiB
JavaScript

function initializeCommentDropdown() {
const announcer = document.getElementById('article-copy-link-announcer');
function isClipboardSupported() {
return (
typeof navigator.clipboard !== 'undefined' && navigator.clipboard !== null
);
}
function isNativeAndroidDevice() {
return (
navigator.userAgent === 'DEV-Native-android' &&
typeof AndroidBridge !== 'undefined' &&
AndroidBridge !== null
);
}
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 execCopyText() {
showAnnouncer();
document.execCommand('copy');
}
function copyText() {
const inputValue = document.getElementById('article-copy-link-input').value;
if (isNativeAndroidDevice()) {
AndroidBridge.copyToClipboard(inputValue);
showAnnouncer();
} else if (isClipboardSupported()) {
navigator.clipboard
.writeText(inputValue)
.then(() => {
showAnnouncer();
})
.catch((err) => {
execCopyText();
});
} else {
execCopyText();
}
}
function shouldCloseDropdown(event) {
return !(
event.target.matches('.dropdown-icon') ||
event.target.matches('.dropbtn') ||
event.target.matches('clipboard-copy') ||
event.target.matches('clipboard-copy input') ||
event.target.matches('clipboard-copy svg') ||
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) {
var button = e.target.parentElement;
var dropdownContent = button.parentElement.getElementsByClassName(
'crayons-dropdown',
)[0];
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 addDropdownListener(dropdown) {
dropdown.addEventListener('click', dropdownFunction);
}
setTimeout(function addListeners() {
getAllByClassName('dropbtn').forEach(addDropdownListener);
}, 100);
}