PopupWindow.js

    0

    0

    The code defines a simple class named PopupWindow. The class has a single constructor, which takes three arguments: an ID, a URL, and an options object.

    The open() method is called when the user clicks on the popup window. This method gets the URL and ID of the popup, and then uses the window.open() method to open the popup window in the current browser.

    The close() method is called when the user clicks on the popup window to close it. The close() method cancels the promise that was created in open() and closes the popup window.

    The poll() method is called every 500 milliseconds. This method checks to see if the popup window is open and if it is not, it opens the popup window. If the popup window is already open, the poll() method just returns the promise that was created in open(). Otherwise, the poll() method calls the then() method with the arguments that were passed to open(). The then() method uses these arguments to create a new PopupWindow instance and sets the window.id property to the newly created instance's ID. The then() method then returns the new PopupWindow instance.

    Library: react

    import { toParams, toQuery } from './utils';
    
    class PopupWindow {
      constructor(id, url, options = {}) {
        this.id = id;
        this.url = url;
        this.options = options;
      }
    
      open() {
        const { url, id, options } = this;
    
        this.window = window.open(url, id, toQuery(options, ','));
      }
    
      close() {
        this.cancel();
        this.window.close();
      }
    
      poll() {
        this.promise = new Promise((resolve, reject) => {
          this._iid = window.setInterval(() => {
            try {
              const popup = this.window;
    
              if (!popup || popup.closed !== false) {
                this.close();
    
                reject(new Error('The popup was closed'));
    
                return;
              }
    
              if (popup.location.href === this.url || popup.location.pathname === 'blank') {
                return;
              }
    
              const params = toParams(popup.location.search.replace(/^\?/, ''));
    
              resolve(params);
    
              this.close();
            } catch (error) {
              /*
                * Ignore DOMException: Blocked a frame with origin from accessing a
                * cross-origin frame.
                */
            }
          }, 500);
        });
      }
    
      cancel() {
        if (this._iid) {
          window.clearInterval(this._iid);
          this._iid = null;
        }
      }
    
      then(...args) {
        return this.promise.then(...args);
      }
    
      catch(...args) {
        return this.promise.then(...args);
      }
    
      static open(...args) {
        const popup = new this(...args);
    
        popup.open();
        popup.poll();
    
        return popup;
      }
    }
    
    export default PopupWindow;
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.