Radio.js 8.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 103 104 105 106
    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 [];
	},
	
	setOptions: function(){
		var radioValues = this.getOptions();
        if (!radioValues) radioValues = [];
NoSubject's avatar
NoSubject 已提交
107 108 109 110 111 112
        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;
R
roo00 已提交
113

NoSubject's avatar
NoSubject 已提交
114 115
                var radio = new Element("input", {
                    "type": "radio",
116
                    "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
NoSubject's avatar
NoSubject 已提交
117 118 119 120
                    "value": value,
                    "showText": text,
                    "styles": this.json.buttonStyles
                }).inject(this.node);
NoSubject's avatar
NoSubject 已提交
121 122 123 124 125 126 127
                //radio.appendText(text, "after");

                var textNode = new Element( "span", {
                    "text" : text,
                    "styles" : { "cursor" : "default" }
                }).inject(this.node);
                textNode.addEvent("click", function( ev ){
NoSubject's avatar
NoSubject 已提交
128
                    if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
NoSubject's avatar
NoSubject 已提交
129 130 131 132 133
                    this.radio.checked = true;
                    this.radio.fireEvent("change");
                    this.radio.fireEvent("click");
                }.bind( {radio : radio} ) );

NoSubject's avatar
NoSubject 已提交
134 135 136 137
                radio.addEvent("click", function(){
                    this.validationMode();
                    if (this.validation()) this._setBusinessData(this.getInputData("change"));
                }.bind(this));
R
roo00 已提交
138

NoSubject's avatar
NoSubject 已提交
139 140 141 142 143 144 145 146
                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));
                        }
R
roo00 已提交
147
                    }
NoSubject's avatar
NoSubject 已提交
148
                }.bind(this));
R
roo00 已提交
149
            }.bind(this));
NoSubject's avatar
NoSubject 已提交
150
        }
R
roo00 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	},
	_setValue: function(value){
        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 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        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 已提交
196 197 198
	},
    resetData: function(){
        this.setData(this.getValue());
NoSubject's avatar
NoSubject 已提交
199 200 201 202 203 204 205 206 207
    },
    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 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    },
	setData: function(data){
        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 已提交
222
        this.fireEvent("setData");
R
roo00 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	},

    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 已提交
238 239

            if (!this.node.isIntoView()) this.node.scrollIntoView();
R
roo00 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        }
    },

    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;
            }
        }
    }
	
256
});