|
| 1 | + |
| 2 | +// tslint:disable |
| 3 | + |
| 4 | +/** |
| 5 | + * Client rect utilities. |
| 6 | + * This file is taken from Angular Material repository. This is the reason why the tslint is disabled on this case. |
| 7 | + * Don't enable it until some custom change is done on this file. |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +/** Gets a mutable version of an element's bounding `ClientRect`. */ |
| 12 | +export function getMutableClientRect(element: Element): ClientRect { |
| 13 | + const clientRect = element.getBoundingClientRect(); |
| 14 | + |
| 15 | + // We need to clone the `clientRect` here, because all the values on it are readonly |
| 16 | + // and we need to be able to update them. Also we can't use a spread here, because |
| 17 | + // the values on a `ClientRect` aren't own properties. See: |
| 18 | + // https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect#Notes |
| 19 | + return { |
| 20 | + top: clientRect.top, |
| 21 | + right: clientRect.right, |
| 22 | + bottom: clientRect.bottom, |
| 23 | + left: clientRect.left, |
| 24 | + width: clientRect.width, |
| 25 | + height: clientRect.height |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Checks whether some coordinates are within a `ClientRect`. |
| 31 | + * @param clientRect ClientRect that is being checked. |
| 32 | + * @param x Coordinates along the X axis. |
| 33 | + * @param y Coordinates along the Y axis. |
| 34 | + */ |
| 35 | +export function isInsideClientRect(clientRect: ClientRect, x: number, y: number) { |
| 36 | + const {top, bottom, left, right} = clientRect; |
| 37 | + return y >= top && y <= bottom && x >= left && x <= right; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Updates the top/left positions of a `ClientRect`, as well as their bottom/right counterparts. |
| 42 | + * @param clientRect `ClientRect` that should be updated. |
| 43 | + * @param top Amount to add to the `top` position. |
| 44 | + * @param left Amount to add to the `left` position. |
| 45 | + */ |
| 46 | +export function adjustClientRect(clientRect: ClientRect, top: number, left: number) { |
| 47 | + clientRect.top += top; |
| 48 | + clientRect.bottom = clientRect.top + clientRect.height; |
| 49 | + |
| 50 | + clientRect.left += left; |
| 51 | + clientRect.right = clientRect.left + clientRect.width; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Checks whether the pointer coordinates are close to a ClientRect. |
| 56 | + * @param rect ClientRect to check against. |
| 57 | + * @param threshold Threshold around the ClientRect. |
| 58 | + * @param pointerX Coordinates along the X axis. |
| 59 | + * @param pointerY Coordinates along the Y axis. |
| 60 | + */ |
| 61 | +export function isPointerNearClientRect(rect: ClientRect, |
| 62 | + threshold: number, |
| 63 | + pointerX: number, |
| 64 | + pointerY: number): boolean { |
| 65 | + const {top, right, bottom, left, width, height} = rect; |
| 66 | + const xThreshold = width * threshold; |
| 67 | + const yThreshold = height * threshold; |
| 68 | + |
| 69 | + return pointerY > top - yThreshold && pointerY < bottom + yThreshold && |
| 70 | + pointerX > left - xThreshold && pointerX < right + xThreshold; |
| 71 | +} |
0 commit comments