田源
2024-04-07 2ac55ce0edf4870a29691b56bfad59f4830a11a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
139
140
141
142
143
144
145
146
147
148
149
150
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
layui.define(['layer'],function(exports){
    /**
     * 枚举的缓存和自动获取
     * weidy@2018-03-06
     */
 
    var Combox = function(){
        this.comboxStore = {};
        this.backPath = configData.compatibility?path:configData.objectServicePath;
        this.defaultUrl = '/webEnumController/getEnum';
        this.newEnumUrl = 'enumController/getEnum';
        this.autoDestoryKey = {};
        this.FORM_DISPLAY_DATA_SECRET = "myDataSecret";
        //xxx:[{key:'yy',value:'xx'}]
    };
    Combox.prototype.init = function(){
 
    };
    //添加枚举对象
    Combox.prototype.newCombox=function(comboxKey,options,reload){
        var that = this;
        if(!(comboxKey in that.comboxStore) || reload){
            //如果已经存在的时候,就不再执行
            if(options&&$webUtil.isNotNull(options.url)){
                that.loadByUrl(comboxKey,options);
            }else{
                if(options.data){
                    that.loadByData(comboxKey,options);
                }else{
                    if(options.failCallback){
                        options.failCallback(comboxKey,'没有设置url参数或者data参数');
                    }
                }
            }
            if(options.autoDestory == true && !that.autoDestoryKey[comboxKey]){
                that.autoDestoryKey[comboxKey] = options;
            }
        }else{
            //说明已经有了
            if(options && options.callback){
                options.callback(comboxKey,that.getComboxMap(comboxKey));
            }
        }
    };
    Combox.prototype.loadByUrl = function(comboxKey,options){
        var that = this;
        var isCustomUrl = false;
        if($webUtil.isNull(options.url) || 'default' == options.url){
            if(configData.compatibility) {
                options.url = that.defaultUrl;
            }else{
                options.url = that.newEnumUrl;
            }
        }else{
            isCustomUrl = true;
        }
        var requestData = {
            comboxKey:comboxKey,
            id:comboxKey
        };
        if(options.extraParams){
            for(var key in options.extraParams){
                requestData[key] = options.extraParams[key];
            }
        }
        $webUtil.get(options.url,requestData,function(result){
            if(result.success){
                if(result.data && result.obj == null){
                    result.obj = result.data
                }
                if(!isCustomUrl) {
                    that.comboxStore[comboxKey] = result.obj;
                }
                if(options.callback){
                    var map = {};
                    layui.each(result.obj,function(_index,item){
                        map[item.key] = item.value;
                    });
                    options.callback(comboxKey,map,result.obj);
                }
            }else{
                if(options.failCallback){
                    options.failCallback(comboxKey,result.msg);
                }
            }
        },function(x,r){
            $webUtil.showErrorMsg("获取枚举的内容出错,可能服务器没有启动");
        },(options.backPath?options.backPath:that.backPath),true,false);
 
    };
    Combox.prototype.loadByData = function(comboxKey,options){
        var that = this;
        that.comboxStore[comboxKey] = options.data;
        if(options.callback){
            options.callback(comboxKey,that.getComboxMap(comboxKey),options.data);
        }
    };
    Combox.prototype.reload = function(comboxKey,options){
        //重新加载
        var that = this;
        if(comboxKey && options && options.data){
            that.loadByData(comboxKey,options);
        }else if(comboxKey && options && options.url){
            that.loadByUrl(comboxKey,options);
        }
    };
    //获取枚举的映射
    Combox.prototype.getComboxMap = function(comboxKey){
        var that = this;
        if(!comboxKey || $webUtil.isNull(comboxKey)){
            return {};
        }
        if(comboxKey in that.comboxStore){
            var array = that.comboxStore[comboxKey];
            var map = {};
            layui.each(array,function(_index,item){
                map[item.key] = item.value;
            });
            return map;
        }
    };
    //获取枚举的text
    Combox.prototype.getComboxText = function(comboxKey,comboxValue){
        var that = this;
        var map = that.getComboxMap(comboxKey);
        if(map && comboxValue in map){
            return map[comboxValue];
        }
        return "";
    };
    /**
     * 获取下拉菜单里的所有属性,即获取加载的所有值
     * @param comboxKey
     * @returns {*}
     */
    Combox.prototype.getComboxAttrs = function(comboxKey){
        var that = this;
        if(that.comboxStore && that.comboxStore[comboxKey]){
            return that.comboxStore[comboxKey];
        }
        return [];
    };
    /**
     * 销毁,会获取之前的配置,配置中设置了可以销毁时才能销毁
     * @param comboxKey 枚举项
     */
    Combox.prototype.destory = function(comboxKey){
        var that = this;
        if(that.autoDestoryKey && $webUtil.isNotNull(that.autoDestoryKey[comboxKey])){
            var options = that.autoDestoryKey[comboxKey];
            if(options.destoryListeners){
                options.destoryListeners();
            }
            delete that.autoDestoryKey[comboxKey];
            delete that.comboxStore[comboxKey];
        }
    };
    /**
     * 获取密级的字段
     * @param controlSecret 是否控制密级
     * @returns {{field: string, hidden: boolean, comboxKey: string, text: string, type: string, required: *}}
     */
    Combox.prototype.getSecretObject = function (controlSecret,readOnly) {
        var that = this;
        return {
            field:'secretGrade',
                text:'密级',
            type:'combox',
            comboxKey:that.FORM_DISPLAY_DATA_SECRET,
            readOnly:readOnly,
            required:controlSecret,
            hidden:!controlSecret
        }
    };
    var combox = new Combox();
    exports('vciWebComboxStore',combox);
});