From 21c4e0662f13602716332d6742e32b54fad33341 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 26 Dec 2021 23:01:16 +0100 Subject: [PATCH] Make the share event work on any answer ...not just the initially loaded set --- .../retrospring/features/answerbox/index.ts | 13 +++---------- .../retrospring/features/answerbox/share.ts | 12 ++++++------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/index.ts b/app/javascript/retrospring/features/answerbox/index.ts index 9f836842..846a1ca8 100644 --- a/app/javascript/retrospring/features/answerbox/index.ts +++ b/app/javascript/retrospring/features/answerbox/index.ts @@ -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); } } diff --git a/app/javascript/retrospring/features/answerbox/share.ts b/app/javascript/retrospring/features/answerbox/share.ts index 34c2ba54..f623ec75 100644 --- a/app/javascript/retrospring/features/answerbox/share.ts +++ b/app/javascript/retrospring/features/answerbox/share.ts @@ -1,11 +1,11 @@ import noop from 'utilities/noop'; -export function createShareEvent(answerbox: HTMLElement): () => void { - return function (): void { - navigator.share({ - url: answerbox.querySelector('.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('.answerbox__answer-date > a, a.answerbox__permalink').href + }) .then(noop) .catch(noop) - } }