All files / lib field.js

100% Statements 31/31
100% Branches 18/18
100% Functions 9/9
100% Lines 30/30
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                      60x         60x           60x   60x 24x 18x 18x 27x   6x 3x 3x     36x     60x   60x   60x   60x 63x 63x   63x 147x     63x 33x                       6x 6x 6x 3x 3x 3x                   21x               3x            
/** Class representing a field from Contentstack UI. Only available for Custom Field extension */
 
class Field {
  /**
   * @hideconstructor
   */
  constructor(fieldDataObject, connection, emitter) {
    /**
     * The UID of the current field is defined in the content type of the entry.
     * @type {string}
     */
    this.uid = fieldDataObject.data.uid;
    /**
     * The data type of the current field is set using this method.
     * @type {string}
     */
    this.data_type = fieldDataObject.data.schema.data_type;
    /**
     * The schema of the current field (schema of fields such as ‘Single Line Textbox’, ‘Number’,
     *  and so on) is set using this method.
     * @type {Object}
     */
    this.schema = fieldDataObject.data.schema;
 
    if (this.data_type === 'file') {
      if (fieldDataObject.data.value) {
        this._resolvedData = fieldDataObject.data.value;
        this._data = this.schema.multiple === true ?
          fieldDataObject.data.value.map(file => file.uid) :
          fieldDataObject.data.value.uid;
      } else if (this.schema.multiple === true) {
        this._resolvedData = [];
        this._data = [];
      }
    } else {
      this._data = fieldDataObject.data.value;
    }
 
    this._connection = connection;
 
    this._self = fieldDataObject.data.self || false;
 
    const fieldObj = this;
 
    emitter.on('updateFields', (event) => {
      const path = fieldObj.uid.split('.');
      let value = event.data;
 
      path.forEach((key) => {
        if (value) { value = value[key]; }
      });
 
      if (fieldObj._data !== value) {
        fieldObj._data = value;
      }
    });
  }
 
  /**
   * Sets the data for the current field.
   * @param {Object|string|number} data Data to be set on the field
   * @return {external:Promise} A promise object which is resolved when data is set for a field. Note: The data set by this function will only be saved when user saves the entry.
   */
 
  setData(data) {
    const currentFieldObj = this;
    const dataObj = { data, uid: currentFieldObj.uid, self: currentFieldObj._self };
    return this._connection.sendToParent('setData', dataObj).then(() => {
      this._data = data;
      return Promise.resolve(currentFieldObj);
    }).catch(e => Promise.reject(e));
  }
 
  /**
    * Gets the data of the current field
    * @param  {Object} options Options object for get Data method.
    * @param  {boolean} options.resolved If the resolved parameter is set to true for the File field, then the method will return a resolved asset object along with all the field metadata, e.g. 'field.getData({resolved:true})'.
    * @return {Object|string|number} Returns the field data.
    */
  getData({ resolved = false } = {}) {
    return resolved ? this._resolvedData : this._data;
  }
 
  /**
   * Sets the focus for a field when an extension is being used. This method shows user presence and highlights the extension field that the user is currently accessing in Contentstack UI.
   * @return {Object} A promise object which is resolved when Contentstack UI returns an acknowledgement of the focused state.
   */
  setFocus() {
    return this._connection.sendToParent('focus');
  }
}
 
 
export default Field;