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