| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
- /** @class Select 下拉选择组件。
- * @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(); //字段是否为空
- * field.resetData(); //重置字段的值为默认值或置空
- * @extends MWF.xApplication.process.Xform.$Input
- * @category FormComponents
- * @hideconstructor
- */
- MWF.xApplication.process.Xform.Select = MWF.APPSelect = new Class(
- /** @lends MWF.xApplication.process.Xform.Select# */
- {
- Implements: [Events],
- Extends: MWF.APP$Input,
- iconStyle: "selectIcon",
- /**
- * @ignore
- * @member {Element} descriptionNode
- * @memberOf MWF.xApplication.process.Xform.Select#
- */
- initialize: function(node, json, form, options){
- this.node = $(node);
- this.node.store("module", this);
- this.json = json;
- this.form = form;
- this.field = true;
- },
- _loadNode: function(){
- if (this.readonly|| this.json.isReadonly){
- this._loadNodeRead();
- }else{
- this._loadNodeEdit();
- }
- },
- _loadNodeRead: function(){
- this.node.empty();
- this.node.set({
- "nodeId": this.json.id,
- "MWFType": this.json.type
- });
- var optionItems = this.getOptions();
- var value = this.getValue();
- if (value){
- if (typeOf(value)!=="array") value = [value];
- var texts = [];
- optionItems.each(function(item){
- var tmps = item.split("|");
- var t = tmps[0];
- var v = tmps[1] || t;
- if (v){
- if (value.indexOf(v)!=-1){
- texts.push(t);
- }
- }
- });
- this.node.set("text", texts.join(", "));
- }
- },
- _loadDomEvents: function(){
- Object.each(this.json.events, function(e, key){
- if (e.code){
- if (this.options.moduleEvents.indexOf(key)===-1){
- this.node.addEvent(key, function(event){
- return this.form.Macro.fire(e.code, this, event);
- }.bind(this));
- }
- }
- }.bind(this));
- },
- _loadEvents: function(){
- Object.each(this.json.events, function(e, key){
- if (e.code){
- if (this.options.moduleEvents.indexOf(key)!=-1){
- this.addEvent(key, function(event){
- return this.form.Macro.fire(e.code, this, event);
- }.bind(this));
- }else{
- this.node.addEvent(key, function(event){
- return this.form.Macro.fire(e.code, this, event);
- }.bind(this));
- }
- }
- }.bind(this));
- },
- addModuleEvent: function(key, fun){
- if (this.options.moduleEvents.indexOf(key)!==-1){
- this.addEvent(key, function(event){
- return (fun) ? fun(this, event) : null;
- }.bind(this));
- }else{
- this.node.addEvent(key, function(event){
- return (fun) ? fun(this, event) : null;
- }.bind(this));
- }
- },
- _loadStyles: function(){
- if (this.areaNode){
- if (this.json.styles) if (this.areaNode) this.areaNode.setStyles(this.json.styles);
- if (this.json.inputStyles) this.node.setStyles(this.json.inputStyles);
- }else{
- if (this.json.styles) this.node.setStyles(this.json.styles);
- }
- },
- _resetNodeEdit: function(){
- this.node.empty();
- var select = new Element("select");
- select.set(this.json.properties);
- select.inject(this.node);
- },
- _loadNodeEdit: function(){
- if (!this.json.preprocessing) this._resetNodeEdit();
- var select = this.node.getFirst();
- this.areaNode = this.node;
- this.node = select;
- this.node.set({
- "id": this.json.id,
- "MWFType": this.json.type,
- "styles": {
- "margin-right": "12px"
- }
- });
-
- this.setOptions();
- this.node.addEvent("change", function(){
- var v = this.getInputData("change");
- this._setBusinessData(v);
- this.validationMode();
- if (this.validation()) this._setBusinessData(v);
- }.bind(this));
- },
- /**
- * @summary 刷新选择项,如果选择项是脚本,重新计算。
- * @example
- * this.form.get('fieldId').resetOption();
- */
- resetOption: function(){
- this.node.empty();
- this.setOptions();
- this.fireEvent("resetOption")
- },
- /**
- * @summary 获取选择项。
- * @return {Array} 返回选择项数组,如果使用选择项脚本,根据脚本返回决定
- * @example
- * this.form.get('fieldId').getOptions();
- */
- getOptions: function(){
- if (this.json.itemType == "values"){
- return this.json.itemValues;
- }else{
- return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
- }
- return [];
- },
- setOptions: function(){
- var optionItems = this.getOptions();
- this._setOptions(optionItems);
- },
- _setOptions: function(optionItems){
- var p = o2.promiseAll(optionItems).then(function(options){
- this.moduleSelectAG = null;
- if (!options) options = [];
- if (o2.typeOf(options)==="array"){
- options.each(function(item){
- var tmps = item.split("|");
- var text = tmps[0];
- var value = tmps[1] || text;
- var option = new Element("option", {
- "value": value,
- "text": text
- }).inject(this.node);
- }.bind(this));
- this.fireEvent("setOptions", [options])
- }
- }.bind(this), function(){
- this.moduleSelectAG = null;
- }.bind(this));
- this.moduleSelectAG = p;
- if (p) p.then(function(){
- this.moduleSelectAG = null;
- }.bind(this), function(){
- this.moduleSelectAG = null;
- }.bind(this));
- // this.moduleSelectAG = o2.AG.all(optionItems).then(function(options){
- // this.moduleSelectAG = null;
- // if (!options) options = [];
- // if (o2.typeOf(options)==="array"){
- // options.each(function(item){
- // var tmps = item.split("|");
- // var text = tmps[0];
- // var value = tmps[1] || text;
- //
- // var option = new Element("option", {
- // "value": value,
- // "text": text
- // }).inject(this.node);
- // }.bind(this));
- // this.fireEvent("setOptions", [options])
- // }
- // }.bind(this));
- // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
- // this.moduleSelectAG = null;
- // }.bind(this));
- },
- // __setOptions: function(){
- // var optionItems = this.getOptions();
- // if (!optionItems) optionItems = [];
- // if (o2.typeOf(optionItems)==="array"){
- // optionItems.each(function(item){
- // var tmps = item.split("|");
- // var text = tmps[0];
- // var value = tmps[1] || text;
- //
- // var option = new Element("option", {
- // "value": value,
- // "text": text
- // }).inject(this.node);
- // }.bind(this));
- // this.fireEvent("setOptions", [optionItems])
- // }
- // },
- addOption: function(text, value){
- var option = new Element("option", {
- "value": value || text,
- "text": text
- }).inject(this.node);
- this.fireEvent("addOption", [text, value])
- },
- _setValue: function(value, m){
- var mothed = m || "__setValue";
- if (!!value){
- var p = o2.promiseAll(value).then(function(v){
- if (o2.typeOf(v)=="array") v = v[0];
- if (this.moduleSelectAG){
- this.moduleValueAG = this.moduleSelectAG;
- this.moduleSelectAG.then(function(){
- this[mothed](v);
- return v;
- }.bind(this), function(){});
- }else{
- this[mothed](v)
- }
- return v;
- }.bind(this), function(){});
- this.moduleValueAG = p;
- if (this.moduleValueAG) this.moduleValueAG.then(function(){
- this.moduleValueAG = null;
- }.bind(this), function(){
- this.moduleValueAG = null;
- }.bind(this));
- }else{
- this[mothed](value);
- }
- // this.moduleValueAG = o2.AG.all(value).then(function(v){
- // if (o2.typeOf(v)=="array") v = v[0];
- // if (this.moduleSelectAG){
- // this.moduleValueAG = this.moduleSelectAG;
- // this.moduleSelectAG.then(function(){
- // this.__setValue(v);
- // }.bind(this));
- // }else{
- // this.__setValue(v)
- // }
- // return v;
- // }.bind(this));
- // if (value && value.isAG){
- // this.moduleValueAG = o2.AG.all(value),then(function(v){
- // this._setValue(v);
- // }.bind(this));
- // // this.moduleValueAG = value;
- // // value.addResolve(function(v){
- // // this._setValue(v);
- // // }.bind(this));
- // }else{
- //
- // }
- },
- __setValue: function(value){
- if (!this.readonly && !this.json.isReadonly ) {
- this._setBusinessData(value);
- for (var i=0; i<this.node.options.length; i++){
- var option = this.node.options[i];
- if (option.value==value){
- option.selected = true;
- // break;
- }else{
- option.selected = false;
- }
- }
- }
- this.moduleValueAG = null;
- },
- // _setValue: function(value){
- // if (!this.readonly && !this.json.isReadonly ) {
- // this._setBusinessData(value);
- // for (var i=0; i<this.node.options.length; i++){
- // var option = this.node.options[i];
- // if (option.value==value){
- // option.selected = true;
- // // break;
- // }else{
- // option.selected = false;
- // }
- // }
- // }
- // //this.node.set("value", value);
- // },
- /**
- * @summary 获取选中项的value和text。
- * @return {Object} 返回选中项的value和text,如:
- * <pre><code class='language-js'>{"value": ["male"], "text": ["男"]}
- * {"value": [""], "text": [""]}
- * </code></pre>
- * @example
- * var data = this.form.get('fieldId').getTextData();
- * var text = data.text[0] //获取选中项的文本
- */
- getTextData: function(){
- var value = [];
- var text = [];
- if (this.readonly|| this.json.isReadonly){
- var ops = this.getOptionsObj();
- var data = this._getBusinessData();
- var d = typeOf(data) === "array" ? data : [data];
- d.each( function (v) {
- var idx = ops.valueList.indexOf( v );
- value.push( v || "" );
- text.push( idx > -1 ? ops.textList[idx] : (v || "") );
- });
- }else{
- var ops = this.node.getElements("option");
- ops.each(function(op){
- if (op.selected){
- var v = op.get("value");
- var t = op.get("text");
- value.push(v || "");
- text.push(t || v || "");
- }
- });
- }
- if (!value.length) value = [""];
- if (!text.length) text = [""];
- return {"value": value, "text": text};
- },
- getInputData: function(){
- if( this.readonly || this.json.isReadonly ){
- return this._getBusinessData();
- }else{
- var ops = this.node.getElements("option");
- var value = [];
- ops.each(function(op){
- if (op.selected){
- var v = op.get("value");
- if (v) value.push(v);
- }
- });
- if (!value.length) return null;
- return (value.length==1) ? value[0] : value;
- }
- },
- resetData: function(){
- this.setData(this.getValue());
- },
- /**
- * @summary 获取整理后的选择项。
- * @return {Object} 返回整理后的选择项,如:
- * <pre><code class='language-js'>{"value": ["","female","male"], "text": ["","女","男"]}
- * </code></pre>
- * @example
- * var optionData = this.form.get('fieldId').getOptionsObj();
- */
- getOptionsObj : function(){
- var textList = [];
- var valueList = [];
- var optionItems = this.getOptions();
- if (!optionItems) optionItems = [];
- if (o2.typeOf(optionItems)==="array"){
- optionItems.each(function(item){
- var tmps = item.split("|");
- textList.push( tmps[0] );
- valueList.push( tmps[1] || tmps[0] );
- }.bind(this));
- }
- return { textList : textList, valueList : valueList };
- },
- setData: function(data){
- return this._setValue(data, "__setData");
- // if (data && data.isAG){
- // this.moduleValueAG = o2.AG.all(data).then(function(v){
- // if (o2.typeOf(v)=="array") v = v[0];
- // this.__setData(v);
- // }.bind(this));
- // }else{
- // this.__setData(data);
- // }
- // if (data && data.isAG){
- // this.moduleValueAG = data;
- // data.addResolve(function(v){
- // this.setData(v);
- // }.bind(this));
- // }else{
- // this.__setData(data);
- // this.moduleValueAG = null;
- // }
- },
- __setData: function(data){
- this._setBusinessData(data);
- if (this.readonly|| this.json.isReadonly){
- var d = typeOf(data) === "array" ? data : [data];
- var ops = this.getOptionsObj();
- var result = [];
- d.each( function (v) {
- var idx = ops.valueList.indexOf( v );
- result.push( idx > -1 ? ops.textList[idx] : v);
- })
- this.node.set("text", result.join(","));
- }else{
- var ops = this.node.getElements("option");
- ops.each(function(op){
- if (typeOf(data)=="array"){
- if (data.indexOf(op.get("value"))!=-1){
- op.set("selected", true);
- }else{
- op.set("selected", false);
- }
- }else{
- if (data == op.get("value")){
- op.set("selected", true);
- }else{
- op.set("selected", false);
- }
- }
- });
- this.validationMode();
- }
- this.fireEvent("setData", [data]);
- }
-
- });
|