Fix getDataExplorerWindow (#285)

This commit is contained in:
Steve Faulkner
2020-10-16 16:01:41 -05:00
committed by GitHub
parent 9a5d46b6e0
commit 23714831bd
2 changed files with 35 additions and 50 deletions
+11 -16
View File
@@ -1,23 +1,18 @@
export const getDataExplorerWindow = (currentWindow: Window): Window | undefined => {
// Start with the current window and traverse up the parent hierarchy to find a window
// with `dataExplorerPlatform` property
let dataExplorerWindow: Window | undefined = currentWindow;
// Data explorer is always loaded in an iframe, so traverse the parents until we hit the top and return the first child window.
try {
// TODO: Need to `any` here since the window imports Explorer which can't be in strict mode yet
// eslint-disable-next-line @typescript-eslint/no-explicit-any
while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform === undefined) {
// If a window does not have a parent, its parent property is a reference to itself.
if (dataExplorerWindow.parent === dataExplorerWindow) {
dataExplorerWindow = undefined;
} else {
dataExplorerWindow = dataExplorerWindow.parent;
while (currentWindow) {
if (currentWindow.parent === currentWindow) {
return undefined;
}
if (currentWindow.parent === currentWindow.top) {
return currentWindow;
}
currentWindow = currentWindow.parent;
}
} catch (error) {
// This can happen if we come across parent from a different origin
dataExplorerWindow = undefined;
// Hitting a cross domain error means we are in the portal and the current window is data explorer
return currentWindow;
}
return dataExplorerWindow;
return undefined;
};