diff options
author | Allan Wang <me@allanwang.ca> | 2019-05-01 17:05:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-01 17:05:22 -0700 |
commit | e7a499f6314f8d6345b19c3e221f3cd9cd176d4f (patch) | |
tree | a19cbd9b60a218cdc3370ea83e565fd8ab086881 /app/src/web/ts/context_a.ts | |
parent | da93672c2ed6b54e0e7119a6b55715185112df3e (diff) | |
parent | 547bf6f932b56a4f66e9eb1d9f175e7db4e4512e (diff) | |
download | frost-e7a499f6314f8d6345b19c3e221f3cd9cd176d4f.tar.gz frost-e7a499f6314f8d6345b19c3e221f3cd9cd176d4f.tar.bz2 frost-e7a499f6314f8d6345b19c3e221f3cd9cd176d4f.zip |
Merge pull request #1416 from AllanWang/misc
Misc
Diffstat (limited to 'app/src/web/ts/context_a.ts')
-rw-r--r-- | app/src/web/ts/context_a.ts | 33 |
1 files changed, 22 insertions, 11 deletions
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 @@ -7,6 +7,25 @@ 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. */ type EventHandler = (e: Event, target: HTMLElement) => Boolean @@ -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>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); |