All files / lib extension.js

100% Statements 24/24
100% Branches 14/14
100% Functions 4/4
100% Lines 24/24
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                12x                 10x   10x           10x         10x         10x     10x         4x     4x         4x               10x   10x         6x       10x         8x             10x   10x 18x 8x 8x     18x 8x           6x       2x            
import postRobot from 'post-robot';
import Field from './field.js';
import Window from './window.js';
import Stack from './stack';
import Entry from './entry.js';
import Store from './store.js';
import EventEmitter from 'wolfy87-eventemitter';
 
const emitter = new EventEmitter();
 
/** Class representing an extension from Contentstack UI. */
 
class Extension {
  /**
   * @hideconstructor
   */
  constructor(initData) {
    const initializationData = initData;
 
    this.postRobot = postRobot;
    /**
     * This method gives you the configuration parameters. Check out our {@link https://www.contentstack.com/docs/guide/extensions|UI Extension documentation} .
     * @type {Object}
     */
 
    this.config = initializationData.data.config;
    /**
     * This object holds details of the current user.
     * @type {Object}
     */
    this.currentUser = initializationData.data.user;
    /**
     * type of extension, 'FIELD' || 'WIDGET' || 'DASHBOARD'.
     * @type {string}
     */
    this.type = initializationData.data.type || 'FIELD';
 
 
    if (this.type === 'FIELD') {
      /**
     * This method gives you the instance configuration parameters set from the content type builder page in the field settings. This is only available for the Custom Field extension.
     * @type {Object}
     */
      this.fieldConfig = initializationData.data.field_config;
 
 
      initializationData.data.self = true;
      /**
       * Gives you the extension field object which allows you to interact with the field. Only available for the Custom Field extension.
       * @type {Field}
       */
      this.field = new Field(initializationData, postRobot, emitter);
    }
 
    /**
     * Store to persist data for extension.
     * Note: Data is stored in the browser {@link external:localStorage} and will be lost if the {@link external:localStorage} is cleared in the browser.
     * @type {Store}
     */
    this.store = new Store(postRobot);
 
    if (this.type !== 'DASHBOARD') {
      /**
     * This gives you the entry object which allows you to interact with the current entry. Not available in case of the Dashboard Widget extension.
     * @type {Entry}
     */
      this.entry = new Entry(initializationData, postRobot, emitter);
    }
 
 
    if (this.type === 'FIELD' || this.type === 'DASHBOARD') {
      /**
     * The window object provides users with methods that allow them to adjust the size of the iframe that contains the extension. Not available in case of custom widgets.
     * @type {Window}
     */
      this.window = new Window(postRobot, this.type, emitter, initializationData.data.dashboard_width);
    }
 
    /**
     * This method returns stack object which allows users to read and manipulate a range of objects in a stack.
     * @type {Stack}
     */
    this.stack = new Stack(initializationData.data.stack, postRobot);
 
    postRobot.on('extensionEvent', (event) => {
      if (event.data.name === 'entrySave') {
        emitter.emitEvent('entrySave', [{ data: event.data.data }]);
        emitter.emitEvent('updateFields', [{ data: event.data.data }]);
      }
 
      if (event.data.name === 'dashboardResize') {
        emitter.emitEvent('dashboardResize', [{ state: event.data.state }]);
      }
    });
  }
 
  static initialize(version) {
    return postRobot.sendToParent('init', { version });
  }
 
  setReady() {
    return this.postRobot.sendToParent('ready');
  }
}
 
 
export default Extension;