export function getElementRect (element: HTMLElement) { const clientRect = getElementClientRect(element); if (!isiOS7() && clientRect) { const scroll = getScrollXY(getWindow(element)); clientRect.left += scroll.x; clientRect.right += scroll.x; clientRect.top += scroll.y; clientRect.bottom += scroll.y; } return clientRect; } export function getElementClientRect (element: HTMLElement) { // @ts-ignore const clientRect = (element instanceof window.SVGElement ? element.getBoundingClientRect() : element.getClientRects()[0]); return clientRect && { left : clientRect.left, right : clientRect.right, top : clientRect.top, bottom: clientRect.bottom, width : clientRect.width || clientRect.right - clientRect.left, height: clientRect.height || clientRect.bottom - clientRect.top, }; } export function getWindow (node: Node) { if (isWindow(node)) { return node; } const rootNode = (node.ownerDocument || node); // @ts-ignore return rootNode.defaultView || window; } // @ts-ignore export const isWindow = (thing: Node) => !!(thing && thing.Window) && (thing instanceof thing.Window); export function getScrollXY (relevantWindow: Window) { relevantWindow = relevantWindow || window; return { x: relevantWindow.scrollX || relevantWindow.document.documentElement.scrollLeft, y: relevantWindow.scrollY || relevantWindow.document.documentElement.scrollTop, }; } export const isiOS7 = (): boolean => { return (/iP(hone|od|ad)/.test(navigator.platform) && /OS 7[^\d]/.test(navigator.appVersion)); } export const openLink = (link: string, inNewTab?: boolean) => { Object.assign(document.createElement("a"), { ...inNewTab && { target: "_blank" }, href: link }).click(); };