Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Visualization Typescript Conversions - lib/dom.js (#69630)

Kamil Mielnikc51eeace1d05
diff --git a/frontend/src/metabase/lib/dom.js b/frontend/src/metabase/lib/dom.ts
similarity index 65%
rename from frontend/src/metabase/lib/dom.js
rename to frontend/src/metabase/lib/dom.ts
index 202fedbd14d..31827e086d6 100644
--- a/frontend/src/metabase/lib/dom.js
+++ b/frontend/src/metabase/lib/dom.ts
@@ -1,22 +1,26 @@
import querystring from "querystring";
+import type { LocationDescriptor } from "history";
import _ from "underscore";
import { handleLinkSdkPlugin } from "embedding-sdk-shared/lib/sdk-global-plugins";
import { isEmbeddingSdk } from "metabase/embedding-sdk/config";
import { isCypressActive, isStorybookActive } from "metabase/env";
import MetabaseSettings from "metabase/lib/settings";
+import { isObject } from "metabase-types/guards";
+
+import { checkNotNull } from "./types";
// IE doesn't support scrollX/scrollY:
-export const getScrollX = () =>
+export const getScrollX = (): number =>
typeof window.scrollX === "undefined" ? window.pageXOffset : window.scrollX;
-export const getScrollY = () =>
+export const getScrollY = (): number =>
typeof window.scrollY === "undefined" ? window.pageYOffset : window.scrollY;
// denotes whether the current page is loaded in an iframe or not
// Cypress renders the whole app within an iframe, but we want to exclude it from this check to avoid certain components (like Nav bar) not rendering
// Storybook also uses an iframe to display story content, so we want to ignore it
-export const isWithinIframe = function () {
+export const isWithinIframe = function (): boolean {
try {
// Mock that we're embedding, so we could test embed components
if (window.overrideIsWithinIframe) {
@@ -39,7 +43,7 @@ window.METABASE = true;
// check whether scrollbars are visible to the user,
// this is off by default on Macs, but can be changed
// Always on on most other non mobile platforms
-export const getScrollBarSize = _.memoize(() => {
+export const getScrollBarSize = _.memoize((): number => {
const scrollableElem = document.createElement("div"),
innerElem = document.createElement("div");
scrollableElem.style.width = "30px";
@@ -59,6 +63,7 @@ export const getScrollBarSize = _.memoize(() => {
// exceptions
export const HAS_LOCAL_STORAGE = (function () {
try {
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
window.localStorage; // This will trigger an exception if access is denied.
return true;
} catch (e) {
@@ -67,7 +72,10 @@ export const HAS_LOCAL_STORAGE = (function () {
}
})();
-export function isObscured(element, offset) {
+export function isObscured(
+ element: HTMLElement,
+ offset?: { left: number; top: number },
+): boolean {
if (!document.elementFromPoint) {
return false;
}
@@ -86,7 +94,11 @@ export function isObscured(element, offset) {
}
// based on http://stackoverflow.com/a/38039019/113
-export function elementIsInView(element, percentX = 1, percentY = 1) {
+export function elementIsInView(
+ element: HTMLElement,
+ percentX: number = 1,
+ percentY: number = 1,
+): boolean {
const tolerance = 0.01; //needed because the rects returned by getBoundingClientRect provide the position up to 10 decimals
const elementRect = element.getBoundingClientRect();
@@ -113,122 +125,12 @@ export function elementIsInView(element, percentX = 1, percentY = 1) {
});
}
-export function getSelectionPosition(element) {
- // input, textarea, IE
- if (element.setSelectionRange || element.createTextRange) {
- return [element.selectionStart, element.selectionEnd];
- } else {
- // contenteditable
- try {
- const selection = window.getSelection();
- // Clone the Range otherwise setStart/setEnd will mutate the actual selection in Chrome 58+ and Firefox!
- const range = selection.getRangeAt(0).cloneRange();
- const { startContainer, startOffset } = range;
- range.setStart(element, 0);
- const end = range.toString().length;
- range.setEnd(startContainer, startOffset);
- const start = range.toString().length;
-
- return [start, end];
- } catch (e) {
- return [0, 0];
- }
- }
-}
-
-export function setSelectionPosition(element, [start, end]) {
- // input, textarea
- if (element.setSelectionRange) {
- element.focus();
- element.setSelectionRange(start, end);
- } else if (element.createTextRange) {
- // IE
- const range = element.createTextRange();
- range.collapse(true);
- range.moveEnd("character", end);
- range.moveStart("character", start);
- range.select();
- } else {
- // contenteditable
- const selection = window.getSelection();
- const startPos = getTextNodeAtPosition(element, start);
- const endPos = getTextNodeAtPosition(element, end);
- selection.removeAllRanges();
- const range = new Range();
- range.setStart(startPos.node, startPos.position);
- range.setEnd(endPos.node, endPos.position);
- selection.addRange(range);
- }
-}
-
-export function saveSelection(element) {
- const range = getSelectionPosition(element);
- return () => setSelectionPosition(element, range);
-}
-
-export function getCaretPosition(element) {
- return getSelectionPosition(element)[1];
-}
-
-export function setCaretPosition(element, position) {
- setSelectionPosition(element, [position, position]);
-}
-
-export function saveCaretPosition(element) {
- const position = getCaretPosition(element);
- return () => setCaretPosition(element, position);
-}
-
-function getTextNodeAtPosition(root, index) {
- const treeWalker = document.createTreeWalker(
- root,
- NodeFilter.SHOW_TEXT,
- (elem) => {
- if (index > elem.textContent.length) {
- index -= elem.textContent.length;
- return NodeFilter.FILTER_REJECT;
- }
- return NodeFilter.FILTER_ACCEPT;
- },
- );
- const c = treeWalker.nextNode();
- return {
- node: c ? c : root,
- position: c ? index : 0,
- };
-}
-
-export function constrainToScreen(element, direction, padding) {
- if (!element) {
- return false;
- }
- if (direction === "bottom") {
- const screenBottom = window.innerHeight + getScrollY();
- const overflowY = element.getBoundingClientRect().bottom - screenBottom;
- if (overflowY + padding > 0) {
- element.style.maxHeight =
- element.getBoundingClientRect().height - overflowY - padding + "px";
- return true;
- }
- } else if (direction === "top") {
- const screenTop = getScrollY();
- const overflowY = screenTop - element.getBoundingClientRect().top;
- if (overflowY + padding > 0) {
- element.style.maxHeight =
- element.getBoundingClientRect().height - overflowY - padding + "px";
- return true;
- }
- } else {
- throw new Error("Direction " + direction + " not implemented");
- }
- return false;
-}
-
-export function getSitePath() {
- return new URL(MetabaseSettings.get("site-url")).pathname.toLowerCase();
+export function getSitePath(): string {
+ const siteUrl = checkNotNull(MetabaseSettings.get("site-url"));
+ return new URL(siteUrl).pathname.toLowerCase();
}
-function isMetabaseUrl(url) {
+function isMetabaseUrl(url: string): boolean {
const urlPath = new URL(url, window.location.origin).pathname.toLowerCase();
if (!isAbsoluteUrl(url)) {
@@ -249,67 +151,63 @@ function isMetabaseUrl(url) {
return isSameOrSiteUrlOrigin(url) && urlPath.startsWith(getSitePath());
}
-function isAbsoluteUrl(url) {
+function isAbsoluteUrl(url: string): boolean {
return ["/", "http:", "https:", "mailto:"].some((prefix) =>
url.startsWith(prefix),
);
}
-function getWithSiteUrl(url) {
+function getWithSiteUrl(url: string): string {
const siteUrl = MetabaseSettings.get("site-url");
- return url.startsWith("/") ? siteUrl + url : url;
+ return url.startsWith("/") ? (siteUrl ?? "") + url : url;
}
// Used for tackling Safari rendering issues
// http://stackoverflow.com/a/3485654
-export function forceRedraw(domNode) {
+export function forceRedraw(domNode: HTMLElement): void {
domNode.style.display = "none";
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
domNode.offsetHeight;
domNode.style.display = "";
}
-export function moveToBack(element) {
- if (element && element.parentNode) {
- element.parentNode.insertBefore(element, element.parentNode.firstChild);
- }
-}
-
-export function moveToFront(element) {
- if (element && element.parentNode) {
- element.parentNode.appendChild(element);
- }
-}
-
// need to keep track of the latest click's state because sometimes
// `open` is called asynchronously, thus window.event isn't the click event
-let metaKey;
-let ctrlKey;
+let metaKey: boolean = false;
+let ctrlKey: boolean = false;
window.addEventListener(
"mouseup",
- (e) => {
+ (e: MouseEvent) => {
metaKey = e.metaKey;
ctrlKey = e.ctrlKey;
},
true,
);
+type OpenOptions = {
+ openInSameWindow?: (url: string) => void;
+ openInBlankWindow?: (url: string) => void;
+ openInSameOrigin?: (location: LocationDescriptor) => void;
+ ignoreSiteUrl?: boolean;
+} & ShouldOpenInBlankWindowOptions;
+
/**
* helper for opening links in same or different window depending on origin and
* meta key state
*/
export async function open(
- url,
+ url: string,
{
// custom function for opening in same window
- openInSameWindow = (url) => clickLink(url, false),
+ openInSameWindow = (url: string) => clickLink(url, false),
// custom function for opening in new window
- openInBlankWindow = (url) => clickLink(url, true),
+ openInBlankWindow = (url: string) => clickLink(url, true),
// custom function for opening in same app instance
openInSameOrigin,
ignoreSiteUrl = false,
...options
- } = {},
-) {
+ }: OpenOptions = {},
+): Promise<void> {
url = ignoreSiteUrl ? url : getWithSiteUrl(url);
// In the sdk, allow the host app to override how to open links
@@ -326,19 +224,26 @@ export async function open(
} else if (isSameOrigin(url)) {
if (!isMetabaseUrl(url)) {
clickLink(url, false);
+ } else if (openInSameOrigin) {
+ const location = getLocation(url);
+ if (isObject(location) && "pathname" in location) {
+ openInSameOrigin(location);
+ } else {
+ openInSameWindow(url);
+ }
} else {
- openInSameOrigin(getLocation(url));
+ openInSameWindow(url);
}
} else {
openInSameWindow(url);
}
}
-export function openInBlankWindow(url) {
+export function openInBlankWindow(url: string): void {
clickLink(getWithSiteUrl(url), true);
}
-function clickLink(url, blank = false) {
+function clickLink(url: string, blank = false): void {
const a = document.createElement("a");
a.style.display = "none";
document.body.appendChild(a);
@@ -354,18 +259,27 @@ function clickLink(url, blank = false) {
}
}
+type ShouldOpenInBlankWindowOptions = {
+ event?: MouseEvent | null;
+ blank?: boolean;
+ blankOnMetaOrCtrlKey?: boolean;
+ blankOnDifferentOrigin?: boolean;
+};
+
export function shouldOpenInBlankWindow(
- url,
+ url: string,
{
- event = window.event,
+ event = (typeof window !== "undefined" ? window.event : undefined) as
+ | MouseEvent
+ | undefined,
// always open in new window
blank = false,
// open in new window if command-click
blankOnMetaOrCtrlKey = true,
// open in new window for different origin
blankOnDifferentOrigin = true,
- } = {},
-) {
+ }: ShouldOpenInBlankWindowOptions = {},
+): boolean {
if (isEmbeddingSdk()) {
// always open in new window in modular embedding (react SDK + modular embedding)
return true;
@@ -383,7 +297,7 @@ export function shouldOpenInBlankWindow(
return false;
}
-const getOrigin = (url) => {
+const getOrigin = (url: string): string | null => {
try {
return new URL(url, window.location.origin).origin;
} catch {
@@ -391,7 +305,7 @@ const getOrigin = (url) => {
}
};
-const getLocation = (url) => {
+const getLocation = (url: string): LocationDescriptor => {
try {
const { pathname, search, hash } = new URL(url, window.location.origin);
const query = querystring.parse(search.substring(1));
@@ -406,13 +320,7 @@ const getLocation = (url) => {
}
};
-/**
- * Returns the pathname without the site subpath, if any
- *
- * @param {string} pathname the pathname
- * @returns the pathname without it subpath, if any
- */
-export function getPathnameWithoutSubPath(pathname) {
+export function getPathnameWithoutSubPath(pathname: string): string {
const pathnameSections = pathname.split("/");
const sitePathSections = getSitePath().split("/");
@@ -421,7 +329,10 @@ export function getPathnameWithoutSubPath(pathname) {
: pathname;
}
-function isPathnameContainSitePath(pathnameSections, sitePathSections) {
+function isPathnameContainSitePath(
+ pathnameSections: string[],
+ sitePathSections: string[],
+): boolean {
for (let index = 0; index < sitePathSections.length; index++) {
const sitePathSection = sitePathSections[index].toLowerCase();
const pathnameSection = pathnameSections[index].toLowerCase();
@@ -434,32 +345,36 @@ function isPathnameContainSitePath(pathnameSections, sitePathSections) {
return true;
}
-export function isSameOrigin(url) {
+export function isSameOrigin(url: string): boolean {
const origin = getOrigin(url);
return origin == null || origin === window.location.origin;
}
-function isSiteUrlOrigin(url) {
- const siteUrl = getOrigin(MetabaseSettings.get("site-url"));
+function isSiteUrlOrigin(url: string): boolean {
+ const siteUrl = MetabaseSettings.get("site-url");
+ const siteUrlOrigin = siteUrl ? getOrigin(siteUrl) : null;
const urlOrigin = getOrigin(url);
- return siteUrl === urlOrigin;
+ return siteUrlOrigin === urlOrigin;
}
// When a url is either has the same origin or it is the same with the site url
// we want to open it in the same window (https://github.com/metabase/metabase/issues/24451)
-export function isSameOrSiteUrlOrigin(url) {
+export function isSameOrSiteUrlOrigin(url: string): boolean {
return isSameOrigin(url) || isSiteUrlOrigin(url);
}
-export function getUrlTarget(url) {
+export function getUrlTarget(
+ url: string | undefined,
+): "_self" | "_blank" | undefined {
if (isEmbeddingSdk()) {
// always open in new window in modular embedding (react SDK + modular embedding)
return "_blank";
}
- return isSameOrSiteUrlOrigin(url) ? "_self" : "_blank";
+
+ return url == null || isSameOrSiteUrlOrigin(url) ? "_self" : "_blank";
}
-export function removeAllChildren(element) {
+export function removeAllChildren(element: HTMLElement | null): void {
if (!element) {
return;
}
@@ -469,13 +384,22 @@ export function removeAllChildren(element) {
}
}
-export function parseDataUri(url) {
+interface ParsedDataUri {
+ mimeType: string | undefined;
+ charset: string | undefined;
+ data: string;
+ base64: string;
+}
+
+export function parseDataUri(
+ url: string | null | undefined,
+): ParsedDataUri | null {
// https://regexr.com/8e8gt
const match =
url &&
url.match(/^data:(?:([^;]+)(?:;([^;]+))?)?(;base64)?,((?:(?!\1|,).)*)$/);
if (match) {
- let [, mimeType, charset, base64, data] = match;
+ let [, mimeType, charset, base64, data]: (string | undefined)[] = match;
if (charset === "base64" && !base64) {
base64 = charset;
charset = undefined;
@@ -493,14 +417,14 @@ export function parseDataUri(url) {
/**
* @returns the clip-path CSS property referencing the clip path in the current document, taking into account the <base> tag.
*/
-export function clipPathReference(id) {
+export function clipPathReference(id: string): string {
// add the current page URL (with fragment removed) to support pages with <base> tag.
// https://stackoverflow.com/questions/18259032/using-base-tag-on-a-page-that-contains-svg-marker-elements-fails-to-render-marke
const url = window.location.href.replace(/#.*$/, "") + "#" + id;
return `url(${url})`;
}
-export function initializeIframeResizer(onReady = () => {}) {
+export function initializeIframeResizer(onReady = () => {}): void {
if (!isWithinIframe()) {
return;
}
@@ -519,40 +443,38 @@ export function initializeIframeResizer(onReady = () => {}) {
// Make iframe-resizer available to the embed
// We only care about contentWindow so require that minified file
-
import("iframe-resizer/js/iframeResizer.contentWindow.js");
}
}
-export function isEventOverElement(event, element) {
+export function isEventOverElement(
+ event: Pick<MouseEvent, "clientX" | "clientY">,
+ element: Element,
+): boolean {
const { clientX: x, clientY: y } = event;
const { top, bottom, left, right } = element.getBoundingClientRect();
return y >= top && y <= bottom && x >= left && x <= right;
}
-export function isReducedMotionPreferred() {
+export function isReducedMotionPreferred(): boolean {
const mediaQuery = window.matchMedia?.("(prefers-reduced-motion: reduce)");
- return mediaQuery && mediaQuery.matches;
+ return mediaQuery != null && mediaQuery.matches;
}
-/**
- * @returns {HTMLElement | undefined}
- */
-export function getMainElement() {
+export function getMainElement(): HTMLElement | undefined {
const [main] = document.getElementsByTagName("main");
return main;
}
-export function isSmallScreen() {
+export function isSmallScreen(): boolean {
const mediaQuery = window.matchMedia("(max-width: 40em)");
- return mediaQuery && mediaQuery.matches;
+ return mediaQuery != null && mediaQuery.matches;
}
-/**
- * @param {MouseEvent<Element, MouseEvent>} event
- */
-export const getEventTarget = (event) => {
+export const getEventTarget = (
+ event: MouseEvent | React.MouseEvent,
+): HTMLElement => {
let target = document.getElementById("popover-event-target");
if (!target) {
target = document.createElement("div");
@@ -569,7 +491,7 @@ export const getEventTarget = (event) => {
* Wrapper around window.location is used as we can't override window in jest with jsdom anymore
* https://github.com/jsdom/jsdom/issues/3492
*/
-export function reload() {
+export function reload(): void {
window.location.reload();
}
@@ -577,11 +499,11 @@ export function reload() {
* Wrapper around window.location is used as we can't override window in jest with jsdom anymore
* https://github.com/jsdom/jsdom/issues/3492
*/
-export function redirect(url) {
+export function redirect(url: string): void {
window.location.href = url;
}
-export function openSaveDialog(fileName, fileContent) {
+export function openSaveDialog(fileName: string, fileContent: Blob): void {
const url = URL.createObjectURL(fileContent);
const link = document.createElement("a");
link.href = url;
diff --git a/frontend/src/metabase/lib/iframe.js b/frontend/src/metabase/lib/iframe.js
index 67a400ddf22..878e4525144 100644
--- a/frontend/src/metabase/lib/iframe.js
+++ b/frontend/src/metabase/lib/iframe.js
@@ -2,7 +2,7 @@
// used for detecting if we're previewing an embed
export const IFRAMED_IN_SELF = (function () {
try {
- return window.self !== window.parent && window.parent.METABASE;
+ return window.self !== window.parent && Boolean(window.parent.METABASE);
} catch (e) {
return false;
}
diff --git a/frontend/src/metabase/parameters/components/widgets/TextWidget/TextWidget.tsx b/frontend/src/metabase/parameters/components/widgets/TextWidget/TextWidget.tsx
index e27585f281d..429a8b11868 100644
--- a/frontend/src/metabase/parameters/components/widgets/TextWidget/TextWidget.tsx
+++ b/frontend/src/metabase/parameters/components/widgets/TextWidget/TextWidget.tsx
@@ -46,8 +46,10 @@ export class TextWidget extends Component<TextWidgetProps, State> {
UNSAFE_componentWillReceiveProps(nextProps: TextWidgetProps) {
if (nextProps.value !== this.props.value) {
this.setState({ value: nextProps.value }, () => {
- // HACK: Address Safari rendering bug which causes https://github.com/metabase/metabase/issues/5335
- forceRedraw(this.inputRef.current);
+ if (this.inputRef.current) {
+ // HACK: Address Safari rendering bug which causes https://github.com/metabase/metabase/issues/5335
+ forceRedraw(this.inputRef.current);
+ }
});
}
}
diff --git a/frontend/src/metabase/redux/app.ts b/frontend/src/metabase/redux/app.ts
index 8de460d8561..05706e89a85 100644
--- a/frontend/src/metabase/redux/app.ts
+++ b/frontend/src/metabase/redux/app.ts
@@ -48,22 +48,13 @@ export function resetErrorPage() {
};
}
-interface IOpenUrlOptions {
- blank?: boolean;
- event?: Event;
- blankOnMetaOrCtrlKey?: boolean;
- blankOnDifferentOrigin?: boolean;
-}
-
-export const openUrl =
- (url: string, options: IOpenUrlOptions = {}) =>
- (dispatch: Dispatch) => {
- if (shouldOpenInBlankWindow(url, options)) {
- openInBlankWindow(url);
- } else {
- dispatch(push(url));
- }
- };
+export const openUrl = (url: string) => (dispatch: Dispatch) => {
+ if (shouldOpenInBlankWindow(url)) {
+ openInBlankWindow(url);
+ } else {
+ dispatch(push(url));
+ }
+};
const errorPage = handleActions(
{
diff --git a/frontend/src/metabase/visualizations/components/ChartTooltip/ChartTooltip.tsx b/frontend/src/metabase/visualizations/components/ChartTooltip/ChartTooltip.tsx
index a49fb01413c..aa8f6887ec7 100644
--- a/frontend/src/metabase/visualizations/components/ChartTooltip/ChartTooltip.tsx
+++ b/frontend/src/metabase/visualizations/components/ChartTooltip/ChartTooltip.tsx
@@ -69,7 +69,7 @@ const ChartTooltip = ({
const target = hasTargetElement
? hovered?.element
- : hasTargetEvent
+ : hovered?.event != null
? getEventTarget(hovered.event)
: null;
diff --git a/frontend/src/types/global.d.ts b/frontend/src/types/global.d.ts
index 55a5ce6d983..f7cc776f1b1 100644
--- a/frontend/src/types/global.d.ts
+++ b/frontend/src/types/global.d.ts
@@ -3,6 +3,17 @@ interface Window {
MetabaseRoot?: string;
MetabaseNonce?: string;
MetabaseUserColorScheme?: string;
+
+ overrideIsWithinIframe?: boolean; // Mock that we're embedding, so we could test embed components
+ METABASE?: boolean; // Add a global so we can check if the parent iframe is Metabase
+
+ // Make iFrameResizer available so that embed users can
+ // have their embeds autosize to their content
+ iFrameResizer?: {
+ autoResize?: boolean;
+ heightCalculationMethod?: string;
+ onReady?: () => void;
+ };
}
// This allows importing static SVGs from TypeScript files
@@ -19,4 +30,6 @@ declare module "*.css" {
export default classes;
}
+declare module "iframe-resizer/js/iframeResizer.contentWindow.js";
+
type Nullable<T> = T | null;
diff --git a/frontend/test/metabase/lib/dom.unit.spec.js b/frontend/test/metabase/lib/dom.unit.spec.js
index 26e5e162785..d8ec33e32d4 100644
--- a/frontend/test/metabase/lib/dom.unit.spec.js
+++ b/frontend/test/metabase/lib/dom.unit.spec.js
@@ -1,36 +1,12 @@
import { ensureMetabaseProviderPropsStore } from "embedding-sdk-shared/lib/ensure-metabase-provider-props-store";
import { mockIsEmbeddingSdk } from "metabase/embedding-sdk/mocks/config-mock";
import {
- getSelectionPosition,
getUrlTarget,
open,
parseDataUri,
- setSelectionPosition,
shouldOpenInBlankWindow,
} from "metabase/lib/dom";
-describe("getSelectionPosition/setSelectionPosition", () => {
- let container;
-
- beforeEach(() => {
- container = document.createElement("div");
- document.body.appendChild(container);
- });
-
- afterEach(() => {
- document.body.removeChild(container);
- });
-
- it("should get/set selection on input correctly", () => {
- const input = document.createElement("input");
- container.appendChild(input);
- input.value = "hello world";
- setSelectionPosition(input, [3, 6]);
- const position = getSelectionPosition(input);
- expect(position).toEqual([3, 6]);
- });
-});
-
describe("parseDataUri", () => {
it("parses a valid text data URI", () => {
const dataUri = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";