mwf.xdesktop.requireapp("process.xform", "$input", null, false);
/** @class textarea 多行文本组件。
* @o2cn 多行文本输入
* @example
* //可以在脚本中获取该组件
* //方法1:
* var field = this.form.get("fieldid"); //获取组件对象
* //方法2
* var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
*
* var data = field.getdata(); //获取值
* field.setdata("字符串值"); //设置值
* field.hide(); //隐藏字段
* var id = field.json.id; //获取字段标识
* var flag = field.isempty(); //字段是否为空
* @extends mwf.xapplication.process.xform.$input
* @o2category formcomponents
* @o2range {process|cms|portal}
* @hideconstructor
*/
mwf.xapplication.process.xform.textarea = mwf.apptextarea = new class({
implements: [events],
extends: mwf.app$input,
_loadnode: function(){
if (this.isreadonly()){
this._loadnoderead();
}else{
this._loadnodeedit();
}
},
_loadnoderead: function(){
this.node.empty();
this.node.set({
"nodeid": this.json.id,
"mwftype": this.json.type
});
},
_loadmergeeditnodebydefault: function(){
var data = this.getsortedsectiondata();
data = data.map(function(d){ return d.data; });
this._setbusinessdata( data.join("\n") );
this._loadnode();
},
getinputdata: function(){
if( this.isreadonly()) {
return this._getbusinessdata();
}else if (this.node.getfirst()){
return this.node.getfirst().get("value");
}else{
return this._getbusinessdata();
}
},
tohtml: function(value){
var reg = new regexp("\n","g");
var reg2 = new regexp("\u003c","g"); //尖括号转义,否则内容会截断
var reg3 = new regexp("\u003e","g");
return ( value || "").replace(reg2,"<").replace(reg3,">").replace(reg,"
");
},
__setdata: function(data){
var old = this.getinputdata();
this._setbusinessdata(data);
if( this.isreadonly()){
this.node.set("html", this.tohtml(data));
}else if (this.node.getfirst()){
this.node.getfirst().set("value", data);
this.checkdescription();
this.validationmode();
}else{
this.node.set("html", this.tohtml(data));
}
if (old!==data) this.fireevent("change");
this.modulevalueag = null;
},
_setvalue: function(value){
if (!value) value = "";
var p = o2.promiseall(value).then(function(v){
if (o2.typeof(v)=="array") v = v[0];
this._setbusinessdata(v);
if (this.node.getfirst()) this.node.getfirst().set("value", v || "");
if (this.isreadonly()){
this.node.set("html", this.tohtml(value));
}
this.fieldmoduleloaded = true;
//this.__setvalue(v);
}.bind(this), function(){});
this.modulevalueag = p;
p.then(function(){
this.modulevalueag = null;
}.bind(this), function(){
this.modulevalueag = null;
}.bind(this));
// this.modulevalueag = o2.ag.all(value).then(function(v){
// this.modulevalueag = null;
// if (o2.typeof(v)=="array") v = v[0];
// this._setbusinessdata(v);
// if (this.node.getfirst()) this.node.getfirst().set("value", v || "");
// if (this.readonly || this.json.isreadonly){
// var reg = new regexp("\n","g");
// var reg2 = new regexp("\u003c","g"); //尖括号转义,否则内容会截断
// var reg3 = new regexp("\u003e","g");
// var text = value.replace(reg2,"<").replace(reg3,">").replace(reg,"
");
// this.node.set("html", text);
// }
// }.bind(this));
//
// if (this.modulevalueag) this.modulevalueag.then(function(){
// this.modulevalueag = null;
// }.bind(this));
return value;
// if (value && value.isag){
// this.modulevalueag = value;
// value.addresolve(function(v){
// this._setvalue(v);
// }.bind(this));
// }else{
// this._setbusinessdata(value);
// if (this.node.getfirst()) this.node.getfirst().set("value", value || "");
// if (this.readonly || this.json.isreadonly){
// var reg = new regexp("\n","g");
// var reg2 = new regexp("\u003c","g"); //尖括号转义,否则内容会截断
// var reg3 = new regexp("\u003e","g");
// var text = value.replace(reg2,"<").replace(reg3,">").replace(reg,"
");
// this.node.set("html", text);
// }
// return value;
// }
},
gettextdata: function(){
//var value = this.node.get("value");
//var text = this.node.get("text");
if (this.isreadonly()){
var value = this._getbusinessdata();
return {"value": [value || ""] , "text": [value || ""]};
}else{
var value = (this.node.getfirst()) ? this.node.getfirst().get("value") : this.node.get("text");
var text = (this.node.getfirst()) ? this.node.getfirst().get("text") : this.node.get("text");
return {"value": [value || ""] , "text": [text || value || ""]};
}
},
// _setvalue: function(value){
// this._setbusinessdata(value);
// if (this.node.getfirst()) this.node.getfirst().set("value", value || "");
// if (this.readonly || this.json.isreadonly){
// var reg = new regexp("\n","g");
// var reg2 = new regexp("\u003c","g"); //尖括号转义,否则内容会截断
// var reg3 = new regexp("\u003e","g");
// var text = value.replace(reg2,"<").replace(reg3,">").replace(reg,"
");
// this.node.set("html", text);
// }
// },
_resetnodeedit: function(){
var input = new element("textarea", {"styles": {
"background": "transparent",
"width": (this.json.inputstyles && this.json.inputstyles.width) ? this.json.inputstyles.width : "100%",
"border": "0px"
}});
var node = new element("div", {"styles": {
// "ovwrflow": (this.json.styles && this.json.styles.overflow) ? this.json.styles.overflow : "hidden",
"position": "relative",
"padding-right": "2px"
}}).inject(this.node, "after");
input.inject(node);
this.node.destroy();
this.node = node;
},
_loadnodeedit: function(){
if (!this.json.preprocessing) this._resetnodeedit();
var input = this.node.getfirst();
if( !input && this.nodehtml ){
this.node.set("html", this.nodehtml);
input = this.node.getfirst();
}
input.set(this.json.properties);
if( this.form.json.textareadisableresize )input.setstyle("resize","none");
this.node.set({
"id": this.json.id,
"mwftype": this.json.type
});
this.node.addevent("change", function(){
this._setbusinessdata(this.getinputdata());
this.fireevent("change");
}.bind(this));
this.node.addevent("input", function(e){
var v=e.target.get("value");
this._setbusinessdata(v);
}.bind(this));
this.node.getfirst().addevent("blur", function(){
this.validation();
}.bind(this));
this.node.getfirst().addevent("keyup", function(){
this.validationmode();
}.bind(this));
},
_afterloaded: function(){
if (!this.readonly){
this.loaddescription();
}
},
loaddescription: function(){
if (this.isreadonly())return;
var v = this._getbusinessdata();
if (!v){
if (this.json.description){
var size, w;
if( this.node.offsetparent === null ){ //隐藏
size = { y: 26 }
}else{
size = this.node.getfirst().getsize();
w = size.x-3;
if( this.json.showicon!='no' && !this.form.json.hidemoduleicon ){
w = size.x-23;
}
}
this.descriptionnode = new element("div", {"styles": this.form.css.descriptionnode, "text": this.json.description}).inject(this.node);
this.descriptionnode.setstyles({
"height": "" size.y "px",
"line-height": "" size.y "px"
});
if( w )this.descriptionnode.setstyles({
"width": "" w "px"
});
this.setdescriptionevent();
}
}
},
setdescriptionevent: function(){
if (this.descriptionnode){
if (common.browser.platform.name==="ios"){
this.descriptionnode.addevents({
"click": function(){
this.descriptionnode.setstyle("display", "none");
this.node.getfirst().focus();
}.bind(this)
});
}else if (common.browser.platform.name==="android"){
this.descriptionnode.addevents({
"click": function(){
this.descriptionnode.setstyle("display", "none");
this.node.getfirst().focus();
}.bind(this)
});
}else{
this.descriptionnode.addevents({
"click": function(){
this.descriptionnode.setstyle("display", "none");
this.node.getfirst().focus();
}.bind(this)
});
}
this.node.getfirst().addevents({
"focus": function(){
this.descriptionnode.setstyle("display", "none");
}.bind(this),
"blur": function(){
if (!this.node.getfirst().get("value")) this.descriptionnode.setstyle("display", "block");
}.bind(this)
});
}
},
setexceldata: function (d) {
var value = d.replace(/
/g,"\n"); //换行符
this.exceldata = value;
this.setdata(value, true);
}
});
source