import {FormInput} from "../controller.js";

/**
 * 单行文本组件
 * @param {[type]} dataModel [description]
 */
export default class Text extends FormInput {

  constructor(__tfp, dataModel, parent, inputType) {
    if(inputType) {
      super(__tfp, inputType, dataModel, parent);
    } else {
      super(__tfp, "Text", dataModel, parent);
    }
  }

  get value() { 
    if(!this.dataModel.value) return "";
    return this.dataModel.value; 
  }
  set value(value) {
    this.dataModel.value = value;
    if(this._jqObj) {
      this._jqObj.val(value);
      this.exeEventHandler("onChange");
    }
  }

  get maxLength() { return this.dataModel.maxLength }
  set maxLength(value) { 
    this.dataModel.maxLength = value;
    if(this._jqObj) this._jqObj.attr("maxlength", value);
  }

  get placeHolder() { return this.dataModel.placeHolder }
  set placeHolder(value) { 
    this.dataModel.placeHolder = value;
    if(this._jqObj) this._jqObj.attr("placeholder", value);
  }

  get format() { return this.dataModel.format }
  set format(value) {
    this.dataModel.format = value;
  }

  initRuntime() {
    let that = this;
    this._jqObj.change(function() {
      that.dataModel.value = that._jqObj.val();
    });
  }
}