UNPKG

@ventum-digital/wait-on

Version:

Functions for waiting on DOM elements in SailPoint IdentityIQ

161 lines (159 loc) 7.3 kB
/******/ // The require scope /******/ var __webpack_require__ = {}; /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; /*!************************!*\ !*** ./build/index.js ***! \************************/ __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ waitOn: () => (/* binding */ waitOn), /* harmony export */ waitOnAngularCtrlScope: () => (/* binding */ waitOnAngularCtrlScope), /* harmony export */ waitOnElement: () => (/* binding */ waitOnElement), /* harmony export */ waitOnElementToBeNull: () => (/* binding */ waitOnElementToBeNull) /* harmony export */ }); // noinspection JSUnusedGlobalSymbols /** * Waits for a DOM element to appear in the document and executes a callback function when the element is found. * * @param {string} selector - The CSS selector of the element to wait for. * @param {function} callback - The function to be executed once the element is found. The found element is passed as an argument to this function. * @param {number} [timeout=1000] - The interval in milliseconds to wait before checking for the element again. * @param {?number} [maxTimeToWait=null] - The maximum time in milliseconds to wait for the element to appear. If this time passes without finding the element, the function stops checking. If null, there is no limit. */ function waitOnElement(selector, callback, timeout = 1000, maxTimeToWait = null) { const loadElement = (waitedFor = 0) => { const element = document.querySelector(selector); if (maxTimeToWait && waitedFor > maxTimeToWait) { return null; } // Recursively check if the element is loaded if (!element) { return setTimeout(() => { loadElement(waitedFor + timeout); }, timeout); } else { // Call the passed callback function callback(element); } }; loadElement(); } /** * Waits for a specific DOM element, identified by a CSS selector, to become null (i.e., removed from the DOM) * and invokes a callback function once the element is no longer present. The check is performed periodically * with a defined timeout interval. * * @param {string} selector - The CSS selector used to identify the element in the DOM. * @param {Function} callback - The function to be executed once the element becomes null or is removed from the DOM. * @param {number} [timeout=1000] - The interval duration, in milliseconds, after which the DOM is rechecked. Defaults to 1000 ms. */ function waitOnElementToBeNull(selector, callback, timeout = 1000) { const waitForUnload = () => { const element = document.querySelector(selector); // Recursively check if the element is loaded if (element) { return setTimeout(waitForUnload, timeout); } else { // Call the passed callback function callback(); } }; waitForUnload(); } /** * Waits for a specified condition to be true before invoking a callback function. * * This function recursively checks the provided condition at regular intervals * and executes the callback when the condition returns true. A timeout period * defines the interval between condition checks. * * @param {Function} condition - A function returning a boolean value that determines if the wait should end. * @param {Function} callback - The function to execute once the condition evaluates to true. * @param {number} [timeout=1000] - The interval in milliseconds to wait between condition checks. */ function waitOn(condition, callback, timeout = 1000) { const waitFor = () => { // Recursively check if the condition is met if (!condition()) { return setTimeout(waitFor, timeout); } else { // Call the passed callback function callback(); } }; waitFor(); } /** * Retrieves the AngularJS scope associated with a given DOM element selected by a CSS selector and runs a callback function with the scope. * * @param {string} selector - The CSS selector used to identify the DOM element. * @param {(scope: AngularScope) => void} callback - A callback function that is executed once the AngularJS scope is retrieved. The scope is passed as an argument to the callback. * @return {void} This function does not return a value but executes a callback with the AngularJS scope as an argument. * * @throws {Error} Throws an error if the element with the specified selector is not found in the document. */ function waitOnAngularCtrlScope(selector, callback, timeout = 1000) { const querySelector = document.querySelector(selector); const loadAngularScope = () => { var _a; // @ts-ignore const scope = (_a = angular.element(querySelector)) === null || _a === void 0 ? void 0 : _a.scope(); // Recursively check if the scope is loaded if (scope === undefined) { return setTimeout(loadAngularScope, timeout); } else { // Call the passed callback function callback(scope); } }; if (querySelector) { // @ts-ignore angular.element(querySelector).ready(() => { return loadAngularScope(); }); } else { throw new Error(`Element with selector ${selector} not found`); } } var __webpack_exports__waitOn = __webpack_exports__.waitOn var __webpack_exports__waitOnAngularCtrlScope = __webpack_exports__.waitOnAngularCtrlScope var __webpack_exports__waitOnElement = __webpack_exports__.waitOnElement var __webpack_exports__waitOnElementToBeNull = __webpack_exports__.waitOnElementToBeNull export { __webpack_exports__waitOn as waitOn, __webpack_exports__waitOnAngularCtrlScope as waitOnAngularCtrlScope, __webpack_exports__waitOnElement as waitOnElement, __webpack_exports__waitOnElementToBeNull as waitOnElementToBeNull }; //# sourceMappingURL=index.js.map