o2.xdesktop.requireapp("process.xform", "$input", null, false);
/** @classdesc $selector 组件类,此类为所有可选组件的父类
* @class
* @hideconstructor
* @o2category formcomponents
* @extends mwf.xapplication.process.xform.$input
* @abstract
*/
mwf.xapplication.process.xform.$selector = mwf.app$selector = new class(
/** @lends mwf.xapplication.process.xform.$selector# */
{
extends: mwf.app$input,
/**
* 组件加载后触发。如果选项加载为异步,则异步处理完成后触发此事件
* @event mwf.xapplication.process.xform.$selector#load
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
_showvalue: function(node, value){
var optionitems = this.getoptions();
if( optionitems && typeof(optionitems.then) === "function" ){
optionitems.then(function (opt) {
this.__showvalue(node, value, opt)
}.bind(this));
}else{
this.__showvalue(node, value, optionitems)
}
},
/**
* @summary 刷新选择项,如果选择项是脚本,重新计算。
* @example
* this.form.get('fieldid').resetoption();
*/
resetoption: function(){
this.node.empty();
this.setoptions();
this.fireevent("resetoption");
},
/**
* @summary 获取选择项。
* @return {array | promise} 返回选择项数组或promise,如:[
* "女|female",
* "男|male"
* ]
* @example
* this.form.get('fieldid').getoptions();
* @example
* //异步
* var opt = this.form.get('fieldid').getoptions();
* promise.resolve(opt).then(function(options){
* //options为选择项数组
* })
*/
getoptions: function(async, refresh){
this.optionscache = null;
var opt = this._getoptions(async, refresh);
if( (opt && typeof(opt.then) === "function") ){
var p = promise.resolve( opt ).then(function(option){
this.moduleselectag = null;
this.optionscache = (option || []);
return this.optionscache;
}.bind(this));
this.moduleselectag = p;
return p;
}else{
this.optionscache = (opt || []);
return this.optionscache;
}
},
_getoptions: function(async, refresh){
debugger;
switch (this.json.itemtype) {
case "values":
return this.json.itemvalues;
case "script":
return this.form.macro.exec(((this.json.itemscript) ? this.json.itemscript.code : ""), this);
default:
break;
}
var opts, firstopts = this.getfirstoption();
switch (this.json.itemtype) {
case "dict":
opts = this.getoptionswithdict( async, refresh ); break;
case "view":
opts = this.getoptionswithview( async, refresh ); break;
case "statement":
opts = this.getoptionswithstatement( async, refresh ); break;
}
if( opts && typeof(opts.then) === "function" ){
return promise.resolve(opts).then(function ( opts ) {
return this._contactoption( firstopts, opts );
}.bind(this));
}else{
return this._contactoption( firstopts, opts );
}
// if( (defaultopts && typeof(defaultopts.then) === "function" ) || (opts && typeof(opts.then) === "function" ) ){
// return promise.all( [defaultopts, opts] ).then(function (arr) {
// return this._contactoption( arr[0], arr[1] );
// }.bind(this));
// }else{
// return this._contactoption( defaultopts, opts );
// }
},
_contactoption: function(opt1, opt2){
var opta, optb;
if( !opt1 )opt1 = [];
if( !opt2 )opt2 = [];
opta = typeof(opt1) !== "array" ? [opt1]: opt1;
optb = typeof(opt2) !== "array" ? [opt2]: opt2;
opta.each(function (o) {
if( o )optb.unshift( o );
});
return optb;
},
getfirstoption: function(){
//return this.form.macro.exec(((this.json.defaultoptionsscript) ? this.json.defaultoptionsscript.code : ""), this);
if( !this.json.firstoptionenable )return [];
return [this.json.firstoption||"|"];
},
/**
* @summary 获取整理后的选择项。
* @param {boolean} [refresh] 是否忽略缓存重新计算可选项。
* @return {object} 返回整理后的选择项数组或promise,如:
* {"valuelist": ["","female","male"], "textlist": ["","女","男"]}
*
* @example
* var optiondata = this.form.get('fieldid').getoptionsobj();
* @example
* //异步
* var opt = this.form.get('fieldid').getoptionsobj(true);
* promise.resolve(opt).then(function(optiondata){
* //optiondata为选择项
* })
*/
getoptionsobj : function( refresh ){
var optionitems = (refresh!==true && this.optionscache) ? this.optionscache : this.getoptions();
if( optionitems && typeof(optionitems.then) === "function" ){
return promise.resolve( optionitems ).then(function(optitems){
return this._getoptionsobj( optitems );
}.bind(this));
}else{
return this._getoptionsobj( optionitems );
}
},
_getoptionsobj: function( optitems ){
var textlist = [];
var valuelist = [];
optitems.each(function(item){
var tmps = item.split("|");
textlist.push( tmps[0] );
valuelist.push( tmps[1] || tmps[0] );
});
return { textlist : textlist, valuelist : valuelist };
},
setoptions: function(){
var optionitems = this.getoptions();
this._setoptions(optionitems);
},
/**
* @summary 获取选中项的value和text。
* @return {object} 返回选中项的value和text,如:
* {"value": ["male"], "text": ["男"]}
* {"value": [""], "text": [""]}
*
* @example
* var data = this.form.get('fieldid').gettextdata();
* var text = data.text[0] //获取选中项的文本
*/
gettextdata: function(){
var ops;
if (this.isreadonly()){
ops = this.getoptionsobj();
var data = this._getbusinessdata();
var d = typeof(data) === "array" ? data : [data];
if( ops && typeof(ops.then) === "function" ){
return promise.resolve(ops).then(function (opts) {
return this._gettextdata(d, opts)
}.bind(this));
}else{
return this._gettextdata(d, ops)
}
}else{
return this._getinputtextdata();
}
},
_gettextdata: function(d, opts){
var value = [], text = [];
d.each( function (v) {
var idx = opts.valuelist.indexof( v );
value.push( v || "" );
text.push( idx > -1 ? opts.textlist[idx] : (v || "") );
});
if (!value.length) value = [""];
if (!text.length) text = [""];
return {"value": value, "text": text};
},
getoptionswithdict: function ( async, refresh ) {
if( !this.json.itemdict || !this.json.itemdict.length )return [];
var obj = this.json.itemdict[0];
var dict = new this.form.macro.environment.dict({
"type": obj.apptype,
"application": obj.appid,
"name": obj.id
});
var paths = (this.json.itemdictpath || "").split("/");
paths.splice(0, 1); //第一个是root,删掉
var path = paths.length ? paths.join(".") : null;
var asy = o2.typeof( async ) === "boolean" ? async : (this.json.itemdictasync !== false);
var data = dict.get( path, null, null, asy, refresh === true );
if( data && typeof(data.then) === "function" ){
return data.then(function (data) {
return this.parsedictoptions(data);
}.bind(this));
}else{
return this.parsedictoptions(data);
}
},
getstring: function( d ){
switch (o2.typeof(d)) {
case "null": return "";
case "string": return d;
case "boolean": case "number": case "date": return d.tostring();
default: return "";
}
},
parsedictoptions: function (d) {
var arr = [], value, text, valuekey = this.json.dictvaluekey, textkey = this.json.dicttextkey;
switch ( o2.typeof(d) ) {
case "array":
d.each(function (i) {
switch ( o2.typeof(i) ) {
case "object":
if( valuekey && textkey ){
value = this.getstring( i[valuekey] );
text = this.getstring( i[textkey] );
arr.push( text "|" value );
}else if( valuekey ){
arr.push( this.getstring( i[valuekey] ) );
}else if( textkey ){
arr.push( this.getstring(i[textkey] ));
}
break;
case "null": break;
default: arr.push( i.tostring() ); break;
}
}.bind(this));
return arr;
case "object":
object.each(d, function (i, key) {
switch ( o2.typeof(i) ) {
case "object":
if( valuekey && textkey ){
value = this.getstring( i[valuekey] );
text = this.getstring(i[ textkey] );
arr.push( value "|" text );
}else if( valuekey ){
arr.push( this.getstring( i[valuekey] ) );
}else if( textkey ){
arr.push( this.getstring(i[ textkey] ) );
}
break;
case "null": break;
default: arr.push( i.tostring() "|" key.tostring() ); break;
}
}.bind(this))
return arr;
case "null":
return [];
default:
return [d.tostring()];
}
},
getoptionswithview: function(async, refresh){
if( !this.json.itemview )return [];
var obj = this.json.itemview;
var asy = o2.typeof( async ) === "boolean" ? async : (this.json.itemviewasync !== false);
var filter = [];
if (this.json.viewfilterlist && this.json.viewfilterlist.length){
this.json.viewfilterlist.each(function(entry){
entry.value = this.form.macro.exec(entry.code.code, this);
filter.push(entry);
}.bind(this));
}
var data = this.form.macro.environment.view.lookup({
"view": obj.id,
"application": obj.application,
"filter": filter
}, null, asy);
if( data && typeof(data.then) === "function" ){
return data.then(function (data) {
return this.parseviewoptions(data);
}.bind(this));
}else{
return this.parseviewoptions(data);
}
},
parseviewoptions: function(json){
var arr = [], value, text, valuekey = this.json.viewvaluecolumn, textkey = this.json.viewtextcolumn;
json.grid.each(function(d){
var i = d.data || {};
if( valuekey && textkey ){
value = valuekey === "bundle" ? d.bundle : (this.getstring( i[valuekey] ));
text = textkey === "bundle" ? d.bundle : (this.getstring(i[ textkey] ));
arr.push( text "|" value );
}else if( valuekey ){
arr.push( valuekey === "bundle" ? d.bundle : (this.getstring( i[valuekey] )) );
}else if( textkey ){
arr.push( textkey === "bundle" ? d.bundle : (this.getstring(i[ textkey] )) );
}
}.bind(this))
return arr.unique();
},
getoptionswithstatement: function(async, refresh){
if( !this.json.itemstatement )return [];
var obj = this.json.itemstatement;
var asy = o2.typeof( async ) === "boolean" ? async : (this.json.itemviewasync !== false);
var filter = [];
if (this.json.statementfilterlist && this.json.statementfilterlist.length){
this.json.statementfilterlist.each(function(entry){
entry.value = this.form.macro.exec(entry.code.code, this);
filter.push(entry);
}.bind(this));
}
var parameter = {};
if( this.json.statementparameterlist && this.json.statementparameterlist.length ){
this.json.statementparameterlist.each(function(entry){
parameter[entry.parameter] = this.parseparameter(entry);
}.bind(this));
}
var data = this.form.macro.environment.statement.execute({
"name" : obj.name,
"mode" : "data",
"page" : 1, //(number)可选,当前页码,默认为1
"pagesize" : 1000, //(number)可选,每页的数据条数,默认为20
"filter": filter,
"parameter" : parameter,
"parameterlist": this.json.parameterlist
}, null, asy);
if( data && typeof(data.then) === "function" ){
return data.then(function (data) {
return this.parsestatementoptions(data);
}.bind(this));
}else{
return this.parsestatementoptions(data);
}
},
parsestatementoptions: function(json){
var arr = [], value, text, valuekey = this.json.statementvaluecolumn, textkey = this.json.statementtextcolumn;
json.data.each(function(d){
if( valuekey && textkey ){
value = this.getdatabypath(d, valuekey);
text = this.getdatabypath(d, textkey);
arr.push( text "|" value );
}else if( valuekey ){
value = this.getdatabypath(d, valuekey);
arr.push( value );
}else if( textkey ){
text = this.getdatabypath(d, textkey);
arr.push( text );
}
}.bind(this));
return arr.unique();
},
parseparameter: function (f) {
var value = f.value;
if( f.valuetype === "script" ){
value = this.form.macro.exec(f.valuescript ? f.valuescript.code : "", this);
}
if (typeof(value) === "date") {
value = value.format("db");
}
var user = layout.user;
switch (value) {
case "@year":
value = (new date().getfullyear()).tostring();
break;
case "@season":
var m = new date().format("%m");
if (["01", "02", "03"].contains(m)) {
value = "1"
} else if (["04", "05", "06"].contains(m)) {
value = "2"
} else if (["07", "08", "09"].contains(m)) {
value = "3"
} else {
value = "4"
}
break;
case "@month":
value = new date().format("%y-%m");
break;
case "@time":
value = new date().format("db");
break;
case "@date":
value = new date().format("%y-%m-%d");
break;
default:
}
if (f.formattype === "datetimevalue" || f.formattype === "datetimevalue") {
value = "{ts '" value "'}"
} else if (f.formattype === "datevalue") {
value = "{d '" value "'}"
} else if (f.formattype === "timevalue") {
value = "{t '" value "'}"
}
return value;
},
getdatabypath: function (obj, path) {
var pathlist = path.split(".");
for (var i = 0; i < pathlist.length; i ) {
var p = pathlist[i];
if ((/(^[1-9]\d*$)/.test(p))) p = p.toint();
if (obj[p]) {
obj = obj[p];
} else if(obj[p] === undefined || obj[p] === null) {
obj = "";
break;
} else {
obj = obj[p];
break;
}
}
return this.getstring( obj );
}
});
source