From 5398c336ef8821d286a60fda593376eed99381a2 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 1 May 2019 15:36:32 -0700 Subject: Add better handling for launchpad --- app/src/web/ts/context_a.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'app/src/web/ts/context_a.ts') diff --git a/app/src/web/ts/context_a.ts b/app/src/web/ts/context_a.ts index 5eec7611..b9549f3f 100644 --- a/app/src/web/ts/context_a.ts +++ b/app/src/web/ts/context_a.ts @@ -6,6 +6,25 @@ (function () { let longClick = false; + /** + * Go up at most [depth] times, to retrieve a parent matching the provided predicate + * If one is found, it is returned immediately. + * Otherwise, null is returned. + */ + function _parentEl(el: HTMLElement, depth: number, predicate: (el: HTMLElement) => boolean): HTMLElement | null { + for (let i = 0; i < depth + 1; i++) { + if (predicate(el)) { + return el + } + const parent = el.parentElement; + if (!parent) { + return null + } + el = parent + } + return null + } + /** * Given event and target, return true if handled and false otherwise. */ @@ -55,16 +74,8 @@ * Opens image activity for posts with just one image */ const _frostImage: EventHandler = (e, target) => { - let element: Element = target; - // Notifications are two layers under - for (let i = 0; i < 2; i++) { - if (element.tagName !== 'A') { - element = element.parentElement; - } else { - break - } - } - if (element.tagName !== 'A') { + const element = _parentEl(target, 2, (el) => el.tagName === 'A'); + if (!element) { return false; } const url = element.getAttribute('href'); @@ -92,7 +103,7 @@ return true; }; - const handlers = [_frostImage, _frostCopyComment, _frostCopyPost]; + const handlers: EventHandler[] = [_frostImage, _frostCopyComment, _frostCopyPost]; const _frostAContext = (e: Event) => { Frost.longClick(true); -- cgit v1.2.3