How to find the main window from your custom page

This document contains a javascript function that, when executed, will obtain the Relativity main window. The Relativity main window contains useful information about Relativity and functions that you can execute to access Relativity specific data. The function below can be executed in any Relativity window, including popups, and it will recursively traverse the window hierarchy to find the main window.

To use the script below:

  1. Copy the code to your application.
  2. Execute the method by calling getRelativityPageBaseWindow()

See Supported JavaScript APIs on Relativity's main window for more information on what's expected to exist on the Relativity main window.

Function code:

Copy
/**
* @desc returns the window with the relativity context
* @returns {Object} the window with the relativity context
*/
function getRelativityPageBaseWindow() {
    /**
    * @const {Array<string>} Collection of supported properties expected to be on the window object.
    */
    const RELATIVITY_NAMESPACE_AND_SUPPORTED_PROPS = [
        "relativity",
        "relativity.config",
        "relativity.internal",
        "relativity.navigation"
    ];
    
    /**
    * @desc checks that the provided window object has the given properties on it
    * @param {Array<String>} props the Array of property names
    * @param {Object} winObj the window Object
    * @returns {Object} the window with the given properties
    */
    function findWindowByPropertiesInHierarchy(props, winObj) {
        let win = winObj;
        while (continueHierarchyCheck(props, win)) {
            win = win.opener || win; // win.opener is done before parent because opener will be falsy (null) on windows where window.parent !== window

            if (!ensureProps(win, props)) {
                win = win.parent || false; // save vs malformation
            }
        }
        return (!!win && ensureProps(win, props)) ? win : {};
    }
    
    /**
    * @desc checks whether we have to continue hierarchy check for given properties.
    * @param {Array<String>} props the Array of property names.
    * @param {Object} win the window Object
    * @returns {Boolean} returns a boolean value.
    */
    function continueHierarchyCheck(props, win) {
        return !!win && !(ensureProps(win, props) || (win === win.parent && !win.opener));
    }
    
    /**
    * @desc This function checks whether the given object has the given properties
    * @param {Object} obj the Object to evaluate
    * @param {Array} propNames the Array to evaluate against obj
    * @returns {Boolean} if the given properties are found
    */
    function ensureProps(obj, propNames) {
        try {
            return !!obj &&
                Array.isArray(propNames) &&
                propNames.every((prop) => checkProperty(obj, prop));
        } catch (e) {
            return e && false;
        }
    }
    
    /**
    * @desc This function checks whether the given object has the given property
    * @param {Object} obj the Object to evaluate
    * @param {String} prop the property to evaluate against obj
    * @returns {Boolean} if the given property is found
    */
    function checkProperty(obj, prop) {
        const propsAsArray = prop.split(".");
        return !!propsAsArray.reduce((prev, curr) => prev && prev[curr] || (typeof prev === "object" && prev.hasOwnProperty(curr)), obj);
    }
    
    return findWindowByPropertiesInHierarchy(RELATIVITY_NAMESPACE_AND_SUPPORTED_PROPS, window);
}