Make the share event work on any answer
...not just the initially loaded set
This commit is contained in:
parent
08a0f2e1ca
commit
21c4e0662f
|
@ -1,17 +1,10 @@
|
|||
import registerEvents from 'utilities/registerEvents';
|
||||
import { createShareEvent } from './share';
|
||||
import { on } from 'retrospring/utilities/on';
|
||||
import { shareEventHandler } from './share';
|
||||
|
||||
export default (): void => {
|
||||
if ('share' in navigator) {
|
||||
document.body.classList.add('cap-web-share');
|
||||
const entries: NodeList = document.querySelectorAll('.answerbox:not(.js-initialized)');
|
||||
|
||||
entries.forEach((element: HTMLElement) => {
|
||||
registerEvents([
|
||||
{ type: 'click', target: element.querySelector('[name=ab-share]'), handler: createShareEvent(element) }
|
||||
]);
|
||||
|
||||
element.classList.add('js-initialized');
|
||||
});
|
||||
on('click', '[name=ab-share]', shareEventHandler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import noop from 'utilities/noop';
|
||||
|
||||
export function createShareEvent(answerbox: HTMLElement): () => void {
|
||||
return function (): void {
|
||||
navigator.share({
|
||||
url: answerbox.querySelector<HTMLAnchorElement>('.answerbox__answer-date > a, a.answerbox__permalink').href
|
||||
})
|
||||
export function shareEventHandler(event: Event): void {
|
||||
const answerbox = (event.target as HTMLElement).closest('.answerbox');
|
||||
|
||||
navigator.share({
|
||||
url: answerbox.querySelector<HTMLAnchorElement>('.answerbox__answer-date > a, a.answerbox__permalink').href
|
||||
})
|
||||
.then(noop)
|
||||
.catch(noop)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue