diff options
author | Sam Wilkins <samwilkins333@gmail.com> | 2020-02-16 16:00:31 -0500 |
---|---|---|
committer | Sam Wilkins <samwilkins333@gmail.com> | 2020-02-16 16:00:31 -0500 |
commit | 9e326c4b71235554131bb16e28dd7ff9b39a3899 (patch) | |
tree | 1da7785767f7e57dc17f54df6507fffa742d9031 /src/Utils.ts | |
parent | ae4b072bb8f360c60cd61bcd1e49a075010e20b4 (diff) | |
parent | a451bce9b49b28434f5180044b11467c21615f06 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web
Diffstat (limited to 'src/Utils.ts')
-rw-r--r-- | src/Utils.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Utils.ts b/src/Utils.ts index b564564be..6a0b3fad8 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -473,4 +473,40 @@ export function clearStyleSheetRules(sheet: any) { return true; } return false; +} + +export function setupMoveUpEvents( + target: object, + e: React.PointerEvent, + moveEvent: (e: PointerEvent, down: number[], delta: number[]) => boolean, + upEvent: (e: PointerEvent) => void, + clickEvent: (e: PointerEvent) => void) { + (target as any)._downX = (target as any)._lastX = e.clientX; + (target as any)._downY = (target as any)._lastY = e.clientY; + + const _moveEvent = (e: PointerEvent): void => { + if (Math.abs(e.clientX - (target as any)._downX) > 4 || Math.abs(e.clientY - (target as any)._downY) > 4) { + if (moveEvent(e, [(target as any)._downX, (target as any)._downY], + [e.clientX - (target as any)._lastX, e.clientY - (target as any)._lastY])) { + document.removeEventListener("pointermove", _moveEvent); + document.removeEventListener("pointerup", _upEvent); + } + } + (target as any)._lastX = e.clientX; + (target as any)._lastY = e.clientY; + e.stopPropagation(); + } + const _upEvent = (e: PointerEvent): void => { + upEvent(e); + if (Math.abs(e.clientX - (target as any)._downX) < 4 || Math.abs(e.clientY - (target as any)._downY) < 4) { + clickEvent(e); + } + document.removeEventListener("pointermove", _moveEvent); + document.removeEventListener("pointerup", _upEvent); + } + e.stopPropagation(); + document.removeEventListener("pointermove", _moveEvent); + document.removeEventListener("pointerup", _upEvent); + document.addEventListener("pointermove", _moveEvent); + document.addEventListener("pointerup", _upEvent); }
\ No newline at end of file |