FCheckItem_wev8.js 3.68 KB
define(['mUtil', "Component", "Form"],function(mUtil, Component, Form) {
    var FCheckItem = function(options) {
        Component.super(this, options);    
        
        this.type = "FCheckItem";
        this.tpl = this.type + "_html";
        this.css = this.type + "_css";

        var vm = this.viewModel = {
            form: "",
            field: {
                label: "", //显示名
                name: "",  //字段名
                id: "",    //字段id
                value: "" //字段值
            },
            detailtable: {
                isA: false    //是否是明细表字段
            },
            readonly: false,
            required: false
        };
        
        var successClass = "wev-check-success",
            errorClass = "wev-check-error",
            blankClass = "wev-check-blank",
            $wevField, $checkItem, $checkEdit, $field;

        this.mounted = function(){
            var that = this, $el = this.$el;

            $wevField = this.$el.find(".wev-field");
            $checkItem = $el.find(".wev-check-item");
            $checkEdit = $el.find(".wev-check-item .wev-check-edit");
            $field = $el.find("input[name='fieldname_" + vm.field.name + "']");

            if (!vm.readonly) {
                that.$comp.on("click", function (e) {
                    var currValue = $field.val(), nextValue = "";
                    switch (currValue) {
                        case "1":
                            nextValue = "0";
                            break;
                        case "0":
                            nextValue = "";
                            break;
                        default:
                            nextValue = "1";
                            break;
                    }
                    that.setValue(nextValue);
                    e.stopPropagation();
                });
            }

            $checkEdit.on("click", function (e) {
                mUtil.trigger("oncheckedit", that.pageid, vm.form.replace("form_", ""), [that, vm.field.id]);
                e.stopPropagation();
            });

            if(vm.field.value){
                this.setValue(vm.field.value);
            }
        };
        
    
        this.getData = function(){
            return mUtil.parseJSON("fieldname_" + vm.field.name, $field.val());
        };
        
        this.getShowData = function(){
            var val = $field.val(), showData = mUtil.parseJSON(vm.field.name, val);
            showData[vm.field.name + "_showvalue"] = val;
            return showData;
        };
        
        this.reset = function(){
            this.setValue(vm.field.value);
        };

        this.checkRequired = function () {
            var required = vm.required && !$field.val();

            $wevField.toggleClass("wev-required-remind", required);
            return required && vm.field.label;
        };
        
        this.setValueByDB = function(value){
            value = value || '';
            vm.field.value = value;
            this.setValue(value);
        };
        
        this.setValue = function(v){
            var activeClass = blankClass;
            switch (v) {
                case "1":
                    activeClass = successClass;
                    break;
                case "0":
                    activeClass = errorClass;
                    break;
                default:
                    v = "";
                    activeClass = blankClass;
                    break;
            }
            $checkItem.removeClass(successClass).removeClass(errorClass)
                .removeClass(blankClass).addClass(activeClass);
            $field.val(v);
        };
    };

    return Component.init(FCheckItem);
});