All files / lib entry.js

100% Statements 38/38
100% Branches 18/18
100% Functions 8/8
100% Lines 36/36
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 109 110 111 112 113 114 115                          36x   36x         36x   36x   36x   36x   36x 45x                   3x                                   15x 15x 15x   15x 15x 15x 45x 6x 6x     105x 39x   36x 36x   21x 15x   6x 6x 6x       3x     12x         12x 12x                   6x 6x 3x 3x     3x          
import Field from './field.js';
 
/** Class representing an entry from Contentstack UI. Not available for Dashboard Widget extension.  */
 
class Entry {
  /**
   * @hideconstructor
   */
  constructor(initializationData, connection, emitter) {
    /**
     * Gets the content type of the current entry.
     * @type {Object}
     */
    this.content_type = initializationData.data.content_type;
 
    this._data = initializationData.data.entry;
    /**
     * Gets the locale of the current entry.
     * @type {string}
     */
    this.locale = initializationData.data.locale;
 
    this._connection = connection;
 
    this._emitter = emitter;
 
    const thisEntry = this;
 
    this._emitter.on('entrySave', (event) => {
      thisEntry._data = event.data;
    });
  }
 
  /**
   * Gets data of the current entry.
   * @return {Object} Returns entry data.
  */
 
  getData() {
    return this._data;
  }
 
 
  /**
   * Gets the field object which allows you to interact with the field.
   * This object will have all the same methods and properties of extension.field except for field.setData().
   * @example
   * var field = entry.getField('field_uid');
   * var fieldSchema = field.schema;
   * var fieldUid = field.uid;
   * var fieldData = field.getData();
   * @param {string} uid Unique ID of the field
   * @return {Object} Field object
  */
 
 
  getField(uid) {
    const path = uid.split('.');
    let value = this._data;
    let schema = this.content_type.schema;
 
    try {
      let skipNext = false;
      path.forEach((key, index) => {
        if (skipNext) {
          skipNext = false;
          return;
        }
 
        schema = schema.find(x => x.uid === key);
        if (!schema) { throw Error('schema not found'); }
 
        value = value[key];
        if (schema.data_type === 'group' && schema.multiple === false
          && path.length !== (index + 1)) {
          schema = schema.schema;
        } else if (schema.data_type === 'group' && schema.multiple === true
         && path.length !== (index + 1)) {
          schema = schema.schema;
          value = value[path[index + 1]];
          skipNext = true;
        }
      });
    } catch (e) {
      throw Error('Invalid uid, Field not found');
    }
 
    const fieldIntilaizationDataObject = {
      data: {
        uid, value, schema, data_type: schema.data_type
      }
    };
    const fieldObject = new Field(fieldIntilaizationDataObject, this._connection, this._emitter);
    return fieldObject;
  }
 
  /**
   * This function executes the callback function every time an entry is saved.
   * @param {function} callback The function to be called when an entry is saved.
   */
 
 
  onSave(callback) {
    const entryObj = this;
    if (callback && typeof (callback) === 'function') {
      entryObj._emitter.on('entrySave', (event) => {
        callback(event.data);
      });
    } else {
      throw Error('Callback must be a function');
    }
  }
}
export default Entry;