All files window.js

100% Statements 22/22
91.67% Branches 11/12
100% Functions 4/4
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 593x           10x 10x 10x                 9x 7x     9x 3x   6x 6x               3x 1x   2x 2x 2x 2x   2x               2x 1x   1x 1x 1x        
let config = { attributes: true, childList: true, subtree: true };
let observer
/** Class representing a iframe window from contentstack ui. */
 
class Window {
  constructor(connection) {
    this._connection = connection;
    this._height;
    this._autoResizingEnabled = false;
  }
 
  /**
   * This method updates the extension height on Contentstack UI. If argument height is not passed then it will calculate the scroll height and set the height of extension window accordingly.
   * @param {string|number} height Desired height of the window iframe window
   * @return {Object} A Promise object which will resolve on acknowledgement for height update.
   */
  updateHeight(height) {
    if (!height || isNaN(height)) {
      height = Math.ceil(document.documentElement.getBoundingClientRect().height);
    }
 
    if (this._height === height) {
      return Promise.resolve();
    }
    this._height = height;
    return this._connection.sendToParent('resize', height);
  }
 
  /**
   * This method enables auto resizing of the extension height.
   * @return {Object} Field object.
   */
  enableAutoResizing() {
    if (this._autoResizingEnabled) {
      return this;
    }
    this._autoResizingEnabled = true;
    observer = new MutationObserver(this.updateHeight.bind(this));
    Eif (window.document.body) {
      observer.observe(window.document.body, config);
    }
    return this;
  }
 
  /**
   * This method disables auto resizing of the extension height.
   * @return {Object} Field object.
   */
  disableAutoResizing() {
    if (!this._autoResizingEnabled) {
      return this;
    }
    this._autoResizingEnabled = false;
    observer.disconnect();
    return this;
  }
}
 
export default Window;