All files field.js

100% Statements 18/18
100% Branches 2/2
100% Functions 7/7
100% Lines 18/18

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72                    9x         9x         9x   9x   9x   9x   9x 10x 10x 9x                       2x 2x 2x 1x 1x 1x                 1x               1x            
/** Class representing a field from contentstack ui. */
 
class Field {
 
  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;
 
    this._data = fieldDataObject.data.value;
 
    this._connection = connection;
 
    let fieldObj = this
 
    emitter.on('entryUpdate', function (event) {
      let newData = event.data[fieldObj.uid];
      if (fieldObj._data !== newData) {
        fieldObj._data === newData;
      }
    });
  }
 
  /**
   * Sets the data for the current field.
   * @param {Object|string|number} data Data to be set on the field
   * @return {Object} A Promise object which will return Field object.
   */
 
  setData(data) {
    let currentFieldObj = this;
    let dataObj = {data, uid:currentFieldObj.uid}
    return this._connection.sendToParent('setData', dataObj).then(() => {
      this._data = data;
      return Promise.resolve(currentFieldObj);
    }).catch((e) => Promise.reject(e));
  }
 
  /**
   * Gets data of the current field
   * @return {Object|string|number} Returns field data.
   */
 
  getData() {
    return this._data;
  }
 
  /**
   * Sets the focus for a field when an extension is being used. This method shows user presence and highlights field in Contentstack UI by returning a promise object.
   * @return {Object} A Promise object which will resolve on Contentstack UI acknowledgement of focused state.
   */
  setFocus() {
    return this._connection.sendToParent('focus')
  }
 
}
 
 
export default Field;