tfp
Version:
A Web UI framework for TaskBuilder
110 lines (101 loc) • 2.93 kB
JavaScript
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._jqObj && this._jqObj.val() != "") {
this.dataModel.value = this._jqObj.val();
}
if (this.dataModel.value || this.dataModel.value == 0) return this.dataModel.value;
return "";
}
set value (value) {
let val = value;
if (isNull(value)) {
val = "";
this.dataModel.value = null;
} else {
//如果设置了计算公式,则需要替换掉金额里的逗号
if (this.dataModel.formula) {
val = (val + "").trim().replaceAll(",", "");
if (isNaN(val)) return;
}
try {
if (this.dataModel.dataType == "number") {
val = parseInt(val);
} else if (this.dataModel.dataType == "digit") {
val = parseFloat(val);
}
} catch (err) {
console.error(err.message);
//this._tfp.showMsg(err.message);
return;
}
this.dataModel.value = val;
}
if (this._jqObj) {
this._jqObj.val(val);
}
if (!this._tfp.isDesigning) {
this.valueOnChange();
this.exeEventHandler("onChange", val);
}
}
get maxlength () { return this.dataModel.maxlength }
set maxlength (value) {
this.dataModel.maxlength = value;
}
get placeholder () { return this.dataModel.placeholder }
set placeholder (value) {
this._jqObj.attr('placeholder', value);
this.dataModel.placeholder = value;
}
get dataType () {
if (!this.dataModel.dataType) this.dataModel.dataType = "text";
return this.dataModel.dataType;
}
set dataType (value) {
this.dataModel.dataType = value;
}
initRuntime () {
let that = this;
this._jqObj.blur(function () {
let val = $(this).val().trim();
if (!isNull(val)) {
if (that.dataModel.dataType && that.dataModel.dataType != "text") {
if (isNaN(val)) {
$(this).focus();
return;
}
}
try {
if (that.dataModel.dataType == "number") {
if (!isInt(val)) {
$(this).focus();
return;
}
val = parseInt(val);
} else if (that.dataModel.dataType == "idcard") {
val = parseFloat(val);
}
} catch (err) {
console.error(err.message);
$(this).focus();
return;
}
}
that.dataModel.value = val;
that.valueOnChange();
that.exeEventHandler("onChange", val);
});
}
}