Radio.js 13.0 KB
Newer Older
R
roo00 已提交
1
MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
2
MWF.require("MWF.widget.UUID", null, false);
R
roo00 已提交
3 4 5 6 7 8
MWF.xApplication.process.Xform.Radio = MWF.APPRadio =  new Class({
	Implements: [Events],
	Extends: MWF.APP$Input,

    loadDescription: function(){},
    _loadNode: function(){
R
roo00 已提交
9
        if (this.readonly || this.json.isReadonly ){
R
roo00 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
            this._loadNodeRead();
        }else{
            this._loadNodeEdit();
        }
    },
    _loadNodeRead: function(){
        this.node.empty();
        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 已提交
27 28 29 30 31
                // if (value.indexOf(v)!=-1){
                //     texts = t;
                //     break;
                // }
                if (value == v){
R
roo00 已提交
32 33 34 35 36 37 38
                    texts = t;
                    break;
                }
            }
            this.node.set("text", texts);
        }
    },
39 40 41 42
    _resetNodeEdit: function(){
        var div = new Element("div");
        div.set(this.json.properties);
        div.inject(this.node, "after");
R
roo00 已提交
43

44 45 46
        this.node.destroy();
        this.node = div;
    },
R
roo00 已提交
47 48
    _loadNodeEdit: function(){
		//this.container = new Element("select");
49 50
        if (!this.json.preprocessing) this._resetNodeEdit();

R
roo00 已提交
51 52 53 54 55 56 57 58 59
		this.node.set({
			"id": this.json.id,
			"MWFType": this.json.type,
			"styles": {
				"display": "inline"
			}
		});
		this.setOptions();
	},
NoSubject's avatar
NoSubject 已提交
60 61
    _loadDomEvents: function(){
    },
R
roo00 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    _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 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
    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));
        }
    },
R
roo00 已提交
91 92 93 94 95 96 97 98 99 100 101 102
    resetOption: function(){
        this.node.empty();
        this.setOptions();
    },
	getOptions: function(){
		if (this.json.itemType == "values"){
			return this.json.itemValues;
		}else{
			return this.form.Macro.exec(this.json.itemScript.code, this);
		}
		return [];
	},
NoSubject's avatar
NoSubject 已提交
103 104 105 106 107 108
    setOptions: function(){
        var optionItems = this.getOptions();
        this._setOptions(optionItems);
    },

	_setOptions: function(optionItems){
109
        var p = o2.promiseAll(optionItems).then(function(radioValues){
NoSubject's avatar
NoSubject 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            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 已提交
139

NoSubject's avatar
NoSubject 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
                    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));
                            }
NoSubject's avatar
NoSubject 已提交
153
                        }
NoSubject's avatar
NoSubject 已提交
154
                    }.bind(this));
NoSubject's avatar
NoSubject 已提交
155
                }.bind(this));
NoSubject's avatar
NoSubject 已提交
156
            }
157 158 159
        }.bind(this));
        this.moduleSelectAG = p;
        if (p) p.then(function(){
NoSubject's avatar
NoSubject 已提交
160 161
            this.moduleSelectAG = null;
        }.bind(this));
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 206 207 208 209 210 211 212 213 214

        // 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 已提交
215
	},
NoSubject's avatar
NoSubject 已提交
216 217

    _setValue: function(value){
218
        var p = o2.promiseAll(value).then(function(v){
NoSubject's avatar
NoSubject 已提交
219
            if (o2.typeOf(v)=="array") v = v[0];
NoSubject's avatar
NoSubject 已提交
220 221 222 223
            if (this.moduleSelectAG){
                this.moduleValueAG = this.moduleSelectAG;
                this.moduleSelectAG.then(function(){
                    this.__setValue(v);
224
                    return v;
NoSubject's avatar
NoSubject 已提交
225 226 227 228 229 230
                }.bind(this));
            }else{
                this.__setValue(v)
            }
            return v;
        }.bind(this));
NoSubject's avatar
NoSubject 已提交
231

232
        this.moduleValueAG = p;
NoSubject's avatar
NoSubject 已提交
233 234 235
        if (this.moduleValueAG) this.moduleValueAG.then(function(){
            this.moduleValueAG = null;
        }.bind(this));
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

        // 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 已提交
253 254 255
    },

    __setValue: function(value){
R
roo00 已提交
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
        this._setBusinessData(value);
		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;
			}
		}
	},
	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 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        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 已提交
299 300 301
	},
    resetData: function(){
        this.setData(this.getValue());
NoSubject's avatar
NoSubject 已提交
302 303 304 305 306 307 308 309 310
    },
    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 已提交
311
    },
NoSubject's avatar
NoSubject 已提交
312 313

    setData: function(data){
314 315 316 317 318 319 320 321 322
        return this._setValue(data);
        // 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 已提交
323 324 325 326 327 328 329 330 331 332

        // 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 已提交
333 334 335
    },

    __setData: function(data){
R
roo00 已提交
336 337 338 339 340 341 342 343 344 345 346 347
        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);
				}
			}
		}
R
roo00 已提交
348
        this.fireEvent("setData");
R
roo00 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	},

    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 已提交
364 365

            if (!this.node.isIntoView()) this.node.scrollIntoView();
R
roo00 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        }
    },

    validationMode: function(routeName, opinion){
        if (!this.validationConfig(routeName, opinion))  return false;

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