Radio.js 17.0 KB
Newer Older
R
roo00 已提交
1
MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
2
MWF.require("MWF.widget.UUID", null, false);
U
jsdoc  
unknown 已提交
3
/** @class Radio 单选按钮。
4
 * @o2cn 单选按钮
U
jsdoc  
unknown 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 * @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
U
jsodc  
unknown 已提交
18 19
 * @o2category FormComponents
 * @o2range {Process|CMS|Portal}
U
jsdoc  
unknown 已提交
20 21 22 23 24
 * @hideconstructor
 */
MWF.xApplication.process.Xform.Radio = MWF.APPRadio =  new Class(
    /** @lends MWF.xApplication.process.Xform.Radio# */
    {
R
roo00 已提交
25 26
	Implements: [Events],
	Extends: MWF.APP$Input,
U
jsdoc  
unknown 已提交
27 28 29 30 31
    /**
     * @ignore
     * @member {Element} descriptionNode
     * @memberOf MWF.xApplication.process.Xform.Radio#
     */
R
roo00 已提交
32 33
    loadDescription: function(){},
    _loadNode: function(){
R
roo00 已提交
34
        if (this.readonly || this.json.isReadonly ){
R
roo00 已提交
35 36 37 38 39 40 41
            this._loadNodeRead();
        }else{
            this._loadNodeEdit();
        }
    },
    _loadNodeRead: function(){
        this.node.empty();
42 43 44 45
        this.node.set({
            "nodeId": this.json.id,
            "MWFType": this.json.type
        });
R
roo00 已提交
46 47 48 49 50 51 52 53 54 55
        var radioValues = this.getOptions();
        var value = this.getValue();
        if (value){
            var texts = "";
            for (var i=0; i<radioValues.length; i++){
                var item = radioValues[i];
                var tmps = item.split("|");
                var t = tmps[0];
                var v = tmps[1] || t;

NoSubject's avatar
NoSubject 已提交
56 57 58 59 60
                // if (value.indexOf(v)!=-1){
                //     texts = t;
                //     break;
                // }
                if (value == v){
R
roo00 已提交
61 62 63 64 65 66 67
                    texts = t;
                    break;
                }
            }
            this.node.set("text", texts);
        }
    },
68 69 70 71
    _resetNodeEdit: function(){
        var div = new Element("div");
        div.set(this.json.properties);
        div.inject(this.node, "after");
R
roo00 已提交
72

73 74 75
        this.node.destroy();
        this.node = div;
    },
R
roo00 已提交
76 77
    _loadNodeEdit: function(){
		//this.container = new Element("select");
78 79
        if (!this.json.preprocessing) this._resetNodeEdit();

R
roo00 已提交
80 81 82 83 84 85 86 87 88
		this.node.set({
			"id": this.json.id,
			"MWFType": this.json.type,
			"styles": {
				"display": "inline"
			}
		});
		this.setOptions();
	},
NoSubject's avatar
NoSubject 已提交
89 90
    _loadDomEvents: function(){
    },
R
roo00 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    _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));
    },
NoSubject's avatar
NoSubject 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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{
            var inputs = this.node.getElements("input");
            inputs.each(function(input){
                input.addEvent(key, function(event){
                    return (fun) ? fun(this, event) : null;
                }.bind(this));
            }.bind(this));
        }
    },
U
jsdoc  
unknown 已提交
120 121 122 123 124
    /**
     * @summary 刷新选择项,如果选择项是脚本,重新计算。
     * @example
     * this.form.get('fieldId').resetOption();
     */
R
roo00 已提交
125 126 127 128
    resetOption: function(){
        this.node.empty();
        this.setOptions();
    },
U
unknown 已提交
129 130 131 132 133 134 135 136 137
        /**
         * @summary 获取选择项。
         * @return {Array} 返回选择项数组,如果使用选择项脚本,根据脚本返回决定,如:<pre><code class='language-js'>[
         *  "女|female",
         *  "男|male"
         * ]</code></pre>
         * @example
         * this.form.get('fieldId').getOptions();
         */
R
roo00 已提交
138 139 140 141 142 143 144 145
	getOptions: function(){
		if (this.json.itemType == "values"){
			return this.json.itemValues;
		}else{
			return this.form.Macro.exec(this.json.itemScript.code, this);
		}
		return [];
	},
U
unknown 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    /**
     * @summary 获取整理后的选择项。
     * @return {Object} 返回整理后的选择项,如:
     * <pre><code class='language-js'>{"valueList": ["","female","male"], "textList": ["","女","男"]}
     * </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 };
    },

NoSubject's avatar
NoSubject 已提交
170 171 172 173 174 175
    setOptions: function(){
        var optionItems = this.getOptions();
        this._setOptions(optionItems);
    },

	_setOptions: function(optionItems){
176
        var p = o2.promiseAll(optionItems).then(function(radioValues){
NoSubject's avatar
NoSubject 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            this.moduleSelectAG = null;

            if (!radioValues) radioValues = [];
            if (o2.typeOf(radioValues)==="array"){
                var flag = (new MWF.widget.UUID).toString();
                radioValues.each(function(item){
                    var tmps = item.split("|");
                    var text = tmps[0];
                    var value = tmps[1] || text;

                    var radio = new Element("input", {
                        "type": "radio",
                        "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
                        "value": value,
                        "showText": text,
                        "styles": this.json.buttonStyles
                    }).inject(this.node);
                    //radio.appendText(text, "after");

                    var textNode = new Element( "span", {
                        "text" : text,
                        "styles" : { "cursor" : "default" }
                    }).inject(this.node);
                    textNode.addEvent("click", function( ev ){
                        if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
                        this.radio.checked = true;
                        this.radio.fireEvent("change");
                        this.radio.fireEvent("click");
                    }.bind( {radio : radio} ) );
R
roo00 已提交
206

NoSubject's avatar
NoSubject 已提交
207 208
                    radio.addEvent("click", function(){
                        this.validationMode();
U
unknown 已提交
209
                        if (this.validation()) {
210
                            this._setBusinessData(this.getInputData("change"));
U
unknown 已提交
211 212
                            this.fireEvent("change");
                        }
NoSubject's avatar
NoSubject 已提交
213 214 215 216 217 218 219 220 221 222
                    }.bind(this));

                    Object.each(this.json.events, function(e, key){
                        if (e.code){
                            if (this.options.moduleEvents.indexOf(key)!=-1){
                            }else{
                                radio.addEvent(key, function(event){
                                    return this.form.Macro.fire(e.code, this, event);
                                }.bind(this));
                            }
NoSubject's avatar
NoSubject 已提交
223
                        }
NoSubject's avatar
NoSubject 已提交
224
                    }.bind(this));
NoSubject's avatar
NoSubject 已提交
225
                }.bind(this));
NoSubject's avatar
NoSubject 已提交
226
            }
NoSubject's avatar
NoSubject 已提交
227 228
        }.bind(this), function(){
            this.moduleSelectAG = null;
229 230 231
        }.bind(this));
        this.moduleSelectAG = p;
        if (p) p.then(function(){
NoSubject's avatar
NoSubject 已提交
232
            this.moduleSelectAG = null;
NoSubject's avatar
NoSubject 已提交
233 234
        }.bind(this), function(){
            this.moduleSelectAG = null;
NoSubject's avatar
NoSubject 已提交
235
        }.bind(this));
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

        // this.moduleSelectAG = o2.AG.all(optionItems).then(function(radioValues){
        //     this.moduleSelectAG = null;
        //
        //     if (!radioValues) radioValues = [];
        //     if (o2.typeOf(radioValues)==="array"){
        //         var flag = (new MWF.widget.UUID).toString();
        //         radioValues.each(function(item){
        //             var tmps = item.split("|");
        //             var text = tmps[0];
        //             var value = tmps[1] || text;
        //
        //             var radio = new Element("input", {
        //                 "type": "radio",
        //                 "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
        //                 "value": value,
        //                 "showText": text,
        //                 "styles": this.json.buttonStyles
        //             }).inject(this.node);
        //             //radio.appendText(text, "after");
        //
        //             var textNode = new Element( "span", {
        //                 "text" : text,
        //                 "styles" : { "cursor" : "default" }
        //             }).inject(this.node);
        //             textNode.addEvent("click", function( ev ){
        //                 if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
        //                 this.radio.checked = true;
        //                 this.radio.fireEvent("change");
        //                 this.radio.fireEvent("click");
        //             }.bind( {radio : radio} ) );
        //
        //             radio.addEvent("click", function(){
        //                 this.validationMode();
        //                 if (this.validation()) this._setBusinessData(this.getInputData("change"));
        //             }.bind(this));
        //
        //             Object.each(this.json.events, function(e, key){
        //                 if (e.code){
        //                     if (this.options.moduleEvents.indexOf(key)!=-1){
        //                     }else{
        //                         radio.addEvent(key, function(event){
        //                             return this.form.Macro.fire(e.code, this, event);
        //                         }.bind(this));
        //                     }
        //                 }
        //             }.bind(this));
        //         }.bind(this));
        //     }
        // }.bind(this))
        // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
        //     this.moduleSelectAG = null;
        // }.bind(this));
R
roo00 已提交
289
	},
NoSubject's avatar
NoSubject 已提交
290

291
    _setValue: function(value, m, fireChange){
292
        var mothed = m || "__setValue";
293 294 295 296 297 298
	    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(){
299
                        this[mothed](v, fireChange);
300
                        return v;
NoSubject's avatar
NoSubject 已提交
301
                    }.bind(this), function(){});
302
                }else{
303
                    this[mothed](v, fireChange)
304 305
                }
                return v;
NoSubject's avatar
NoSubject 已提交
306
            }.bind(this), function(){});
307 308 309 310

            this.moduleValueAG = p;
            if (this.moduleValueAG) this.moduleValueAG.then(function(){
                this.moduleValueAG = null;
NoSubject's avatar
NoSubject 已提交
311 312
            }.bind(this), function(){
                this.moduleValueAG = null;
313 314
            }.bind(this));
        }else{
315
            this[mothed](value, fireChange);
316
        }
NoSubject's avatar
NoSubject 已提交
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

        // 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 (this.moduleValueAG) this.moduleValueAG.then(function(){
        //     this.moduleValueAG = null;
        // }.bind(this));
NoSubject's avatar
NoSubject 已提交
335 336 337
    },

    __setValue: function(value){
338
        this.moduleValueAG = null;
R
roo00 已提交
339
        this._setBusinessData(value);
NoSubject's avatar
NoSubject 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352

        if (this.readonly || this.json.isReadonly ){
            this._loadNodeRead();
        }else{
            var radios = this.node.getElements("input");
            for (var i=0; i<radios.length; i++){
                var radio = radios[i];
                if (radio.value==value){
                    radio.checked = true;
                    break;
                }
            }
        }
353
        this.fieldModuleLoaded = true;
R
roo00 已提交
354
	},
U
jsdoc  
unknown 已提交
355 356 357 358 359 360 361 362 363 364
    /**
     * @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] //获取选中项的文本
     */
R
roo00 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	getTextData: function(){
		var inputs = this.node.getElements("input");
		var value = "";
		var text = "";
		if (inputs.length){
			for (var i=0; i<inputs.length; i++){
				var input = inputs[i];
				if (input.checked){
					value = input.get("value");
					text = input.get("showText");
					break;
				}
			}
		}
		return {"value": [value] || "", "text": [text || value || ""]};
	},
    getInputData: function(){
NoSubject's avatar
NoSubject 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        if (this.readonly || this.json.isReadonly ){
            return this._getBusinessData();
        }else{
            var inputs = this.node.getElements("input");
            var value = "";
            if (inputs.length){
                for (var i=0; i<inputs.length; i++){
                    var input = inputs[i];
                    if (input.checked){
                        value = input.get("value");
                        break;
                    }
                }
            }
            return value;
        }
R
roo00 已提交
398 399 400
	},
    resetData: function(){
        this.setData(this.getValue());
NoSubject's avatar
NoSubject 已提交
401
    },
U
unknown 已提交
402

U
jsdoc  
unknown 已提交
403 404 405 406 407 408
    /**
     * @summary 获取选中的Dom对象。
     * @return {Element} 返回选中的Dom对象
     * @example
     * var input = this.form.get('fieldId').getSelectedInput();
     */
NoSubject's avatar
NoSubject 已提交
409 410 411 412 413 414 415 416
    getSelectedInput: function(){
        var inputs = this.node.getElements("input");
        if (inputs.length){
            for (var i=0; i<inputs.length; i++){
                if (inputs[i].checked) return inputs[i];
            }
        }
        return null;
R
roo00 已提交
417
    },
NoSubject's avatar
NoSubject 已提交
418

419 420
    setData: function(data, fireChange){
        return this._setValue(data, "__setData", fireChange);
421 422 423 424 425 426 427 428
        // 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);
        // }
NoSubject's avatar
NoSubject 已提交
429 430 431 432 433 434 435 436 437 438

        // if (data && data.isAG){
        //     this.moduleValueAG = data;
        //     data.addResolve(function(v){
        //         this.setData(v);
        //     }.bind(this));
        // }else{
        //     this.__setData(data);
        //     this.moduleValueAG = null;
        // }
NoSubject's avatar
NoSubject 已提交
439 440
    },

441
    __setData: function(data, fireChange){
442
        this.moduleValueAG = null;
443
        var old = this.getInputData();
R
roo00 已提交
444 445 446 447 448 449 450 451 452 453 454
        this._setBusinessData(data);
		var inputs = this.node.getElements("input");
		
		if (inputs.length){
			for (var i=0; i<inputs.length; i++){
				if (data==inputs[i].get("value")){
					inputs[i].set("checked", true);
				}else{
					inputs[i].set("checked", false);
				}
			}
455
            this.validationMode();
R
roo00 已提交
456
		}
457
        this.fieldModuleLoaded = true;
R
roo00 已提交
458
        this.fireEvent("setData");
459
        if (fireChange && old!==data) this.fireEvent("change");
R
roo00 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	},

    notValidationMode: function(text){
        if (!this.isNotValidationMode){
            this.isNotValidationMode = true;
            this.node.store("background", this.node.getStyles("background"));
            this.node.setStyle("background", "#ffdcdc");

            this.errNode = this.createErrorNode(text);
            if (this.iconNode){
                this.errNode.inject(this.iconNode, "after");
            }else{
                this.errNode.inject(this.node, "after");
            }
            this.showNotValidationMode(this.node);
R
roo00 已提交
475 476

            if (!this.node.isIntoView()) this.node.scrollIntoView();
R
roo00 已提交
477 478 479 480
        }
    },

    validationMode: function(routeName, opinion){
481
        // if (!this.validationConfig(routeName, opinion))  return false;
R
roo00 已提交
482 483 484 485 486 487 488 489 490 491 492

        if (this.isNotValidationMode){
            this.isNotValidationMode = false;
            this.node.setStyles(this.node.retrieve("background"));
            if (this.errNode){
                this.errNode.destroy();
                this.errNode = null;
            }
        }
    }
	
493
});