All files / lib entry.js

100% Statements 38/38
100% Branches 18/18
100% Functions 8/8
100% Lines 37/37

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 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                        13x   13x         13x   13x   13x   13x   13x 14x                   1x                                   5x 5x 5x   5x 5x 5x   15x 2x 2x     35x 13x 1x   12x 12x 7x 5x 2x 2x 2x       1x     4x 4x 4x                   2x 2x 1x 1x     1x        
import Field from "./field.js";
 
/** Class representing a entry from contentstack ui. */
 
class Entry {
 
  constructor(initializationData, connection, emitter) {
 
     /**
     * Gets contenttype of the current entry.
     * @type {Object}
     */
    this.content_type = initializationData.data.content_type;
    
    this._data = initializationData.data.entry;
     /**
     * Gets locale of the current entry.
     * @type {string}
     */
    this.locale = initializationData.data.locale;
 
    this._connection = connection;
 
    this._emitter = emitter;
 
    let thisEntry = this
 
    this._emitter.on('entrySave', function (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. These 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 of the field
   * @return {Object} Field Object
   */
 
 
  getField(uid) {
 
    let 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')
    }
 
    let fieldIntilaizationDataObject = { data: { uid, value, schema, data_type: schema.data_type } };
    let fieldObject = new Field(fieldIntilaizationDataObject, this._connection, this._emitter );
    return fieldObject;
  }
 
  /**
   * This function will call the callback function when entry is saved
   * @param {function} function to be called when entry has been saved
   */
 
 
  onSave(callback) {
    let entryObj = this;
    if (callback && typeof (callback) === "function") {
      entryObj._emitter.on('entrySave', function (event) {
        callback(event.data);
      });
    }else {
      throw Error('Callback must be a function')
    }
  }
}
export default Entry;