FCheck_wev8.js 3.13 KB
define(['mUtil', "Component", "Form"],function(mUtil, Component, Form) {
	var FCheck = function(options) {		
		Component.super(this, options);	
		
		this.type = "FCheck";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";
        this.dataload = true;

		var vm = this.viewModel = {
			form: "",
			field: {
				label: "",//显示名
				name: "", //字段名
				id: "",   //字段id
				value: "0", //字段值
				defaultValue: ""
			},
			detailtable: {
				isA: false	//是否是明细表字段
			},
			needWrap: false,
			readonly: false
		};
		
		var activeClass = "wev-active", $toggle, $field; 
		
		this.dbValueHasSet = false; //是否已设置表单数据值,调用this.setValueByDB()方法后修改此状态

		this.beforeMount = function () {
			//主表或明细新增,并且值为空 但默认值不为空时,使用默认值代替赋值
            Form.utils.setDefaultValue(this.pageid, vm, "0");
		};
		
		this.mounted = function(){
			var that = this, $el = this.$el;

			$toggle = $el.find(".wev-toggle");
			$field = $el.find("input[name='fieldname_" + vm.field.name + "']");

			if (!vm.readonly) {
			    $toggle.on("click", function () {
	                var isActive = $(this).hasClass(activeClass);

	                that.setValue(!isActive ? "1" : "0");
	            });
			}
			if(vm.field.needParseSqlValue){
			    //解析默认值,默认值sql
	            return Form.utils.parseDefaultSqlValue(vm.field.defaultValue, this.pageid, function(result){
	                vm.field.value = result;
	                //设置默认值
	                vm.field.value && that.setValue(vm.field.value);
	            });
			}
		};
		
	
		this.getData = function(){
			var val = $toggle.hasClass(activeClass) ? "1" : "0";

			return mUtil.parseJSON("fieldname_" + vm.field.name, val);
		};
		
		this.getShowData = function(){
			var val = $toggle.hasClass(activeClass) && "1", showData = mUtil.parseJSON(vm.field.name, val);
			showData[vm.field.name + "_showvalue"] = (val == "1" ? "是" : "否");
			return showData;
		};
		
		this.reset = function(){
			this.setValue(vm.field.value);
		};
		
		this.checkRequired = function () { };
		
		this.bindTrigger = function(triggers, isTrigger){
			var $container = this.$container,
				fieldid = vm.field.id;
			
			if (fieldid && mUtil.isObject(triggers) && triggers["field" + fieldid]) {
				var trigger = triggers["field" + fieldid];

				$field.on("change", function () {
					Mobile_NS.readyToTrigger(trigger, $field, $container);
				});
				
				if ((vm.form.indexOf("detailtable") == -1 && !$p("billid")) || isTrigger) {
					Mobile_NS.readyToTrigger(trigger, $field, $container);
				}
			}
		};
		
		this.setValueByDB = function(value){
			this.dbValueHasSet = true;
			value = value || '0';
			if(value != '0' || vm.field.value == '0') {
                vm.field.value = value;
                this.setValue(value);
            }
		};
		
		this.setValue = function(v){
			v = Number(v || "");

			$toggle.toggleClass(activeClass, !!v);
			$field.val(v).triggerHandler("change");
			this.dbValueHasSet && mUtil.trigger('dataload', this.pageid, this.id);
		};
    };

    return Component.init(FCheck);
});